def test_get_openvas_result(self, mock_add_scan_log_to_list, MockDBClass): w = DummyDaemon() target_element = w.create_xml_target() targets = OspRequest.process_target_element(target_element) w.create_scan('123-456', targets, None, []) results = [ "LOG|||192.168.0.1|||localhost|||general/Host_Details||||||Host" " dead", ] MockDBClass.get_result.return_value = results mock_add_scan_log_to_list.return_value = None w.report_openvas_results(MockDBClass, '123-456') mock_add_scan_log_to_list.assert_called_with( host='192.168.0.1', hostname='localhost', name='', port='general/Host_Details', qod='', test_id='', uri='', value='Host dead', )
def test_report_openvas_scan_status(self, mock_db, mock_sort_host_finished, mock_set_scan_progress_batch): w = DummyDaemon() mock_set_scan_progress_batch.return_value = None mock_sort_host_finished.return_value = None mock_db.get_scan_status.return_value = [ '192.168.0.1/15/1000', '192.168.0.2/15/0', '192.168.0.3/15/-1', '192.168.0.4/1500/1500', ] target_element = w.create_xml_target() targets = OspRequest.process_target_element(target_element) w.create_scan('123-456', targets, None, []) w.report_openvas_scan_status(mock_db, '123-456') mock_set_scan_progress_batch.assert_called_with( '123-456', host_progress={ '192.168.0.1': 1, '192.168.0.3': -1, '192.168.0.4': 100, }, ) mock_sort_host_finished.assert_called_with( '123-456', ['192.168.0.3', '192.168.0.4'])
def test_get_openvas_result_host_deny( self, mock_add_scan_error_to_list, MockDBClass ): w = DummyDaemon() target_element = w.create_xml_target() targets = OspRequest.process_target_element(target_element) w.create_scan('123-456', targets, None, []) results = [ "ERRMSG|||127.0.0.1|||localhost|||||||||Host access denied.", ] MockDBClass.get_result.return_value = results mock_add_scan_error_to_list.return_value = None w.report_openvas_results(MockDBClass, '123-456') mock_add_scan_error_to_list.assert_called_with( host='127.0.0.1', hostname='localhost', name='', port='', test_id='', uri='', value='Host access denied.', )
def test_update_progress(self, mock_set_scan_host_progress): w = DummyDaemon() mock_set_scan_host_progress.return_value = None msg = '0/-1' target_element = w.create_xml_target() targets = OspRequest.process_target_element(target_element) w.create_scan('123-456', targets, None, []) w.update_progress('123-456', 'localhost', msg) mock_set_scan_host_progress.assert_called_with('123-456', 'localhost', 100)
def test_get_openvas_result_dead_hosts(self, MockDBClass): w = DummyDaemon() target_element = w.create_xml_target() targets = OspRequest.process_target_element(target_element) w.create_scan('123-456', targets, None, []) results = ["DEADHOST||| ||| ||| |||4", None] mock_db = MockDBClass.return_value mock_db.get_result.side_effect = results w.scan_collection.set_amount_dead_hosts = MagicMock() w.report_openvas_results(mock_db, '123-456', 'localhost') w.scan_collection.set_amount_dead_hosts.assert_called_with( '123-456', total_dead=4, )
def test_result_without_vt_oid(self, mock_add_scan_alarm_to_list, MockDBClass): w = DummyDaemon() logging.Logger.warning = Mock() target_element = w.create_xml_target() targets = OspRequest.process_target_element(target_element) w.create_scan('123-456', targets, None, []) w.scan_collection.scans_table['123-456']['results'] = list() results = ["ALARM||| ||| ||| ||| |||some alarm|||path", None] MockDBClass.get_result.return_value = results mock_add_scan_alarm_to_list.return_value = None w.report_openvas_results(MockDBClass, '123-456') assert_called_once(logging.Logger.warning)
def test_get_openvas_result_hosts_count(self, MockDBClass): w = DummyDaemon() target_element = w.create_xml_target() targets = OspRequest.process_target_element(target_element) w.create_scan('123-456', targets, None, []) results = [ "HOSTS_COUNT||| ||| ||| ||| |||4", ] MockDBClass.get_result.return_value = results w.set_scan_total_hosts = MagicMock() w.report_openvas_results(MockDBClass, '123-456') w.set_scan_total_hosts.assert_called_with( '123-456', 4, )
def test_get_openvas_result_host_start(self, mock_add_scan_log_to_list, MockDBClass): w = DummyDaemon() target_element = w.create_xml_target() targets = OspRequest.process_target_element(target_element) w.create_scan('123-456', targets, None, []) results = [ "HOST_START|||192.168.10.124||| ||| ||||||today 1", ] MockDBClass.get_result.return_value = results mock_add_scan_log_to_list.return_value = None w.report_openvas_results(MockDBClass, '123-456') mock_add_scan_log_to_list.assert_called_with( host='192.168.10.124', name='HOST_START', value='today 1', )
def test_get_openvas_result(self, mock_add_scan_log_to_list, MockDBClass): w = DummyDaemon() target_element = w.create_xml_target() targets = OspRequest.process_target_element(target_element) w.create_scan('123-456', targets, None, []) results = ["LOG||| |||general/Host_Details||| |||Host dead", None] mock_db = MockDBClass.return_value mock_db.get_result.side_effect = results mock_add_scan_log_to_list.return_value = None w.load_vts() w.report_openvas_results(mock_db, '123-456', 'localhost') mock_add_scan_log_to_list.assert_called_with( host='localhost', hostname='', name='', port='general/Host_Details', qod='', test_id='', value='Host dead', )
def handle_xml(self, xml: Element) -> bytes: """Handles <start_scan> command. Return: Response string for <start_scan> command. """ current_queued_scans = self._daemon.get_count_queued_scans() if ( self._daemon.max_queued_scans and current_queued_scans >= self._daemon.max_queued_scans ): logger.info( 'Maximum number of queued scans set to %d reached.', self._daemon.max_queued_scans, ) raise OspdCommandError( 'Maximum number of queued scans set to %d reached.' % self._daemon.max_queued_scans, 'start_scan', ) target_str = xml.get('target') ports_str = xml.get('ports') # For backward compatibility, if target and ports attributes are set, # <targets> element is ignored. if target_str is None or ports_str is None: target_element = xml.find('targets/target') if target_element is None: raise OspdCommandError('No targets or ports', 'start_scan') else: scan_target = OspRequest.process_target_element(target_element) else: scan_target = { 'hosts': target_str, 'ports': ports_str, 'credentials': {}, 'exclude_hosts': '', 'finished_hosts': '', 'options': {}, } logger.warning( "Legacy start scan command format is being used, which " "is deprecated since 20.08. Please read the documentation " "for start scan command." ) scan_id = xml.get('scan_id') if scan_id is not None and scan_id != '' and not valid_uuid(scan_id): raise OspdCommandError('Invalid scan_id UUID', 'start_scan') if xml.get('parallel'): logger.warning( "parallel attribute of start_scan will be ignored, sice " "parallel scan is not supported by OSPd." ) scanner_params = xml.find('scanner_params') if scanner_params is None: raise OspdCommandError('No scanner_params element', 'start_scan') # params are the parameters we got from the <scanner_params> XML. params = self._daemon.preprocess_scan_params(scanner_params) # VTS is an optional element. If present should not be empty. vt_selection = {} # type: Dict scanner_vts = xml.find('vt_selection') if scanner_vts is not None: if len(scanner_vts) == 0: raise OspdCommandError('VTs list is empty', 'start_scan') else: vt_selection = OspRequest.process_vts_params(scanner_vts) scan_params = self._daemon.process_scan_params(params) scan_id_aux = scan_id scan_id = self._daemon.create_scan( scan_id, scan_target, scan_params, vt_selection ) if not scan_id: id_ = Element('id') id_.text = scan_id_aux return simple_response_str('start_scan', 100, 'Continue', id_) logger.info( 'Scan %s added to the queue in position %d.', scan_id, current_queued_scans + 1, ) id_ = Element('id') id_.text = scan_id return simple_response_str('start_scan', 200, 'OK', id_)
def handle_xml(self, xml: Element) -> bytes: """ Handles <start_scan> command. Return: Response string for <start_scan> command. """ target_str = xml.get('target') ports_str = xml.get('ports') # For backward compatibility, if target and ports attributes are set, # <targets> element is ignored. if target_str is None or ports_str is None: target_element = xml.find('targets/target') if target_element is None: raise OspdCommandError('No targets or ports', 'start_scan') else: scan_target = OspRequest.process_target_element(target_element) else: scan_target = { 'hosts': target_str, 'ports': ports_str, 'credentials': {}, 'exclude_hosts': '', 'finished_hosts': '', 'options': {}, } logger.warning( "Legacy start scan command format is beeing used, which " "is deprecated since 20.04. Please read the documentation " "for start scan command.") scan_id = xml.get('scan_id') if scan_id is not None and scan_id != '' and not valid_uuid(scan_id): raise OspdCommandError('Invalid scan_id UUID', 'start_scan') if xml.get('parallel'): logger.warning( "parallel attribute of start_scan will be ignored, sice " "parallel scan is not supported by OSPd.") scanner_params = xml.find('scanner_params') if scanner_params is None: raise OspdCommandError('No scanner_params element', 'start_scan') params = self._daemon.preprocess_scan_params(scanner_params) # VTS is an optional element. If present should not be empty. vt_selection = {} # type: Dict scanner_vts = xml.find('vt_selection') if scanner_vts is not None: if len(scanner_vts) == 0: raise OspdCommandError('VTs list is empty', 'start_scan') else: vt_selection = OspRequest.process_vts_params(scanner_vts) # Dry run case. if 'dry_run' in params and int(params['dry_run']): scan_func = self._daemon.dry_run_scan scan_params = None else: scan_func = self._daemon.start_scan scan_params = self._daemon.process_scan_params(params) scan_id_aux = scan_id scan_id = self._daemon.create_scan(scan_id, scan_target, scan_params, vt_selection) if not scan_id: id_ = Element('id') id_.text = scan_id_aux return simple_response_str('start_scan', 100, 'Continue', id_) scan_process = create_process(func=scan_func, args=(scan_id, scan_target)) self._daemon.scan_processes[scan_id] = scan_process scan_process.start() id_ = Element('id') id_.text = scan_id return simple_response_str('start_scan', 200, 'OK', id_)
def process_targets_element(scanner_target) -> List: return OspRequest.process_target_element(scanner_target)