📄 nmapparser.py
字号:
# Status element self.write_parser.startElement("status", Attributes(dict(state=host.state))) self.write_parser.endElement("status") ################## # Address elements ## IPv4 if type(host.ip) == type({}): self.write_parser.startElement("address", Attributes(dict(addr=host.ip.get("addr", ""), vendor=host.ip.get("vendor", ""), addrtype=host.ip.get("type", "")))) self.write_parser.endElement("address") ## IPv6 if type(host.ipv6) == type({}): self.write_parser.startElement("address", Attributes(dict(addr=host.ipv6.get("addr", ""), vendor=host.ipv6.get("vendor", ""), addrtype=host.ipv6.get("type", "")))) self.write_parser.endElement("address") ## MAC if type(host.mac) == type({}): self.write_parser.startElement("address", Attributes(dict(addr=host.mac.get("addr", ""), vendor=host.mac.get("vendor", ""), addrtype=host.mac.get("type", "")))) self.write_parser.endElement("address") # End of Address elements ######################### ################### # Hostnames element self.write_parser.startElement("hostnames", Attributes({})) for hname in host.hostnames: if type(hname) == type({}): self.write_parser.startElement("hostname", Attributes(dict(name = hname.get("hostname", ""), type = hname.get("hostname_type", "")))) self.write_parser.endElement("hostname") self.write_parser.endElement("hostnames") # End of Hostnames element ########################## ############### # Ports element self.write_parser.startElement("ports", Attributes({})) for ps in host.ports: ## Extraports elements for ext in ps["extraports"]: if type(ext) == type({}): self.write_parser.startElement("extraports", Attributes(dict(count = ext.get("count", ""), state = ext.get("state", "")))) self.write_parser.endElement("extraports") ## Port elements for p in ps["port"]: if type(p) == type({}): self.write_parser.startElement("port", Attributes(dict(portid = p.get("portid", ""), protocol = p.get("protocol", "")))) ### Port state self.write_parser.startElement("state", Attributes(dict(state=p.get("port_state", "")))) self.write_parser.endElement("state") ### Port service info self.write_parser.startElement("service", Attributes(dict(conf = p.get("service_conf", ""), method = p.get("service_method", ""), name = p.get("service_name", ""), product = p.get("service_product", ""), version = p.get("service_version", ""), extrainfo = p.get("service_extrainfo", "")\ ))) self.write_parser.endElement("service") self.write_parser.endElement("port") self.write_parser.endElement("ports") # End of Ports element ###################### ############ # OS element self.write_parser.startElement("os", Attributes({})) ## Ports used elements for pu in host.ports_used: if type(pu) == type({}): self.write_parser.startElement("portused", Attributes(dict(state = pu.get("state", ""), proto = pu.get("proto", ""), portid = pu.get("portid", "")))) self.write_parser.endElement("portused") ## Osclass elements for oc in host.osclasses: if type(oc) == type({}): self.write_parser.startElement("osclass", Attributes(dict(vendor = oc.get("vendor", ""), osfamily = oc.get("osfamily", ""), type = oc.get("type", ""), osgen = oc.get("osgen", ""), accuracy = oc.get("accuracy", "")))) self.write_parser.endElement("osclass") ## Osmatch elements if type(host.osmatch) == type({}): self.write_parser.startElement("osmatch", Attributes(dict(name = host.osmatch.get("name", ""), accuracy = host.osmatch.get("accuracy", "")))) self.write_parser.endElement("osmatch") self.write_parser.endElement("os") # End of OS element ################### # Uptime element if type(host.uptime) == type({}): self.write_parser.startElement("uptime", Attributes(dict(seconds = host.uptime.get("seconds", ""), lastboot = host.uptime.get("lastboot", "")))) self.write_parser.endElement("uptime") ##################### # Sequences elementes ## TCP Sequence element # Cannot use dict() here, because of the 'class' attribute. if type(host.tcpsequence) == type({}): self.write_parser.startElement("tcpsequence", Attributes({"index":host.tcpsequence.get("index", ""), "class":host.tcpsequence.get("class", ""), "difficulty":host.tcpsequence.get("difficulty", ""), "values":host.tcpsequence.get("values", "")})) self.write_parser.endElement("tcpsequence") ## IP ID Sequence element if type(host.ipidsequence) == type({}): self.write_parser.startElement("ipidsequence", Attributes({"class":host.ipidsequence.get("class", ""), "values":host.ipidsequence.get("values", "")})) self.write_parser.endElement("ipidsequence") ## TCP TS Sequence element if type(host.tcptssequence) == type({}): self.write_parser.startElement("tcptssequence", Attributes({"class":host.tcptssequence.get("class", ""), "values":host.tcptssequence.get("values", "")})) self.write_parser.endElement("tcptssequence") # End of sequences elements ########################### # End host element self.write_parser.endElement("host") def _write_debugging(self): self.write_parser.startElement("debugging", Attributes(dict( level=str(self.debugging_level)))) self.write_parser.endElement("debugging") def _write_verbose(self): self.write_parser.startElement("verbose", Attributes(dict( level=str(self.verbose_level)))) self.write_parser.endElement("verbose") def _write_scaninfo(self): for scan in self.scaninfo: if type(scan) == type({}): self.write_parser.startElement("scaninfo", Attributes(dict(type = scan.get("type", ""), protocol = scan.get("protocol", ""), numservices = scan.get("numservices", ""), services = scan.get("services", "")))) self.write_parser.endElement("scaninfo") def _write_nmaprun(self): self.write_parser.startElement("nmaprun", Attributes(dict(annotation = str(self.profile_annotation), args = str(self.nmap_command), description = str(self.profile_description), hint = str(self.profile_hint), nmap_output = str(self.nmap_output), options = str(self.profile_options), profile = str(self.profile), profile_name = str(self.profile_name), scanner = str(self.scanner), start = str(self.start), startstr = str(self.formated_date), target = str(self.target), version = str(self.scanner_version), scan_name = str(self.scan_name)))) def _verify_file(self, xml_file): if type(xml_file) in StringTypes: if os.access(os.path.split(xml_file)[0], os.W_OK): xml_file = open(xml_file, "w") xml_file.seek(0) return xml_file else: raise Exception("Don't have write permissions to given path.") elif type(xml_file) not in StringTypes: try: mode = xml_file.mode if mode == "r+" or mode == "w" or mode == "w+": xml_file.seek(0) except IOError: raise Exception("File descriptor is not able to write!") else: return xml_file def set_unsaved(self): self.unsaved = True def is_unsaved(self): return self.unsaveddef nmap_parser_sax(nmap_xml_file=""): parser = make_parser() nmap_parser = NmapParserSAX() parser.setContentHandler(nmap_parser) nmap_parser.set_parser(parser) nmap_parser.set_xml_file(nmap_xml_file) return nmap_parserNmapParser = nmap_parser_saxif __name__ == '__main__': file_to_parse = open("/home/adriano/umit/test/diff1.usr") file_to_write = open("/tmp/teste_write.xml", "w+") np = NmapParser(file_to_parse) np.parse() from pprint import pprint print "Comment:", pprint(np.nmap["hosts"][-1].comment) #comment = property(get_comment, set_comment) print "TCP sequence:", pprint(np.nmap["hosts"][-1].tcpsequence) #tcpsequence = property(get_tcpsequence, set_tcpsequence) print "TCP TS sequence:", pprint(np.nmap["hosts"][-1].tcptssequence) #tcptssequence = property(get_tcptssequence, set_tcptssequence) print "IP ID sequence:", pprint(np.nmap["hosts"][-1].ipidsequence) #ipidsequence = property(get_ipidsequence, set_ipidsequence) print "Uptime:", pprint(np.nmap["hosts"][-1].uptime) #uptime = property(get_uptime, set_uptime) print "OS Match:", pprint(np.nmap["hosts"][-1].osmatch) #osmatch = property(get_osmatch, set_osmatch) print "Ports:", pprint(np.nmap["hosts"][-1].ports) #ports = property(get_ports, set_ports) print "Ports used:", pprint(np.nmap["hosts"][-1].ports_used) #ports_used = property(get_ports_used, set_ports_used) print "OS Class:", pprint(np.nmap["hosts"][-1].osclasses) #osclasses = property(get_osclasses, set_osclasses) print "Hostnames:", pprint(np.nmap["hosts"][-1].hostnames) #hostnames = property(get_hostnames, set_hostnames) print "IP:", pprint(np.nmap["hosts"][-1].ip) #ip = property(get_ip, set_ip) print "IPv6:", pprint(np.nmap["hosts"][-1].ipv6) #ipv6 = property(get_ipv6, set_ipv6) print "MAC:", pprint(np.nmap["hosts"][-1].mac) #mac = property(get_mac, set_mac) print "State:", pprint(np.nmap["hosts"][-1].state) #state = property(get_state, set_state) """ print "Profile:", np.profile print "Profile name:", np.profile_name print "Profile description:", np.profile_description print "Profile hint:", np.profile_hint print "Profile annotation:", np.profile_annotation print "Profile options:", np.profile_options print "Target:", np.target print "Nmap output:", np.nmap_output print "Debugging:", np.debugging_level print "Verbose:", np.verbose_level print "Scaninfo:", np.scaninfo print "Services scanned:", np.services_scanned print "Nmap command:", np.nmap_command print "Scan type:", np.scan_type print "Protocol:", np.protocol print "Num services:", np.num_services print "Date:", np.date print "Open ports:", np.open_ports print "Filtered ports:", np.filtered_ports print "Closed ports:", np.closed_ports print "Formated date:", np.formated_date print "Scanner:", np.scanner print "Scanner version:", np.scanner_version print "IPv4:", np.ipv4 print "MAC:", np.mac print "IPv6:", np.ipv6 print "Hostnames", np.hostnames print "Ports:", np.ports print "Hosts:", np.hosts print "Runstats:", np.runstats print "Hosts down:", np.hosts_down print "Hosts up:", np.hosts_up print "Hosts scanned:", np.hosts_scanned print "Finished time:", np.finish_time print "Finished epoc time:", np.finish_epoc_time print "Formated finish date:", np.formated_finish_date print "Comments:", np.comments print "Start:", np.start """
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -