SIGN IN SIGN UP
sqlmapproject / sqlmap UNCLAIMED

Automatic SQL injection and database takeover tool

36983 0 0 Python
2019-11-20 16:46:24 +01:00
#!/usr/bin/env python
"""
2026-01-01 19:12:07 +01:00
Copyright (c) 2006-2026 sqlmap developers (https://sqlmap.org)
2019-11-20 16:46:24 +01:00
See the file 'LICENSE' for copying permission
"""
import os
2019-11-21 11:41:46 +01:00
import re
2019-11-22 14:39:44 +01:00
import socket
import subprocess
import sys
import tempfile
2019-11-22 14:39:44 +01:00
import threading
2019-11-21 11:36:13 +01:00
import webbrowser
2019-11-21 13:58:46 +01:00
from lib.core.common import getSafeExString
from lib.core.common import saveConfig
from lib.core.data import paths
2019-11-21 11:41:46 +01:00
from lib.core.defaults import defaults
from lib.core.enums import MKSTEMP_PREFIX
2019-11-21 13:58:46 +01:00
from lib.core.exception import SqlmapMissingDependence
2021-03-07 21:15:59 +01:00
from lib.core.exception import SqlmapSystemException
2019-11-21 11:36:13 +01:00
from lib.core.settings import DEV_EMAIL_ADDRESS
from lib.core.settings import IS_WIN
2019-11-21 11:36:13 +01:00
from lib.core.settings import ISSUES_PAGE
from lib.core.settings import GIT_PAGE
from lib.core.settings import SITE
from lib.core.settings import VERSION_STRING
from lib.core.settings import WIKI_PAGE
2019-11-22 14:39:44 +01:00
from thirdparty.six.moves import queue as _queue
2020-01-09 13:19:54 +01:00
alive = None
2019-11-22 14:39:44 +01:00
line = ""
process = None
queue = None
2019-11-21 11:36:13 +01:00
2019-11-20 16:46:24 +01:00
def runGui(parser):
2019-11-21 13:58:46 +01:00
try:
2019-12-05 22:45:57 +01:00
from thirdparty.six.moves import tkinter as _tkinter
from thirdparty.six.moves import tkinter_scrolledtext as _tkinter_scrolledtext
from thirdparty.six.moves import tkinter_ttk as _tkinter_ttk
from thirdparty.six.moves import tkinter_messagebox as _tkinter_messagebox
2019-11-21 13:58:46 +01:00
except ImportError as ex:
raise SqlmapMissingDependence("missing dependence ('%s')" % getSafeExString(ex))
2019-11-20 16:46:24 +01:00
# Reference: https://www.reddit.com/r/learnpython/comments/985umy/limit_user_input_to_only_int_with_tkinter/e4dj9k9?utm_source=share&utm_medium=web2x
2019-12-05 22:45:57 +01:00
class ConstrainedEntry(_tkinter.Entry):
2019-11-20 16:46:24 +01:00
def __init__(self, master=None, **kwargs):
2019-12-05 22:45:57 +01:00
self.var = _tkinter.StringVar()
2019-11-20 16:46:24 +01:00
self.regex = kwargs["regex"]
del kwargs["regex"]
2019-12-05 22:45:57 +01:00
_tkinter.Entry.__init__(self, master, textvariable=self.var, **kwargs)
2019-11-20 16:46:24 +01:00
self.old_value = ''
self.var.trace('w', self.check)
self.get, self.set = self.var.get, self.var.set
def check(self, *args):
if re.search(self.regex, self.get()):
self.old_value = self.get()
else:
self.set(self.old_value)
2021-03-07 21:15:59 +01:00
try:
window = _tkinter.Tk()
except Exception as ex:
errMsg = "unable to create GUI window ('%s')" % getSafeExString(ex)
raise SqlmapSystemException(errMsg)
2025-12-31 15:05:04 +01:00
window.title("sqlmap - Tkinter GUI")
2019-11-20 16:46:24 +01:00
# Set theme and colors
bg_color = "#f5f5f5"
fg_color = "#333333"
accent_color = "#2c7fb8"
window.configure(background=bg_color)
# Configure styles
2019-12-05 22:45:57 +01:00
style = _tkinter_ttk.Style()
# Try to use a more modern theme if available
available_themes = style.theme_names()
if 'clam' in available_themes:
style.theme_use('clam')
elif 'alt' in available_themes:
style.theme_use('alt')
# Configure notebook style
style.configure("TNotebook", background=bg_color)
style.configure("TNotebook.Tab",
padding=[10, 4],
background="#e1e1e1",
font=('Helvetica', 9))
style.map("TNotebook.Tab",
background=[("selected", accent_color), ("active", "#7fcdbb")],
foreground=[("selected", "white"), ("active", "white")])
# Configure button style
style.configure("TButton",
padding=4,
relief="flat",
background=accent_color,
foreground="white",
font=('Helvetica', 9))
style.map("TButton",
background=[('active', '#41b6c4')])
2019-11-20 16:46:24 +01:00
2019-11-22 14:39:44 +01:00
# Reference: https://stackoverflow.com/a/10018670
def center(window):
window.update_idletasks()
width = window.winfo_width()
frm_width = window.winfo_rootx() - window.winfo_x()
win_width = width + 2 * frm_width
height = window.winfo_height()
titlebar_height = window.winfo_rooty() - window.winfo_y()
win_height = height + titlebar_height + frm_width
x = window.winfo_screenwidth() // 2 - win_width // 2
y = window.winfo_screenheight() // 2 - win_height // 2
window.geometry('{}x{}+{}+{}'.format(width, height, x, y))
window.deiconify()
def onKeyPress(event):
global line
global queue
if process:
if event.char == '\b':
line = line[:-1]
else:
line += event.char
def onReturnPress(event):
global line
global queue
if process:
try:
process.stdin.write(("%s\n" % line.strip()).encode())
process.stdin.flush()
except socket.error:
line = ""
event.widget.master.master.destroy()
return "break"
except:
return
2019-11-22 14:39:44 +01:00
2019-12-05 22:45:57 +01:00
event.widget.insert(_tkinter.END, "\n")
2019-11-22 14:39:44 +01:00
return "break"
2019-11-21 15:58:04 +01:00
def run():
global alive
2019-11-22 14:39:44 +01:00
global process
global queue
config = {}
2019-11-21 15:58:04 +01:00
for key in window._widgets:
dest, widget_type = key
2019-11-21 15:58:04 +01:00
widget = window._widgets[key]
if hasattr(widget, "get") and not widget.get():
value = None
elif widget_type == "string":
2019-11-21 15:58:04 +01:00
value = widget.get()
elif widget_type == "float":
2019-11-21 15:58:04 +01:00
value = float(widget.get())
elif widget_type == "int":
2019-11-21 15:58:04 +01:00
value = int(widget.get())
else:
2019-11-22 14:39:44 +01:00
value = bool(widget.var.get())
2019-11-21 15:58:04 +01:00
config[dest] = value
2019-11-21 15:58:04 +01:00
for option in parser.option_list:
# Only set default if not already set by the user
if option.dest not in config or config[option.dest] is None:
config[option.dest] = defaults.get(option.dest, None)
handle, configFile = tempfile.mkstemp(prefix=MKSTEMP_PREFIX.CONFIG, text=True)
os.close(handle)
2019-11-21 15:58:04 +01:00
saveConfig(config, configFile)
def enqueue(stream, queue):
global alive
for line in iter(stream.readline, b''):
queue.put(line)
alive = False
stream.close()
alive = True
process = subprocess.Popen([sys.executable or "python", os.path.join(paths.SQLMAP_ROOT_PATH, "sqlmap.py"), "-c", configFile], shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE, bufsize=1, close_fds=not IS_WIN)
# Reference: https://stackoverflow.com/a/4896288
queue = _queue.Queue()
thread = threading.Thread(target=enqueue, args=(process.stdout, queue))
thread.daemon = True
thread.start()
2019-11-22 14:39:44 +01:00
2019-12-05 22:45:57 +01:00
top = _tkinter.Toplevel()
2019-11-22 14:39:44 +01:00
top.title("Console")
top.configure(background=bg_color)
# Create a frame for the console
console_frame = _tkinter.Frame(top, bg=bg_color)
console_frame.pack(fill=_tkinter.BOTH, expand=True, padx=10, pady=10)
2019-11-22 14:39:44 +01:00
# Reference: https://stackoverflow.com/a/13833338
text = _tkinter_scrolledtext.ScrolledText(console_frame, undo=True, wrap=_tkinter.WORD,
bg="#2c3e50", fg="#ecf0f1",
insertbackground="white",
font=('Consolas', 10))
2019-11-22 14:39:44 +01:00
text.bind("<Key>", onKeyPress)
text.bind("<Return>", onReturnPress)
text.pack(fill=_tkinter.BOTH, expand=True)
2019-11-22 14:39:44 +01:00
text.focus()
center(top)
2019-11-20 16:46:24 +01:00
2020-01-09 13:19:54 +01:00
while True:
line = ""
try:
line = queue.get(timeout=.1)
2019-12-05 22:45:57 +01:00
text.insert(_tkinter.END, line)
except _queue.Empty:
2019-12-05 22:45:57 +01:00
text.see(_tkinter.END)
text.update_idletasks()
2020-01-09 13:19:54 +01:00
if not alive:
break
# Create a menu bar
menubar = _tkinter.Menu(window, bg=bg_color, fg=fg_color)
2019-11-20 16:46:24 +01:00
filemenu = _tkinter.Menu(menubar, tearoff=0, bg=bg_color, fg=fg_color)
2019-12-05 22:45:57 +01:00
filemenu.add_command(label="Open", state=_tkinter.DISABLED)
filemenu.add_command(label="Save", state=_tkinter.DISABLED)
2019-11-20 16:46:24 +01:00
filemenu.add_separator()
filemenu.add_command(label="Exit", command=window.quit)
menubar.add_cascade(label="File", menu=filemenu)
2019-11-21 15:58:04 +01:00
menubar.add_command(label="Run", command=run)
2019-11-20 16:46:24 +01:00
helpmenu = _tkinter.Menu(menubar, tearoff=0, bg=bg_color, fg=fg_color)
2019-11-21 11:36:13 +01:00
helpmenu.add_command(label="Official site", command=lambda: webbrowser.open(SITE))
helpmenu.add_command(label="Github pages", command=lambda: webbrowser.open(GIT_PAGE))
helpmenu.add_command(label="Wiki pages", command=lambda: webbrowser.open(WIKI_PAGE))
helpmenu.add_command(label="Report issue", command=lambda: webbrowser.open(ISSUES_PAGE))
2019-11-20 16:46:24 +01:00
helpmenu.add_separator()
2025-12-31 14:58:26 +01:00
helpmenu.add_command(label="About", command=lambda: _tkinter_messagebox.showinfo("About", "%s\n\n (%s)" % (VERSION_STRING, DEV_EMAIL_ADDRESS)))
2019-11-20 16:46:24 +01:00
menubar.add_cascade(label="Help", menu=helpmenu)
window.config(menu=menubar, bg=bg_color)
2019-11-21 15:58:04 +01:00
window._widgets = {}
2019-11-20 16:46:24 +01:00
# Create header frame
header_frame = _tkinter.Frame(window, bg=bg_color, height=60)
header_frame.pack(fill=_tkinter.X, pady=(0, 5))
header_frame.pack_propagate(0)
2019-11-20 16:46:24 +01:00
# Add header label
title_label = _tkinter.Label(header_frame, text="Configuration",
font=('Helvetica', 14),
fg=accent_color, bg=bg_color)
title_label.pack(side=_tkinter.LEFT, padx=15)
2019-11-21 15:58:04 +01:00
# Add run button in header
run_button = _tkinter_ttk.Button(header_frame, text="Run", command=run, width=12)
run_button.pack(side=_tkinter.RIGHT, padx=15)
# Create notebook
notebook = _tkinter_ttk.Notebook(window)
notebook.pack(expand=1, fill="both", padx=5, pady=(0, 5))
2019-11-20 16:46:24 +01:00
# Store tab information for background loading
tab_frames = {}
tab_canvases = {}
tab_scrollable_frames = {}
tab_groups = {}
# Create empty tabs with scrollable areas first (fast)
for group in parser.option_groups:
# Create a frame with scrollbar for the tab
tab_frame = _tkinter.Frame(notebook, bg=bg_color)
tab_frames[group.title] = tab_frame
# Create a canvas with scrollbar
canvas = _tkinter.Canvas(tab_frame, bg=bg_color, highlightthickness=0)
scrollbar = _tkinter_ttk.Scrollbar(tab_frame, orient="vertical", command=canvas.yview)
scrollable_frame = _tkinter.Frame(canvas, bg=bg_color)
# Store references
tab_canvases[group.title] = canvas
tab_scrollable_frames[group.title] = scrollable_frame
tab_groups[group.title] = group
# Configure the canvas scrolling
scrollable_frame.bind(
"<Configure>",
lambda e, canvas=canvas: canvas.configure(scrollregion=canvas.bbox("all"))
)
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
# Pack the canvas and scrollbar
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
# Add the tab to the notebook
notebook.add(tab_frame, text=group.title)
# Add a loading indicator
loading_label = _tkinter.Label(scrollable_frame, text="Loading options...",
font=('Helvetica', 12),
fg=accent_color, bg=bg_color)
loading_label.pack(expand=True)
# Function to populate a tab in the background
def populate_tab(tab_name):
group = tab_groups[tab_name]
scrollable_frame = tab_scrollable_frames[tab_name]
canvas = tab_canvases[tab_name]
# Remove loading indicator
for child in scrollable_frame.winfo_children():
child.destroy()
# Add content to the scrollable frame
row = 0
2019-11-20 16:46:24 +01:00
2019-11-20 17:28:25 +01:00
if group.get_description():
desc_label = _tkinter.Label(scrollable_frame, text=group.get_description(),
wraplength=600, justify="left",
font=('Helvetica', 9),
fg="#555555", bg=bg_color)
desc_label.grid(row=row, column=0, columnspan=3, sticky="w", padx=10, pady=(10, 5))
row += 1
2019-11-20 17:28:25 +01:00
2019-11-20 16:46:24 +01:00
for option in group.option_list:
# Option label
option_label = _tkinter.Label(scrollable_frame,
text=parser.formatter._format_option_strings(option) + ":",
font=('Helvetica', 9),
fg=fg_color, bg=bg_color,
anchor="w")
option_label.grid(row=row, column=0, sticky="w", padx=10, pady=2)
# Input widget
2019-11-20 16:46:24 +01:00
if option.type == "string":
widget = _tkinter.Entry(scrollable_frame, font=('Helvetica', 9),
relief="sunken", bd=1, width=20)
widget.grid(row=row, column=1, sticky="w", padx=5, pady=2)
2019-11-20 16:46:24 +01:00
elif option.type == "float":
widget = ConstrainedEntry(scrollable_frame, regex=r"\A\d*\.?\d*\Z",
font=('Helvetica', 9),
relief="sunken", bd=1, width=10)
widget.grid(row=row, column=1, sticky="w", padx=5, pady=2)
2019-11-20 16:46:24 +01:00
elif option.type == "int":
widget = ConstrainedEntry(scrollable_frame, regex=r"\A\d*\Z",
font=('Helvetica', 9),
relief="sunken", bd=1, width=10)
widget.grid(row=row, column=1, sticky="w", padx=5, pady=2)
2019-11-20 16:46:24 +01:00
else:
2019-12-05 22:45:57 +01:00
var = _tkinter.IntVar()
widget = _tkinter.Checkbutton(scrollable_frame, variable=var,
bg=bg_color, activebackground=bg_color)
2019-11-20 16:46:24 +01:00
widget.var = var
widget.grid(row=row, column=1, sticky="w", padx=5, pady=2)
# Help text (truncated to improve performance)
help_text = option.help
if len(help_text) > 100:
help_text = help_text[:100] + "..."
2019-11-20 16:46:24 +01:00
help_label = _tkinter.Label(scrollable_frame, text=help_text,
font=('Helvetica', 8),
fg="#666666", bg=bg_color,
wraplength=400, justify="left")
help_label.grid(row=row, column=2, sticky="w", padx=5, pady=2)
2019-11-20 16:46:24 +01:00
# Store widget reference
2019-11-21 15:58:04 +01:00
window._widgets[(option.dest, option.type)] = widget
# Set default value
2019-11-20 16:46:24 +01:00
default = defaults.get(option.dest)
if default:
if hasattr(widget, "insert"):
widget.insert(0, default)
elif hasattr(widget, "var"):
widget.var.set(1 if default else 0)
2019-11-20 16:46:24 +01:00
row += 1
# Add some padding at the bottom
_tkinter.Label(scrollable_frame, bg=bg_color, height=1).grid(row=row, column=0)
# Update the scroll region after adding all widgets
canvas.update_idletasks()
canvas.configure(scrollregion=canvas.bbox("all"))
# Update the UI to show the tab is fully loaded
window.update_idletasks()
# Function to populate tabs in the background
def populate_tabs_background():
for tab_name in tab_groups.keys():
# Schedule each tab to be populated with a small delay between them
window.after(100, lambda name=tab_name: populate_tab(name))
# Start populating tabs in the background after a short delay
window.after(500, populate_tabs_background)
2019-11-20 16:46:24 +01:00
# Set minimum window size
window.update()
window.minsize(800, 500)
2019-11-20 16:46:24 +01:00
# Center the window on screen
center(window)
2019-11-21 11:36:13 +01:00
# Start the GUI
window.mainloop()