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

Skip to content

Commit edd6699

Browse files
committed
code refactoring and added /status method for scan (issue #297)
1 parent c47b44e commit edd6699

1 file changed

Lines changed: 40 additions & 14 deletions

File tree

lib/utils/api.py

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,12 @@ def engine_get_id(self):
152152
else:
153153
return None
154154

155+
def engine_get_returncode(self):
156+
self.process.poll()
157+
return self.process.returncode
158+
155159
def engine_has_terminated(self):
156-
return isinstance(self.process.returncode, int) == True
160+
return isinstance(self.engine_get_returncode(), int)
157161

158162
# Wrapper functions for sqlmap engine
159163
class StdDbOut(object):
@@ -271,7 +275,6 @@ def task_new():
271275
tasks[taskid] = Task(taskid)
272276

273277
logger.debug("Created new task ID: %s" % taskid)
274-
275278
return jsonize({"taskid": taskid})
276279

277280
@get("/task/<taskid>/delete")
@@ -284,7 +287,6 @@ def task_delete(taskid):
284287
tasks.pop(taskid)
285288

286289
logger.debug("Deleted task ID: %s" % taskid)
287-
288290
return jsonize({"success": True})
289291
else:
290292
abort(500, "Invalid task ID")
@@ -296,10 +298,10 @@ def task_delete(taskid):
296298
@get("/admin/<taskid>/list")
297299
def task_list(taskid):
298300
"""
299-
List task poll
301+
List task pull
300302
"""
301303
if is_admin(taskid):
302-
logger.debug("Listed task poll")
304+
logger.debug("Listed task pull")
303305
return jsonize({"tasks": tasks, "tasks_num": len(tasks)})
304306
else:
305307
abort(401)
@@ -316,7 +318,7 @@ def task_flush(taskid):
316318
tasks[task].clean_filesystem()
317319

318320
tasks = dict()
319-
logger.debug("Flushed task poll")
321+
logger.debug("Flushed task pull")
320322
return jsonize({"success": True})
321323
else:
322324
abort(401)
@@ -341,6 +343,8 @@ def option_get(taskid):
341343
"""
342344
Get the value of an option (command line switch) for a certain task ID
343345
"""
346+
global tasks
347+
344348
if taskid not in tasks:
345349
abort(500, "Invalid task ID")
346350

@@ -349,7 +353,7 @@ def option_get(taskid):
349353
if option in tasks[taskid]:
350354
return jsonize({option: tasks[taskid].get_option(option)})
351355
else:
352-
return jsonize({option: "Not set"})
356+
return jsonize({option: "not set"})
353357

354358
@post("/option/<taskid>/set")
355359
def option_set(taskid):
@@ -384,12 +388,10 @@ def scan_start(taskid):
384388
# Overwrite output directory value to a temporary directory
385389
tasks[taskid].set_output_directory()
386390

387-
# Launch sqlmap engine in a separate thread
388-
logger.debug("Starting a scan for task ID %s" % taskid)
389-
390-
# Launch sqlmap engine
391+
# Launch sqlmap engine in a separate process
391392
tasks[taskid].engine_start()
392393

394+
logger.debug("Started scan for task ID %s" % taskid)
393395
return jsonize({"success": True, "engineid": tasks[taskid].engine_get_id()})
394396

395397
@get("/scan/<taskid>/stop")
@@ -402,7 +404,10 @@ def scan_stop(taskid):
402404
if taskid not in tasks:
403405
abort(500, "Invalid task ID")
404406

405-
return jsonize({"success": tasks[taskid].engine_stop()})
407+
tasks[taskid].engine_stop()
408+
409+
logger.debug("Stopped scan for task ID %s" % taskid)
410+
return jsonize({"success": True})
406411

407412
@get("/scan/<taskid>/kill")
408413
def scan_kill(taskid):
@@ -414,7 +419,25 @@ def scan_kill(taskid):
414419
if taskid not in tasks:
415420
abort(500, "Invalid task ID")
416421

417-
return jsonize({"success": tasks[taskid].engine_kill()})
422+
tasks[taskid].engine_kill()
423+
424+
logger.debug("Killed scan for task ID %s" % taskid)
425+
return jsonize({"success": True})
426+
427+
@get("/scan/<taskid>/status")
428+
def scan_status(taskid):
429+
"""
430+
Returns status of a scan
431+
"""
432+
global tasks
433+
434+
if taskid not in tasks:
435+
abort(500, "Invalid task ID")
436+
437+
status = "terminated" if tasks[taskid].engine_has_terminated() is True else "running"
438+
439+
logger.debug("Requested status of scan for task ID %s" % taskid)
440+
return jsonize({"status": status, "returncode": tasks[taskid].engine_get_returncode()})
418441

419442
@get("/scan/<taskid>/data")
420443
def scan_data(taskid):
@@ -438,6 +461,7 @@ def scan_data(taskid):
438461
for error in db.execute("SELECT error FROM errors WHERE taskid = ? ORDER BY id ASC", (taskid,)):
439462
json_errors_message.append(error)
440463

464+
logger.debug("Retrieved data and error messages for scan for task ID %s" % taskid)
441465
return jsonize({"data": json_data_message, "error": json_errors_message})
442466

443467
# Functions to handle scans' logs
@@ -463,6 +487,7 @@ def scan_log_limited(taskid, start, end):
463487
for time_, level, message in db.execute("SELECT time, level, message FROM logs WHERE taskid = ? AND id >= ? AND id <= ? ORDER BY id ASC", (taskid, start, end)):
464488
json_log_messages.append({"time": time_, "level": level, "message": message})
465489

490+
logger.debug("Retrieved subset of log messages for scan for task ID %s" % taskid)
466491
return jsonize({"log": json_log_messages})
467492

468493
@get("/scan/<taskid>/log")
@@ -481,6 +506,7 @@ def scan_log(taskid):
481506
for time_, level, message in db.execute("SELECT time, level, message FROM logs WHERE taskid = ? ORDER BY id ASC", (taskid,)):
482507
json_log_messages.append({"time": time_, "level": level, "message": message})
483508

509+
logger.debug("Retrieved log messages for scan for task ID %s" % taskid)
484510
return jsonize({"log": json_log_messages})
485511

486512
# Function to handle files inside the output directory
@@ -501,7 +527,7 @@ def download(taskid, target, filename):
501527
if os.path.exists(path):
502528
return static_file(filename, root=path)
503529
else:
504-
abort(500)
530+
abort(500, "File does not exist")
505531

506532
def server(host="0.0.0.0", port=RESTAPI_SERVER_PORT):
507533
"""

0 commit comments

Comments
 (0)