-
Notifications
You must be signed in to change notification settings - Fork 30
/
scanner.py
81 lines (70 loc) · 2.2 KB
/
scanner.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
# coding=utf-8
'''
@Author:XingSongYan
@CreateDate: Tue Jan 19 16:32:19 HKT 2016
@FileName:
@Description:个人练手写的网站后台扫描器
'''
import requests
from multiprocessing.dummy import Pool as ThreadPool
from optparse import OptionParser
TIMEOUT = 5
def _get_args():
parser = OptionParser(usage="usage: %prog [options] args")
parser.add_option("-u", "--url", help="Target URL", dest='url')
parser.add_option("-d", "--dic", help="Dictionary path", dest='dic')
parser.add_option("-n", "--number", help="Number of Thread,Default 5",
dest="num", type="int", default=5)
parser.add_option("-t", "--timeout", help="Timeout,Default 5",
dest="timeout", type="int", default=5)
opts, args = parser.parse_args()
return opts
def _mult_getdata(alist, pro_num):
'''开启多线程探测
alist = [(url,dir),(url,dir)...]
'''
pool = ThreadPool(processes=pro_num)
result = pool.map(_check, alist)
pool.close()
pool.join()
return result
def _check(target_list):
url, dirstr = target_list
if dirstr.startswith('/'):
dirstr = dirstr[1:] # 字典中某些路径开头包括/,这里进行统一
dirstr = dirstr.replace("\r\n", "")
final_url = "%s/%s" % (url, dirstr)
print final_url
try:
r = requests.head(final_url, timeout=TIMEOUT)
if r.status_code not in [404, 500]:
return final_url
except Exception as e:
print e
return ''
def main():
opts = _get_args()
url = opts.url
if url.startswith("http") is False:
url = "http://%s" % url
if url.endswith("/"):
url = url[:-1]
num = opts.num
dict_path = opts.dic or "all.txt"
global TIMEOUT
TIMEOUT = opts.timeout
if url:
f = open(dict_path).read()
dirs = f.split("\r\n")
tempdirs = []
for r in dirs:
r = r.strip()
#r = r.replace("\\","/")
tempdirs.append((url, r))
result = _mult_getdata(tempdirs, num)
print "find dirs:", [x for x in result if x]
else:
print "Missing a mandatory option -u,use -h for help."
return 0
if __name__ == "__main__":
main()