66"""
77
88import json
9+ import logging
910import optparse
1011import os
1112import shutil
1213import sys
14+ import StringIO
1315import tempfile
1416import threading
15-
16- sys .path .append (os .path .join (os .path .abspath (os .path .dirname (__file__ )), ".." , ".." ))
17+ import types
1718
1819from extra .bottle .bottle import abort
1920from extra .bottle .bottle import error
2627from extra .bottle .bottle import static_file
2728from extra .bottle .bottle import template
2829from lib .controller .controller import start
30+ from lib .core .common import setPaths
2931from lib .core .convert import hexencode
32+ from lib .core .convert import stdoutencode
3033from lib .core .data import paths
3134from lib .core .datatype import AttribDict
3235from lib .core .data import cmdLineOptions
3336from lib .core .data import kb
3437from lib .core .data import logger
38+ from lib .core .log import FORMATTER
39+ from lib .core .log import LOGGER_HANDLER
3540from lib .core .log import LOGGER_OUTPUT
3641from lib .core .exception import SqlmapMissingDependence
3742from lib .core .option import init
3843from lib .core .settings import UNICODE_ENCODING
39- from lib .core .settings import RESTAPI_SERVER_PORT
44+ from _sqlmap import modulePath
45+
46+ RESTAPI_SERVER_HOST = "127.0.0.1"
47+ RESTAPI_SERVER_PORT = 8775
4048
4149# Local global variables
4250adminid = ""
@@ -238,6 +246,8 @@ def scan_start(taskid):
238246 for key , value in request .json .items ():
239247 tasks [taskid ][key ] = value
240248
249+ print "TASKS:" , tasks
250+
241251 # Overwrite output directory (oDir) value to a temporary directory
242252 tasks [taskid ].oDir = tempfile .mkdtemp (prefix = "sqlmap-" )
243253
@@ -317,9 +327,9 @@ def download(taskid, target, filename):
317327 else :
318328 abort (500 )
319329
320- def restAPISetup (host = "0.0.0.0" , port = RESTAPI_SERVER_PORT ):
330+ def restAPIRun (host = "0.0.0.0" , port = RESTAPI_SERVER_PORT ):
321331 """
322- Setup REST-JSON API
332+ REST-JSON API server
323333 """
324334 global adminid
325335 global tasks
@@ -330,38 +340,56 @@ def restAPISetup(host="0.0.0.0", port=RESTAPI_SERVER_PORT):
330340 logger .info ("running REST-JSON API server at '%s:%d'.." % (host , port ))
331341 logger .info ("the admin task ID is: %s" % adminid )
332342
333- def restAPIRun (host = "0.0.0.0" , port = RESTAPI_SERVER_PORT ):
334- """
335- Run REST-JSON API
336- """
343+ # Wrap logger stdout onto a custom file descriptor (LOGGER_OUTPUT)
344+ def emit (self , record ):
345+ message = stdoutencode (FORMATTER .format (record ))
346+ print >> LOGGER_OUTPUT , message .strip ('\r ' )
347+
348+ LOGGER_HANDLER .emit = types .MethodType (emit , LOGGER_HANDLER , type (LOGGER_HANDLER ))
349+
350+ # Wrap standard output onto a custom file descriptor
351+ sys .stdout = StringIO .StringIO ()
352+ #sys.stderr = StringIO.StringIO()
353+
354+ # Run RESTful API
337355 run (host = host , port = port , quiet = False , debug = False )
338356
339- def client (host , port ):
357+ def client (host = RESTAPI_SERVER_HOST , port = RESTAPI_SERVER_PORT ):
340358 """
341359 REST-JSON API client
342360 """
343361 addr = "http://%s:%d" % (host , port )
344- print "[*] starting debug REST-JSON client to '%s'..." % addr
362+ logger . info ( " starting debug REST-JSON client to '%s'..." % addr )
345363
346- # TODO: write a simple client with urllib2 , for now use curl from command line
347- print "[!] not yet implemented, use curl from command line instead for now, for example:"
348- print "\n \t $ curl --proxy http://127.0.0.1:8080 http://127.0.0.1:%s /task/new" % port
349- 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
350- print "\t $ curl --proxy http://127.0.0.1:8080 http://127.0.0.1:8775/scan/<taskID>/ output"
351- print "\t $ curl --proxy http://127.0.0.1:8080 http://127.0.0.1:8775/scan/<taskID>/ log\n "
364+ # TODO: write a simple client with requests , for now use curl from command line
365+ logger . error ( " not yet implemented, use curl from command line instead for now, for example:")
366+ print "\n \t $ curl http://%s:%d /task/new" % ( host , port )
367+ print "\t $ curl -H \" Content-Type: application/json\" -X POST -d '{\" url\" : \" http://testphp.vulnweb.com/artists.php?artist=1\" }' http://%s :%d/scan/:taskid /start" % ( host , port )
368+ print "\t $ curl http://%s:%d/scan/:taskid/ output" % ( host , port )
369+ print "\t $ curl http://%s:%d/scan/:taskid/ log\n " % ( host , port )
352370
353371if __name__ == "__main__" :
354372 """
355- REST-JSON API wrapper function
373+ REST-JSON API main function
356374 """
375+ # Set default logging level to debug
376+ logger .setLevel (logging .DEBUG )
377+
378+ paths .SQLMAP_ROOT_PATH = modulePath ()
379+ setPaths ()
380+
381+ # Enforce batch mode and disable coloring
382+ cmdLineOptions .batch = True
383+ cmdLineOptions .disableColoring = True
384+
357385 parser = optparse .OptionParser ()
358386 parser .add_option ("-s" , "--server" , help = "Act as a REST-JSON API server" , default = RESTAPI_SERVER_PORT , action = "store_true" )
359387 parser .add_option ("-c" , "--client" , help = "Act as a REST-JSON API client" , default = RESTAPI_SERVER_PORT , action = "store_true" )
360- parser .add_option ("-H" , "--host" , help = "Host of the REST-JSON API server" , default = "0.0.0.0" , action = "store" )
361- parser .add_option ("-p" , "--port" , help = "Port of the the REST-JSON API server" , default = RESTAPI_SERVER_PORT , action = "store" )
388+ parser .add_option ("-H" , "--host" , help = "Host of the REST-JSON API server" , default = RESTAPI_SERVER_HOST , action = "store" )
389+ parser .add_option ("-p" , "--port" , help = "Port of the the REST-JSON API server" , default = RESTAPI_SERVER_PORT , type = "int" , action = "store" )
362390 (args , _ ) = parser .parse_args ()
363391
364392 if args .server is True :
365- restAPIrun (args .host , args .port )
393+ restAPIRun (args .host , args .port )
366394 elif args .client is True :
367395 client (args .host , args .port )
0 commit comments