Changeset 3887

Show
Ignore:
Timestamp:
01/05/09 22:52:05 (4 years ago)
Author:
gpolo
Message:

Removed trailling whitespaces

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branch/nmapparser/NmapParser.py

    r3885 r3887  
    3030from xml.sax.handler import ContentHandler 
    3131from xml.sax.saxutils import XMLGenerator 
    32 from xml.sax.xmlreader import AttributesImpl as Attributes 
     32from xml.sax.xmlreader import AttributesImpl 
    3333 
    3434months = ('', 
     
    4949    def __init__(self, id): 
    5050        self.id = id 
    51      
     51 
    5252    # Host ID 
    5353    def get_id(self): 
     
    6262            raise Exception("Invalid id! It must represent an integer, " 
    6363                    "received %r" % id) 
    64      
     64 
    6565    # TCP SEQUENCE 
    6666    def set_tcpsequence(self, sequence): 
     
    6969        else: 
    7070            self._tcpsequence = sequence 
    71      
     71 
    7272    def get_tcpsequence(self): 
    7373        if self._tcpsequence: 
     
    8181        else: 
    8282            self._tcptssequence = sequence 
    83      
     83 
    8484    def get_tcptssequence(self): 
    8585        if self._tcptssequence: 
     
    9898        else: 
    9999            self._ipidsequence = sequence 
    100      
     100 
    101101    def get_ipidsequence(self): 
    102102        if self._ipidsequence: 
     
    107107    def set_osclasses(self, classes): 
    108108        self._osclasses = classes 
    109      
     109 
    110110    def get_osclasses(self): 
    111111        return self._osclasses 
    112     
     112 
    113113    # OS MATCHES 
    114114    def set_osmatches(self, matches): 
    115115        if type(matches) == type([]): 
    116116            self._osmatches = matches 
    117      
     117 
    118118    def get_osmatches(self): 
    119119        if self._osmatches: 
    120120            return self._osmatches 
    121121        return [] 
    122      
     122 
    123123    # OS MATCH 
    124124    def set_osmatch(self, match): 
     
    127127        else: 
    128128            self._osmatch = match 
    129      
     129 
    130130    def get_osmatch(self): 
    131131        if self._osmatch: 
    132132            return self._osmatch 
    133133        return {} 
    134      
     134 
    135135    # OS FINGERPRINT 
    136136    def set_osfingerprint(self, fingerprint): 
     
    139139        else: 
    140140            self._osfingerprint = fingerprint 
    141      
     141 
    142142    def get_osfingerprint(self): 
    143143        if self._osfingerprint: 
     
    148148    def set_ports_used(self, ports): 
    149149        self._ports_used = ports 
    150      
     150 
    151151    def get_ports_used(self): 
    152152        return self._ports_used 
    153      
     153 
    154154    # TRACEROUTE 
    155155    def set_trace(self, trace): 
     
    170170                return hop 
    171171        return None 
     172 
    172173    def get_number_of_hops(self): 
    173174        count = 0 
     
    176177                count = int(hop['ttl']) 
    177178        return count 
    178      
     179 
    179180    # UPTIME 
    180181    # FORMAT: {"seconds":"", "lastboot":""} 
    181182    def set_uptime(self, uptime): 
    182183        self._uptime = uptime 
    183      
     184 
    184185    def get_uptime(self): 
    185186        if self._uptime: 
    186187            return self._uptime 
    187          
     188 
    188189        # Avoid empty dict return 
    189190        return {"seconds":"", "lastboot":""} 
     
    192193    def set_ports(self, port_list): 
    193194        self._ports = port_list 
    194      
     195 
    195196    def get_ports(self): 
    196197        return self._ports 
     
    205206    def set_hostnames(self, hostname_list): 
    206207        self._hostnames = hostname_list 
    207      
     208 
    208209    def get_hostnames(self): 
    209210        return self._hostnames 
     
    212213    def set_ip_address(self, addr): 
    213214        self.set_ip(addr) 
    214      
     215 
    215216    def get_ip_address(self): 
    216217        return self.get_ip() 
     
    225226    def get_comment(self): 
    226227        return self._comment 
    227      
     228 
    228229    def set_comment(self, comment): 
    229230        self._comment = comment 
     
    232233    def set_mac_address(self, addr): 
    233234        self.set_mac(addr) 
    234      
     235 
    235236    def get_mac_address(self): 
    236237        return self.get_mac() 
     
    245246    def set_ipv6_address(self, addr): 
    246247        self.set_ipv6(addr) 
    247      
     248 
    248249    def get_ipv6_address(self): 
    249250        return self.get_ipv6() 
     
    258259    def set_state(self, status): 
    259260        self._state = status 
    260      
     261 
    261262    def get_state(self): 
    262263        return self._state 
     
    280281        else: 
    281282            hostname = 'Unknown Host' 
    282          
     283 
    283284        return hostname 
    284285 
     
    286287        ports = self.get_ports() 
    287288        open = 0 
    288          
     289 
    289290        for i in ports: 
    290291            port = i['port'] 
     
    292293                if re.findall('open', p['port_state']): 
    293294                    open+=1 
    294          
     295 
    295296        return open 
    296      
     297 
    297298    def get_filtered_ports(self): 
    298299        ports = self.get_ports() 
    299300        extraports = self.get_extraports() 
    300301        filtered = 0 
    301          
     302 
    302303        for i in ports: 
    303304            port = i['port'] 
     
    309310                filtered += int(extra["count"]) 
    310311        return filtered 
    311      
     312 
    312313    def get_closed_ports(self): 
    313314        ports = self.get_ports() 
    314315        extraports = self.get_extraports() 
    315316        closed = 0 
    316          
     317 
    317318        for i in ports: 
    318319            port = i['port'] 
     
    324325                closed += int(extra["count"]) 
    325326        return closed 
    326      
     327 
    327328    def get_scanned_ports(self): 
    328329        ports = self.get_ports() 
    329330        extraports = self.get_extraports() 
    330331        scanned = 0 
    331          
     332 
    332333        for i in ports: 
    333334            port = i['port'] 
     
    374375    trace = property(get_trace, set_trace) 
    375376    hops = property(get_hops, set_hops) 
    376      
     377 
    377378 
    378379    _id = 0 
     
    400401class ParserBasics(object): 
    401402    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                } 
    408411 
    409412    def set_host_comment(self, host_id, comment): 
     
    413416                break 
    414417        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!") 
    417420 
    418421    def get_host_comment(self, host_id): 
     
    421424                return host.comment 
    422425        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!") 
    425428 
    426429    def get_profile(self): 
     
    429432    def set_profile(self, profile): 
    430433        self.nmap['nmaprun']['profile'] = profile 
    431      
     434 
    432435    def get_profile_name(self): 
    433436        return self.nmap['nmaprun'].get('profile_name', '') 
     
    435438    def set_profile_name(self, name): 
    436439        self.nmap['nmaprun']['profile_name'] = name 
    437      
     440 
    438441    def get_profile_description(self): 
    439442        return self.nmap['nmaprun'].get('description', '') 
     
    441444    def set_profile_description(self, description): 
    442445        self.nmap['nmaprun']['description'] = description 
    443      
     446 
    444447    def get_profile_hint(self): 
    445448        return self.nmap['nmaprun'].get('hint', '') 
     
    447450    def set_profile_hint(self, hint): 
    448451        self.nmap['nmaprun']['hint'] = hint 
    449      
     452 
    450453    def get_profile_annotation(self): 
    451454        return self.nmap['nmaprun'].get('annotation', '') 
     
    453456    def set_profile_annotation(self, annotation): 
    454457        self.nmap['nmaprun']['annotation'] = annotation 
    455      
     458 
    456459    def get_profile_options(self): 
    457460        options = self.nmap['nmaprun'].get('options', '') 
     
    467470            self.nmap['nmaprun']['options'] = options.keys() 
    468471        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 
    472475    def get_target(self): 
    473476        return self.nmap['nmaprun'].get('target', '') 
     
    481484    def set_nmap_output(self, nmap_output): 
    482485        self.nmap['nmaprun']['nmap_output'] = nmap_output 
    483      
     486 
    484487    def get_debugging_level (self): 
    485488        return self.nmap.get('debugging', '') 
     
    487490    def set_debugging_level(self, level): 
    488491        self.nmap['debugging'] = level 
    489      
     492 
    490493    def get_verbose_level (self): 
    491494        return self.nmap.get('verbose', '') 
     
    493496    def set_verbose_level(self, level): 
    494497        self.nmap['verbose'] = level 
    495      
     498 
    496499    def get_scaninfo(self): 
    497500        return self.nmap.get('scaninfo', '') 
     
    499502    def set_scaninfo(self, info): 
    500503        self.nmap['scaninfo'] = info 
    501      
     504 
    502505    def get_services_scanned (self): 
    503506        if self._services_scanned == None: 
    504507            return self._services_scanned 
    505          
     508 
    506509        services = [] 
    507510        for scan in self.nmap.get('scaninfo', []): 
     
    535538        if self._num_services == None: 
    536539            return self._num_services 
    537          
     540 
    538541        num = 0 
    539542        for n in self.nmap.get('scaninfo', []): 
     
    560563            self.nmap['nmaprun']['start'] = date 
    561564        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 
    565568    def get_open_ports(self): 
    566569        ports = 0 
     
    576579        for h in self.nmap.get('hosts', []): 
    577580            ports += h.get_filtered_ports() 
    578  
    579581 
    580582        return ports 
     
    590592    def get_formated_date(self): 
    591593        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]), 
    594596                                      str(date[0]), 
    595                                       str(date[3]).zfill(2),  
     597                                      str(date[3]).zfill(2), 
    596598                                      str(date[4]).zfill(2)) 
    597599 
     
    601603    def set_scanner(self, scanner): 
    602604        self.nmap['nmaprun']['scanner'] = scanner 
    603      
     605 
    604606    def get_scanner_version (self): 
    605607        return self.nmap['nmaprun'].get('version', '') 
     
    619621            except: 
    620622                pass 
    621          
     623 
    622624        return addresses 
    623625 
     
    633635            except: 
    634636                pass 
    635          
     637 
    636638        return addresses 
    637639 
     
    660662        for port in self.nmap.get('hosts', []): 
    661663            ports.append(port.get_ports()) 
    662          
     664 
    663665        return ports 
    664666 
     
    672674    def get_trace(self): 
    673675        return self.nmap.get('trace', None) 
    674      
    675      
     676 
     677 
    676678    def get_runstats(self): 
    677679        return self.nmap.get('runstats', None) 
     
    679681    def set_runstats(self, stats): 
    680682        self.nmap['runstats'] = stats 
    681      
     683 
    682684    def get_hosts_down(self): 
    683685        return int(self.nmap['runstats'].get('hosts_down', '0')) 
     
    685687    def set_hosts_down(self, down): 
    686688        self.nmap['runstats']['hosts_down'] = int(down) 
    687      
     689 
    688690    def get_hosts_up(self): 
    689691        return int(self.nmap['runstats'].get('hosts_up', '0')) 
     
    691693    def set_hosts_up(self, up): 
    692694        self.nmap['runstats']['hosts_up'] = int(up) 
    693      
     695 
    694696    def get_hosts_scanned(self): 
    695697        return int(self.nmap['runstats'].get('hosts_scanned', '0')) 
     
    697699    def set_hosts_scanned(self, scanned): 
    698700        self.nmap['runstats']['hosts_scanned'] = int(scanned) 
    699      
     701 
    700702    def get_finish_time (self): 
    701703        return time.localtime(int(self.nmap['runstats'].get('finished_time', 
     
    716718    def set_scan_name(self, scan_name): 
    717719        self.nmap["scan_name"] = scan_name 
    718      
     720 
    719721    def get_formated_finish_date(self): 
    720722        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]), 
    723725                                      str(date[0]), 
    724                                       str(date[3]).zfill(2),  
     726                                      str(date[3]).zfill(2), 
    725727                                      str(date[4]).zfill(2)) 
    726728 
    727     def _verify_output_options (self, command): 
     729    def _verify_output_options(self, command): 
    728730        found = re.findall ('(-o[XGASN]{1}) {0,1}', command) 
    729731        splited = command.split (' ') 
    730          
     732 
    731733        if found: 
    732734            for option in found: 
     
    734736                del(splited[pos+1]) 
    735737                del(splited[pos]) 
    736          
    737         return ' '.join (splited) 
     738 
     739        return ' '.join(splited) 
    738740 
    739741    def get_comments(self): 
     
    742744    profile = property(get_profile, set_profile) 
    743745    profile_name = property(get_profile_name, set_profile_name) 
    744     profile_description = property(get_profile_description,  
     746    profile_description = property(get_profile_description, 
    745747                                   set_profile_description) 
    746748    profile_hint = property(get_profile_hint, set_profile_hint) 
    747     profile_annotation = property(get_profile_annotation,  
     749    profile_annotation = property(get_profile_annotation, 
    748750                                  set_profile_annotation) 
    749751    profile_options = property(get_profile_options, set_profile_options) 
     
    783785    trace = property(get_trace) 
    784786    hops = property(get_hops) 
    785      
     787 
    786788    _num_services = None 
    787789    _services_scanned = None 
     
    799801        self.in_os = False 
    800802        self.list_extraports = [] 
    801          
     803 
    802804        # Creating a traceroute condition 
    803805        self.in_trace = False 
    804          
     806 
    805807        self.nmap_xml_file = None 
    806808        self.unsaved = False 
     
    827829    def _parse_nmaprun(self, attrs): 
    828830        run_tag = "nmaprun" 
    829          
     831 
    830832        self.nmap[run_tag]["nmap_output"] = attrs.get("nmap_output", "") 
    831833        self.nmap[run_tag]["profile"] = attrs.get("profile", "") 
     
    840842        self.nmap[run_tag]["scanner"] = attrs.get("scanner", "") 
    841843        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", 
    843845                                                           "") 
    844846        self.nmap["scan_name"] = attrs.get("scan_name", "") 
     
    846848    def _parse_scaninfo(self, attrs): 
    847849        dic = {} 
    848          
     850 
    849851        dic["type"] = attrs.get("type", "") 
    850852        dic["protocol"] = attrs.get("protocol", "") 
    851853        dic["numservices"] = attrs.get("numservices", "") 
    852854        dic["services"] = attrs.get("services", "") 
    853          
     855 
    854856        self.nmap["scaninfo"].append(dic) 
    855857 
     
    900902 
    901903    def _parse_host_port(self, attrs): 
    902         self.dic_port = {"protocol":attrs.get("protocol", ""),  
     904        self.dic_port = {"protocol":attrs.get("protocol", ""), 
    903905                         "portid":attrs.get("portid", "")} 
    904906 
     
    920922        elif tmp != {} and tmp.has_key('accuracy'): 
    921923            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 
    926928        self.list_osmatches.append(tmp) 
    927929 
    928930    def _parse_host_portused(self, attrs): 
    929         self.list_portused.append(self._parsing(attrs,  
     931        self.list_portused.append(self._parsing(attrs, 
    930932                                                ['state','proto','portid'])) 
    931933 
     
    936938                                                       'osgen', 
    937939                                                       'accuracy'])) 
    938      
     940 
    939941    def _parse_host_osfingerprint(self, attrs): 
    940942        self.host_info.set_osfingerprint(self._parsing(attrs, ['fingerprint'])) 
    941          
     943 
    942944 
    943945    def _parsing(self, attrs, attrs_list): 
     
    958960                                                             'difficulty', 
    959961                                                             'values'])) 
    960      
     962 
    961963    def _parse_host_tcptssequence(self, attrs): 
    962964        self.host_info.set_tcptssequence(self._parsing(attrs, ['class', 
     
    966968        self.host_info.set_ipidsequence(self._parsing(attrs, ['class', 
    967969                                                              'values'])) 
     970 
    968971    def _parse_host_trace(self, attrs): 
    969972        self.host_info.set_trace(self._parsing(attrs, ['port', 'proto'])) 
     
    973976        self.list_hop.append(tmp) 
    974977 
    975          
     978 
    976979    def startElement(self, name, attrs): 
    977980        if name == "nmaprun": 
     
    10381041        elif self.in_host and name == "ipidsequence": 
    10391042            self._parse_host_ipidsequence(attrs) 
    1040         # Creating a traceroute condition  
     1043        # Creating a traceroute condition 
    10411044        elif self.in_host and name == "trace": 
    10421045            self.in_trace = True 
     
    10761079            del(self.list_osclass) 
    10771080            del(self.list_osmatches) 
    1078              
     1081 
    10791082        # Creating a traceroute condition 
    10801083        elif self.in_host and name == "trace": 
     
    11881191                            Attributes(dict(name = hname.get("hostname", ""), 
    11891192                                        type = hname.get("hostname_type", "")))) 
    1190                      
     1193 
    11911194                    self.write_parser.endElement("hostname") 
    11921195 
     
    12441247            # OS element 
    12451248            self.write_parser.startElement("os", Attributes({})) 
    1246              
     1249 
    12471250            ## Ports used elements 
    12481251            for pu in host.ports_used: 
     
    12751278                                    accuracy = om.get("accuracy", "")))) 
    12761279                    self.write_parser.endElement("osmatch") 
    1277              
     1280 
    12781281            ## Osfingerprint element 
    12791282            if type(host.osfingerprint) == type({}): 
    12801283                self.__remove_none_keys(host.osfingerprint) 
    1281                 self.write_parser.startElement("osfingerprint",  
     1284                self.write_parser.startElement("osfingerprint", 
    12821285                    Attributes(dict(fingerprint = \ 
    12831286                                    host.osfingerprint.get("fingerprint", "")))) 
    12841287                self.write_parser.endElement("osfingerprint") 
    1285              
     1288 
    12861289 
    12871290            self.write_parser.endElement("os") 
     
    13811384            else: 
    13821385                return xml_file 
    1383      
     1386 
    13841387    def __remove_none_keys(self, dic): 
    13851388        for k in dic.keys(): 
     
    13961399    parser = make_parser() 
    13971400    nmap_parser = NmapParserSAX() 
    1398      
     1401 
    13991402    parser.setContentHandler(nmap_parser) 
    14001403    nmap_parser.set_parser(parser)