32
32
from lib .core .data import cmdLineOptions
33
33
from lib .core .data import kb
34
34
from lib .core .data import logger
35
+ from lib .core .log import LOGGER_OUTPUT
35
36
from lib .core .exception import SqlmapMissingDependence
36
37
from lib .core .option import init
37
38
from lib .core .settings import UNICODE_ENCODING
@@ -99,10 +100,7 @@ def task_new():
99
100
global tasks
100
101
101
102
taskid = hexencode (os .urandom (16 ))
102
-
103
- tasks [taskid ] = AttribDict ()
104
- tasks [taskid ].options = AttribDict (cmdLineOptions )
105
- tasks [taskid ].output = ""
103
+ tasks [taskid ] = AttribDict (cmdLineOptions )
106
104
107
105
return jsonize ({"taskid" : taskid })
108
106
@@ -171,9 +169,9 @@ def cleanup(taskid):
171
169
global tasks
172
170
173
171
if is_admin (taskid ):
174
- for task , taskdata in tasks .items ():
175
- if "oDir" in taskdata . options and taskdata . options .oDir is not None :
176
- shutil .rmtree (taskdata . options .oDir )
172
+ for task , options in tasks .items ():
173
+ if "oDir" in options and options .oDir is not None :
174
+ shutil .rmtree (options .oDir )
177
175
178
176
admin_task = tasks [adminid ]
179
177
tasks = AttribDict ()
@@ -192,7 +190,7 @@ def option_list(taskid):
192
190
if taskid not in tasks :
193
191
abort (500 , "Invalid task ID" )
194
192
195
- return jsonize (tasks [taskid ]. options )
193
+ return jsonize (tasks [taskid ])
196
194
197
195
@post ("/option/<taskid>/get" )
198
196
def option_get (taskid ):
@@ -204,8 +202,8 @@ def option_get(taskid):
204
202
205
203
option = request .json .get ("option" , "" )
206
204
207
- if option in tasks [taskid ]. options :
208
- return jsonize ({option : tasks [taskid ]. options [option ]})
205
+ if option in tasks [taskid ]:
206
+ return jsonize ({option : tasks [taskid ][option ]})
209
207
else :
210
208
return jsonize ({option : None })
211
209
@@ -220,7 +218,7 @@ def option_set(taskid):
220
218
abort (500 , "Invalid task ID" )
221
219
222
220
for key , value in request .json .items ():
223
- tasks [taskid ]. options [key ] = value
221
+ tasks [taskid ][key ] = value
224
222
225
223
return jsonize ({"success" : True })
226
224
@@ -238,12 +236,12 @@ def scan(taskid):
238
236
# Initialize sqlmap engine's options with user's provided options
239
237
# within the JSON request
240
238
for key , value in request .json .items ():
241
- tasks [taskid ]. options [key ] = value
239
+ tasks [taskid ][key ] = value
242
240
243
- # Overwrite oDir value to a temporary directory
244
- tasks [taskid ].options . oDir = tempfile .mkdtemp (prefix = "sqlmap-" )
241
+ # Overwrite output directory ( oDir) value to a temporary directory
242
+ tasks [taskid ].oDir = tempfile .mkdtemp (prefix = "sqlmap-" )
245
243
246
- init (tasks [taskid ]. options , True )
244
+ init (tasks [taskid ], True )
247
245
248
246
# Launch sqlmap engine in a separate thread
249
247
thread = threading .Thread (target = start )
@@ -262,11 +260,12 @@ def scan_output(taskid):
262
260
if taskid not in tasks :
263
261
abort (500 , "Invalid task ID" )
264
262
265
- sys .stdout .seek (len (tasks [taskid ]["output" ]))
266
- tasks [taskid ]["output" ] = sys .stdout .read ()
263
+ sys .stdout .seek (0 )
264
+ output = sys .stdout .read ()
265
+ sys .stdout .flush ()
267
266
sys .stdout .truncate (0 )
268
267
269
- return jsonize ({"output" : tasks [ taskid ][ " output" ] })
268
+ return jsonize ({"output" : output })
270
269
271
270
@get ("/scan/<taskid>/delete" )
272
271
def scan_delete (taskid ):
@@ -278,21 +277,26 @@ def scan_delete(taskid):
278
277
if taskid not in tasks :
279
278
abort (500 , "Invalid task ID" )
280
279
281
- if "oDir" in tasks [taskid ]. options and tasks [taskid ]. options .oDir is not None :
282
- shutil .rmtree (tasks [taskid ].options . oDir )
280
+ if "oDir" in tasks [taskid ] and tasks [taskid ].oDir is not None :
281
+ shutil .rmtree (tasks [taskid ].oDir )
283
282
284
283
return jsonize ({"success" : True })
285
284
286
285
# Function to handle scans' logs
287
- @get ("/log /<taskid>/info " )
288
- def log_info (taskid ):
286
+ @get ("/scan /<taskid>/log " )
287
+ def scan_log (taskid ):
289
288
"""
290
289
Read the informational log messages
291
290
"""
292
291
if taskid not in tasks :
293
292
abort (500 , "Invalid task ID" )
294
293
295
- pass
294
+ LOGGER_OUTPUT .seek (0 )
295
+ output = LOGGER_OUTPUT .read ()
296
+ LOGGER_OUTPUT .flush ()
297
+ LOGGER_OUTPUT .truncate (0 )
298
+
299
+ return jsonize ({"log" : output })
296
300
297
301
# Function to handle files inside the output directory
298
302
@get ("/download/<taskid>/<target>/<filename:path>" )
@@ -313,29 +317,31 @@ def download(taskid, target, filename):
313
317
else :
314
318
abort (500 )
315
319
316
- def restAPIsetup (host = "0.0.0.0" , port = RESTAPI_SERVER_PORT ):
320
+ def restAPISetup (host = "0.0.0.0" , port = RESTAPI_SERVER_PORT ):
317
321
"""
318
- Initiate REST-JSON API
322
+ Setup REST-JSON API
319
323
"""
320
324
global adminid
321
325
global tasks
322
326
323
327
adminid = hexencode (os .urandom (16 ))
324
- tasks [adminid ] = AttribDict ()
325
- tasks [adminid ].options = AttribDict (cmdLineOptions )
326
- tasks [adminid ].output = ""
327
- logger .info ("Running REST-JSON API server at '%s:%d'.." % (host , port ))
328
- logger .info ("The admin task ID is: %s" % adminid )
328
+ tasks [adminid ] = AttribDict (cmdLineOptions )
329
+
330
+ logger .info ("running REST-JSON API server at '%s:%d'.." % (host , port ))
331
+ logger .info ("the admin task ID is: %s" % adminid )
329
332
330
- def restAPIrun (host = "0.0.0.0" , port = RESTAPI_SERVER_PORT ):
333
+ def restAPIRun (host = "0.0.0.0" , port = RESTAPI_SERVER_PORT ):
334
+ """
335
+ Run REST-JSON API
336
+ """
331
337
run (host = host , port = port , quiet = False , debug = False )
332
338
333
339
def client (host , port ):
334
340
addr = "http://%s:%d" % (host , port )
335
- print "[INFO] Starting debug REST-JSON client to '%s'..." % addr
341
+ print "[*] starting debug REST-JSON client to '%s'..." % addr
336
342
337
343
# TODO: write a simple client with urllib2, for now use curl from command line
338
- print "[ERROR] Not yet implemented, use curl from command line instead for now, for example:"
344
+ print "[!] not yet implemented, use curl from command line instead for now, for example:"
339
345
print "\n \t $ curl --proxy http://127.0.0.1:8080 http://127.0.0.1:%s/task/new" % port
340
346
print "\t $ curl --proxy http://127.0.0.1:8080 -H \" Content-Type: application/json\" -X POST -d '{\" url\" : \" http://testphp.vulnweb.com/artists.php?artist=1\" }' http://127.0.0.1:%d/scan/<taskID>/start" % port
341
347
print "\t $ curl --proxy http://127.0.0.1:8080 http://127.0.0.1:8775/scan/<taskID>/output\n "
0 commit comments