forked from zer0h/httpscan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpscan.py
More file actions
101 lines (89 loc) · 3.07 KB
/
httpscan.py
File metadata and controls
101 lines (89 loc) · 3.07 KB
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
#!/usr/bin/env python
#coding:utf-8
# Author: Zeroh
# Modified: Cobranail
import re
import sys
import Queue
import threading
import optparse
import requests
from IPy import IP
printLock = threading.Semaphore(1) #lock Screen print
TimeOut = (0.5,1) #request timeout, for slow connection
#User-Agent
header = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36','Connection':'close'}
class scan():
def __init__(self,cidr, threads_num, ports):
self.threads_num = threads_num
self.ports=[]
ports_set=[]
if ',' in ports:
ports_set=ports.split(',')
else:
ports_set=[ports]
#print ports_set
for ps in ports_set:
if '-' in ps:
pp = ps.split('-')
self.ports = self.ports+range(int(pp[0]),int(pp[1])+1)
else:
self.ports.append(ps)
#print self.ports
self.cidr = IP(cidr)
#build ip queue
self.IPs = Queue.Queue()
for ip in self.cidr:
for port in self.ports:
ipp = str(ip)+':'+str(port)
#print ipp
self.IPs.put(ipp)
def request(self):
with threading.Lock():
while self.IPs.qsize() > 0:
ip = self.IPs.get()
#print str(ip)
try:
r = requests.Session().get('http://'+str(ip),headers=header,timeout=TimeOut)
status = r.status_code
title = re.search(r'<title>(.*)</title>', r.text) #get the title
if title:
title = title.group(1).strip().strip("\r").strip("\n")[:30]
else:
title = "None"
banner = ''
try:
banner += r.headers['Server'][:20] #get the server banner
except:pass
printLock.acquire()
print "|%-24s|%-6s|%-20s|%-30s|" % (ip,status,banner,title)
print "+------------------------+------+--------------------+------------------------------+"
#Save log
with open("./log/"+self.cidr.strNormal(3)+".log",'a') as f:
f.write(ip+"\n")
except Exception,e:
printLock.acquire()
finally:
printLock.release()
#Multi thread
def run(self):
for i in range(self.threads_num):
t = threading.Thread(target=self.request)
t.start()
if __name__ == "__main__":
parser = optparse.OptionParser("Usage: %prog [options] target")
parser.add_option("-t", "--thread", dest = "threads_num",
default = 10, type = "int",
help = "[optional]number of theads,default=10")
parser.add_option("-p", "--ports", dest = "ports",
default = '80', type = "string",
help = "[optional]number of theads,default=10")
(options, args) = parser.parse_args()
if len(args) < 1:
parser.print_help()
sys.exit(0)
print "+------------------------+------+--------------------+------------------------------+"
print "| IP |Status| Server | Title |"
print "+------------------------+------+--------------------+------------------------------+"
s = scan(cidr=args[0],threads_num=options.threads_num, ports=options.ports)
s.run()