Changeset 4774

Show
Ignore:
Timestamp:
05/07/09 02:47:30 (4 years ago)
Author:
rcarvalho
Message:

Merged revisions 4237 via svnmerge from
http://svn.umitproject.org/svnroot/umit/trunk

................

r4237 | gpolo | 2009-03-01 21:17:05 -0300 (Dom, 01 Mar 2009) | 9 lines


Merged revisions 4236 via svnmerge from
http://svn.umitproject.org/svnroot/umit/branch/NetworkInventory


........

r4236 | gpolo | 2009-03-01 21:13:07 -0300 (Sun, 01 Mar 2009) | 1 line


Merged r4124:4235 from nmapparser branch.

........

................

Location:
branch/umitweb-ng
Files:
2 modified

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 python 
    21# -*- coding: utf-8 -*- 
    32# 
     
    65# 
    76# Author: Adriano Monteiro Marques <adriano@umitproject.org> 
     7#         Guilherme Polo <ggpolo@gmail.com> 
    88#         João Paulo de Souza Medeiros <ignotus21@gmail.com> 
    99#         Luis A. Bastiao Silva <luis.kop@gmail.com> 
     
    3535 
    3636class AttributesImplDict(dict, AttributesImpl): 
    37     pass 
     37    """Use this for displaying AttributesImpl just like a dict.""" 
    3838 
    3939class HostInfo(object): 
     
    5858        self.comment = '' 
    5959 
     60        # XXX this structure it not being used yet. 
    6061        self.nmap_host = { 
    6162                'status': {'state': '', 'reason': ''}, 
     
    8081            return self._id 
    8182        except AttributeError: 
    82             raise Exception("Id was not set yet.") 
     83            raise Exception("Id is not set yet.") 
    8384 
    8485    def set_id(self, host_id): 
     
    604605                self.nmap_xml_file.seek(0) 
    605606                self.parser.parse(self.nmap_xml_file) 
    606  
    607                 # Closing file to avoid problems with file descriptors 
    608                 self.nmap_xml_file.close() 
    609607        else: 
    610608            raise Exception("There's no file to be parsed!") 
     
    650648 
    651649    def startElement(self, name, attrs): 
     650        # AttributesImplDict is used here so utils/xmldisplay.py can display 
     651        # instances of AttributesImpl without any effort. 
    652652        attrs = AttributesImplDict(attrs) 
    653653 
     
    765765        ## Finished element 
    766766        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                ) 
    770771        self.write_parser.endElement('finished') 
    771772 
    772773        ## Hosts element 
    773774        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                ) 
    778780        self.write_parser.endElement('hosts') 
    779781 
     
    787789            # Start host element 
    788790            self.write_parser.startElement('host', 
    789                                 AttributesImpl(dict(comment=host.comment))) 
     791                    AttributesImpl({ 
     792                        'comment': host.comment}) 
     793                    ) 
    790794 
    791795            # Status element 
    792796            self.write_parser.startElement('status', 
    793                                 AttributesImpl(dict( 
    794                                     state=host.status['state']))) 
     797                    AttributesImpl({ 
     798                        'state': host.status['state']}) 
     799                    ) 
    795800            self.write_parser.endElement('status') 
    796801 
     
    801806                self.__remove_none(address) 
    802807                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                        ) 
    807813                self.write_parser.endElement('address') 
    808814            # End of Address elements 
     
    815821 
    816822            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') 
    824832 
    825833            self.write_parser.endElement('hostnames') 
     
    836844                self.__remove_none(export) 
    837845                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                        ) 
    841850                self.write_parser.endElement('extraports') 
    842851 
     
    845854                self.__remove_none(port) 
    846855                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                    ) 
    850860 
    851861                ### Port state 
    852862                self.write_parser.startElement('state', 
    853                         AttributesImpl(dict(state=port.get('state', '')))) 
     863                        AttributesImpl({ 
     864                            'state': port.get('state', '')}) 
     865                        ) 
    854866                self.write_parser.endElement('state') 
    855867 
    856868                ### Port service info 
    857869                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                        ) 
    866878                self.write_parser.endElement('service') 
    867879 
     
    879891            ## Ports used elements 
    880892            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') 
    888904 
    889905            ## Osclass elements 
    890906            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') 
    900920 
    901921            ## Osmatch elements 
    902922            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') 
    909933 
    910934            ## Osfingerprint element 
    911             if type(host.osfingerprint) == type({}): 
     935            if isinstance(host.osfingerprint, dict): 
    912936                self.__remove_none(host.osfingerprint) 
    913937                self.write_parser.startElement('osfingerprint', 
    914                     AttributesImpl(dict( 
    915                         fingerprint=host.osfingerprint.get('fingerprint', '')))) 
     938                        AttributesImpl({ 
     939                            'fingerprint': host.osfingerprint.get( 
     940                                'fingerprint', '')}) 
     941                        ) 
    916942                self.write_parser.endElement('osfingerprint') 
    917943 
     
    922948 
    923949            # Uptime element 
    924             if type(host.uptime) == type({}): 
     950            if isinstance(host.uptime, dict): 
    925951                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                        ) 
    929956                self.write_parser.endElement('uptime') 
    930957 
     
    932959            # Sequences elementes 
    933960            ## 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): 
    936962                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                        ) 
    942970                self.write_parser.endElement('tcpsequence') 
    943971 
    944972            ## IP ID Sequence element 
    945             if type(host.ipidsequence) == type({}): 
     973            if isinstance(host.ipidsequence, dict): 
    946974                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                        ) 
    950979                self.write_parser.endElement('ipidsequence') 
    951980 
    952981            ## TCP TS Sequence element 
    953             if type(host.tcptssequence) == type({}): 
     982            if isinstance(host.tcptssequence, dict): 
    954983                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                        ) 
    958988                self.write_parser.endElement('tcptssequence') 
    959989            # End of sequences elements 
     
    964994 
    965995    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                ) 
    9681000        self.write_parser.endElement('debugging') 
    9691001 
    9701002    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                ) 
    9731007        self.write_parser.endElement('verbose') 
    9741008 
    9751009    def _write_scaninfo(self): 
    9761010        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') 
    9841022 
    9851023    def _write_nmaprun(self): 
    9861024        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                ) 
    10011041 
    10021042    def _verify_file(self, xml_file): 
     
    10041044        if isinstance(xml_file, basestring): 
    10051045            xml_file = open(xml_file, 'w') 
    1006             xml_file.seek(0) 
    1007             return xml_file 
    10081046        else: 
    10091047            mode = xml_file.mode 
    10101048            if mode in ('r+', 'w', 'w+'): 
    10111049                xml_file.seek(0) 
    1012             return xml_file 
     1050        return xml_file 
    10131051 
    10141052    def __remove_none(self, dic): 
    10151053        # saxutils will have problems if your dic contain any None items 
    1016         # (it will try to use the method replace, for example, which NoneType 
    1017         # doesn't have). 
     1054        # (it will try to use the replace method, for example, which a 
     1055        # None object doesn't have). 
    10181056        for k, v in dic.items(): 
    10191057            if k is None or v is None: