|
| 1 | +#!/usr/bin/env python |
| 2 | +#coding:utf-8 |
| 3 | +# Author: Zeroh |
| 4 | + |
| 5 | +import re |
| 6 | +import sys |
| 7 | +import Queue |
| 8 | +import threading |
| 9 | +import optparse |
| 10 | +import requests |
| 11 | +from IPy import IP |
| 12 | + |
| 13 | +printLock = threading.Semaphore(1) #lock Screen print |
| 14 | +TimeOut = 5 #request timeout |
| 15 | + |
| 16 | +#User-Agent |
| 17 | +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'} |
| 18 | + |
| 19 | +class scan(): |
| 20 | + |
| 21 | + def __init__(self,cidr,threads_num): |
| 22 | + self.threads_num = threads_num |
| 23 | + self.cidr = IP(cidr) |
| 24 | + #build ip queue |
| 25 | + self.IPs = Queue.Queue() |
| 26 | + for ip in self.cidr: |
| 27 | + ip = str(ip) |
| 28 | + self.IPs.put(ip) |
| 29 | + |
| 30 | + def request(self): |
| 31 | + with threading.Lock(): |
| 32 | + while self.IPs.qsize() > 0: |
| 33 | + ip = self.IPs.get() |
| 34 | + try: |
| 35 | + r = requests.Session().get('http://'+str(ip),headers=header,timeout=TimeOut) |
| 36 | + status = r.status_code |
| 37 | + title = re.search(r'<title>(.*)</title>', r.text) #get the title |
| 38 | + if title: |
| 39 | + title = title.group(1).strip().strip("\r").strip("\n")[:30] |
| 40 | + else: |
| 41 | + title = "None" |
| 42 | + banner = '' |
| 43 | + try: |
| 44 | + banner += r.headers['Server'][:20] #get the server banner |
| 45 | + except:pass |
| 46 | + printLock.acquire() |
| 47 | + print "|%-16s|%-6s|%-20s|%-30s|" % (ip,status,banner,title) |
| 48 | + print "+----------------+------+--------------------+------------------------------+" |
| 49 | + |
| 50 | + #Save log |
| 51 | + with open("./log/"+self.cidr.strNormal(3)+".log",'a') as f: |
| 52 | + f.write(ip+"\n") |
| 53 | + |
| 54 | + except Exception,e: |
| 55 | + printLock.acquire() |
| 56 | + finally: |
| 57 | + printLock.release() |
| 58 | + |
| 59 | + #Multi thread |
| 60 | + def run(self): |
| 61 | + for i in range(self.threads_num): |
| 62 | + t = threading.Thread(target=self.request) |
| 63 | + t.start() |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + parser = optparse.OptionParser("Usage: %prog [options] target") |
| 67 | + parser.add_option("-t", "--thread", dest = "threads_num", |
| 68 | + default = 10, type = "int", |
| 69 | + help = "[optional]number of theads,default=10") |
| 70 | + (options, args) = parser.parse_args() |
| 71 | + if len(args) < 1: |
| 72 | + parser.print_help() |
| 73 | + sys.exit(0) |
| 74 | + |
| 75 | + print "+----------------+------+--------------------+------------------------------+" |
| 76 | + print "| IP |Status| Server | Title |" |
| 77 | + print "+----------------+------+--------------------+------------------------------+" |
| 78 | + |
| 79 | + s = scan(cidr=args[0],threads_num=options.threads_num) |
| 80 | + s.run() |
0 commit comments