Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 27906f3

Browse files
committed
added first methods to interact with sqlmap core, it is now possible to launch a scan from the API, hurray! (issue #297)
1 parent f52d81c commit 27906f3

2 files changed

Lines changed: 82 additions & 16 deletions

File tree

_sqlmap.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from lib.core.testing import liveTest
4646
from lib.parse.cmdline import cmdLineParser
4747
from lib.utils.restapi import restAPIrun
48+
from lib.utils.restapi import restAPIsetup
4849

4950
def modulePath():
5051
"""
@@ -58,13 +59,14 @@ def restApiServe():
5859
logger.setLevel(logging.INFO)
5960
cmdLineOptions.batch = True
6061
cmdLineOptions.disableColoring = True
61-
restAPIrun(port=cmdLineOptions.restApiPort or RESTAPI_SERVER_PORT)
62+
restAPIsetup(port=cmdLineOptions.restApiPort or RESTAPI_SERVER_PORT)
6263
def emit(self, record):
6364
message = stdoutencode(FORMATTER.format(record))
6465
sys.stdout.write("%s\n" % message.strip('\r'))
6566
LOGGER_HANDLER.emit = types.MethodType(emit, LOGGER_HANDLER, type(LOGGER_HANDLER))
6667
sys.stdout = StringIO.StringIO()
67-
sys.stderr = StringIO.StringIO()
68+
#sys.stderr = StringIO.StringIO()
69+
restAPIrun(port=cmdLineOptions.restApiPort or RESTAPI_SERVER_PORT)
6870

6971
def main():
7072
"""

lib/utils/restapi.py

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), "..", ".."))
1919

2020
from extra.bottle.bottle import abort
21-
from extra.bottle.bottle import debug
2221
from extra.bottle.bottle import error
2322
from extra.bottle.bottle import get
2423
from extra.bottle.bottle import hook
@@ -41,7 +40,8 @@
4140
from lib.core.settings import RESTAPI_SERVER_PORT
4241

4342
# Local global variables
44-
options = AttribDict()
43+
options = {}
44+
output = ""
4545
adminid = ""
4646
tasks = []
4747

@@ -51,8 +51,6 @@ def jsonize(data):
5151

5252
def is_admin(taskid):
5353
global adminid
54-
#print "[INFO] Admin ID: %s" % adminid
55-
#print "[INFO] Task ID: %s" % taskid
5654
if adminid != taskid:
5755
return False
5856
else:
@@ -103,7 +101,8 @@ def task_new():
103101
Create new task ID
104102
"""
105103
global tasks
106-
taskid = hexencode(os.urandom(32))
104+
taskid = hexencode(os.urandom(16))
105+
options[taskid] = AttribDict(cmdLineOptions)
107106
tasks.append(taskid)
108107
return jsonize({"taskid": taskid})
109108

@@ -144,22 +143,63 @@ def task_flush(taskid):
144143
##################################
145144
# sqlmap core interact functions #
146145
##################################
146+
147+
@get("/option/<taskid>/list")
148+
def option_list(taskid):
149+
"""
150+
List options for a certain task ID
151+
"""
152+
global options
153+
if taskid not in tasks:
154+
abort(500, "Invalid task ID")
155+
156+
return jsonize(options[taskid])
157+
158+
@post("/option/<taskid>/get")
159+
def option_get(taskid):
160+
"""
161+
Get the value of an option (command line switch) for a certain task ID
162+
"""
163+
global options
164+
if taskid not in tasks:
165+
abort(500, "Invalid task ID")
166+
167+
option = request.json.get("option", "")
168+
169+
if option in options[taskid]:
170+
print {option: options[taskid][option]}
171+
return jsonize({option: options[taskid][option]})
172+
else:
173+
return jsonize({option: None})
174+
175+
@post("/option/<taskid>/set")
176+
def option_set(taskid):
177+
"""
178+
Set an option (command line switch) for a certain task ID
179+
"""
180+
global options
181+
if taskid not in tasks:
182+
abort(500, "Invalid task ID")
183+
184+
for key, value in request.json.items():
185+
options[taskid][key] = value
186+
187+
return jsonize({"success": True})
188+
147189
@post("/scan/<taskid>")
148190
def scan(taskid):
149191
"""
150-
Mount a scan with sqlmap
192+
Launch a scan
151193
"""
152194
global options
153-
154195
if taskid not in tasks:
155196
abort(500, "Invalid task ID")
156197

157198
# Initialize sqlmap engine's options with user's provided options
158199
# within the JSON request
159200
for key, value in request.json.items():
160-
if key != "taskid":
161-
options[key] = value
162-
init(options, True)
201+
options[taskid][key] = value
202+
init(options[taskid], True)
163203

164204
# Launch sqlmap engine in a separate thread
165205
thread = threading.Thread(target=start)
@@ -168,6 +208,29 @@ def scan(taskid):
168208

169209
return jsonize({"success": True})
170210

211+
@get("/scan/<taskid>/status")
212+
def scan_status(taskid):
213+
"""
214+
Verify if sqlmap core is currently running
215+
"""
216+
if taskid not in tasks:
217+
abort(500, "Invalid task ID")
218+
219+
return jsonize({"busy": kb.get("busyFlag")})
220+
221+
@get("/scan/<taskid>/output")
222+
def scan_output(taskid):
223+
"""
224+
Read the standard output of sqlmap core execution
225+
"""
226+
if taskid not in tasks:
227+
abort(500, "Invalid task ID")
228+
229+
global output
230+
sys.stdout.seek(len(output))
231+
output = sys.stdout.read()
232+
return jsonize({"output": output})
233+
171234
@post("/download/<taskid>/<target>/<filename:path>")
172235
def download(taskid, target, filename):
173236
"""
@@ -182,18 +245,19 @@ def download(taskid, target, filename):
182245
else:
183246
abort(500)
184247

185-
def restAPIrun(host="0.0.0.0", port=RESTAPI_SERVER_PORT):
248+
def restAPIsetup(host="0.0.0.0", port=RESTAPI_SERVER_PORT):
186249
"""
187250
Initiate REST-JSON API
188251
"""
189252
global adminid
190-
global options
191253
global tasks
192-
adminid = hexencode(os.urandom(32))
254+
adminid = hexencode(os.urandom(16))
255+
options[adminid] = AttribDict(cmdLineOptions)
193256
tasks.append(adminid)
194-
options = AttribDict(cmdLineOptions)
195257
logger.info("Running REST-JSON API server at '%s:%d'.." % (host, port))
196258
logger.info("The admin task ID is: %s" % adminid)
259+
260+
def restAPIrun(host="0.0.0.0", port=RESTAPI_SERVER_PORT):
197261
run(host=host, port=port)
198262

199263
def client(host, port):

0 commit comments

Comments
 (0)