<s> __all__ = [ "<STR_LIT>" ] <EOL> try : <EOL> from xml . etree import cElementTree as ET <EOL> except ImportError : <EOL> from xml . etree import ElementTree as ET <EOL> from . . data . parsed_data import Port , Host , Vulnerability <EOL> def openvas_parser ( files_path , excluded_hosts = None , scope_hosts = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( files_path , list ) : <EOL> raise TypeError ( "<STR_LIT>" % type ( files_path ) ) <EOL> else : <EOL> for f in files_path : <EOL> if not isinstance ( f , str ) : <EOL> raise TypeError ( "<STR_LIT>" % type ( f ) ) <EOL> if scope_hosts is not None : <EOL> if not isinstance ( scope_hosts , list ) : <EOL> raise TypeError ( "<STR_LIT>" % type ( scope_hosts ) ) <EOL> else : <EOL> for s in scope_hosts : <EOL> if not isinstance ( s , str ) : <EOL> raise TypeError ( "<STR_LIT>" % type ( s ) ) <EOL> if excluded_hosts is not None : <EOL> if not isinstance ( excluded_hosts , list ) : <EOL> raise TypeError ( "<STR_LIT>" % type ( excluded_hosts ) ) <EOL> else : <EOL> for e in excluded_hosts : <EOL> if not isinstance ( e , str ) : <EOL> raise TypeError ( "<STR_LIT>" % type ( e ) ) <EOL> else : <EOL> excluded_hosts = [ ] <EOL> for f_file in files_path : <EOL> with open ( f_file , "<STR_LIT>" ) as f : <EOL> first_line = f . readline ( ) <EOL> if not first_line . startswith ( "<STR_LIT>" ) or not all ( True for x in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) if x in first_line ) : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> vulnerabilities = { } <EOL> for f_file in files_path : <EOL> root = ET . parse ( f_file ) . getroot ( ) <EOL> for vuln in root . findall ( "<STR_LIT>" ) : <EOL> nvt_tmp = vuln . find ( "<STR_LIT>" ) <EOL> vuln_id = nvt_tmp . get ( "<STR_LIT>" ) <EOL> if not vuln_id or vuln_id == "<STR_LIT:0>" : <EOL> continue <EOL> vuln_threat = vuln . find ( "<STR_LIT>" ) <EOL> if vuln_threat is None : <EOL> vuln_threat = "<STR_LIT>" <EOL> else : <EOL> vuln_threat = vuln_threat . text <EOL> if vuln_threat . lower ( ) == "<STR_LIT>" : <EOL> continue <EOL> vuln_id = vuln_id . split ( "<STR_LIT:.>" ) [ - <NUM_LIT:1> ] <EOL> vuln_level = nvt_tmp . find ( "<STR_LIT>" ) . text <EOL> if vuln_level : <EOL> if vuln_level . lower ( ) == "<STR_LIT:none>" : <EOL> continue <EOL> elif "<STR_LIT>" in vuln_level . lower ( ) : <EOL> continue <EOL> else : <EOL> continue <EOL> vuln_name = nvt_tmp . find ( "<STR_LIT>" ) . text <EOL> vuln_family = nvt_tmp . find ( "<STR_LIT>" ) . text <EOL> vuln_cvss = nvt_tmp . find ( "<STR_LIT>" ) . text <EOL> if vuln_cvss is None : <EOL> vuln_cvss = <NUM_LIT:0.0> <EOL> vuln_cvss = float ( vuln_cvss ) <EOL> vuln_cves = nvt_tmp . find ( "<STR_LIT>" ) . text <EOL> if vuln_cves : <EOL> if vuln_cves . lower ( ) == "<STR_LIT>" : <EOL> vuln_cves = [ ] <EOL> else : <EOL> vuln_cves = [ vuln_cves . lower ( ) ] <EOL> vuln_references = nvt_tmp . find ( "<STR_LIT>" ) . text <EOL> if vuln_references : <EOL> if vuln_references . lower ( ) == "<STR_LIT>" : <EOL> vuln_references = [ ] <EOL> else : <EOL> tmp1 = vuln_references . strip ( ) . lower ( ) <EOL> tmp1_init = tmp1 . find ( "<STR_LIT>" ) <EOL> tmp2 = tmp1 [ tmp1_init + <NUM_LIT:4> : ] . split ( "<STR_LIT:U+002C>" ) <EOL> vuln_references = [ x . strip ( ) for x in tmp2 ] <EOL> vuln_host = vuln . find ( "<STR_LIT>" ) . text <EOL> vuln_port = vuln . find ( "<STR_LIT>" ) . text <EOL> if vuln_host in excluded_hosts : <EOL> continue <EOL> if scope_hosts is not None : <EOL> if vuln_host not in scope_hosts : <EOL> continue <EOL> vuln_description = vuln . find ( "<STR_LIT>" ) . text <EOL> host = Host ( vuln_host ) <EOL> try : <EOL> port = Port . string2port ( vuln_port ) <EOL> except ValueError : <EOL> port = None <EOL> try : <EOL> vuln_store = vulnerabilities [ vuln_id ] <EOL> except KeyError : <EOL> vuln_store = Vulnerability ( vuln_id , <EOL> name = vuln_name , <EOL> threat = vuln_threat , <EOL> cves = vuln_cves , <EOL> cvss = vuln_cvss , <EOL> description = vuln_description , <EOL> references = vuln_references , <EOL> level = vuln_level , <EOL> family = vuln_family ) <EOL> vuln_store . add_host ( host , port ) <EOL> vulnerabilities [ vuln_id ] = vuln_store <EOL> return list ( vulnerabilities . values ( ) ) </s>
