Skip to content

Commit 4c9456d

Browse files
committed
moar logging!
1 parent 438ad73 commit 4c9456d

1 file changed

Lines changed: 34 additions & 12 deletions

File tree

lib/utils/api.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def write(self, value, status=CONTENT_STATUS.IN_PROGRESS, content_type=None):
212212
# Delete partial output from IPC database if we have got a complete output
213213
if status == CONTENT_STATUS.COMPLETE:
214214
if len(output) > 0:
215-
for index in xrange(0, len(output)):
215+
for index in xrange(len(output)):
216216
conf.database_cursor.execute("DELETE FROM data WHERE id = ?",
217217
(output[index][0],))
218218

@@ -328,7 +328,7 @@ def task_new():
328328
taskid = hexencode(os.urandom(8))
329329
DataStore.tasks[taskid] = Task(taskid)
330330

331-
logger.debug("Created new task ID: %s" % taskid)
331+
logger.debug(" [%s] Created new task" % taskid)
332332
return jsonize({"success": True, "taskid": taskid})
333333

334334

@@ -341,9 +341,10 @@ def task_delete(taskid):
341341
DataStore.tasks[taskid].clean_filesystem()
342342
DataStore.tasks.pop(taskid)
343343

344-
logger.debug("Deleted task ID: %s" % taskid)
344+
logger.debug("[%s] Deleted task" % taskid)
345345
return jsonize({"success": True})
346346
else:
347+
logger.warning("[%s] Invalid task ID provided to task_delete()" % taskid)
347348
return jsonize({"success": False, "message": "Invalid task ID"})
348349

349350
###################
@@ -357,10 +358,11 @@ def task_list(taskid):
357358
List task pull
358359
"""
359360
if is_admin(taskid):
360-
logger.debug("Listed task pull")
361+
logger.debug("[%s] Listed task pool" % taskid)
361362
tasks = list(DataStore.tasks)
362363
return jsonize({"success": True, "tasks": tasks, "tasks_num": len(tasks)})
363364
else:
365+
logger.warning("[%s] Unauthorized call to task_list()" % taskid)
364366
return jsonize({"success": False, "message": "Unauthorized"})
365367

366368

@@ -374,9 +376,10 @@ def task_flush(taskid):
374376
DataStore.tasks[task].clean_filesystem()
375377

376378
DataStore.tasks = dict()
377-
logger.debug("Flushed task pull")
379+
logger.debug("[%s] Flushed task pool" % taskid)
378380
return jsonize({"success": True})
379381
else:
382+
logger.warning("[%s] Unauthorized call to task_flush()" % taskid)
380383
return jsonize({"success": False, "message": "Unauthorized"})
381384

382385
##################################
@@ -391,8 +394,10 @@ def option_list(taskid):
391394
List options for a certain task ID
392395
"""
393396
if taskid not in DataStore.tasks:
397+
logger.warning("[%s] Invalid task ID provided to option_list()" % taskid)
394398
return jsonize({"success": False, "message": "Invalid task ID"})
395399

400+
logger.debug("[%s] Listed task options" % taskid)
396401
return jsonize({"success": True, "options": DataStore.tasks[taskid].get_options()})
397402

398403

@@ -402,13 +407,16 @@ def option_get(taskid):
402407
Get the value of an option (command line switch) for a certain task ID
403408
"""
404409
if taskid not in DataStore.tasks:
410+
logger.warning("[%s] Invalid task ID provided to option_get()" % taskid)
405411
return jsonize({"success": False, "message": "Invalid task ID"})
406412

407413
option = request.json.get("option", "")
408414

409415
if option in DataStore.tasks[taskid].options:
416+
logger.debug("[%s] Retrieved value for option %s" % (taskid, option))
410417
return jsonize({"success": True, option: DataStore.tasks[taskid].get_option(option)})
411418
else:
419+
logger.debug("[%s] Requested value for unknown option %s" % (taskid, option))
412420
return jsonize({"success": False, "message": "Unknown option", option: "not set"})
413421

414422

@@ -418,11 +426,13 @@ def option_set(taskid):
418426
Set an option (command line switch) for a certain task ID
419427
"""
420428
if taskid not in DataStore.tasks:
429+
logger.warning("[%s] Invalid task ID provided to option_set()" % taskid)
421430
return jsonize({"success": False, "message": "Invalid task ID"})
422431

423432
for option, value in request.json.items():
424433
DataStore.tasks[taskid].set_option(option, value)
425434

435+
logger.debug("[%s] Requested to set options" % taskid)
426436
return jsonize({"success": True})
427437

428438

@@ -433,6 +443,7 @@ def scan_start(taskid):
433443
Launch a scan
434444
"""
435445
if taskid not in DataStore.tasks:
446+
logger.warning("[%s] Invalid task ID provided to scan_start()" % taskid)
436447
return jsonize({"success": False, "message": "Invalid task ID"})
437448

438449
# Initialize sqlmap engine's options with user's provided options, if any
@@ -445,7 +456,7 @@ def scan_start(taskid):
445456
# Launch sqlmap engine in a separate process
446457
DataStore.tasks[taskid].engine_start()
447458

448-
logger.debug("Started scan for task ID %s" % taskid)
459+
logger.debug("[%s] Started scan" % taskid)
449460
return jsonize({"success": True, "engineid": DataStore.tasks[taskid].engine_get_id()})
450461

451462

@@ -455,11 +466,12 @@ def scan_stop(taskid):
455466
Stop a scan
456467
"""
457468
if taskid not in DataStore.tasks:
469+
logger.warning("[%s] Invalid task ID provided to scan_stop()" % taskid)
458470
return jsonize({"success": False, "message": "Invalid task ID"})
459471

