11
11
12
12
import base64
13
13
import json
14
+ import os
14
15
import re
15
16
import sqlite3
16
17
import sys
19
20
20
21
PY3 = sys .version_info >= (3 , 0 )
21
22
UNICODE_ENCODING = "utf-8"
22
- DEBUG = False
23
+ DEBUG = os . getenv ( 'VULN_SERVER_DEBUG' , '' ). lower () in ( 'true' , '1' , 'yes' , 'on' )
23
24
24
25
if PY3 :
25
26
from http .client import INTERNAL_SERVER_ERROR
@@ -82,12 +83,17 @@ def _(*args, **kwargs):
82
83
83
84
print = _
84
85
86
+ def debug_print (msg ):
87
+ if DEBUG :
88
+ print ("[DEBUG] %s" % msg )
89
+
85
90
class ThreadingServer (ThreadingMixIn , HTTPServer ):
86
91
def finish_request (self , * args , ** kwargs ):
87
92
try :
88
93
HTTPServer .finish_request (self , * args , ** kwargs )
89
94
except Exception :
90
95
if DEBUG :
96
+ debug_print ("Error in finish_request:" )
91
97
traceback .print_exc ()
92
98
93
99
class ReqHandler (BaseHTTPRequestHandler ):
@@ -144,19 +150,26 @@ def do_REQUEST(self):
144
150
try :
145
151
if self .params .get ("echo" , "" ):
146
152
output += "%s<br>" % self .params ["echo" ]
153
+ debug_print ("Echo parameter: %s" % self .params ["echo" ])
147
154
148
155
if self .params .get ("reflect" , "" ):
149
156
output += "%s<br>" % self .params .get ("id" )
157
+ debug_print ("Reflect parameter: %s" % self .params .get ("id" ))
150
158
151
159
with _lock :
152
160
if "query" in self .params :
161
+ debug_print ("Executing query: %s" % self .params ["query" ])
153
162
_cursor .execute (self .params ["query" ])
154
163
elif "id" in self .params :
155
164
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 )
157
168
else :
169
+ debug_print ("Executing query with ID: %s" % self .params ["id" ])
158
170
_cursor .execute ("SELECT * FROM users WHERE id=%s LIMIT 0, 1" % self .params ["id" ])
159
171
results = _cursor .fetchall ()
172
+ debug_print ("Query results: %s" % results )
160
173
161
174
output += "<b>SQL results:</b><br>\n "
162
175
@@ -180,7 +193,9 @@ def do_REQUEST(self):
180
193
output += "</body></html>"
181
194
except Exception as ex :
182
195
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
184
199
185
200
self .send_response (code )
186
201
@@ -213,7 +228,9 @@ def do_POST(self):
213
228
data = self .rfile .read (length )
214
229
data = unquote_plus (data .decode (UNICODE_ENCODING , "ignore" ))
215
230
self .data = data
231
+ debug_print ("Received POST data: %s" % data )
216
232
elif self .headers .get ("Transfer-encoding" ) == "chunked" :
233
+ debug_print ("Processing chunked transfer encoding" )
217
234
data , line = b"" , b""
218
235
count = 0
219
236
@@ -243,13 +260,16 @@ def run(address=LISTEN_ADDRESS, port=LISTEN_PORT):
243
260
try :
244
261
_alive = True
245
262
_server = ThreadingServer ((address , port ), ReqHandler )
263
+ debug_print ("Initializing server at 'http://%s:%d'" % (address , port ))
246
264
print ("[i] running HTTP server at 'http://%s:%d'" % (address , port ))
247
265
_server .serve_forever ()
248
266
except KeyboardInterrupt :
267
+ debug_print ("Received keyboard interrupt" )
249
268
_server .socket .close ()
250
269
raise
251
270
finally :
252
271
_alive = False
272
+ debug_print ("Server stopped" )
253
273
254
274
if __name__ == "__main__" :
255
275
try :
0 commit comments