Example #1
0
    def test_set_alive_pinghost(self, mock_kb):
        dummy = DummyDaemon()

        alive_test_out = [
            "1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping|||no",
            "1.3.6.1.4.1.25623.1.0.100315:2:checkbox:"
            "TCP ping tries also TCP-SYN ping|||no",
            "1.3.6.1.4.1.25623.1.0.100315:7:checkbox:"
            "TCP ping tries only TCP-SYN ping|||no",
            "1.3.6.1.4.1.25623.1.0.100315:3:checkbox:Do an ICMP ping|||yes",
            "1.3.6.1.4.1.25623.1.0.100315:4:checkbox:Use ARP|||no",
            "1.3.6.1.4.1.25623.1.0.100315:5:checkbox:"
            "Mark unrechable Hosts as dead (not scanning)|||yes",
        ]

        t_opt = {'alive_test': 2}
        dummy.scan_collection.get_target_options = MagicMock(return_value=t_opt)

        ov_setting = {'some_setting': 1}

        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
            p_handler = PreferenceHandler(
                '1234-1234', mock_kb, dummy.scan_collection, None
            )
            p_handler._nvts_params = {}  # pylint: disable = protected-access
            p_handler.scan_id = '456-789'
            p_handler.kbdb.add_scan_preferences = MagicMock()
            p_handler.prepare_alive_test_option_for_openvas()

            for (
                key,
                value,
            ) in (
                p_handler._nvts_params.items()  # pylint: disable = protected-access
            ):
                self.assertTrue(f"{key}|||{value}" in alive_test_out)
    def test_build_alive_test_opt(self):
        w = DummyDaemon()

        alive_test_out = {
            "1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping": "no",
            "1.3.6.1.4.1.25623.1.0.100315:2:checkbox:TCP ping tries also TCP-SYN ping": "no",
            "1.3.6.1.4.1.25623.1.0.100315:7:checkbox:TCP ping tries only TCP-SYN ping": "no",
            "1.3.6.1.4.1.25623.1.0.100315:3:checkbox:Do an ICMP ping": "yes",
            "1.3.6.1.4.1.25623.1.0.100315:4:checkbox:Use ARP": "no",
            "1.3.6.1.4.1.25623.1.0.100315:5:checkbox:Mark unrechable Hosts as dead (not scanning)": "yes",
        }

        target_options_dict = {'alive_test': '2'}
        p = PreferenceHandler('1234-1234', None, w.scan_collection, None)
        ret = p.build_alive_test_opt_as_prefs(target_options_dict)

        self.assertEqual(ret, alive_test_out)

        # alive test was supplied via sepertae xml element
        w = DummyDaemon()
        target_options_dict = {'alive_test_methods': '1', 'icmp': '1'}
        p = PreferenceHandler('1234-1234', None, w.scan_collection, None)
        ret = p.build_alive_test_opt_as_prefs(target_options_dict)
        self.assertEqual(ret, alive_test_out)
Example #3
0
    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)
Example #4
0
    def exec_scan(self, scan_id: str):
        """ Starts the OpenVAS scanner for scan_id scan. """
        if self.pending_feed:
            logger.info(
                '%s: There is a pending feed update. '
                'The scan can not be started.',
                scan_id,
            )
            self.add_scan_error(
                scan_id,
                name='',
                host='',
                value=(
                    'It was not possible to start the scan,'
                    'because a pending feed update. Please try later'
                ),
            )
            return 2

        do_not_launch = False

        # Set plugins to run.
        # Make a deepcopy of the vts dictionary. Otherwise, consulting the
        # DictProxy object of multiprocessing directly is to expensinve
        # (interprocess communication).
        temp_vts = self.vts.copy()

        kbdb = self.main_db.get_new_kb_database()
        scan_prefs = PreferenceHandler(
            scan_id, kbdb, self.scan_collection, temp_vts
        )
        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

        temp_vts = None

        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()

        # Release memory used for scan preferences.
        del scan_prefs

        if do_not_launch:
            self.main_db.release_database(kbdb)
            return 2

        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 False

        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 1

            time.sleep(1)

        no_id_found = False
        while True:
            time.sleep(3)
            # Check if the client stopped the whole scan
            if kbdb.scan_is_stopped(openvas_scan_id):
                return 1

            self.report_openvas_results(kbdb, scan_id, "")

            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()

                    self.report_openvas_results(scan_db, scan_id, current_host)
                    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.set_scan_host_finished(scan_id, current_host)
                        self.report_openvas_scan_status(
                            scan_db, scan_id, current_host
                        )
                        self.report_openvas_timestamp_scan_host(
                            scan_db, scan_id, 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):
                break

            no_id_found = True

        # Delete keys from KB related to this scan task.
        self.main_db.release_database(kbdb)