460472
DataStore.tasks[taskid].engine_stop()
461473

462-
logger.debug("Stopped scan for task ID %s" % taskid)
474+
logger.debug("[%s] Stopped scan" % taskid)
463475
return jsonize({"success": True})
464476

465477

@@ -469,11 +481,12 @@ def scan_kill(taskid):
469481
Kill a scan
470482
"""
471483
if taskid not in DataStore.tasks:
484+
logger.warning("[%s] Invalid task ID provided to scan_kill()" % taskid)
472485
return jsonize({"success": False, "message": "Invalid task ID"})
473486

474487
DataStore.tasks[taskid].engine_kill()
475488

476-
logger.debug("Killed scan for task ID %s" % taskid)
489+
logger.debug("[%s] Killed scan" % taskid)
477490
return jsonize({"success": True})
478491

479492

@@ -483,11 +496,12 @@ def scan_status(taskid):
483496
Returns status of a scan
484497
"""
485498
if taskid not in DataStore.tasks:
499+
logger.warning("[%s] Invalid task ID provided to scan_status()" % taskid)
486500
return jsonize({"success": False, "message": "Invalid task ID"})
487501

488502
status = "terminated" if DataStore.tasks[taskid].engine_has_terminated() is True else "running"
489503

490-
logger.debug("Requested status of scan for task ID %s" % taskid)
504+
logger.debug("[%s] Retrieved scan status" % taskid)
491505
return jsonize({
492506
"success": True,
493507
"status": status,
@@ -504,6 +518,7 @@ def scan_data(taskid):
504518
json_errors_message = list()
505519

506520
if taskid not in DataStore.tasks:
521+
logger.warning("[%s] Invalid task ID provided to scan_data()" % taskid)
507522
return jsonize({"success": False, "message": "Invalid task ID"})
508523

509524
# Read all data from the IPC database for the taskid
@@ -519,7 +534,7 @@ def scan_data(taskid):
519534
(taskid,)):
520535
json_errors_message.append(error)
521536

522-
logger.debug("Retrieved data and error messages for scan for task ID %s" % taskid)
537+
logger.debug("[%s] Retrieved scan data and error messages" % taskid)
523538
return jsonize({"success": True, "data": json_data_message, "error": json_errors_message})
524539

525540

@@ -532,9 +547,11 @@ def scan_log_limited(taskid, start, end):
532547
json_log_messages = list()
533548

534549
if taskid not in DataStore.tasks:
550+
logger.warning("[%s] Invalid task ID provided to scan_log_limited()")
535551
return jsonize({"success": False, "message": "Invalid task ID"})
536552

537553
if not start.isdigit() or not end.isdigit() or end < start:
554+
logger.warning("[%s] Invalid start or end value provided to scan_log_limited()" % taskid)
538555
return jsonize({"success": False, "message": "Invalid start or end value, must be digits"})
539556

540557
start = max(1, int(start))
@@ -547,7 +564,7 @@ def scan_log_limited(taskid, start, end):
547564
(taskid, start, end)):
548565
json_log_messages.append({"time": time_, "level": level, "message": message})
549566

550-
logger.debug("Retrieved subset of log messages for scan for task ID %s" % taskid)
567+
logger.debug("[%s] Retrieved scan log messages subset" % taskid)
551568
return jsonize({"success": True, "log": json_log_messages})
552569

553570

@@ -559,14 +576,15 @@ def scan_log(taskid):
559576
json_log_messages = list()
560577

561578
if taskid not in DataStore.tasks:
579+
logger.warning("[%s] Invalid task ID provided to scan_log()")
562580
return jsonize({"success": False, "message": "Invalid task ID"})
563581

564582
# Read all log messages from the IPC database
565583
for time_, level, message in DataStore.current_db.execute(
566584
"SELECT time, level, message FROM logs WHERE taskid = ? ORDER BY id ASC", (taskid,)):
567585
json_log_messages.append({"time": time_, "level": level, "message": message})
568586

569-
logger.debug("Retrieved log messages for scan for task ID %s" % taskid)
587+
logger.debug("[%s] Retrieved scan log messages" % taskid)
570588
return jsonize({"success": True, "log": json_log_messages})
571589

572590

@@ -577,19 +595,23 @@ def download(taskid, target, filename):
577595
Download a certain file from the file system
578596
"""
579597
if taskid not in DataStore.tasks:
598+
logger.warning("[%s] Invalid task ID provided to download()" % taskid)
580599
return jsonize({"success": False, "message": "Invalid task ID"})
581600

582601
# Prevent file path traversal - the lame way
583602
if ".." in target:
603+
logger.warning("[%s] Forbidden path (%s)" % (taskid, target))
584604
return jsonize({"success": False, "message": "Forbidden path"})
585605

586606
path = os.path.join(paths.SQLMAP_OUTPUT_PATH, target)
587607

588608
if os.path.exists(path):
609+
logger.debug("[%s] Retrieved content of file %s" % (taskid, target))
589610
with open(path, 'rb') as inf:
590611
file_content = inf.read()
591612
return jsonize({"success": True, "file": file_content.encode("base64")})
592613
else:
614+
logger.warning("[%s] File does not exist %s" % (taskid, target))
593615
return jsonize({"success": False, "message": "File does not exist"})
594616

595617

0 commit comments

Comments
 (0)