Changeset 4774
- Timestamp:
- 05/07/09 02:47:30 (4 years ago)
- Location:
- branch/umitweb-ng
- Files:
-
- 2 modified
-
. (modified) (1 prop)
-
umitCore/NmapParser.py (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branch/umitweb-ng
- Property svnmerge-integrated changed from /trunk:1-3190,3263-3267,3269,3271-3278,3285,3307-3308,3366,3379,3383,3567,3628,3678-3679,3705,3720-3729,3735-3738,3742-3748,3765-3766,3771,3788-3789,3792,3794,3797-3804,3849,3858,3876-3877,3879-3880,3927-3930,3932,3936,3939-3940,3946-3948,3953-3954,3965-3966,3968,3972,3993,3995,4000,4006-4007,4010,4012-4013,4015,4017-4019,4021-4025,4027,4029-4031,4033,4035-4038,4040-4042,4044-4045,4049,4054-4064,4073,4087-4092,4095-4099,4101-4103,4106-4110,4113,4118-4123,4128-4137,4140-4144,4146,4152-4156,4158-4168,4174-4175,4184,4188-4189,4192,4198-4199,4202-4206,4208-4214,4218,4229-4231,4233 to /trunk:1-3190,3263-3267,3269,3271-3278,3285,3307-3308,3366,3379,3383,3567,3628,3678-3679,3705,3720-3729,3735-3738,3742-3748,3765-3766,3771,3788-3789,3792,3794,3797-3804,3849,3858,3876-3877,3879-3880,3927-3930,3932,3936,3939-3940,3946-3948,3953-3954,3965-3966,3968,3972,3993,3995,4000,4006-4007,4010,4012-4013,4015,4017-4019,4021-4025,4027,4029-4031,4033,4035-4038,4040-4042,4044-4045,4049,4054-4064,4073,4087-4092,4095-4099,4101-4103,4106-4110,4113,4118-4123,4128-4137,4140-4144,4146,4152-4156,4158-4168,4174-4175,4184,4188-4189,4192,4198-4199,4202-4206,4208-4214,4218,4229-4231,4233,4237
-
branch/umitweb-ng/umitCore/NmapParser.py
r4722 r4774 1 #!/usr/bin/env python2 1 # -*- coding: utf-8 -*- 3 2 # … … 6 5 # 7 6 # Author: Adriano Monteiro Marques <adriano@umitproject.org> 7 # Guilherme Polo <ggpolo@gmail.com> 8 8 # João Paulo de Souza Medeiros <ignotus21@gmail.com> 9 9 # Luis A. Bastiao Silva <luis.kop@gmail.com> … … 35 35 36 36 class AttributesImplDict(dict, AttributesImpl): 37 pass37 """Use this for displaying AttributesImpl just like a dict.""" 38 38 39 39 class HostInfo(object): … … 58 58 self.comment = '' 59 59 60 # XXX this structure it not being used yet. 60 61 self.nmap_host = { 61 62 'status': {'state': '', 'reason': ''}, … … 80 81 return self._id 81 82 except AttributeError: 82 raise Exception("Id was not set yet.")83 raise Exception("Id is not set yet.") 83 84 84 85 def set_id(self, host_id): … … 604 605 self.nmap_xml_file.seek(0) 605 606 self.parser.parse(self.nmap_xml_file) 606 607 # Closing file to avoid problems with file descriptors608 self.nmap_xml_file.close()609 607 else: 610 608 raise Exception("There's no file to be parsed!") … … 650 648 651 649 def startElement(self, name, attrs): 650 # AttributesImplDict is used here so utils/xmldisplay.py can display 651 # instances of AttributesImpl without any effort. 652 652 attrs = AttributesImplDict(attrs) 653 653 … … 765 765 ## Finished element 766 766 self.write_parser.startElement('finished', 767 AttributesImpl(dict( 768 time=str(time.mktime(self.finish_epoch_time)), 769 timestr=self.finish_time))) 767 AttributesImpl({ 768 'time': str(time.mktime(self.finish_epoch_time)), 769 'timestr': self.finish_time}) 770 ) 770 771 self.write_parser.endElement('finished') 771 772 772 773 ## Hosts element 773 774 self.write_parser.startElement('hosts', 774 AttributesImpl(dict( 775 up=str(self.hosts_up), 776 down=str(self.hosts_down), 777 total=str(self.hosts_total)))) 775 AttributesImpl({ 776 'up': str(self.hosts_up), 777 'down': str(self.hosts_down), 778 'total': str(self.hosts_total)}) 779 ) 778 780 self.write_parser.endElement('hosts') 779 781 … … 787 789 # Start host element 788 790 self.write_parser.startElement('host', 789 AttributesImpl(dict(comment=host.comment))) 791 AttributesImpl({ 792 'comment': host.comment}) 793 ) 790 794 791 795 # Status element 792 796 self.write_parser.startElement('status', 793 AttributesImpl(dict( 794 state=host.status['state']))) 797 AttributesImpl({ 798 'state': host.status['state']}) 799 ) 795 800 self.write_parser.endElement('status') 796 801 … … 801 806 self.__remove_none(address) 802 807 self.write_parser.startElement('address', 803 AttributesImpl(dict( 804 addr=address.get('addr', ''), 805 vendor=address.get('vendor', ''), 806 addrtype=address.get('addrtype', '')))) 808 AttributesImpl({ 809 'addr': address.get('addr', ''), 810 'vendor': address.get('vendor', ''), 811 'addrtype': address.get('addrtype', '')}) 812 ) 807 813 self.write_parser.endElement('address') 808 814 # End of Address elements … … 815 821 816 822 for hname in host.hostnames: 817 if type(hname) == type({}): 818 self.write_parser.startElement('hostname', 819 AttributesImpl( 820 dict(name = hname.get('hostname', ''), 821 type = hname.get('hostname_type', '')))) 822 823 self.write_parser.endElement('hostname') 823 if not isinstance(hname, dict): 824 continue 825 826 self.write_parser.startElement('hostname', 827 AttributesImpl({ 828 'name': hname.get('hostname', ''), 829 'type': hname.get('hostname_type', '')}) 830 ) 831 self.write_parser.endElement('hostname') 824 832 825 833 self.write_parser.endElement('hostnames') … … 836 844 self.__remove_none(export) 837 845 self.write_parser.startElement('extraports', 838 AttributesImpl(dict( 839 count=str(export.get('count', '')), 840 state=export.get('state', '')))) 846 AttributesImpl({ 847 'count': str(export.get('count', '')), 848 'state': export.get('state', '')}) 849 ) 841 850 self.write_parser.endElement('extraports') 842 851 … … 845 854 self.__remove_none(port) 846 855 self.write_parser.startElement('port', 847 AttributesImpl(dict( 848 portid = port.get('portid', ''), 849 protocol = port.get('protocol', '')))) 856 AttributesImpl({ 857 'portid': port.get('portid', ''), 858 'protocol': port.get('protocol', '')}) 859 ) 850 860 851 861 ### Port state 852 862 self.write_parser.startElement('state', 853 AttributesImpl(dict(state=port.get('state', '')))) 863 AttributesImpl({ 864 'state': port.get('state', '')}) 865 ) 854 866 self.write_parser.endElement('state') 855 867 856 868 ### Port service info 857 869 self.write_parser.startElement('service', 858 AttributesImpl( dict(859 conf=port.get('conf', ''),860 method=port.get('method', ''),861 name=port.get('name', ''),862 product=port.get('product', ''),863 version=port.get('version', ''),864 extrainfo=port.get('extrainfo', '')865 )))870 AttributesImpl({ 871 'conf': port.get('conf', ''), 872 'method': port.get('method', ''), 873 'name': port.get('name', ''), 874 'product': port.get('product', ''), 875 'version': port.get('version', ''), 876 'extrainfo': port.get('extrainfo', '')}) 877 ) 866 878 self.write_parser.endElement('service') 867 879 … … 879 891 ## Ports used elements 880 892 for pu in host.portused: 881 if type(pu) == type({}): 882 self.__remove_none(pu) 883 self.write_parser.startElement('portused', 884 AttributesImpl(dict(state = pu.get('state', ''), 885 proto = pu.get('proto', ''), 886 portid = pu.get('portid', '')))) 887 self.write_parser.endElement('portused') 893 if not isinstance(pu, dict): 894 continue 895 896 self.__remove_none(pu) 897 self.write_parser.startElement('portused', 898 AttributesImpl({ 899 'state': pu.get('state', ''), 900 'proto': pu.get('proto', ''), 901 'portid': pu.get('portid', '')}) 902 ) 903 self.write_parser.endElement('portused') 888 904 889 905 ## Osclass elements 890 906 for oc in host.osclass: 891 if type(oc) == type({}): 892 self.__remove_none(oc) 893 self.write_parser.startElement('osclass', 894 AttributesImpl(dict(vendor = oc.get('vendor', ''), 895 osfamily = oc.get('osfamily', ''), 896 type = oc.get('type', ''), 897 osgen = oc.get('osgen', ''), 898 accuracy = oc.get('accuracy', '')))) 899 self.write_parser.endElement('osclass') 907 if not isinstance(oc, dict): 908 continue 909 910 self.__remove_none(oc) 911 self.write_parser.startElement('osclass', 912 AttributesImpl({ 913 'vendor': oc.get('vendor', ''), 914 'osfamily': oc.get('osfamily', ''), 915 'type': oc.get('type', ''), 916 'osgen': oc.get('osgen', ''), 917 'accuracy': oc.get('accuracy', '')}) 918 ) 919 self.write_parser.endElement('osclass') 900 920 901 921 ## Osmatch elements 902 922 for om in host.osmatch: 903 if type(om) == type({}): 904 self.__remove_none(om) 905 self.write_parser.startElement('osmatch', 906 AttributesImpl(dict(name = om.get('name', ''), 907 accuracy = om.get('accuracy', '')))) 908 self.write_parser.endElement('osmatch') 923 if not isinstance(om, dict): 924 continue 925 926 self.__remove_none(om) 927 self.write_parser.startElement('osmatch', 928 AttributesImpl({ 929 'name': om.get('name', ''), 930 'accuracy': om.get('accuracy', '')}) 931 ) 932 self.write_parser.endElement('osmatch') 909 933 910 934 ## Osfingerprint element 911 if type(host.osfingerprint) == type({}):935 if isinstance(host.osfingerprint, dict): 912 936 self.__remove_none(host.osfingerprint) 913 937 self.write_parser.startElement('osfingerprint', 914 AttributesImpl(dict( 915 fingerprint=host.osfingerprint.get('fingerprint', '')))) 938 AttributesImpl({ 939 'fingerprint': host.osfingerprint.get( 940 'fingerprint', '')}) 941 ) 916 942 self.write_parser.endElement('osfingerprint') 917 943 … … 922 948 923 949 # Uptime element 924 if type(host.uptime) == type({}):950 if isinstance(host.uptime, dict): 925 951 self.write_parser.startElement('uptime', 926 AttributesImpl(dict( 927 seconds = host.uptime.get('seconds', ''), 928 lastboot = host.uptime.get('lastboot', '')))) 952 AttributesImpl({ 953 'seconds': host.uptime.get('seconds', ''), 954 'lastboot': host.uptime.get('lastboot', '')}) 955 ) 929 956 self.write_parser.endElement('uptime') 930 957 … … 932 959 # Sequences elementes 933 960 ## TCP Sequence element 934 # Cannot use dict() here, because of the 'class' attribute. 935 if type(host.tcpsequence) == type({}): 961 if isinstance(host.tcpsequence, dict): 936 962 self.write_parser.startElement('tcpsequence', 937 AttributesImpl({ 938 'index': host.tcpsequence.get('index', ''), 939 'class': host.tcpsequence.get('class', ''), 940 'difficulty': host.tcpsequence.get('difficulty', ''), 941 'values': host.tcpsequence.get('values', '')})) 963 AttributesImpl({ 964 'index': host.tcpsequence.get('index', ''), 965 'class': host.tcpsequence.get('class', ''), 966 'difficulty': host.tcpsequence.get('difficulty', 967 ''), 968 'values': host.tcpsequence.get('values', '')}) 969 ) 942 970 self.write_parser.endElement('tcpsequence') 943 971 944 972 ## IP ID Sequence element 945 if type(host.ipidsequence) == type({}):973 if isinstance(host.ipidsequence, dict): 946 974 self.write_parser.startElement('ipidsequence', 947 AttributesImpl({ 948 'class': host.ipidsequence.get('class', ''), 949 'values': host.ipidsequence.get('values', '')})) 975 AttributesImpl({ 976 'class': host.ipidsequence.get('class', ''), 977 'values': host.ipidsequence.get('values', '')}) 978 ) 950 979 self.write_parser.endElement('ipidsequence') 951 980 952 981 ## TCP TS Sequence element 953 if type(host.tcptssequence) == type({}):982 if isinstance(host.tcptssequence, dict): 954 983 self.write_parser.startElement('tcptssequence', 955 AttributesImpl({ 956 'class': host.tcptssequence.get('class', ''), 957 'values': host.tcptssequence.get('values', '')})) 984 AttributesImpl({ 985 'class': host.tcptssequence.get('class', ''), 986 'values': host.tcptssequence.get('values', '')}) 987 ) 958 988 self.write_parser.endElement('tcptssequence') 959 989 # End of sequences elements … … 964 994 965 995 def _write_debugging(self): 966 self.write_parser.startElement('debugging', AttributesImpl(dict( 967 level=str(self.debugging_level)))) 996 self.write_parser.startElement('debugging', 997 AttributesImpl({ 998 'level': str(self.debugging_level)}) 999 ) 968 1000 self.write_parser.endElement('debugging') 969 1001 970 1002 def _write_verbose(self): 971 self.write_parser.startElement('verbose', AttributesImpl(dict( 972 level=str(self.verbose_level)))) 1003 self.write_parser.startElement('verbose', 1004 AttributesImpl({ 1005 'level': str(self.verbose_level)}) 1006 ) 973 1007 self.write_parser.endElement('verbose') 974 1008 975 1009 def _write_scaninfo(self): 976 1010 for scan in self.scaninfo: 977 if type(scan) == type({}): 978 self.write_parser.startElement('scaninfo', 979 AttributesImpl(dict(type = scan.get('type', ''), 980 protocol = scan.get('protocol', ''), 981 numservices = scan.get('numservices', ''), 982 services = scan.get('services', '')))) 983 self.write_parser.endElement('scaninfo') 1011 if not isinstance(scan, dict): 1012 continue 1013 1014 self.write_parser.startElement('scaninfo', 1015 AttributesImpl({ 1016 'type': scan.get('type', ''), 1017 'protocol': scan.get('protocol', ''), 1018 'numservices': scan.get('numservices', ''), 1019 'services': scan.get('services', '')}) 1020 ) 1021 self.write_parser.endElement('scaninfo') 984 1022 985 1023 def _write_nmaprun(self): 986 1024 self.write_parser.startElement('nmaprun', 987 AttributesImpl(dict(annotation = str(self.profile_annotation), 988 args = str(self.nmap_command), 989 description = str(self.profile_description), 990 hint = str(self.profile_hint), 991 nmap_output = str(self.nmap_output), 992 options = str(self.profile_options), 993 profile = str(self.profile), 994 profile_name = str(self.profile_name), 995 scanner = str(self.scanner), 996 start = str(self.start), 997 startstr = str(self.formated_date), 998 target = str(self.target), 999 version = str(self.scanner_version), 1000 scan_name = str(self.scan_name)))) 1025 AttributesImpl({ 1026 'annotation': str(self.profile_annotation), 1027 'args': str(self.nmap_command), 1028 'description': str(self.profile_description), 1029 'hint': str(self.profile_hint), 1030 'nmap_output': str(self.nmap_output), 1031 'options': str(self.profile_options), 1032 'profile': str(self.profile), 1033 'profile_name': str(self.profile_name), 1034 'scanner': str(self.scanner), 1035 'start': str(self.start), 1036 'startstr': str(self.formated_date), 1037 'target': str(self.target), 1038 'version': str(self.scanner_version), 1039 'scan_name': str(self.scan_name)}) 1040 ) 1001 1041 1002 1042 def _verify_file(self, xml_file): … … 1004 1044 if isinstance(xml_file, basestring): 1005 1045 xml_file = open(xml_file, 'w') 1006 xml_file.seek(0)1007 return xml_file1008 1046 else: 1009 1047 mode = xml_file.mode 1010 1048 if mode in ('r+', 'w', 'w+'): 1011 1049 xml_file.seek(0) 1012 return xml_file1050 return xml_file 1013 1051 1014 1052 def __remove_none(self, dic): 1015 1053 # saxutils will have problems if your dic contain any None items 1016 # (it will try to use the method replace, for example, which NoneType1017 # doesn't have).1054 # (it will try to use the replace method, for example, which a 1055 # None object doesn't have). 1018 1056 for k, v in dic.items(): 1019 1057 if k is None or v is None:
