Beispiel #1
0
    def test_snmp_unknown_auth_alg_credentials(self, mock_kb):
        dummy = DummyDaemon()

        creds = {
            'snmp': {
                'type': 'snmp',
                'username': '******',
                'password': '******',
                'community': 'some comunity',
                'auth_algorithm': 'sha2',
            },
        }

        dummy.scan_collection.get_credentials = MagicMock(return_value=creds)

        p_handler = PreferenceHandler(
            '1234-1234', mock_kb, dummy.scan_collection, None
        )
        p_handler.scan_id = '456-789'
        p_handler.kbdb.add_scan_preferences = MagicMock()
        ret = p_handler.prepare_credentials_for_openvas()
        errors = p_handler.get_error_messages()

        self.assertFalse(ret)
        self.assertIn(
            "Unknown authentication algorithm: "
            + "sha2"
            + ". Use 'md5' or 'sha1'.",
            errors,
        )
Beispiel #2
0
    def test_prepare_alive_test_not_supplied_as_enum(self, mock_kb):
        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",
        }

        t_opt = {'alive_test_methods': '1', 'icmp': '1'}
        w.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 = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
            p._nvts_params = {}
            p.scan_id = '456-789'
            p.kbdb.add_scan_preferences = MagicMock()
            p.prepare_alive_test_option_for_openvas()

            self.assertEqual(p._nvts_params, alive_test_out)
Beispiel #3
0
    def test_set_bad_service_credentials(self, mock_kb):
        dummy = DummyDaemon()

        # bad cred type shh instead of ssh
        creds = {
            'shh': {
                'type': 'up',
                'port': '22',
                'username': '******',
                'password': '******',
            },
        }

        dummy.scan_collection.get_credentials = MagicMock(return_value=creds)

        p_handler = PreferenceHandler(
            '1234-1234', mock_kb, dummy.scan_collection, None
        )
        p_handler.scan_id = '456-789'
        p_handler.kbdb.add_scan_preferences = MagicMock()
        ret = p_handler.prepare_credentials_for_openvas()
        errors = p_handler.get_error_messages()

        self.assertFalse(ret)
        self.assertIn("Unknown service type for credential: shh", errors)
Beispiel #4
0
    def test_set_scan_params(self, mock_kb):
        dummy = DummyDaemon()

        ospd_param_dict = {
            'drop_privileges': {
                'type': 'boolean',
                'name': 'drop_privileges',
                'default': 0,
                'mandatory': 1,
                'description': '',
            },
        }

        opt = {'drop_privileges': 1}

        dummy.scan_collection.get_options = MagicMock(return_value=opt)

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

        p_handler.kbdb.add_scan_preferences.assert_called_with(
            p_handler.scan_id, ['drop_privileges|||yes']
        )
Beispiel #5
0
    def test_snmp_no_priv_alg_but_pw_credentials(self, mock_kb):
        dummy = DummyDaemon()

        creds = {
            'snmp': {
                'type': 'snmp',
                'username': '******',
                'password': '******',
                'community': 'some comunity',
                'auth_algorithm': 'sha1',
                'privacy_password': '******',
            },
        }

        dummy.scan_collection.get_credentials = MagicMock(return_value=creds)

        p_handler = PreferenceHandler(
            '1234-1234', mock_kb, dummy.scan_collection, None
        )
        p_handler.scan_id = '456-789'
        p_handler.kbdb.add_scan_preferences = MagicMock()
        ret = p_handler.prepare_credentials_for_openvas()
        errors = p_handler.get_error_messages()

        self.assertFalse(ret)
        self.assertIn(
            "When no privacy algorithm is used, the privacy"
            + " password also has to be empty.",
            errors,
        )
Beispiel #6
0
    def test_set_scan_params(self, mock_kb):
        w = DummyDaemon()

        OSPD_PARAMS_MOCK = {
            'drop_privileges': {
                'type': 'boolean',
                'name': 'drop_privileges',
                'default': 0,
                'mandatory': 1,
                'description': '',
            },
        }

        opt = {'drop_privileges': 1}

        w.scan_collection.get_options = MagicMock(return_value=opt)

        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
        p.scan_id = '456-789'
        p.kbdb.add_scan_preferences = MagicMock()
        p.prepare_scan_params_for_openvas(OSPD_PARAMS_MOCK)

        p.kbdb.add_scan_preferences.assert_called_with(
            p.scan_id, ['drop_privileges|||yes']
        )
