📄 nmapparser.py
字号:
profile_options = property(get_profile_options, set_profile_options) target = property(get_target, set_target) nmap_output = property(get_nmap_output, set_nmap_output) debugging_level = property(get_debugging_level, set_debugging_level) verbose_level = property(get_verbose_level, set_verbose_level) scaninfo = property(get_scaninfo, set_scaninfo) services_scanned = property(get_services_scanned, set_services_scanned) nmap_command = property(get_nmap_command, set_nmap_command) scan_type = property(get_scan_type) protocol = property(get_protocol) num_services = property(get_num_services, set_num_services) date = property(get_date, set_date) open_ports = property(get_open_ports) filtered_ports = property(get_filtered_ports) closed_ports = property(get_closed_ports) formated_date = property(get_formated_date) scanner = property(get_scanner, set_scanner) scanner_version = property(get_scanner_version, set_scanner_version) ipv4 = property(get_ipv4) mac = property(get_mac) ipv6 = property(get_ipv6) hostnames = property(get_hostnames) ports = property(get_ports) hosts = property(get_hosts) runstats = property(get_runstats, set_runstats) hosts_down = property(get_hosts_down, set_hosts_down) hosts_up = property(get_hosts_up, set_hosts_up) hosts_scanned = property(get_hosts_scanned, set_hosts_scanned) finish_time = property(get_finish_time, set_finish_time) finish_epoc_time = property(get_finish_epoc_time, set_finish_epoc_time) formated_finish_date = property(get_formated_finish_date) comments = property(get_comments) start = property(get_start, set_start) scan_name = property(get_scan_name, set_scan_name) _num_services = None _services_scanned = Noneclass NmapParserSAX(ParserBasics, ContentHandler): def __init__(self): ParserBasics.__init__(self) self.id_sequence = 0 self.in_run_stats = False self.in_host = False self.in_ports = False self.in_port = False self.in_os = False self.list_extraports = [] self.nmap_xml_file = None self.unsaved = False def set_parser(self, parser): self.parser = parser def set_xml_file(self, nmap_xml_file): self.nmap_xml_file = nmap_xml_file def parse(self): if self.nmap_xml_file: if type(self.nmap_xml_file) in StringTypes: self.parser.parse(self.nmap_xml_file) else: log.debug(">>> XML content: %s" % self.nmap_xml_file.read()) self.nmap_xml_file.seek(0) self.parser.parse(self.nmap_xml_file) # Closing file to avoid problems with file descriptors self.nmap_xml_file.close() else: raise Exception("There's no file to be parsed!") def _parse_nmaprun(self, attrs): run_tag = "nmaprun" self.nmap[run_tag]["nmap_output"] = attrs.get("nmap_output", "") self.nmap[run_tag]["profile"] = attrs.get("profile", "") self.nmap[run_tag]["profile_name"] = attrs.get("profile_name", "") self.nmap[run_tag]["hint"] = attrs.get("hint", "") self.nmap[run_tag]["description"] = attrs.get("description", "") self.nmap[run_tag]["annotation"] = attrs.get("annotation", "") self.nmap[run_tag]["options"] = attrs.get("options", "") self.nmap[run_tag]["target"] = attrs.get("target", "") self.nmap[run_tag]["start"] = attrs.get("start", "") self.nmap[run_tag]["args"] = attrs.get("args", "") self.nmap[run_tag]["scanner"] = attrs.get("scanner", "") self.nmap[run_tag]["version"] = attrs.get("version", "") self.nmap[run_tag]["xmloutputversion"] = attrs.get("xmloutputversion", "") self.nmap["scan_name"] = attrs.get("scan_name", "") def _parse_scaninfo(self, attrs): dic = {} dic["type"] = attrs.get("type", "") dic["protocol"] = attrs.get("protocol", "") dic["numservices"] = attrs.get("numservices", "") dic["services"] = attrs.get("services", "") self.nmap["scaninfo"].append(dic) def _parse_verbose(self, attrs): self.nmap["verbose"] = attrs.get("level", "") def _parse_debugging(self, attrs): self.nmap["debugging"] = attrs.get("level", "") def _parse_runstats_finished(self, attrs): self.nmap["runstats"]["finished_time"] = attrs.get("time", "") def _parse_runstats_hosts(self, attrs): self.nmap["runstats"]["hosts_up"] = attrs.get("up", "") self.nmap["runstats"]["hosts_down"] = attrs.get("down", "") self.nmap["runstats"]["hosts_scanned"] = attrs.get("total", "") def generate_id(self): self.id_sequence += 1 return self.id_sequence def _parse_host(self, attrs): self.host_info = HostInfo(self.generate_id()) self.host_info.comment = attrs.get("comment", "") def _parse_host_status(self, attrs): self.host_info.set_state(attrs.get("state", "")) def _parse_host_address(self, attrs): address_attributes = {"type":attrs.get("addrtype", ""), "vendor":attrs.get("vendor", ""), "addr":attrs.get("addr", "")} if address_attributes["type"] == "ipv4": self.host_info.set_ip(address_attributes) elif address_attributes["type"] == "ipv6": self.host_info.set_ipv6(address_attributes) elif address_attributes["type"] == "mac": self.host_info.set_mac(address_attributes) def _parse_host_hostname(self, attrs): self.list_hostnames.append({"hostname":attrs.get("name", ""), "hostname_type":attrs.get("type", "")}) def _parse_host_extraports(self, attrs): self.list_extraports.append({"state":attrs.get("state", ""), "count":attrs.get("count", "")}) def _parse_host_port(self, attrs): self.dic_port = {"protocol":attrs.get("protocol", ""), "portid":attrs.get("portid", "")} def _parse_host_port_state(self, attrs): self.dic_port["port_state"] = attrs.get("state", "") def _parse_host_port_service(self, attrs): self.dic_port["service_name"] = attrs.get("name", "") self.dic_port["service_method"] = attrs.get("method", "") self.dic_port["service_conf"] = attrs.get("conf", "") self.dic_port["service_product"] = attrs.get("product", "") self.dic_port["service_version"] = attrs.get("version", "") self.dic_port["service_extrainfo"] = attrs.get("extrainfo", "") def _parse_host_osmatch(self, attrs): self.host_info.set_osmatch(self._parsing(attrs, ['name', 'accuracy'])) def _parse_host_portused(self, attrs): self.list_portused.append(self._parsing(attrs, ['state','proto','portid'])) def _parse_host_osclass(self, attrs): self.list_osclass.append(self._parsing(attrs, ['type', 'vendor', 'osfamily', 'osgen', 'accuracy'])) def _parsing(self, attrs, attrs_list): # Returns a dict with the attributes of a given tag with the # atributes names as keys and their respective values dic = {} for at in attrs_list: dic[at] = attrs.get(at, "") return dic def _parse_host_uptime(self, attrs): self.host_info.set_uptime(self._parsing(attrs, ["seconds", "lastboot"])) def _parse_host_tcpsequence(self, attrs): self.host_info.set_tcpsequence(self._parsing(attrs, ['index', 'class', 'difficulty', 'values'])) def _parse_host_tcptssequence(self, attrs): self.host_info.set_tcptssequence(self._parsing(attrs, ['class', 'values'])) def _parse_host_ipidsequence(self, attrs): self.host_info.set_ipidsequence(self._parsing(attrs, ['class', 'values'])) def startElement(self, name, attrs): if name == "nmaprun": self._parse_nmaprun(attrs) elif name == "scaninfo": self._parse_scaninfo(attrs) elif name == "verbose": self._parse_verbose(attrs) elif name == "debugging": self._parse_debugging(attrs) elif name == "runstats": self.in_run_stats = True elif self.in_run_stats and name == "finished": self._parse_runstats_finished(attrs) elif self.in_run_stats and name == "hosts": self._parse_runstats_hosts(attrs) elif name == "host": self.in_host = True self._parse_host(attrs) self.list_ports = [] elif self.in_host and name == "status": self._parse_host_status(attrs) elif self.in_host and name == "address": self._parse_host_address(attrs) elif self.in_host and name == "hostnames": self.in_hostnames = True self.list_hostnames = [] elif self.in_host and self.in_hostnames and name == "hostname": self._parse_host_hostname(attrs) elif self.in_host and name == "ports": self.list_extraports = [] self.list_port = [] self.in_ports = True elif self.in_host and self.in_ports and name == "extraports": self._parse_host_extraports(attrs) elif self.in_host and self.in_ports and name == "port": self.in_port = True self._parse_host_port(attrs) elif self.in_host and self.in_ports and \ self.in_port and name == "state": self._parse_host_port_state(attrs) elif self.in_host and self.in_ports and \ self.in_port and name == "service": self._parse_host_port_service(attrs) elif self.in_host and name == "os": self.in_os = True self.list_portused = [] self.list_osclass = [] elif self.in_host and self.in_os and name == "osmatch": self._parse_host_osmatch(attrs) elif self.in_host and self.in_os and name == "portused": self._parse_host_portused(attrs) elif self.in_host and self.in_os and name == "osclass": self._parse_host_osclass(attrs) elif self.in_host and name == "uptime": self._parse_host_uptime(attrs) elif self.in_host and name == "tcpsequence": self._parse_host_tcpsequence(attrs) elif self.in_host and name == "tcptssequence": self._parse_host_tcptssequence(attrs) elif self.in_host and name == "ipidsequence": self._parse_host_ipidsequence(attrs) def endElement(self, name): if name == "runstats": self.in_run_stats = False elif name == "host": self.in_host = False self.host_info.set_ports(self.list_ports) self.nmap["hosts"].append(self.host_info) del(self.list_ports) elif self.in_host and name == "hostnames": self.in_hostnames = False self.host_info.set_hostnames(self.list_hostnames) elif self.in_host and name == "ports": self.in_ports = False self.list_ports.append({"extraports":self.list_extraports, "port":self.list_port}) elif self.in_host and self.in_ports and name == "port": self.in_port = False self.list_port.append(self.dic_port) del(self.dic_port) elif self.in_host and self.in_os and name == "os": self.in_os = False self.host_info.set_ports_used(self.list_portused) self.host_info.set_osclasses(self.list_osclass) del(self.list_portused) del(self.list_osclass) def write_xml(self, xml_file): xml_file = self._verify_file(xml_file) self.write_parser = XMLGenerator(xml_file) # First, start the document: self.write_parser.startDocument() # Nmaprun element: self._write_nmaprun() # Scaninfo element: self._write_scaninfo() # Verbose element: self._write_verbose() # Debugging element: self._write_debugging() # Hosts elements: self._write_hosts() # Runstats element: self._write_runstats() # End of the xml file: self.write_parser.endElement("nmaprun") self.write_parser.endDocument() def _write_runstats(self): ################## # Runstats element self.write_parser.startElement("runstats", Attributes(dict())) ## Finished element self.write_parser.startElement("finished", Attributes(dict(time = str(self.finish_epoc_time)))) self.write_parser.endElement("finished") ## Hosts element self.write_parser.startElement("hosts", Attributes(dict(up = str(self.hosts_up), down = str(self.hosts_down), total = str(self.hosts_scanned)))) self.write_parser.endElement("hosts") self.write_parser.endElement("runstats") # End of Runstats element ######################### def _write_hosts(self): for host in self.hosts: # Start host element self.write_parser.startElement("host", Attributes(dict(comment=host.comment)))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -