From c6db883a31d2de0adca7e2be8c87a4aa26aee319 Mon Sep 17 00:00:00 2001 From: Brendan McDevitt Date: Sun, 7 Jan 2018 05:26:32 -0500 Subject: [PATCH] day 6 - nmap python --- ...018-01-01-100-days-of-code-day006.markdown | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 _100-days-of-code/2018-01-01-100-days-of-code-day006.markdown diff --git a/_100-days-of-code/2018-01-01-100-days-of-code-day006.markdown b/_100-days-of-code/2018-01-01-100-days-of-code-day006.markdown new file mode 100644 index 0000000..37a9318 --- /dev/null +++ b/_100-days-of-code/2018-01-01-100-days-of-code-day006.markdown @@ -0,0 +1,88 @@ +--- +layout: post +title: "day 6" +date: 2018-01-06 +categories: programming +--- + +# 100 Days of Code + +### Day 6: +Today I made use of the python-nmap library and made a class that I will be +able to import into future modules. I started out making things like a ping sweeping tool and +banner grabber, as well as a ssl cipher suite checker. I made a subclass of the nmap +PortScanner class to start so I have access to all of those utilities. Subclasses are something I have been looking into recently. +It is an easy way to add existing functionality to another object. This class will end up being the base +library that I use when I make my own port scanning scripts. here is what I +have so far: + +```python +class NmapUtility(nmap.PortScanner): + + def __init__(self, hostname, hosts=False): + """ Initialize with hostname and optional list of hosts """ + self.hostname = hostname + + def scan_host(self, hostname, portrange): + """ Scan a host using nmap.scan """ + + return self.scan(hostname, portrange) + + def scan_hosts(self, hosts): + """ Scan a list of hosts """ + pass + + def ping_sweep(self, hosts): + """ Ping sweep a list of hosts """ + self.scan(hosts=hosts, arguments='-n -sP -PE -PA21,23,80,3389') + hosts_list = [(x, self[x]['status']['state']) for x in self.all_hosts()] + for host, status in hosts_list: + print('{0}:{1}'.format(host, status)) + + def nmap_version(self): + """ Get nmap version being used """ + + return self.nmapVersion() + + def command_line(self): + """ Run nmap.command_line """ + + return self.command_line + + def cipher_check(self, hostname, portrange): + """ Run --script ssl-enum-ciphers on hostname """ + + return self.scan(hostname, + portrange, + arguments='--script ssl-enum-ciphers') + + def get_csv(self): + """ Run scan.csv() """ + + return self.csv() + + def all_tcp(self, hostname=False): + """ Get all ports for tcp protocol in sorted output """ + if hostname: + return self[hostname].all_tcp() + + return self[self.hostname].all_tcp() + + def all_udp(self, hostname=False): + """ Get all ports for udp protocol in sorted output + requires scanHost() or scan() to be run first + """ + if hostname: + return self[hostname].all_udp() + + return self[self.hostname].all_udp() + + def banner_grab(self, portrange, hostname=False): + """ Grab banners from ports """ + + if hostname: + return self.scan(hostname, portrange, + arguments='-sV --script=banner') + return self.scan(self.hostname, portrange, + arguments='-sV --script=banner') +