Thread classes outsourced to AnmapThread.py
Ursprung
26b4dbba0d
Commit
5213448211
@ -0,0 +1,100 @@
|
|||||||
|
from nmap import PortScanner
|
||||||
|
from threading import Thread
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
|
class AnmapThread(Thread):
|
||||||
|
def __init__(self, hostname, ports, verbose, out):
|
||||||
|
Thread.__init__(self)
|
||||||
|
self.host = hostname
|
||||||
|
self.ports = ports
|
||||||
|
self.nm = PortScanner()
|
||||||
|
self.verbose = verbose
|
||||||
|
self.daemon = True
|
||||||
|
self.out = out
|
||||||
|
|
||||||
|
|
||||||
|
class ThoroughAnmapThread(AnmapThread):
|
||||||
|
def run(self):
|
||||||
|
log("Starting thorough scan on " + self.host, self.verbose)
|
||||||
|
self.nm.scan(self.host, "1," + ",".join(self.ports),
|
||||||
|
arguments='-sSVC -A -Pn{}'.format(output(self.out, self.host, 2)))
|
||||||
|
if self.out:
|
||||||
|
with open(output(True, self.host, 5), "w") as out:
|
||||||
|
out.write(self.nm.get_nmap_last_output())
|
||||||
|
host = self.nm[self.host]
|
||||||
|
for p in host.all_tcp():
|
||||||
|
if p == 1:
|
||||||
|
continue
|
||||||
|
log("Port {}/tcp: {}".format(p, host['tcp'][p]), self.verbose)
|
||||||
|
log("Finished thorough scan on " + self.host, self.verbose)
|
||||||
|
|
||||||
|
|
||||||
|
class UDPAnmapThread(AnmapThread):
|
||||||
|
def run(self):
|
||||||
|
log("Starting UDP scan on " + self.host, self.verbose)
|
||||||
|
self.nm.scan(self.host, arguments='-sVCU -A -Pn --top-ports {}{}'.
|
||||||
|
format(self.ports, output(self.out, self.host, 3)))
|
||||||
|
if self.out:
|
||||||
|
with open(output(True, self.host, 6), "w") as out:
|
||||||
|
out.write(self.nm.get_nmap_last_output())
|
||||||
|
host = self.nm[self.host]
|
||||||
|
for p in host.all_udp():
|
||||||
|
log("Port {}/udp: {}".format(p, host['udp'][p]), self.verbose)
|
||||||
|
log("Finished UDP scan on " + self.host, self.verbose)
|
||||||
|
|
||||||
|
|
||||||
|
class BaseAnmapThread(AnmapThread):
|
||||||
|
def __init__(self, hostname, ports, verbose, out):
|
||||||
|
AnmapThread.__init__(self, hostname, ports, verbose, out)
|
||||||
|
self.host_list = dict()
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
log("Starting quick scan", self.verbose)
|
||||||
|
self.nm.scan(self.host, arguments='-sS -Pn -p{}{}'.format(self.ports, output(self.out, self.host, 1)))
|
||||||
|
if self.out:
|
||||||
|
with open(output(True, self.host, 4), "w") as out:
|
||||||
|
out.write(self.nm.get_nmap_last_output())
|
||||||
|
log("Finished quick scan", self.verbose)
|
||||||
|
for hostname in self.nm.all_hosts():
|
||||||
|
host = self.nm[hostname]
|
||||||
|
port_list = list()
|
||||||
|
for p in host.all_tcp():
|
||||||
|
if self.nm[hostname]['tcp'][p]['state'] == 'open':
|
||||||
|
port_list.append(str(p))
|
||||||
|
if port_list is not list():
|
||||||
|
self.host_list[hostname] = port_list
|
||||||
|
|
||||||
|
def rjoin(self):
|
||||||
|
Thread.join(self)
|
||||||
|
return self.host_list
|
||||||
|
|
||||||
|
|
||||||
|
def output(o, host, st):
|
||||||
|
host = host.replace("/", "x")
|
||||||
|
host = host.replace(" ", "")
|
||||||
|
if not o:
|
||||||
|
return ""
|
||||||
|
if st == 1:
|
||||||
|
return " -oG nmap_{}_S_{}.gnmap".format(host, date())
|
||||||
|
if st == 2:
|
||||||
|
return " -oG nmap_{}_SVCA_{}.gnmap".format(host, date())
|
||||||
|
if st == 3:
|
||||||
|
return " -oG nmap_{}_VCUA_{}.gnmap".format(host, date())
|
||||||
|
if st == 4:
|
||||||
|
return "nmap_{}_S_{}.xml".format(host, date())
|
||||||
|
if st == 5:
|
||||||
|
return "nmap_{}_SVCA_{}.xml".format(host, date())
|
||||||
|
if st == 6:
|
||||||
|
return "nmap_{}_VCUA_{}.xml".format(host, date())
|
||||||
|
|
||||||
|
|
||||||
|
def log(message, verbose):
|
||||||
|
if verbose:
|
||||||
|
print("{}: {}".format(date(True), message))
|
||||||
|
|
||||||
|
|
||||||
|
def date(long=False):
|
||||||
|
if long:
|
||||||
|
return datetime.now().strftime("%Y-%m-%d_%H%M%S")
|
||||||
|
return datetime.now().strftime("%Y-%m-%d_%H%M")
|
Laden…
In neuem Issue referenzieren