Changeset 3887
- Timestamp:
- 01/05/09 22:52:05 (4 years ago)
- Files:
-
- 1 modified
-
branch/nmapparser/NmapParser.py (modified) (72 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branch/nmapparser/NmapParser.py
r3885 r3887 30 30 from xml.sax.handler import ContentHandler 31 31 from xml.sax.saxutils import XMLGenerator 32 from xml.sax.xmlreader import AttributesImpl as Attributes32 from xml.sax.xmlreader import AttributesImpl 33 33 34 34 months = ('', … … 49 49 def __init__(self, id): 50 50 self.id = id 51 51 52 52 # Host ID 53 53 def get_id(self): … … 62 62 raise Exception("Invalid id! It must represent an integer, " 63 63 "received %r" % id) 64 64 65 65 # TCP SEQUENCE 66 66 def set_tcpsequence(self, sequence): … … 69 69 else: 70 70 self._tcpsequence = sequence 71 71 72 72 def get_tcpsequence(self): 73 73 if self._tcpsequence: … … 81 81 else: 82 82 self._tcptssequence = sequence 83 83 84 84 def get_tcptssequence(self): 85 85 if self._tcptssequence: … … 98 98 else: 99 99 self._ipidsequence = sequence 100 100 101 101 def get_ipidsequence(self): 102 102 if self._ipidsequence: … … 107 107 def set_osclasses(self, classes): 108 108 self._osclasses = classes 109 109 110 110 def get_osclasses(self): 111 111 return self._osclasses 112 112 113 113 # OS MATCHES 114 114 def set_osmatches(self, matches): 115 115 if type(matches) == type([]): 116 116 self._osmatches = matches 117 117 118 118 def get_osmatches(self): 119 119 if self._osmatches: 120 120 return self._osmatches 121 121 return [] 122 122 123 123 # OS MATCH 124 124 def set_osmatch(self, match): … … 127 127 else: 128 128 self._osmatch = match 129 129 130 130 def get_osmatch(self): 131 131 if self._osmatch: 132 132 return self._osmatch 133 133 return {} 134 134 135 135 # OS FINGERPRINT 136 136 def set_osfingerprint(self, fingerprint): … … 139 139 else: 140 140 self._osfingerprint = fingerprint 141 141 142 142 def get_osfingerprint(self): 143 143 if self._osfingerprint: … … 148 148 def set_ports_used(self, ports): 149 149 self._ports_used = ports 150 150 151 151 def get_ports_used(self): 152 152 return self._ports_used 153 153 154 154 # TRACEROUTE 155 155 def set_trace(self, trace): … … 170 170 return hop 171 171 return None 172 172 173 def get_number_of_hops(self): 173 174 count = 0 … … 176 177 count = int(hop['ttl']) 177 178 return count 178 179 179 180 # UPTIME 180 181 # FORMAT: {"seconds":"", "lastboot":""} 181 182 def set_uptime(self, uptime): 182 183 self._uptime = uptime 183 184 184 185 def get_uptime(self): 185 186 if self._uptime: 186 187 return self._uptime 187 188 188 189 # Avoid empty dict return 189 190 return {"seconds":"", "lastboot":""} … … 192 193 def set_ports(self, port_list): 193 194 self._ports = port_list 194 195 195 196 def get_ports(self): 196 197 return self._ports … … 205 206 def set_hostnames(self, hostname_list): 206 207 self._hostnames = hostname_list 207 208 208 209 def get_hostnames(self): 209 210 return self._hostnames … … 212 213 def set_ip_address(self, addr): 213 214 self.set_ip(addr) 214 215 215 216 def get_ip_address(self): 216 217 return self.get_ip() … … 225 226 def get_comment(self): 226 227 return self._comment 227 228 228 229 def set_comment(self, comment): 229 230 self._comment = comment … … 232 233 def set_mac_address(self, addr): 233 234 self.set_mac(addr) 234 235 235 236 def get_mac_address(self): 236 237 return self.get_mac() … … 245 246 def set_ipv6_address(self, addr): 246 247 self.set_ipv6(addr) 247 248 248 249 def get_ipv6_address(self): 249 250 return self.get_ipv6() … … 258 259 def set_state(self, status): 259 260 self._state = status 260 261 261 262 def get_state(self): 262 263 return self._state … … 280 281 else: 281 282 hostname = 'Unknown Host' 282 283 283 284 return hostname 284 285 … … 286 287 ports = self.get_ports() 287 288 open = 0 288 289 289 290 for i in ports: 290 291 port = i['port'] … … 292 293 if re.findall('open', p['port_state']): 293 294 open+=1 294 295 295 296 return open 296 297 297 298 def get_filtered_ports(self): 298 299 ports = self.get_ports() 299 300 extraports = self.get_extraports() 300 301 filtered = 0 301 302 302 303 for i in ports: 303 304 port = i['port'] … … 309 310 filtered += int(extra["count"]) 310 311 return filtered 311 312 312 313 def get_closed_ports(self): 313 314 ports = self.get_ports() 314 315 extraports = self.get_extraports() 315 316 closed = 0 316 317 317 318 for i in ports: 318 319 port = i['port'] … … 324 325 closed += int(extra["count"]) 325 326 return closed 326 327 327 328 def get_scanned_ports(self): 328 329 ports = self.get_ports() 329 330 extraports = self.get_extraports() 330 331 scanned = 0 331 332 332 333 for i in ports: 333 334 port = i['port'] … … 374 375 trace = property(get_trace, set_trace) 375 376 hops = property(get_hops, set_hops) 376 377 377 378 378 379 _id = 0 … … 400 401 class ParserBasics(object): 401 402 def __init__ (self): 402 self.nmap = {'nmaprun':{},\ 403 'scaninfo':[],\ 404 'verbose':'',\ 405 'debugging':'',\ 406 'hosts':[],\ 407 'runstats':{}} 403 self.nmap = { 404 'nmaprun': {}, 405 'scaninfo': [], 406 'verbose': '', 407 'debugging': '', 408 'hosts': [], 409 'runstats': {} 410 } 408 411 409 412 def set_host_comment(self, host_id, comment): … … 413 416 break 414 417 else: 415 raise Exception("Comment could not be saved! Host not \416 found at NmapParser!")418 raise Exception("Comment could not be saved! Host not " 419 "found at NmapParser!") 417 420 418 421 def get_host_comment(self, host_id): … … 421 424 return host.comment 422 425 else: 423 raise Exception("Comment could not be saved! Host not \424 found at NmapParser!")426 raise Exception("Comment could not be saved! Host not " 427 "found at NmapParser!") 425 428 426 429 def get_profile(self): … … 429 432 def set_profile(self, profile): 430 433 self.nmap['nmaprun']['profile'] = profile 431 434 432 435 def get_profile_name(self): 433 436 return self.nmap['nmaprun'].get('profile_name', '') … … 435 438 def set_profile_name(self, name): 436 439 self.nmap['nmaprun']['profile_name'] = name 437 440 438 441 def get_profile_description(self): 439 442 return self.nmap['nmaprun'].get('description', '') … … 441 444 def set_profile_description(self, description): 442 445 self.nmap['nmaprun']['description'] = description 443 446 444 447 def get_profile_hint(self): 445 448 return self.nmap['nmaprun'].get('hint', '') … … 447 450 def set_profile_hint(self, hint): 448 451 self.nmap['nmaprun']['hint'] = hint 449 452 450 453 def get_profile_annotation(self): 451 454 return self.nmap['nmaprun'].get('annotation', '') … … 453 456 def set_profile_annotation(self, annotation): 454 457 self.nmap['nmaprun']['annotation'] = annotation 455 458 456 459 def get_profile_options(self): 457 460 options = self.nmap['nmaprun'].get('options', '') … … 467 470 self.nmap['nmaprun']['options'] = options.keys() 468 471 else: 469 raise Exception("Profile option error: wrong argument format! \470 Need a string or list.")471 472 raise Exception("Profile option error: wrong argument format! " 473 "Need a string or list.") 474 472 475 def get_target(self): 473 476 return self.nmap['nmaprun'].get('target', '') … … 481 484 def set_nmap_output(self, nmap_output): 482 485 self.nmap['nmaprun']['nmap_output'] = nmap_output 483 486 484 487 def get_debugging_level (self): 485 488 return self.nmap.get('debugging', '') … … 487 490 def set_debugging_level(self, level): 488 491 self.nmap['debugging'] = level 489 492 490 493 def get_verbose_level (self): 491 494 return self.nmap.get('verbose', '') … … 493 496 def set_verbose_level(self, level): 494 497 self.nmap['verbose'] = level 495 498 496 499 def get_scaninfo(self): 497 500 return self.nmap.get('scaninfo', '') … … 499 502 def set_scaninfo(self, info): 500 503 self.nmap['scaninfo'] = info 501 504 502 505 def get_services_scanned (self): 503 506 if self._services_scanned == None: 504 507 return self._services_scanned 505 508 506 509 services = [] 507 510 for scan in self.nmap.get('scaninfo', []): … … 535 538 if self._num_services == None: 536 539 return self._num_services 537 540 538 541 num = 0 539 542 for n in self.nmap.get('scaninfo', []): … … 560 563 self.nmap['nmaprun']['start'] = date 561 564 else: 562 raise Exception("Wrong date format. Date should be saved \563 in epoch format!")564 565 raise Exception("Wrong date format. Date should be saved " 566 "in epoch format!") 567 565 568 def get_open_ports(self): 566 569 ports = 0 … … 576 579 for h in self.nmap.get('hosts', []): 577 580 ports += h.get_filtered_ports() 578 579 581 580 582 return ports … … 590 592 def get_formated_date(self): 591 593 date = self.get_date() 592 return "%s %s, %s - %s:%s" % (months[date[1]], 593 str(date[2]), 594 return "%s %s, %s - %s:%s" % (months[date[1]], 595 str(date[2]), 594 596 str(date[0]), 595 str(date[3]).zfill(2), 597 str(date[3]).zfill(2), 596 598 str(date[4]).zfill(2)) 597 599 … … 601 603 def set_scanner(self, scanner): 602 604 self.nmap['nmaprun']['scanner'] = scanner 603 605 604 606 def get_scanner_version (self): 605 607 return self.nmap['nmaprun'].get('version', '') … … 619 621 except: 620 622 pass 621 623 622 624 return addresses 623 625 … … 633 635 except: 634 636 pass 635 637 636 638 return addresses 637 639 … … 660 662 for port in self.nmap.get('hosts', []): 661 663 ports.append(port.get_ports()) 662 664 663 665 return ports 664 666 … … 672 674 def get_trace(self): 673 675 return self.nmap.get('trace', None) 674 675 676 677 676 678 def get_runstats(self): 677 679 return self.nmap.get('runstats', None) … … 679 681 def set_runstats(self, stats): 680 682 self.nmap['runstats'] = stats 681 683 682 684 def get_hosts_down(self): 683 685 return int(self.nmap['runstats'].get('hosts_down', '0')) … … 685 687 def set_hosts_down(self, down): 686 688 self.nmap['runstats']['hosts_down'] = int(down) 687 689 688 690 def get_hosts_up(self): 689 691 return int(self.nmap['runstats'].get('hosts_up', '0')) … … 691 693 def set_hosts_up(self, up): 692 694 self.nmap['runstats']['hosts_up'] = int(up) 693 695 694 696 def get_hosts_scanned(self): 695 697 return int(self.nmap['runstats'].get('hosts_scanned', '0')) … … 697 699 def set_hosts_scanned(self, scanned): 698 700 self.nmap['runstats']['hosts_scanned'] = int(scanned) 699 701 700 702 def get_finish_time (self): 701 703 return time.localtime(int(self.nmap['runstats'].get('finished_time', … … 716 718 def set_scan_name(self, scan_name): 717 719 self.nmap["scan_name"] = scan_name 718 720 719 721 def get_formated_finish_date(self): 720 722 date = self.get_finish_time() 721 return "%s %s, %s - %s:%s" % (months[date[1]], 722 str(date[2]), 723 return "%s %s, %s - %s:%s" % (months[date[1]], 724 str(date[2]), 723 725 str(date[0]), 724 str(date[3]).zfill(2), 726 str(date[3]).zfill(2), 725 727 str(date[4]).zfill(2)) 726 728 727 def _verify_output_options (self, command):729 def _verify_output_options(self, command): 728 730 found = re.findall ('(-o[XGASN]{1}) {0,1}', command) 729 731 splited = command.split (' ') 730 732 731 733 if found: 732 734 for option in found: … … 734 736 del(splited[pos+1]) 735 737 del(splited[pos]) 736 737 return ' '.join (splited)738 739 return ' '.join(splited) 738 740 739 741 def get_comments(self): … … 742 744 profile = property(get_profile, set_profile) 743 745 profile_name = property(get_profile_name, set_profile_name) 744 profile_description = property(get_profile_description, 746 profile_description = property(get_profile_description, 745 747 set_profile_description) 746 748 profile_hint = property(get_profile_hint, set_profile_hint) 747 profile_annotation = property(get_profile_annotation, 749 profile_annotation = property(get_profile_annotation, 748 750 set_profile_annotation) 749 751 profile_options = property(get_profile_options, set_profile_options) … … 783 785 trace = property(get_trace) 784 786 hops = property(get_hops) 785 787 786 788 _num_services = None 787 789 _services_scanned = None … … 799 801 self.in_os = False 800 802 self.list_extraports = [] 801 803 802 804 # Creating a traceroute condition 803 805 self.in_trace = False 804 806 805 807 self.nmap_xml_file = None 806 808 self.unsaved = False … … 827 829 def _parse_nmaprun(self, attrs): 828 830 run_tag = "nmaprun" 829 831 830 832 self.nmap[run_tag]["nmap_output"] = attrs.get("nmap_output", "") 831 833 self.nmap[run_tag]["profile"] = attrs.get("profile", "") … … 840 842 self.nmap[run_tag]["scanner"] = attrs.get("scanner", "") 841 843 self.nmap[run_tag]["version"] = attrs.get("version", "") 842 self.nmap[run_tag]["xmloutputversion"] = attrs.get("xmloutputversion", 844 self.nmap[run_tag]["xmloutputversion"] = attrs.get("xmloutputversion", 843 845 "") 844 846 self.nmap["scan_name"] = attrs.get("scan_name", "") … … 846 848 def _parse_scaninfo(self, attrs): 847 849 dic = {} 848 850 849 851 dic["type"] = attrs.get("type", "") 850 852 dic["protocol"] = attrs.get("protocol", "") 851 853 dic["numservices"] = attrs.get("numservices", "") 852 854 dic["services"] = attrs.get("services", "") 853 855 854 856 self.nmap["scaninfo"].append(dic) 855 857 … … 900 902 901 903 def _parse_host_port(self, attrs): 902 self.dic_port = {"protocol":attrs.get("protocol", ""), 904 self.dic_port = {"protocol":attrs.get("protocol", ""), 903 905 "portid":attrs.get("portid", "")} 904 906 … … 920 922 elif tmp != {} and tmp.has_key('accuracy'): 921 923 last_osmatch = self.host_info.get_osmatch() 922 if last_osmatch.has_key('accuracy') and \923 tmp['accuracy'] > last_osmatch['accuracy']:924 self.host_info.set_osmatch(tmp)925 924 if (last_osmatch.has_key('accuracy') and 925 tmp['accuracy'] > last_osmatch['accuracy']): 926 self.host_info.set_osmatch(tmp) 927 926 928 self.list_osmatches.append(tmp) 927 929 928 930 def _parse_host_portused(self, attrs): 929 self.list_portused.append(self._parsing(attrs, 931 self.list_portused.append(self._parsing(attrs, 930 932 ['state','proto','portid'])) 931 933 … … 936 938 'osgen', 937 939 'accuracy'])) 938 940 939 941 def _parse_host_osfingerprint(self, attrs): 940 942 self.host_info.set_osfingerprint(self._parsing(attrs, ['fingerprint'])) 941 943 942 944 943 945 def _parsing(self, attrs, attrs_list): … … 958 960 'difficulty', 959 961 'values'])) 960 962 961 963 def _parse_host_tcptssequence(self, attrs): 962 964 self.host_info.set_tcptssequence(self._parsing(attrs, ['class', … … 966 968 self.host_info.set_ipidsequence(self._parsing(attrs, ['class', 967 969 'values'])) 970 968 971 def _parse_host_trace(self, attrs): 969 972 self.host_info.set_trace(self._parsing(attrs, ['port', 'proto'])) … … 973 976 self.list_hop.append(tmp) 974 977 975 978 976 979 def startElement(self, name, attrs): 977 980 if name == "nmaprun": … … 1038 1041 elif self.in_host and name == "ipidsequence": 1039 1042 self._parse_host_ipidsequence(attrs) 1040 # Creating a traceroute condition 1043 # Creating a traceroute condition 1041 1044 elif self.in_host and name == "trace": 1042 1045 self.in_trace = True … … 1076 1079 del(self.list_osclass) 1077 1080 del(self.list_osmatches) 1078 1081 1079 1082 # Creating a traceroute condition 1080 1083 elif self.in_host and name == "trace": … … 1188 1191 Attributes(dict(name = hname.get("hostname", ""), 1189 1192 type = hname.get("hostname_type", "")))) 1190 1193 1191 1194 self.write_parser.endElement("hostname") 1192 1195 … … 1244 1247 # OS element 1245 1248 self.write_parser.startElement("os", Attributes({})) 1246 1249 1247 1250 ## Ports used elements 1248 1251 for pu in host.ports_used: … … 1275 1278 accuracy = om.get("accuracy", "")))) 1276 1279 self.write_parser.endElement("osmatch") 1277 1280 1278 1281 ## Osfingerprint element 1279 1282 if type(host.osfingerprint) == type({}): 1280 1283 self.__remove_none_keys(host.osfingerprint) 1281 self.write_parser.startElement("osfingerprint", 1284 self.write_parser.startElement("osfingerprint", 1282 1285 Attributes(dict(fingerprint = \ 1283 1286 host.osfingerprint.get("fingerprint", "")))) 1284 1287 self.write_parser.endElement("osfingerprint") 1285 1288 1286 1289 1287 1290 self.write_parser.endElement("os") … … 1381 1384 else: 1382 1385 return xml_file 1383 1386 1384 1387 def __remove_none_keys(self, dic): 1385 1388 for k in dic.keys(): … … 1396 1399 parser = make_parser() 1397 1400 nmap_parser = NmapParserSAX() 1398 1401 1399 1402 parser.setContentHandler(nmap_parser) 1400 1403 nmap_parser.set_parser(parser)
