Last active
December 11, 2019 02:03
-
-
Save Oritz/79e6e9c246771c6173c5feed9792e76f to your computer and use it in GitHub Desktop.
Nmap 多线程扫描
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/usr/bin/env python | |
#-*-coding:utf8-*- | |
import Queue | |
import time | |
import sys | |
import logging | |
import threading | |
from libnmap.process import NmapProcess | |
from libnmap.parser import NmapParser, NmapParserException | |
from netaddr import IPNetwork,IPRange | |
scanResult = [] | |
threads = [] | |
ips = [] | |
logging.basicConfig(level=logging.DEBUG,\ | |
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',\ | |
datefmt='%a, %d %b %Y %H:%M:%S', filename= 'info.log', filemode='wa') | |
class Mynmap(object): | |
def __init__(self, scanIp): | |
self.scanIp = scanIp | |
def start_nmap(self): | |
self.nmap_scan = NmapProcess(self.scanIp, options='-sS -T4 --open -p 22-65534') | |
self.rc = self.nmap_scan.run() | |
if self.nmap_scan.rc == 0: | |
return self.nmap_scan.stdout | |
else: | |
print self.nmap_scan.stderr | |
logging.info('nmap scan error'+ self.scanIp) | |
return False | |
def start_parse(self): | |
# nmap xml parse func | |
try: | |
self.start_nmap_scan = self.start_nmap() | |
if self.start_nmap is not False: | |
self.parse = NmapParser.parse(self.start_nmap_scan) | |
self.nmap_scanreport = self.startReport() | |
else: | |
sys.exit(0) | |
except NmapParserException as e: | |
logging.info(e) | |
sys.exit(0) | |
def startReport(self): | |
self.report = self.parse | |
if self.report: | |
for self.host in self.report.hosts: | |
for self.serv in self.host.services: | |
if self.serv.state == 'open': | |
service = self.serv.service if self.serv.service else 'unknown' | |
result = ("%s\t%s\t%s" % (self.host.address, self.serv.port, service)) | |
print result | |
scanResult.append(result) | |
class MyThread(threading.Thread): | |
def __init__(self, inputi): | |
self.inputi = inputi | |
threading.Thread.__init__(self) | |
def run(self): | |
while True: | |
if self.inputi.qsize() > 0: | |
self.ip = self.inputi.get() | |
self.myNmap = Mynmap(self.ip) | |
self.myNmap.start_parse() | |
else: | |
break | |
class Mscan(object): | |
def start_nmap_scan(self, ip_file): | |
q = Queue.Queue(0) | |
lists = self.parse_file(ip_file) | |
for ip_list in lists: | |
q.put(ip_list) | |
for j in range(80): | |
threads.append(MyThread(q)) | |
for x in threads: | |
x.start() | |
for y in threads: | |
y.join() | |
result_filename = ip_file + '_' + str(time.time()) | |
with open(result_filename, 'w') as result_file: | |
for result in scanResult: | |
result_file.write("%s\n" % result) | |
return scanResult | |
def parse_file(self, ip_file): | |
with open(ip_file) as data: | |
for raw_ip in data.readlines(): | |
raw_ip = raw_ip.strip() | |
if raw_ip.find('/') != -1: | |
for ip in IPNetwork(raw_ip): | |
ips.append(str(ip)) | |
elif raw_ip.find('-') != -1: | |
ip_range = raw_ip.split('-', 1) | |
for ip in IPRange(ip_range[0], ip_range[1]): | |
ips.append(str(ip)) | |
else: ips.append(raw_ip) | |
return ips | |
if __name__ == '__main__': | |
ip_file = sys.argv[1] | |
start = time.time() | |
Mscan = Mscan() | |
print Mscan.start_nmap_scan(ip_file) | |
end = time.time() | |
print "Total time: " + str(end - start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment