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

Skip to content

Commit ef40779

Browse files
committed
upgraded to use custom subprocessng for non-blocking send and read functions for spawned processes. Added new method to display range of log messages, just in case and improved parsing/unpickling of read log messages
1 parent 2126a5b commit ef40779

1 file changed

Lines changed: 43 additions & 8 deletions

File tree

lib/utils/api.py

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import types
1515

1616
from subprocess import PIPE
17-
from subprocess import Popen
17+
from subprocess import STDOUT
1818

1919
from lib.controller.controller import start
2020
from lib.core.common import unArrayizeValue
@@ -32,6 +32,9 @@
3232
from lib.core.optiondict import optDict
3333
from lib.core.option import init
3434
from lib.core.settings import UNICODE_ENCODING
35+
from lib.core.subprocessng import Popen as execute
36+
from lib.core.subprocessng import send_all
37+
from lib.core.subprocessng import recv_some
3538
from thirdparty.bottle.bottle import abort
3639
from thirdparty.bottle.bottle import error
3740
from thirdparty.bottle.bottle import get
@@ -273,7 +276,7 @@ def scan_start(taskid):
273276
tasks[taskid]["fdLog"] = pipes[taskid][1]
274277

275278
# Launch sqlmap engine
276-
procs[taskid] = Popen("python sqlmap.py --pickled-options %s" % base64pickle(tasks[taskid]), shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=False)
279+
procs[taskid] = execute("python sqlmap.py --pickled-options %s" % base64pickle(tasks[taskid]), shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=False)
277280

278281
return jsonize({"success": True})
279282

@@ -288,11 +291,9 @@ def scan_output(taskid):
288291
if taskid not in tasks:
289292
abort(500, "Invalid task ID")
290293

291-
stdout, stderr = procs[taskid].communicate()
294+
stdout = recv_some(procs[taskid], t=1, e=0)
292295

293-
print "stderr:", stderr
294-
295-
return jsonize({"stdout": stdout, "stderr": stderr})
296+
return jsonize({"stdout": stdout})
296297

297298
@get("/scan/<taskid>/delete")
298299
def scan_delete(taskid):
@@ -309,16 +310,50 @@ def scan_delete(taskid):
309310

310311
return jsonize({"success": True})
311312

312-
# Function to handle scans' logs
313+
# Functions to handle scans' logs
314+
@get("/scan/<taskid>/log/<start>/<end>")
315+
def scan_log_limited(taskid, start, end):
316+
"""
317+
Retrieve the log messages
318+
"""
319+
log = None
320+
321+
if taskid not in tasks:
322+
abort(500, "Invalid task ID")
323+
324+
if not start.isdigit() or not end.isdigit() or end <= start:
325+
abort(500, "Invalid start or end value, must be digits")
326+
327+
start = max(0, int(start)-1)
328+
end = max(1, int(end))
329+
pickledLog = os.read(pipes[taskid][0], 100000)
330+
331+
try:
332+
log = base64unpickle(pickledLog)
333+
log = log[slice(start, end)]
334+
except (KeyError, IndexError, TypeError), e:
335+
logger.error("handled exception when trying to unpickle logger dictionary in scan_log_limited(): %s" % str(e))
336+
337+
return jsonize({"log": log})
338+
313339
@get("/scan/<taskid>/log")
314340
def scan_log(taskid):
315341
"""
316342
Retrieve the log messages
317343
"""
344+
log = None
345+
318346
if taskid not in tasks:
319347
abort(500, "Invalid task ID")
320348

321-
return jsonize({"log": base64unpickle(os.read(pipes[taskid][0], 100000))})
349+
pickledLog = os.read(pipes[taskid][0], 100000)
350+
351+
try:
352+
log = base64unpickle(pickledLog)
353+
except (KeyError, IndexError, TypeError), e:
354+
logger.error("handled exception when trying to unpickle logger dictionary in scan_log(): %s" % str(e))
355+
356+
return jsonize({"log": log})
322357

323358
# Function to handle files inside the output directory
324359
@get("/download/<taskid>/<target>/<filename:path>")

0 commit comments

Comments
 (0)