1818sys .path .append (os .path .join (os .path .abspath (os .path .dirname (__file__ )), ".." , ".." ))
1919
2020from extra .bottle .bottle import abort
21- from extra .bottle .bottle import debug
2221from extra .bottle .bottle import error
2322from extra .bottle .bottle import get
2423from extra .bottle .bottle import hook
4140from lib .core .settings import RESTAPI_SERVER_PORT
4241
4342# Local global variables
44- options = AttribDict ()
43+ options = {}
44+ output = ""
4545adminid = ""
4646tasks = []
4747
@@ -51,8 +51,6 @@ def jsonize(data):
5151
5252def is_admin (taskid ):
5353 global adminid
54- #print "[INFO] Admin ID: %s" % adminid
55- #print "[INFO] Task ID: %s" % taskid
5654 if adminid != taskid :
5755 return False
5856 else :
@@ -103,7 +101,8 @@ def task_new():
103101 Create new task ID
104102 """
105103 global tasks
106- taskid = hexencode (os .urandom (32 ))
104+ taskid = hexencode (os .urandom (16 ))
105+ options [taskid ] = AttribDict (cmdLineOptions )
107106 tasks .append (taskid )
108107 return jsonize ({"taskid" : taskid })
109108
@@ -144,22 +143,63 @@ def task_flush(taskid):
144143##################################
145144# sqlmap core interact functions #
146145##################################
146+
147+ @get ("/option/<taskid>/list" )
148+ def option_list (taskid ):
149+ """
150+ List options for a certain task ID
151+ """
152+ global options
153+ if taskid not in tasks :
154+ abort (500 , "Invalid task ID" )
155+
156+ return jsonize (options [taskid ])
157+
158+ @post ("/option/<taskid>/get" )
159+ def option_get (taskid ):
160+ """
161+ Get the value of an option (command line switch) for a certain task ID
162+ """
163+ global options
164+ if taskid not in tasks :
165+ abort (500 , "Invalid task ID" )
166+
167+ option = request .json .get ("option" , "" )
168+
169+ if option in options [taskid ]:
170+ print {option : options [taskid ][option ]}
171+ return jsonize ({option : options [taskid ][option ]})
172+ else :
173+ return jsonize ({option : None })
174+
175+ @post ("/option/<taskid>/set" )
176+ def option_set (taskid ):
177+ """
178+ Set an option (command line switch) for a certain task ID
179+ """
180+ global options
181+ if taskid not in tasks :
182+ abort (500 , "Invalid task ID" )
183+
184+ for key , value in request .json .items ():
185+ options [taskid ][key ] = value
186+
187+ return jsonize ({"success" : True })
188+
147189@post ("/scan/<taskid>" )
148190def scan (taskid ):
149191 """
150- Mount a scan with sqlmap
192+ Launch a scan
151193 """
152194 global options
153-
154195 if taskid not in tasks :
155196 abort (500 , "Invalid task ID" )
156197
157198 # Initialize sqlmap engine's options with user's provided options
158199 # within the JSON request
159200 for key , value in request .json .items ():
160- if key != "taskid" :
161- options [key ] = value
162- init (options , True )
201+ options [taskid ][key ] = value
202+ init (options [taskid ], True )
163203
164204 # Launch sqlmap engine in a separate thread
165205 thread = threading .Thread (target = start )
@@ -168,6 +208,29 @@ def scan(taskid):
168208
169209 return jsonize ({"success" : True })
170210
211+ @get ("/scan/<taskid>/status" )
212+ def scan_status (taskid ):
213+ """
214+ Verify if sqlmap core is currently running
215+ """
216+ if taskid not in tasks :
217+ abort (500 , "Invalid task ID" )
218+
219+ return jsonize ({"busy" : kb .get ("busyFlag" )})
220+
221+ @get ("/scan/<taskid>/output" )
222+ def scan_output (taskid ):
223+ """
224+ Read the standard output of sqlmap core execution
225+ """
226+ if taskid not in tasks :
227+ abort (500 , "Invalid task ID" )
228+
229+ global output
230+ sys .stdout .seek (len (output ))
231+ output = sys .stdout .read ()
232+ return jsonize ({"output" : output })
233+
171234@post ("/download/<taskid>/<target>/<filename:path>" )
172235def download (taskid , target , filename ):
173236 """
@@ -182,18 +245,19 @@ def download(taskid, target, filename):
182245 else :
183246 abort (500 )
184247
185- def restAPIrun (host = "0.0.0.0" , port = RESTAPI_SERVER_PORT ):
248+ def restAPIsetup (host = "0.0.0.0" , port = RESTAPI_SERVER_PORT ):
186249 """
187250 Initiate REST-JSON API
188251 """
189252 global adminid
190- global options
191253 global tasks
192- adminid = hexencode (os .urandom (32 ))
254+ adminid = hexencode (os .urandom (16 ))
255+ options [adminid ] = AttribDict (cmdLineOptions )
193256 tasks .append (adminid )
194- options = AttribDict (cmdLineOptions )
195257 logger .info ("Running REST-JSON API server at '%s:%d'.." % (host , port ))
196258 logger .info ("The admin task ID is: %s" % adminid )
259+
260+ def restAPIrun (host = "0.0.0.0" , port = RESTAPI_SERVER_PORT ):
197261 run (host = host , port = port )
198262
199263def client (host , port ):
0 commit comments