def test_set_boreas_alive_test_without_settings(self, mock_kb): w = DummyDaemon() t_opt = {'alive_test': 16} w.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) p.scan_id = '456-789' p.kbdb.add_scan_preferences = MagicMock() p.prepare_boreas_alive_test() p.kbdb.add_scan_preferences.assert_not_called()
def test_set_boreas_alive_test_enum_has_precedence(self, mock_kb): w = DummyDaemon() t_opt = { 'alive_test_methods': "1", 'consider_alive': '1', 'alive_test': AliveTest.ALIVE_TEST_ICMP, } w.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {BOREAS_SETTING_NAME: 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) p.scan_id = '456-789' p.kbdb.add_scan_preferences = MagicMock() p.prepare_boreas_alive_test() # has icmp and not consider_alive calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])] p.kbdb.add_scan_preferences.assert_has_calls(calls)
def exec_scan(self, scan_id: str): """ Starts the OpenVAS scanner for scan_id scan. """ do_not_launch = False kbdb = self.main_db.get_new_kb_database() scan_prefs = PreferenceHandler(scan_id, kbdb, self.scan_collection, self.nvti) kbdb.add_scan_id(scan_id) scan_prefs.prepare_target_for_openvas() if not scan_prefs.prepare_ports_for_openvas(): self.add_scan_error(scan_id, name='', host='', value='No port list defined.') do_not_launch = True # Set credentials if not scan_prefs.prepare_credentials_for_openvas(): self.add_scan_error(scan_id, name='', host='', value='Malformed credential.') do_not_launch = True if not scan_prefs.prepare_plugins_for_openvas(): self.add_scan_error(scan_id, name='', host='', value='No VTS to run.') do_not_launch = True scan_prefs.prepare_main_kbindex_for_openvas() scan_prefs.prepare_host_options_for_openvas() scan_prefs.prepare_scan_params_for_openvas(OSPD_PARAMS) scan_prefs.prepare_reverse_lookup_opt_for_openvas() scan_prefs.prepare_alive_test_option_for_openvas() scan_prefs.prepare_boreas_alive_test() # Release memory used for scan preferences. del scan_prefs if do_not_launch: self.main_db.release_database(kbdb) return result = Openvas.start_scan( scan_id, not self.is_running_as_root and self.sudo_available, self._niceness, ) if result is None: self.main_db.release_database(kbdb) return ovas_pid = result.pid kbdb.add_scan_process_id(ovas_pid) logger.debug('pid = %s', ovas_pid) # Wait until the scanner starts and loads all the preferences. while kbdb.get_status(scan_id) == 'new': res = result.poll() if res and res < 0: self.stop_scan_cleanup(scan_id) logger.error( 'It was not possible run the task %s, since openvas ended ' 'unexpectedly with errors during launching.', scan_id, ) return time.sleep(1) got_results = False while True: if not kbdb.target_is_finished( scan_id) and not self.is_openvas_process_alive( kbdb, ovas_pid, scan_id): logger.error( 'Task %s was unexpectedly stopped or killed.', scan_id, ) self.add_scan_error( scan_id, name='', host='', value='Task was unexpectedly stopped or killed.', ) kbdb.stop_scan(scan_id) for scan_db in kbdb.get_scan_databases(): self.main_db.release_database(scan_db) self.main_db.release_database(kbdb) return # Wait a second before trying to get result from redis if there # was no results before. # Otherwise, wait 50 msec to give access other process to redis. if not got_results: time.sleep(1) else: time.sleep(0.05) got_results = False # Check if the client stopped the whole scan if kbdb.scan_is_stopped(scan_id): # clean main_db, but wait for scanner to finish. while not kbdb.target_is_finished(scan_id): time.sleep(1) self.main_db.release_database(kbdb) return got_results = self.report_openvas_results(kbdb, scan_id) self.report_openvas_scan_status(kbdb, scan_id) # Scan end. No kb in use for this scan id if kbdb.target_is_finished(scan_id): break # Delete keys from KB related to this scan task. self.main_db.release_database(kbdb)
def test_set_boreas_alive_test_with_settings(self, mock_kb): # No Boreas config setting (BOREAS_SETTING_NAME) set w = DummyDaemon() ov_setting = {'not_the_correct_setting': 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) p._openvas_scan_id = '456-789' p.kbdb.add_scan_preferences = MagicMock() p.prepare_boreas_alive_test() p.kbdb.add_scan_preferences.assert_not_called() # Boreas config setting set but invalid alive_test. w = DummyDaemon() t_opt = {'alive_test': "error"} w.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {BOREAS_SETTING_NAME: 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) p._openvas_scan_id = '456-789' p.kbdb.add_scan_preferences = MagicMock() p.prepare_boreas_alive_test() p.kbdb.add_scan_preferences.assert_not_called() # ALIVE_TEST_TCP_SYN_SERVICE as alive test. w = DummyDaemon() t_opt = {'alive_test': AliveTest.ALIVE_TEST_TCP_SYN_SERVICE} w.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {BOREAS_SETTING_NAME: 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) p._openvas_scan_id = '456-789' p.kbdb.add_scan_preferences = MagicMock() p.prepare_boreas_alive_test() calls = [call(p._openvas_scan_id, [BOREAS_ALIVE_TEST + '|||16'])] p.kbdb.add_scan_preferences.assert_has_calls(calls) # ICMP was chosen as alive test. w = DummyDaemon() t_opt = {'alive_test': AliveTest.ALIVE_TEST_ICMP} w.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {BOREAS_SETTING_NAME: 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) p._openvas_scan_id = '456-789' p.kbdb.add_scan_preferences = MagicMock() p.prepare_boreas_alive_test() calls = [call(p._openvas_scan_id, [BOREAS_ALIVE_TEST + '|||2'])] p.kbdb.add_scan_preferences.assert_has_calls(calls) # "Scan Config Default" as alive_test. w = DummyDaemon() t_opt = {'alive_test': AliveTest.ALIVE_TEST_SCAN_CONFIG_DEFAULT} w.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {BOREAS_SETTING_NAME: 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) p._openvas_scan_id = '456-789' p.kbdb.add_scan_preferences = MagicMock() p.prepare_boreas_alive_test() calls = [call(p._openvas_scan_id, [BOREAS_ALIVE_TEST + '|||2'])] p.kbdb.add_scan_preferences.assert_has_calls(calls)
def test_set_boreas_alive_test_not_as_enum(self, mock_kb): # No Boreas config setting (BOREAS_SETTING_NAME) set w = DummyDaemon() ov_setting = {'not_the_correct_setting': 1} t_opt = {} w.scan_collection.get_target_options = MagicMock(return_value=t_opt) with patch.object(Openvas, 'get_settings', return_value=ov_setting): p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) p.scan_id = '456-789' p.kbdb.add_scan_preferences = MagicMock() p.prepare_boreas_alive_test() p.kbdb.add_scan_preferences.assert_not_called() # Boreas config setting set but invalid alive_test. w = DummyDaemon() t_opt = {'alive_test_methods': "1", 'arp': '-1'} w.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {BOREAS_SETTING_NAME: 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) p.scan_id = '456-789' p.kbdb.add_scan_preferences = MagicMock() p.prepare_boreas_alive_test() calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])] p.kbdb.add_scan_preferences.assert_has_calls(calls) # ICMP was chosen as alive test. w = DummyDaemon() t_opt = {'alive_test_methods': "1", 'icmp': '1'} w.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {BOREAS_SETTING_NAME: 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) p.scan_id = '456-789' p.kbdb.add_scan_preferences = MagicMock() p.prepare_boreas_alive_test() calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])] p.kbdb.add_scan_preferences.assert_has_calls(calls) # tcp_syn as alive test. w = DummyDaemon() t_opt = {'alive_test_methods': "1", 'tcp_syn': '1'} w.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {BOREAS_SETTING_NAME: 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) p.scan_id = '456-789' p.kbdb.add_scan_preferences = MagicMock() p.prepare_boreas_alive_test() calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||16'])] p.kbdb.add_scan_preferences.assert_has_calls(calls) # tcp_ack as alive test. w = DummyDaemon() t_opt = {'alive_test_methods': "1", 'tcp_ack': '1'} w.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {BOREAS_SETTING_NAME: 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) p.scan_id = '456-789' p.kbdb.add_scan_preferences = MagicMock() p.prepare_boreas_alive_test() calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||1'])] p.kbdb.add_scan_preferences.assert_has_calls(calls) # arp as alive test. w = DummyDaemon() t_opt = {'alive_test_methods': "1", 'arp': '1'} w.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {BOREAS_SETTING_NAME: 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) p.scan_id = '456-789' p.kbdb.add_scan_preferences = MagicMock() p.prepare_boreas_alive_test() calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||4'])] p.kbdb.add_scan_preferences.assert_has_calls(calls) # arp as alive test. w = DummyDaemon() t_opt = {'alive_test_methods': "1", 'consider_alive': '1'} w.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {BOREAS_SETTING_NAME: 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) p.scan_id = '456-789' p.kbdb.add_scan_preferences = MagicMock() p.prepare_boreas_alive_test() calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||8'])] p.kbdb.add_scan_preferences.assert_has_calls(calls) # all alive test methods w = DummyDaemon() t_opt = { 'alive_test_methods': "1", 'icmp': '1', 'tcp_ack': '1', 'tcp_syn': '1', 'arp': '1', 'consider_alive': '1', } w.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {BOREAS_SETTING_NAME: 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) p.scan_id = '456-789' p.kbdb.add_scan_preferences = MagicMock() p.prepare_boreas_alive_test() calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||31'])] p.kbdb.add_scan_preferences.assert_has_calls(calls) # TCP-SYN alive test and dedicated port list for alive scan provided. w = DummyDaemon() t_opt = { 'alive_test_ports': "80,137", 'alive_test_methods': "1", 'tcp_syn': '1', } w.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {BOREAS_SETTING_NAME: 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) p.scan_id = '456-789' p.kbdb.add_scan_preferences = MagicMock() p.prepare_boreas_alive_test() calls = [ call(p.scan_id, [BOREAS_ALIVE_TEST + '|||16']), call(p.scan_id, [BOREAS_ALIVE_TEST_PORTS + '|||80,137']), ] p.kbdb.add_scan_preferences.assert_has_calls(calls)
def test_set_boreas_alive_test_with_settings(self, mock_kb): # No Boreas config setting (BOREAS_SETTING_NAME) set dummy = DummyDaemon() ov_setting = {'not_the_correct_setting': 1} t_opt = {} dummy.scan_collection.get_target_options = MagicMock(return_value=t_opt) with patch.object(Openvas, 'get_settings', return_value=ov_setting): p_handler = PreferenceHandler( '1234-1234', mock_kb, dummy.scan_collection, None ) p_handler.scan_id = '456-789' p_handler.kbdb.add_scan_preferences = MagicMock() p_handler.prepare_boreas_alive_test() p_handler.kbdb.add_scan_preferences.assert_not_called() # Boreas config setting set but invalid alive_test. dummy = DummyDaemon() t_opt = {'alive_test': "error"} dummy.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {BOREAS_SETTING_NAME: 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p_handler = PreferenceHandler( '1234-1234', mock_kb, dummy.scan_collection, None ) p_handler.scan_id = '456-789' p_handler.kbdb.add_scan_preferences = MagicMock() p_handler.prepare_boreas_alive_test() calls = [call(p_handler.scan_id, [BOREAS_ALIVE_TEST + '|||2'])] p_handler.kbdb.add_scan_preferences.assert_has_calls(calls) # ALIVE_TEST_TCP_SYN_SERVICE as alive test. dummy = DummyDaemon() t_opt = {'alive_test': AliveTest.ALIVE_TEST_TCP_SYN_SERVICE} dummy.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {BOREAS_SETTING_NAME: 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p_handler = PreferenceHandler( '1234-1234', mock_kb, dummy.scan_collection, None ) p_handler.scan_id = '456-789' p_handler.kbdb.add_scan_preferences = MagicMock() p_handler.prepare_boreas_alive_test() calls = [call(p_handler.scan_id, [BOREAS_ALIVE_TEST + '|||16'])] p_handler.kbdb.add_scan_preferences.assert_has_calls(calls) # ICMP was chosen as alive test. dummy = DummyDaemon() t_opt = {'alive_test': AliveTest.ALIVE_TEST_ICMP} dummy.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {BOREAS_SETTING_NAME: 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p_handler = PreferenceHandler( '1234-1234', mock_kb, dummy.scan_collection, None ) p_handler.scan_id = '456-789' p_handler.kbdb.add_scan_preferences = MagicMock() p_handler.prepare_boreas_alive_test() calls = [call(p_handler.scan_id, [BOREAS_ALIVE_TEST + '|||2'])] p_handler.kbdb.add_scan_preferences.assert_has_calls(calls) # "Scan Config Default" as alive_test. dummy = DummyDaemon() t_opt = {'alive_test': AliveTest.ALIVE_TEST_SCAN_CONFIG_DEFAULT} dummy.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {BOREAS_SETTING_NAME: 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p_handler = PreferenceHandler( '1234-1234', mock_kb, dummy.scan_collection, None ) p_handler.scan_id = '456-789' p_handler.kbdb.add_scan_preferences = MagicMock() p_handler.prepare_boreas_alive_test() calls = [call(p_handler.scan_id, [BOREAS_ALIVE_TEST + '|||2'])] p_handler.kbdb.add_scan_preferences.assert_has_calls(calls) # TCP-SYN alive test and dedicated port list for alive scan provided. dummy = DummyDaemon() t_opt = { 'alive_test_ports': "80,137", 'alive_test': AliveTest.ALIVE_TEST_TCP_SYN_SERVICE, } dummy.scan_collection.get_target_options = MagicMock(return_value=t_opt) ov_setting = {BOREAS_SETTING_NAME: 1} with patch.object(Openvas, 'get_settings', return_value=ov_setting): p_handler = PreferenceHandler( '1234-1234', mock_kb, dummy.scan_collection, None ) p_handler.scan_id = '456-789' p_handler.kbdb.add_scan_preferences = MagicMock() p_handler.prepare_boreas_alive_test() calls = [ call(p_handler.scan_id, [BOREAS_ALIVE_TEST + '|||16']), call( p_handler.scan_id, [BOREAS_ALIVE_TEST_PORTS + '|||80,137'] ), ] p_handler.kbdb.add_scan_preferences.assert_has_calls(calls)
def exec_scan(self, scan_id: str): """Starts the OpenVAS scanner for scan_id scan.""" params = self.scan_collection.get_options(scan_id) if params.get("dry_run"): dryrun = DryRun(self) dryrun.exec_dry_run_scan(scan_id, self.nvti, OSPD_PARAMS) return do_not_launch = False kbdb = self.main_db.get_new_kb_database() scan_prefs = PreferenceHandler(scan_id, kbdb, self.scan_collection, self.nvti) kbdb.add_scan_id(scan_id) scan_prefs.prepare_target_for_openvas() if not scan_prefs.prepare_ports_for_openvas(): self.add_scan_error(scan_id, name='', host='', value='Invalid port list.') do_not_launch = True # Set credentials if not scan_prefs.prepare_credentials_for_openvas(): error = ('All authentifications contain errors.' + 'Starting unauthenticated scan instead.') self.add_scan_error( scan_id, name='', host='', value=error, ) logger.error(error) errors = scan_prefs.get_error_messages() for e in errors: error = 'Malformed credential. ' + e self.add_scan_error( scan_id, name='', host='', value=error, ) logger.error(error) if not scan_prefs.prepare_plugins_for_openvas(): self.add_scan_error(scan_id, name='', host='', value='No VTS to run.') do_not_launch = True scan_prefs.prepare_main_kbindex_for_openvas() scan_prefs.prepare_host_options_for_openvas() scan_prefs.prepare_scan_params_for_openvas(OSPD_PARAMS) scan_prefs.prepare_reverse_lookup_opt_for_openvas() scan_prefs.prepare_alive_test_option_for_openvas() # VT preferences are stored after all preferences have been processed, # since alive tests preferences have to be able to overwrite default # preferences of ping_host.nasl for the classic method. scan_prefs.prepare_nvt_preferences() scan_prefs.prepare_boreas_alive_test() # Release memory used for scan preferences. del scan_prefs scan_stopped = self.get_scan_status(scan_id) == ScanStatus.STOPPED if do_not_launch or kbdb.scan_is_stopped(scan_id) or scan_stopped: self.main_db.release_database(kbdb) return openvas_process = Openvas.start_scan( scan_id, not self.is_running_as_root and self.sudo_available, self._niceness, ) if openvas_process is None: self.main_db.release_database(kbdb) return kbdb.add_scan_process_id(openvas_process.pid) logger.debug('pid = %s', openvas_process.pid) # Wait until the scanner starts and loads all the preferences. while kbdb.get_status(scan_id) == 'new': res = openvas_process.poll() if res and res < 0: self.stop_scan_cleanup(kbdb, scan_id, openvas_process) logger.error( 'It was not possible run the task %s, since openvas ended ' 'unexpectedly with errors during launching.', scan_id, ) return time.sleep(1) got_results = False while True: openvas_process_is_alive = self.is_openvas_process_alive( openvas_process) target_is_finished = kbdb.target_is_finished(scan_id) scan_stopped = self.get_scan_status(scan_id) == ScanStatus.STOPPED # Report new Results and update status got_results = self.report_openvas_results(kbdb, scan_id) self.report_openvas_scan_status(kbdb, scan_id) # Check if the client stopped the whole scan if scan_stopped: logger.debug('%s: Scan stopped by the client', scan_id) self.stop_scan_cleanup(kbdb, scan_id, openvas_process) # clean main_db, but wait for scanner to finish. while not kbdb.target_is_finished(scan_id): if not self.is_openvas_process_alive(openvas_process): break logger.debug('%s: Waiting for openvas to finish', scan_id) time.sleep(1) self.main_db.release_database(kbdb) return # Scan end. No kb in use for this scan id if target_is_finished: logger.debug('%s: Target is finished', scan_id) break if not openvas_process_is_alive: logger.error( 'Task %s was unexpectedly stopped or killed.', scan_id, ) self.add_scan_error( scan_id, name='', host='', value='Task was unexpectedly stopped or killed.', ) # check for scanner error messages before leaving. self.report_openvas_results(kbdb, scan_id) kbdb.stop_scan(scan_id) for scan_db in kbdb.get_scan_databases(): self.main_db.release_database(scan_db) self.main_db.release_database(kbdb) return # Wait a second before trying to get result from redis if there # was no results before. # Otherwise, wait 50 msec to give access other process to redis. if not got_results: time.sleep(1) else: time.sleep(0.05) got_results = False # Sleep a second to be sure to get all notus results time.sleep(1) # Delete keys from KB related to this scan task. logger.debug('%s: End Target. Release main database', scan_id) self.main_db.release_database(kbdb)
def exec_scan(self, scan_id: str): """ Starts the OpenVAS scanner for scan_id scan. """ do_not_launch = False kbdb = self.main_db.get_new_kb_database() scan_prefs = PreferenceHandler(scan_id, kbdb, self.scan_collection, self.nvti) openvas_scan_id = scan_prefs.prepare_openvas_scan_id_for_openvas() scan_prefs.prepare_target_for_openvas() if not scan_prefs.prepare_ports_for_openvas(): self.add_scan_error(scan_id, name='', host='', value='No port list defined.') do_not_launch = True # Set credentials if not scan_prefs.prepare_credentials_for_openvas(): self.add_scan_error(scan_id, name='', host='', value='Malformed credential.') do_not_launch = True if not scan_prefs.prepare_plugins_for_openvas(): self.add_scan_error(scan_id, name='', host='', value='No VTS to run.') do_not_launch = True scan_prefs.prepare_main_kbindex_for_openvas() scan_prefs.prepare_host_options_for_openvas() scan_prefs.prepare_scan_params_for_openvas(OSPD_PARAMS) scan_prefs.prepare_reverse_lookup_opt_for_openvas() scan_prefs.prepare_alive_test_option_for_openvas() # VT preferences are stored after all preferences have been processed, # since alive tests preferences have to be able to overwrite default # preferences of ping_host.nasl for the classic method. scan_prefs.prepare_nvt_preferences() scan_prefs.prepare_boreas_alive_test() # Release memory used for scan preferences. del scan_prefs if do_not_launch or kbdb.scan_is_stopped(openvas_scan_id): self.main_db.release_database(kbdb) return result = Openvas.start_scan( openvas_scan_id, not self.is_running_as_root and self.sudo_available, self._niceness, ) if result is None: self.main_db.release_database(kbdb) return ovas_pid = result.pid kbdb.add_scan_process_id(ovas_pid) logger.debug('pid = %s', ovas_pid) # Wait until the scanner starts and loads all the preferences. while kbdb.get_status(openvas_scan_id) == 'new': res = result.poll() if res and res < 0: self.stop_scan_cleanup(scan_id) logger.error( 'It was not possible run the task %s, since openvas ended ' 'unexpectedly with errors during launching.', scan_id, ) return time.sleep(1) no_id_found = False got_results = False while True: if not kbdb.target_is_finished( scan_id) and not self.is_openvas_process_alive( kbdb, ovas_pid, openvas_scan_id): logger.error( 'Task %s was unexpectedly stopped or killed.', scan_id, ) self.add_scan_error( scan_id, name='', host='', value='Task was unexpectedly stopped or killed.', ) kbdb.stop_scan(openvas_scan_id) for scan_db in kbdb.get_scan_databases(): self.main_db.release_database(scan_db) self.main_db.release_database(kbdb) return # Wait a second before trying to get result from redis if there # was no results before. # Otherwise, wait 50 msec to give access other process to redis. if not got_results: time.sleep(1) else: time.sleep(0.05) got_results = False # Check if the client stopped the whole scan if kbdb.scan_is_stopped(openvas_scan_id): logger.debug('%s: Scan stopped by the client', scan_id) # clean main_db, but wait for scanner to finish. while not kbdb.target_is_finished(scan_id): logger.debug('%s: Waiting the scan to finish', scan_id) time.sleep(1) self.main_db.release_database(kbdb) return self.report_openvas_results(kbdb, scan_id, "") res_count = 0 for scan_db in kbdb.get_scan_databases(): id_aux = scan_db.get_scan_id() if not id_aux: continue if id_aux == openvas_scan_id: no_id_found = False current_host = scan_db.get_host_ip() res_count += self.report_openvas_results( scan_db, scan_id, current_host) if res_count > 0: got_results = True self.report_openvas_scan_status(scan_db, scan_id, current_host) self.report_openvas_timestamp_scan_host( scan_db, scan_id, current_host) if scan_db.host_is_finished(openvas_scan_id): self.report_openvas_scan_status( scan_db, scan_id, current_host) self.report_openvas_timestamp_scan_host( scan_db, scan_id, current_host) if current_host: self.sort_host_finished( scan_id, finished_hosts=current_host) kbdb.remove_scan_database(scan_db) self.main_db.release_database(scan_db) # Scan end. No kb in use for this scan id if no_id_found and kbdb.target_is_finished(scan_id): logger.debug('%s: Target is finished', scan_id) break no_id_found = True # Delete keys from KB related to this scan task. logger.debug('%s: End Target. Release main database', scan_id) self.main_db.release_database(kbdb)