1111
1212import base64
1313import json
14+ import os
1415import re
1516import sqlite3
1617import sys
1920
2021PY3 = sys .version_info >= (3 , 0 )
2122UNICODE_ENCODING = "utf-8"
22- DEBUG = False
23+ DEBUG = os . getenv ( 'VULN_SERVER_DEBUG' , '' ). lower () in ( 'true' , '1' , 'yes' , 'on' )
2324
2425if PY3 :
2526 from http .client import INTERNAL_SERVER_ERROR
@@ -82,12 +83,17 @@ def _(*args, **kwargs):
8283
8384 print = _
8485
86+ def debug_print (msg ):
87+ if DEBUG :
88+ print ("[DEBUG] %s" % msg )
89+
8590class ThreadingServer (ThreadingMixIn , HTTPServer ):
8691 def finish_request (self , * args , ** kwargs ):
8792 try :
8893 HTTPServer .finish_request (self , * args , ** kwargs )
8994 except Exception :
9095 if DEBUG :
96+ debug_print ("Error in finish_request:" )
9197 traceback .print_exc ()
9298
9399class ReqHandler (BaseHTTPRequestHandler ):
@@ -144,19 +150,26 @@ def do_REQUEST(self):
144150 try :
145151 if self .params .get ("echo" , "" ):
146152 output += "%s<br>" % self .params ["echo" ]
153+ debug_print ("Echo parameter: %s" % self .params ["echo" ])
147154
148155 if self .params .get ("reflect" , "" ):
149156 output += "%s<br>" % self .params .get ("id" )
157+ debug_print ("Reflect parameter: %s" % self .params .get ("id" ))
150158
151159 with _lock :
152160 if "query" in self .params :
161+ debug_print ("Executing query: %s" % self .params ["query" ])
153162 _cursor .execute (self .params ["query" ])
154163 elif "id" in self .params :
155164 if "base64" in self .params :
156- _cursor .execute ("SELECT * FROM users WHERE id=%s LIMIT 0, 1" % base64 .b64decode ("%s===" % self .params ["id" ], altchars = self .params .get ("altchars" )).decode ())
165+ decoded_id = base64 .b64decode ("%s===" % self .params ["id" ], altchars = self .params .get ("altchars" )).decode ()
166+ debug_print ("Decoded base64 ID: %s" % decoded_id )
167+ _cursor .execute ("SELECT * FROM users WHERE id=%s LIMIT 0, 1" % decoded_id )
157168 else :
169+ debug_print ("Executing query with ID: %s" % self .params ["id" ])
158170 _cursor .execute ("SELECT * FROM users WHERE id=%s LIMIT 0, 1" % self .params ["id" ])
159171 results = _cursor .fetchall ()
172+ debug_print ("Query results: %s" % results )
160173
161174 output += "<b>SQL results:</b><br>\n "
162175
@@ -180,7 +193,9 @@ def do_REQUEST(self):
180193 output += "</body></html>"
181194 except Exception as ex :
182195 code = INTERNAL_SERVER_ERROR
183- output = "%s: %s" % (re .search (r"'([^']+)'" , str (type (ex ))).group (1 ), ex )
196+ error_msg = "%s: %s" % (re .search (r"'([^']+)'" , str (type (ex ))).group (1 ), ex )
197+ debug_print ("Error occurred: %s" % error_msg )
198+ output = error_msg
184199
185200 self .send_response (code )
186201
@@ -213,7 +228,9 @@ def do_POST(self):
213228 data = self .rfile .read (length )
214229 data = unquote_plus (data .decode (UNICODE_ENCODING , "ignore" ))
215230 self .data = data
231+ debug_print ("Received POST data: %s" % data )
216232 elif self .headers .get ("Transfer-encoding" ) == "chunked" :
233+ debug_print ("Processing chunked transfer encoding" )
217234 data , line = b"" , b""
218235 count = 0
219236
@@ -243,13 +260,16 @@ def run(address=LISTEN_ADDRESS, port=LISTEN_PORT):
243260 try :
244261 _alive = True
245262 _server = ThreadingServer ((address , port ), ReqHandler )
263+ debug_print ("Initializing server at 'http://%s:%d'" % (address , port ))
246264 print ("[i] running HTTP server at 'http://%s:%d'" % (address , port ))
247265 _server .serve_forever ()
248266 except KeyboardInterrupt :
267+ debug_print ("Received keyboard interrupt" )
249268 _server .socket .close ()
250269 raise
251270 finally :
252271 _alive = False
272+ debug_print ("Server stopped" )
253273
254274if __name__ == "__main__" :
255275 try :
0 commit comments