#NEWLINE# def crop( config ) : #NEWLINE# #INDENT# '\n    This function takes an OpenVAS XML with results and return Vulnerability info.\n\n    :param config: Config instance\n    :type config: Config\n\n    :raises: TypeError, InvalidFormat\n    ' #NEWLINE# import copy #NEWLINE# if ( not isinstance ( config , Config ) ) : #NEWLINE# #INDENT# raise TypeError ( ( "Expected Config, got '%s' instead" % type ( config ) ) ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# from xml.etree import cElementTree as ET #NEWLINE# #UNINDENT# except ImportError : #NEWLINE# #INDENT# from xml.etree import ElementTree as ET #NEWLINE# #UNINDENT# output_file = config . output_file #NEWLINE# file_path = config . input_files #NEWLINE# scope_hosts = config . scope #NEWLINE# excluded_hosts = config . excluded #NEWLINE# with open ( file_path , 'rU' ) as f : #NEWLINE# #INDENT# first_line = f . readline ( ) #NEWLINE# if ( ( not first_line . startswith ( '<report ' ) ) or ( not all ( ( True for x in ( 'extension' , 'format_id' , 'content_type' ) if ( x in first_line ) ) ) ) ) : #NEWLINE# #INDENT# raise IOError ( 'Invalid report format' ) #NEWLINE# #UNINDENT# #UNINDENT# root = ET . parse ( file_path ) . getroot ( ) #NEWLINE# report = root . getchildren ( ) [ 0 ] #NEWLINE# root_clone = ET . Element ( root . tag ) #NEWLINE# root_clone . attrib = root . attrib #NEWLINE# report_clone = ET . SubElement ( root_clone , 'report' ) #NEWLINE# report_clone . attrib = report . attrib #NEWLINE# for child in report . getchildren ( ) : #NEWLINE# #INDENT# if ( child . tag not in ( 'host_start' , 'host_end' , 'host' , 'ports' , 'results' ) ) : #NEWLINE# #INDENT# tag = copy . deepcopy ( child ) #NEWLINE# report_clone . append ( tag ) #NEWLINE# #UNINDENT# #UNINDENT# results_tag = ET . Element ( 'results' ) #NEWLINE# results_tag . attrib = root . find ( './/results' ) . attrib #NEWLINE# for vuln in root . findall ( './/results/result' ) : #NEWLINE# #INDENT# vuln_host = vuln . find ( './/host' ) . text #NEWLINE# if ( vuln_host in excluded_hosts ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( scope_hosts is not None ) : #NEWLINE# #INDENT# if ( vuln_host not in scope_hosts ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# #UNINDENT# results_tag . append ( vuln ) #NEWLINE# #UNINDENT# report_clone . append ( results_tag ) #NEWLINE# ports_tag = ET . Element ( 'ports' ) #NEWLINE# ports_tag . attrib = root . find ( './/ports' ) . attrib #NEWLINE# for port in root . findall ( './/ports/port' ) : #NEWLINE# #INDENT# vuln_host = port . find ( './/host' ) . text #NEWLINE# if ( vuln_host in excluded_hosts ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( scope_hosts is not None ) : #NEWLINE# #INDENT# if ( vuln_host not in x ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# #UNINDENT# ports_tag . append ( port ) #NEWLINE# #UNINDENT# report_clone . append ( ports_tag ) #NEWLINE# for host in root . findall ( './/report/host' ) : #NEWLINE# #INDENT# vuln_host = host . find ( './/ip' ) . text #NEWLINE# if ( vuln_host in excluded_hosts ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( scope_hosts is not None ) : #NEWLINE# #INDENT# if ( vuln_host not in scope_hosts ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# #UNINDENT# report_clone . append ( host ) #NEWLINE# #UNINDENT# for host_start in root . findall ( './/report/host_start' ) : #NEWLINE# #INDENT# vuln_host = host_start . find ( './/host' ) . text #NEWLINE# if ( vuln_host in excluded_hosts ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( scope_hosts is not None ) : #NEWLINE# #INDENT# if ( vuln_host not in scope_hosts ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# #UNINDENT# report_clone . append ( host_start ) #NEWLINE# #UNINDENT# for host_end in root . findall ( './/report/host_end' ) : #NEWLINE# #INDENT# vuln_host = host_end . find ( './/host' ) . text #NEWLINE# if ( vuln_host in excluded_hosts ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( scope_hosts is not None ) : #NEWLINE# #INDENT# if ( vuln_host not in scope_hosts ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# #UNINDENT# report_clone . append ( host_end ) #NEWLINE# #UNINDENT# tree = ET . ElementTree ( root_clone ) #NEWLINE# tree . write ( output_file , encoding = 'UTF-8' , xml_declaration = False )