Changeset 3875
- Timestamp:
- 12/25/08 21:16:05 (4 years ago)
- Files:
-
- 1 modified
-
branch/NetworkInventory/umitCore/NmapParser.py (modified) (67 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branch/NetworkInventory/umitCore/NmapParser.py
r3874 r3875 51 51 def __init__(self, id): 52 52 self.id = id 53 53 54 54 # Host ID 55 55 def get_id(self): … … 64 64 raise Exception("Invalid id! It must represent an integer, " 65 65 "received %r" % id) 66 66 67 67 # TCP SEQUENCE 68 68 def set_tcpsequence(self, sequence): … … 71 71 else: 72 72 self._tcpsequence = sequence 73 73 74 74 def get_tcpsequence(self): 75 75 if self._tcpsequence: … … 83 83 else: 84 84 self._tcptssequence = sequence 85 85 86 86 def get_tcptssequence(self): 87 87 if self._tcptssequence: … … 100 100 else: 101 101 self._ipidsequence = sequence 102 102 103 103 def get_ipidsequence(self): 104 104 if self._ipidsequence: … … 109 109 def set_osclasses(self, classes): 110 110 self._osclasses = classes 111 111 112 112 def get_osclasses(self): 113 113 return self._osclasses 114 114 115 115 # OS MATCHES 116 116 def set_osmatches(self, matches): 117 117 if isinstance(matches, list): 118 118 self._osmatches = matches 119 119 120 120 def get_osmatches(self): 121 121 if self._osmatches: 122 122 return self._osmatches 123 123 return [] 124 124 125 125 # OS MATCH 126 126 def set_osmatch(self, match): … … 129 129 else: 130 130 self._osmatch = match 131 131 132 132 def get_osmatch(self): 133 133 if self._osmatch: 134 134 return self._osmatch 135 135 return {} 136 136 137 137 # OS FINGERPRINT 138 138 def set_osfingerprint(self, fingerprint): … … 141 141 else: 142 142 self._osfingerprint = fingerprint 143 143 144 144 def get_osfingerprint(self): 145 145 if self._osfingerprint: … … 150 150 def set_ports_used(self, ports): 151 151 self._ports_used = ports 152 152 153 153 def get_ports_used(self): 154 154 return self._ports_used 155 155 156 156 # TRACEROUTE 157 157 def set_trace(self, trace): … … 178 178 count = int(hop['ttl']) 179 179 return count 180 180 181 181 # UPTIME 182 182 # FORMAT: {"seconds":"", "lastboot":""} 183 183 def set_uptime(self, uptime): 184 184 self._uptime = uptime 185 185 186 186 def get_uptime(self): 187 187 if self._uptime: 188 188 return self._uptime 189 189 190 190 # Avoid empty dict return 191 191 return {"seconds":"", "lastboot":""} … … 194 194 def set_ports(self, port_list): 195 195 self._ports = port_list 196 196 197 197 def get_ports(self): 198 198 return self._ports … … 207 207 def set_hostnames(self, hostname_list): 208 208 self._hostnames = hostname_list 209 209 210 210 def get_hostnames(self): 211 211 return self._hostnames … … 216 216 umitCore.NmapParser.set_ip instead.")) 217 217 self.set_ip(addr) 218 218 219 219 def get_ip_address(self): 220 220 log.warning(_("umitCore.NmapParser.get_ip_address deprecated! Use \ … … 231 231 def get_comment(self): 232 232 return self._comment 233 233 234 234 def set_comment(self, comment): 235 235 self._comment = comment … … 240 240 umitCore.NmapParser.set_mac instead.")) 241 241 self.set_mac(addr) 242 242 243 243 def get_mac_address(self): 244 244 log.warning(_("umitCore.NmapParser.get_mac_address deprecated! Use \ … … 257 257 umitCore.NmapParser.set_ipv6 instead.")) 258 258 self.set_ipv6(addr) 259 259 260 260 def get_ipv6_address(self): 261 261 log.warning(_("umitCore.NmapParser.get_ipv6_address deprecated! Use \ … … 272 272 def set_state(self, status): 273 273 self._state = status 274 274 275 275 def get_state(self): 276 276 return self._state … … 294 294 else: 295 295 hostname = _('Unknown Host') 296 296 297 297 return hostname 298 298 … … 300 300 ports = self.get_ports() 301 301 open = 0 302 302 303 303 for i in ports: 304 304 port = i['port'] … … 306 306 if re.findall('open', p['port_state']): 307 307 open+=1 308 308 309 309 return open 310 310 311 311 def get_filtered_ports(self): 312 312 ports = self.get_ports() 313 313 extraports = self.get_extraports() 314 314 filtered = 0 315 315 316 316 for i in ports: 317 317 port = i['port'] … … 323 323 filtered += int(extra["count"]) 324 324 return filtered 325 325 326 326 def get_closed_ports(self): 327 327 ports = self.get_ports() 328 328 extraports = self.get_extraports() 329 329 closed = 0 330 330 331 331 for i in ports: 332 332 port = i['port'] … … 338 338 closed += int(extra["count"]) 339 339 return closed 340 340 341 341 def get_scanned_ports(self): 342 342 ports = self.get_ports() 343 343 extraports = self.get_extraports() 344 344 scanned = 0 345 345 346 346 for i in ports: 347 347 port = i['port'] … … 443 443 def set_profile(self, profile): 444 444 self.nmap['nmaprun']['profile'] = profile 445 445 446 446 def get_profile_name(self): 447 447 return self.nmap['nmaprun'].get('profile_name', '') … … 449 449 def set_profile_name(self, name): 450 450 self.nmap['nmaprun']['profile_name'] = name 451 451 452 452 def get_profile_description(self): 453 453 return self.nmap['nmaprun'].get('description', '') … … 455 455 def set_profile_description(self, description): 456 456 self.nmap['nmaprun']['description'] = description 457 457 458 458 def get_profile_hint(self): 459 459 return self.nmap['nmaprun'].get('hint', '') … … 461 461 def set_profile_hint(self, hint): 462 462 self.nmap['nmaprun']['hint'] = hint 463 463 464 464 def get_profile_annotation(self): 465 465 return self.nmap['nmaprun'].get('annotation', '') … … 467 467 def set_profile_annotation(self, annotation): 468 468 self.nmap['nmaprun']['annotation'] = annotation 469 469 470 470 def get_profile_options(self): 471 471 options = self.nmap['nmaprun'].get('options', '') … … 483 483 raise Exception("Profile option error: wrong argument format! " 484 484 "Need a string, list or dict.") 485 485 486 486 def get_target(self): 487 487 return self.nmap['nmaprun'].get('target', '') … … 495 495 def set_nmap_output(self, nmap_output): 496 496 self.nmap['nmaprun']['nmap_output'] = nmap_output 497 497 498 498 def get_debugging_level (self): 499 499 return self.nmap.get('debugging', '') … … 501 501 def set_debugging_level(self, level): 502 502 self.nmap['debugging'] = level 503 503 504 504 def get_verbose_level (self): 505 505 return self.nmap.get('verbose', '') … … 507 507 def set_verbose_level(self, level): 508 508 self.nmap['verbose'] = level 509 509 510 510 def get_scaninfo(self): 511 511 return self.nmap.get('scaninfo', '') … … 513 513 def set_scaninfo(self, info): 514 514 self.nmap['scaninfo'] = info 515 515 516 516 def get_services_scanned (self): 517 517 if self._services_scanned is None: 518 518 return self._services_scanned 519 519 520 520 services = [] 521 521 for scan in self.nmap.get('scaninfo', []): … … 549 549 if self._num_services is None: 550 550 return self._num_services 551 551 552 552 num = 0 553 553 for n in self.nmap.get('scaninfo', []): … … 576 576 raise Exception("Wrong date format. Date should be saved \ 577 577 in epoch format!") 578 578 579 579 def get_open_ports(self): 580 580 ports = 0 … … 598 598 def get_closed_ports(self): 599 599 ports = 0 600 600 601 601 for h in self.nmap['hosts']: 602 602 ports += h.get_closed_ports() … … 606 606 def get_formated_date(self): 607 607 date = self.get_date() 608 return "%s %s, %s - %s:%s" % (months[date[1]], 609 str(date[2]), 608 return "%s %s, %s - %s:%s" % (months[date[1]], 609 str(date[2]), 610 610 str(date[0]), 611 str(date[3]).zfill(2), 611 str(date[3]).zfill(2), 612 612 str(date[4]).zfill(2)) 613 613 … … 617 617 def set_scanner(self, scanner): 618 618 self.nmap['nmaprun']['scanner'] = scanner 619 619 620 620 def get_scanner_version (self): 621 621 return self.nmap['nmaprun'].get('version', '') … … 637 637 except: 638 638 pass 639 639 640 640 return addresses 641 641 … … 653 653 except: 654 654 pass 655 655 656 656 return addresses 657 657 … … 682 682 for port in self.nmap.get('hosts', []): 683 683 ports.append(port.get_ports()) 684 684 685 685 return ports 686 686 … … 694 694 def get_trace(self): 695 695 return self.nmap.get('trace', None) 696 697 696 697 698 698 def get_runstats(self): 699 699 return self.nmap.get('runstats', None) … … 701 701 def set_runstats(self, stats): 702 702 self.nmap['runstats'] = stats 703 703 704 704 def get_hosts_down(self): 705 705 return int(self.nmap['runstats'].get('hosts_down', '0')) … … 707 707 def set_hosts_down(self, down): 708 708 self.nmap['runstats']['hosts_down'] = int(down) 709 709 710 710 def get_hosts_up(self): 711 711 return int(self.nmap['runstats'].get('hosts_up', '0')) … … 713 713 def set_hosts_up(self, up): 714 714 self.nmap['runstats']['hosts_up'] = int(up) 715 715 716 716 def get_hosts_scanned(self): 717 717 return int(self.nmap['runstats'].get('hosts_scanned', '0')) … … 719 719 def set_hosts_scanned(self, scanned): 720 720 self.nmap['runstats']['hosts_scanned'] = int(scanned) 721 721 722 722 def get_finish_time (self): 723 723 return time.localtime(int(self.nmap['runstats'].get('finished_time', … … 738 738 def set_scan_name(self, scan_name): 739 739 self.nmap["scan_name"] = scan_name 740 740 741 741 def get_formated_finish_date(self): 742 742 date = self.get_finish_time() 743 return "%s %s, %s - %s:%s" % (months[date[1]], 744 str(date[2]), 743 return "%s %s, %s - %s:%s" % (months[date[1]], 744 str(date[2]), 745 745 str(date[0]), 746 str(date[3]).zfill(2), 746 str(date[3]).zfill(2), 747 747 str(date[4]).zfill(2)) 748 748 … … 750 750 found = re.findall ('(-o[XGASN]{1}) {0,1}', command) 751 751 splited = command.split (' ') 752 752 753 753 if found: 754 754 for option in found: … … 756 756 del(splited[pos+1]) 757 757 del(splited[pos]) 758 758 759 759 return ' '.join (splited) 760 760 … … 764 764 profile = property(get_profile, set_profile) 765 765 profile_name = property(get_profile_name, set_profile_name) 766 profile_description = property(get_profile_description, 766 profile_description = property(get_profile_description, 767 767 set_profile_description) 768 768 profile_hint = property(get_profile_hint, set_profile_hint) 769 profile_annotation = property(get_profile_annotation, 769 profile_annotation = property(get_profile_annotation, 770 770 set_profile_annotation) 771 771 profile_options = property(get_profile_options, set_profile_options) … … 805 805 trace = property(get_trace) 806 806 hops = property(get_hops) 807 807 808 808 _num_services = None 809 809 _services_scanned = None … … 821 821 self.in_os = False 822 822 self.list_extraports = [] 823 823 824 824 # Creating a traceroute condition 825 825 self.in_trace = False 826 826 827 827 self.nmap_xml_file = None 828 828 self.unsaved = False … … 850 850 def _parse_nmaprun(self, attrs): 851 851 run_tag = "nmaprun" 852 852 853 853 self.nmap[run_tag]["nmap_output"] = attrs.get("nmap_output", "") 854 854 self.nmap[run_tag]["profile"] = attrs.get("profile", "") … … 863 863 self.nmap[run_tag]["scanner"] = attrs.get("scanner", "") 864 864 self.nmap[run_tag]["version"] = attrs.get("version", "") 865 self.nmap[run_tag]["xmloutputversion"] = attrs.get("xmloutputversion", 865 self.nmap[run_tag]["xmloutputversion"] = attrs.get("xmloutputversion", 866 866 "") 867 867 self.nmap["scan_name"] = attrs.get("scan_name", "") … … 869 869 def _parse_scaninfo(self, attrs): 870 870 dic = {} 871 871 872 872 dic["type"] = attrs.get("type", "") 873 873 dic["protocol"] = attrs.get("protocol", "") 874 874 dic["numservices"] = attrs.get("numservices", "") 875 875 dic["services"] = attrs.get("services", "") 876 876 877 877 self.nmap["scaninfo"].append(dic) 878 878 … … 923 923 924 924 def _parse_host_port(self, attrs): 925 self.dic_port = {"protocol":attrs.get("protocol", ""), 925 self.dic_port = {"protocol":attrs.get("protocol", ""), 926 926 "portid":attrs.get("portid", "")} 927 927 … … 946 946 tmp['accuracy'] > last_osmatch['accuracy']: 947 947 self.host_info.set_osmatch(tmp) 948 948 949 949 self.list_osmatches.append(tmp) 950 950 951 951 def _parse_host_portused(self, attrs): 952 self.list_portused.append(self._parsing(attrs, 952 self.list_portused.append(self._parsing(attrs, 953 953 ['state','proto','portid'])) 954 954 … … 959 959 'osgen', 960 960 'accuracy'])) 961 961 962 962 def _parse_host_osfingerprint(self, attrs): 963 963 self.host_info.set_osfingerprint(self._parsing(attrs, ['fingerprint'])) … … 981 981 'difficulty', 982 982 'values'])) 983 983 984 984 def _parse_host_tcptssequence(self, attrs): 985 985 self.host_info.set_tcptssequence(self._parsing(attrs, ['class', … … 996 996 self.list_hop.append(tmp) 997 997 998 998 999 999 def startElement(self, name, attrs): 1000 1000 if name == "nmaprun": … … 1061 1061 elif self.in_host and name == "ipidsequence": 1062 1062 self._parse_host_ipidsequence(attrs) 1063 # Creating a traceroute condition 1063 # Creating a traceroute condition 1064 1064 elif self.in_host and name == "trace": 1065 1065 self.in_trace = True … … 1099 1099 del(self.list_osclass) 1100 1100 del(self.list_osmatches) 1101 1101 1102 1102 # Creating a traceroute condition 1103 1103 elif self.in_host and name == "trace": … … 1211 1211 Attributes(dict(name = hname.get("hostname", ""), 1212 1212 type = hname.get("hostname_type", "")))) 1213 1213 1214 1214 self.write_parser.endElement("hostname") 1215 1215 … … 1267 1267 # OS element 1268 1268 self.write_parser.startElement("os", Attributes({})) 1269 1269 1270 1270 ## Ports used elements 1271 1271 for pu in host.ports_used: … … 1298 1298 accuracy = om.get("accuracy", "")))) 1299 1299 self.write_parser.endElement("osmatch") 1300 1300 1301 1301 ## Osfingerprint element 1302 1302 if isinstance(host.osfingerprint, dict): 1303 1303 self.__remove_none_keys(host.osfingerprint) 1304 self.write_parser.startElement("osfingerprint", 1304 self.write_parser.startElement("osfingerprint", 1305 1305 Attributes(dict(fingerprint = \ 1306 1306 host.osfingerprint.get("fingerprint", "")))) … … 1404 1404 else: 1405 1405 return xml_file 1406 1406 1407 1407 def __remove_none_keys(self, dic): 1408 1408 pop_list = [] … … 1412 1412 for k in pop_list: 1413 1413 dic.pop(k) 1414 1414 1415 1415 def set_unsaved(self): 1416 1416 self.unsaved = True … … 1422 1422 parser = make_parser() 1423 1423 nmap_parser = NmapParserSAX() 1424 1424 1425 1425 parser.setContentHandler(nmap_parser) 1426 1426 nmap_parser.set_parser(parser)