Example #5
0
    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)
Example #6
0
    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)
Example #7
0
    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)
Example #8
0
    def test_build_credentials(self):
        dummy = DummyDaemon()

        cred_out = [
            '1.3.6.1.4.1.25623.1.0.105058:1:entry:ESXi login name:|||username',
            '1.3.6.1.4.1.25623.1.0.105058:2:password:'******'ESXi login password:|||pass',
            'auth_port_ssh|||22',
            '1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:|||username',
            '1.3.6.1.4.1.25623.1.0.103591:2:'
            'password:SSH key passphrase:|||pass',
            '1.3.6.1.4.1.25623.1.0.103591:4:file:SSH private key:|||',
            '1.3.6.1.4.1.25623.1.0.103591:7:'
            'entry:SSH privilege login name:|||',
            '1.3.6.1.4.1.25623.1.0.103591:8:'
            'password:SSH privilege password:|||',
            '1.3.6.1.4.1.25623.1.0.90023:1:entry:SMB login:|||username',
            '1.3.6.1.4.1.25623.1.0.90023:2:password]:SMB password :|||pass',
            '1.3.6.1.4.1.25623.1.0.105076:1:'
            'password:SNMP Community:some comunity',
            '1.3.6.1.4.1.25623.1.0.105076:2:entry:SNMPv3 Username:username',
            '1.3.6.1.4.1.25623.1.0.105076:3:password:SNMPv3 Password:pass',
            '1.3.6.1.4.1.25623.1.0.105076:4:radio:SNMPv3'
            ' Authentication Algorithm:some auth algo',
            '1.3.6.1.4.1.25623.1.0.105076:5:password:'******'SNMPv3 Privacy Password:privacy pass',
            '1.3.6.1.4.1.25623.1.0.105076:6:radio:'
            'SNMPv3 Privacy Algorithm:privacy algo',
        ]
        cred_dict = {
            'ssh': {
                'type': 'usk',
                'port': '22',
                'username': '******',
                'password': '******',
                'private': 'some key',
                'priv_username': '******',
                'priv_password': '******',
            },
            'smb': {'type': 'up', 'username': '******', 'password': '******'},
            'esxi': {
                'type': 'up',
                'username': '******',
                'password': '******',
            },
            'snmp': {
                'type': 'snmp',
                'username': '******',
                'password': '******',
                'community': 'some comunity',
                'auth_algorithm': 'md5',
                'privacy_password': '******',
                'privacy_algorithm': 'aes',
            },
        }

        p_handler = PreferenceHandler(
            '1234-1234', None, dummy.scan_collection, None
        )
        ret = p_handler.build_credentials_as_prefs(cred_dict)

        self.assertEqual(len(ret), len(cred_out))
        self.assertIn('auth_port_ssh|||22', ret)
        self.assertIn(
            '1.3.6.1.4.1.25623.1.0.90023:1:entry:SMB login:|||username',
            ret,
        )
        self.assertIn(
            '1.3.6.1.4.1.25623.1.0.103591:8:'
            'password:SSH privilege password:|||su_pass',
            ret,
        )
Example #9
0
    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)
Example #10
0
    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)