Beispiel #7
0
    def test_set_alive_pinghost(self, mock_kb):
        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",
        ]

        t_opt = {'alive_test': 2}
        w.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 = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
            p._nvts_params = {}
            p.scan_id = '456-789'
            p.kbdb.add_scan_preferences = MagicMock()
            p.prepare_alive_test_option_for_openvas()

            for key, value in p._nvts_params.items():
                self.assertTrue(
                    "{0}|||{1}".format(key, value) in alive_test_out
                )
Beispiel #8
0
    def test_missing_type_for_ssh_credentials(self, mock_kb):
        dummy = DummyDaemon()

        creds = {
            'ssh': {
                'port': '22',
                'username': '******',
                'password': '******',
            },
        }

        dummy.scan_collection.get_credentials = MagicMock(return_value=creds)

        p_handler = PreferenceHandler(
            '1234-1234', mock_kb, dummy.scan_collection, None
        )
        p_handler.scan_id = '456-789'
        p_handler.kbdb.add_scan_preferences = MagicMock()
        ret = p_handler.prepare_credentials_for_openvas()
        errors = p_handler.get_error_messages()

        self.assertFalse(ret)
        self.assertIn(
            "Missing Credential Type for SSH."
            + " Use 'up' for Username + Password"
            + " or 'usk' for Username + SSH Key.",
            errors,
        )
Beispiel #9
0
    def test_set_ports_invalid(self, mock_kb):
        w = DummyDaemon()

        w.scan_collection.get_ports = MagicMock(return_value='2,-9,4')

        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
        p.scan_id = '456-789'
        p.kbdb.add_scan_preferences = MagicMock()
        self.assertFalse(p.prepare_ports_for_openvas())
Beispiel #10
0
    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()
Beispiel #11
0
    def test_set_target(self, mock_kb):
        w = DummyDaemon()

        w.scan_collection.get_host_list = MagicMock(return_value='192.168.0.1')

        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
        p.scan_id = '456-789'
        p.kbdb.add_scan_preferences = MagicMock()
        p.prepare_target_for_openvas()

        p.kbdb.add_scan_preferences.assert_called_with(
            p.scan_id, ['TARGET|||192.168.0.1'],
        )
Beispiel #12
0
    def test_set_ports(self, mock_kb):
        w = DummyDaemon()

        w.scan_collection.get_ports = MagicMock(return_value='80,443')

        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
        p.scan_id = '456-789'
        p.kbdb.add_scan_preferences = MagicMock()
        p.prepare_ports_for_openvas()

        p.kbdb.add_scan_preferences.assert_called_with(
            p.scan_id, ['port_range|||80,443'],
        )
Beispiel #13
0
    def test_set_host_options_none(self, mock_kb):
        w = DummyDaemon()

        exc = ''

        w.scan_collection.get_exclude_hosts = MagicMock(return_value=exc)

        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
        p.scan_id = '456-789'
        p.kbdb.add_scan_preferences = MagicMock()
        p.prepare_host_options_for_openvas()

        p.kbdb.add_scan_preferences.assert_not_called()
Beispiel #14
0
    def test_set_credentials_empty(self, mock_kb):
        w = DummyDaemon()

        creds = {}

        w.scan_collection.get_credentials = MagicMock(return_value=creds)

        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
        p.scan_id = '456-789'
        p.kbdb.add_scan_preferences = MagicMock()
        r = p.prepare_credentials_for_openvas()

        self.assertTrue(r)
Beispiel #15
0
    def test_set_alive_no_invalid_alive_test_no_enum(self, mock_kb):
        w = DummyDaemon()

        t_opt = {'alive_test_methods': '1', 'icmp': '-1'}
        w.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 = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
            p.scan_id = '456-789'
            p.kbdb.add_scan_preferences = MagicMock()
            p.prepare_alive_test_option_for_openvas()

            p.kbdb.add_scan_preferences.assert_not_called()
Beispiel #16
0
    def test_set_reverse_lookup_opt(self, mock_kb):
        w = DummyDaemon()

        t_opt = {'reverse_lookup_only': 1}
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)

        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
        p.scan_id = '456-789'
        p.kbdb.add_scan_preferences = MagicMock()
        p.prepare_reverse_lookup_opt_for_openvas()

        p.kbdb.add_scan_preferences.assert_called_with(
            p.scan_id,
            ['reverse_lookup_only|||yes', 'reverse_lookup_unify|||no',],
        )
Beispiel #17
0
    def test_set_alive_no_setting(self, mock_kb):
        dummy = DummyDaemon()

        t_opt = {}
        dummy.scan_collection.get_target_options = MagicMock(return_value=t_opt)

        ov_setting = {}

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

            p_handler.kbdb.add_scan_preferences.assert_not_called()
Beispiel #18
0
    def test_prepare_alive_test_no_enum_no_alive_test(self, mock_kb):
        dummy = DummyDaemon()

        t_opt = {'alive_test_methods': '1', 'icmp': '0'}
        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()

            p_handler.kbdb.add_scan_preferences.assert_not_called()
Beispiel #19
0
    def test_set_host_options(self, mock_kb):
        dummy = DummyDaemon()

        exc = '192.168.0.1'

        dummy.scan_collection.get_exclude_hosts = MagicMock(return_value=exc)

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

        p_handler.kbdb.add_scan_preferences.assert_called_with(
            p_handler.scan_id,
            ['exclude_hosts|||192.168.0.1'],
        )
Beispiel #20
0
    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)
Beispiel #21
0
    def test_set_credentials(self, mock_kb):
        w = DummyDaemon()

        # bad cred type shh instead of ssh
        creds = {
            'shh': {
                'type': 'ssh',
                'port': '22',
                'username': '******',
                'password': '******',
            },
        }

        w.scan_collection.get_credentials = MagicMock(return_value=creds)

        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
        p.scan_id = '456-789'
        p.kbdb.add_scan_preferences = MagicMock()
        r = p.prepare_credentials_for_openvas()

        self.assertFalse(r)
Beispiel #22
0
    def test_missing_ssh_port_credentials(self, mock_kb):
        dummy = DummyDaemon()

        creds = {
            'ssh': {
                'type': 'up',
                'username': '******',
                'password': '******',
            },
        }

        dummy.scan_collection.get_credentials = MagicMock(return_value=creds)

        p_handler = PreferenceHandler(
            '1234-1234', mock_kb, dummy.scan_collection, None
        )
        p_handler.scan_id = '456-789'
        p_handler.kbdb.add_scan_preferences = MagicMock()
        ret = p_handler.prepare_credentials_for_openvas()

        self.assertTrue(ret)
Beispiel #23
0
    def test_set_credentials(self, mock_kb):
        dummy = DummyDaemon()

        creds = {
            'ssh': {
                'type': 'up',
                'port': '22',
                'username': '******',
                'password': '******',
                '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',
            },
        }

        dummy.scan_collection.get_credentials = MagicMock(return_value=creds)

        p_handler = PreferenceHandler(
            '1234-1234', mock_kb, dummy.scan_collection, None
        )
        p_handler.scan_id = '456-789'
        p_handler.kbdb.add_credentials_to_scan_preferences = MagicMock()
        ret = p_handler.prepare_credentials_for_openvas()

        self.assertTrue(ret)
        assert_called_once(p_handler.kbdb.add_credentials_to_scan_preferences)
Beispiel #24
0
    def test_set_credentials(self, mock_kb):
        w = DummyDaemon()

        creds = {
            'ssh': {
                'type': 'ssh',
                'port': '22',
                'username': '******',
                'password': '******',
            },
            'smb': {
                'type': 'smb',
                'username': '******',
                'password': '******'
            },
            'esxi': {
                'type': 'esxi',
                'username': '******',
                'password': '******',
            },
            'snmp': {
                'type': 'snmp',
                'username': '******',
                'password': '******',
                'community': 'some comunity',
                'auth_algorithm': 'some auth algo',
                'privacy_password': '******',
                'privacy_algorithm': 'privacy algo',
            },
        }

        w.scan_collection.get_credentials = MagicMock(return_value=creds)

        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
        p.scan_id = '456-789'
        p.kbdb.add_scan_preferences = MagicMock()
        r = p.prepare_credentials_for_openvas()

        self.assertTrue(r)
        assert_called_once(p.kbdb.add_scan_preferences)
Beispiel #25
0
    def test_ssh_port_out_of_range_credentials(self, mock_kb):
        dummy = DummyDaemon()

        creds = {
            'ssh': {
                'type': 'up',
                'port': '65536',
                'username': '******',
                'password': '******',
            },
        }

        dummy.scan_collection.get_credentials = MagicMock(return_value=creds)

        p_handler = PreferenceHandler(
            '1234-1234', mock_kb, dummy.scan_collection, None
        )
        p_handler.scan_id = '456-789'
        p_handler.kbdb.add_scan_preferences = MagicMock()
        ret = p_handler.prepare_credentials_for_openvas()
        errors = p_handler.get_error_messages()

        self.assertFalse(ret)
        self.assertIn("Port for SSH is out of range (1-65535): 65536", errors)
Beispiel #26
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)
Beispiel #27
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)
Beispiel #28
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}
        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': "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.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)

        # 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.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)

        # 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.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)

        # "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.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 alive test and dedicated port list for alive scan provided.
        w = DummyDaemon()
        t_opt = {
            'alive_test_ports': "80,137",
            '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.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)