-
Notifications
You must be signed in to change notification settings - Fork 46
/
crawler.py
executable file
·276 lines (192 loc) · 6.77 KB
/
crawler.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
# coding=utf-8
'''
crawler.py
'''
import sys
import traceback
import itertools
import time
from Queue import Queue
from LogManager import log as om
# HTTPRequest
from teye_web.http.URL import URL
from teye_web.http.Request import Request
from teye_web.http.Response import Response
# url function
from teye_web.http.function import is_similar_url
# Document Parser
import teye_web.parser.dpCache as dpCache
# wCurl
from wCurl import wcurl
# 404 Check
from teye_util.page_404 import is_404
class Crawler(object):
def __init__(self, depth_limit=1, time_limit=30, req_limit=100, filter_similar=True):
'''
'''
self.root = ''
self._target_domain = ''
self.depth_limit = depth_limit
self.time_limit = time_limit
self.req_limit = req_limit
self._sleeptime = 1
self._url_list = []
self._already_visit_url = set()
self._already_seen_urls = set()
self._already_send_reqs = set()
self._relate_ext = ['html', 'shtm', 'htm', 'shtml']
self._white_ext = ['asp', 'aspx', 'jsp', 'php', 'do', 'action']
self._black_ext = ["ico", "jpg", "gif", "js", "png", "bmp", "css", "zip", "rar", "ttf"]
self._blockwords = ['mailto:', 'javascript:', 'file://', 'tel:']
self.num_urls = 0
self.num_reqs = 0
self._wRequestList = []
self._start_time = None
self._other_domains = set()
def get_discovery_time(self):
'''
爬虫爬行的时间,单位为:分钟
'''
now = time.time()
diff = now - self._start_time
return diff / 60
def _do_with_reqs(self, reqs):
'''
'''
result = []
count = len(reqs)
if reqs is None or count == 0:
return result
for i in xrange(count):
filter = False
filter_url = reqs[i].get_url()
for j in xrange(count - i - 1):
k = i + j + 1
store_url = reqs[k].get_url()
if is_similar_url(filter_url, store_url):
filter = True
break
if not filter:
result.append(reqs[i])
return result
def _get_reqs_from_resp(self, response):
'''
'''
new_reqs = []
try:
doc_parser = dpCache.dpc.getDocumentParserFor(response)
except Exception, e:
pass
else:
re_urls, tag_urls = doc_parser.get_get_urls()
form_reqs = doc_parser.get_form_reqs()
seen = set()
for new_url in itertools.chain(re_urls, tag_urls):
if new_url in seen:
continue
seen.add(new_url)
if new_url.get_host() != self._target_domain:
if new_url.get_host() not in self._other_domains:
self._other_domains.add(new_url.get_host())
continue
if new_url not in self._url_list:
self._url_list.append(new_url)
wreq = self._url_to_req(new_url, response)
if wreq not in self._wRequestList:
new_reqs.append(wreq)
self._wRequestList.append(wreq)
for item in form_reqs:
if item not in self._wRequestList:
new_reqs.append(item)
self._wRequestList.append(item)
return new_reqs
def _url_to_req(self, new_url, response, method="GET"):
'''
'''
req = Request(new_url)
req.set_method(method)
new_referer = response.get_url()
req.set_referer(new_referer)
new_cookies = response.get_cookies()
req.set_cookies(new_cookies)
return req
def crawl(self, root_url):
'''
将URL对象存入到队列
'''
if not isinstance(root_url, URL):
root_url_obj = URL(root_url)
else:
root_url_obj = root_url
self._target_domain = root_url_obj.get_host()
self._url_list.append(root_url_obj)
root_req = Request(root_url_obj)
q = Queue()
q.put((root_req, 0))
self._start_time = time.time()
while True:
if q.empty():
print "reqs empty break"
break
this_req, depth = q.get()
# 将静态链接进行过滤
if this_req.get_url().get_ext() in self._black_ext:
continue
# 控制爬行的深度
if depth > self.depth_limit:
print "depth limit break"
break
# 控制爬行的时间
if self.get_discovery_time() > self.time_limit:
print "time limit break"
break
# 控制爬行的链接数,避免内存泄露
if self.num_reqs > self.req_limit:
print "reqs num limit break"
break
if this_req in self._already_send_reqs:
continue
try:
self._already_send_reqs.add(this_req)
om.info("%s Request:%s" % (this_req.get_method(), this_req.get_url().url_string))
response = None
try:
response = wcurl._send_req(this_req)
except Exception, e:
print str(e)
pass
if is_404(response):
continue
if response is None:
continue
# 获取HTTP响应中的请求
new_reqs = self._get_reqs_from_resp(response)
# 过滤相似和包含的请求
filter_reqs = self._do_with_reqs(new_reqs)
depth = depth + 1
for req in filter_reqs:
q.put((req, depth))
self.num_reqs = len(self._already_send_reqs)
om.info("Already Send Reqs:" + str(self.num_reqs) + " Left Reqs:" + str(q.qsize()))
except Exception, e:
traceback.print_exc()
om.info("ERROR: Can't process url '%s' (%s)" % (this_req.get_url(), e))
continue
time.sleep(self._sleeptime)
return self._do_with_reqs(self._wRequestList)
if __name__ == "__main__":
'''
'''
# url1="http://testphp.vulnweb.com/showimage.php"
# url2="http://testphp.vulnweb.com/showimage.php?id=1"
# print is_similar_url(url1,url2)
# sys.exit()
w = Crawler()
wurl = "http://192.168.1.105:8080/wavsep/active/index-sql.jsp"
a = w.crawl(wurl)
for item in a:
# print "\r\n"
# print item
print item.get_url()
print "Found URL Num:" + str(len(a))
sys.exit()