Created
April 25, 2018 09:13
-
-
Save VGostyuzhov/76e2916791f46d1373ce6c9046fb0c3a to your computer and use it in GitHub Desktop.
Parse nmap output to find POODLE vulnerabilities.
This file contains 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
import xml.etree.ElementTree as ET | |
import os, re, pandas | |
class Host: | |
def __init__(self, ip, ports=[]): | |
self.ip = ip | |
self.ports = [] | |
def add_port(self, port): | |
self.ports.append(port) | |
class Port: | |
def __init__(self, number, service = '', script_output = ''): | |
self.number = number | |
self.service = service | |
self.script_output = script_output | |
def ImportNmapFromXML(xml_root): | |
file_hosts = [] | |
for host in xml_root.findall("./host"): | |
h_ip = host.find("./address[@addrtype='ipv4']").attrib['addr'] | |
new_host = Host(h_ip) | |
vulnerable = False | |
for port in host.findall("./ports/port"): | |
p_number = port.attrib['portid'] | |
p_protocol = port.attrib['protocol'] | |
if port.find('./script') is not None: | |
vulnerable = True | |
p_script_output = port.find('./script').attrib['output'] | |
else: | |
p_script_output = '' | |
if port.find('./service') is not None: | |
p_service = port.find('./service').attrib['name'] | |
else: | |
p_service ='' | |
new_port = Port(p_number, p_service, p_script_output) | |
new_host.add_port(new_port) | |
if vulnerable: file_hosts.append(new_host) | |
return file_hosts | |
def parseNmapFiles(folder): | |
hosts = [] | |
for filename in os.listdir(folder): | |
if filename.endswith('xml'): #Parse XML files | |
try: | |
tree = ET.parse(folder + '/' + filename) | |
print folder + '/' + filename | |
except: | |
print "\nThere was an error with parsing XML file: %s \n" % folder + '/' + filename | |
continue | |
hosts += ImportNmapFromXML(tree.getroot()) | |
return hosts | |
def convertToDict(hosts): | |
results = [] | |
for host in hosts: | |
for port in host.ports: | |
host_dict = {} | |
host_dict['IP'] = host.ip | |
host_dict['Port'] = port.number | |
host_dict['Vulnerability'] = port.script_output | |
host_dict['Service'] = port.service | |
results.append(host_dict) | |
return results | |
def saveToExcel(results): | |
result_xl = pandas.ExcelWriter('nmap_poodle.xls') | |
result_nmap = pandas.DataFrame(results) | |
result_nmap.to_excel(result_xl, "POODLE") | |
result_xl.save() | |
if __name__ == "__main__": | |
nmap_folder = './Sources' | |
hosts = parseNmapFiles(nmap_folder) | |
results = convertToDict(hosts) | |
saveToExcel(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment