Exemple #1
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._openvas_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)
Exemple #2
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_setiting': 1}
        Openvas.get_settings = Mock()
        Openvas.get_settings.reprepare_mock()
        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_alive_test_option_for_openvas()

        p.kbdb.add_scan_preferences.assert_called_with(
            p._openvas_scan_id,
            alive_test_out,
        )
Exemple #3
0
    def test_prepare_nvt_prefs_no_prefs(self, mock_kb):
        w = DummyDaemon()

        p = PreferenceHandler('456-789', mock_kb, w.scan_collection, None)
        p._nvts_params = {}
        p._openvas_scan_id = '456-789'
        p.kbdb.add_scan_preferences = MagicMock()
        p.prepare_nvt_preferences()

        p.kbdb.add_scan_preferences.assert_not_called()
Exemple #4
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._openvas_scan_id = '456-789'
            p.kbdb.add_scan_preferences = MagicMock()
            p.prepare_boreas_alive_test()

            p.kbdb.add_scan_preferences.assert_not_called()
Exemple #5
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._openvas_scan_id = '456-789'
        p.kbdb.add_scan_preferences = MagicMock()
        p.prepare_host_options_for_openvas()

        p.kbdb.add_scan_preferences.assert_not_called()
Exemple #6
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._openvas_scan_id = '456-789'
        p.kbdb.add_scan_preferences = MagicMock()
        r = p.prepare_credentials_for_openvas()

        self.assertTrue(r)
Exemple #7
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._openvas_scan_id = '456-789'
        p.kbdb.add_scan_preferences = MagicMock()
        p.prepare_ports_for_openvas()

        p.kbdb.add_scan_preferences.assert_called_with(
            p._openvas_scan_id,
            ['port_range|||80,443'],
        )
Exemple #8
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._openvas_scan_id = '456-789'
        p.kbdb.add_scan_preferences = MagicMock()
        p.prepare_target_for_openvas()

        p.kbdb.add_scan_preferences.assert_called_with(
            p._openvas_scan_id,
            ['TARGET|||192.168.0.1'],
        )
Exemple #9
0
    def test_set_alive_no_setting(self, mock_kb):
        w = DummyDaemon()

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

        ov_setting = {}

        Openvas.get_settings = MagicMock(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_alive_test_option_for_openvas()

        p.kbdb.add_scan_preferences.assert_not_called()
Exemple #10
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._openvas_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._openvas_scan_id,
            [
                'reverse_lookup_only|||yes',
                'reverse_lookup_unify|||no',
            ],
        )
Exemple #11
0
    def test_set_host_options_no_finished(self, mock_kb):
        w = DummyDaemon()

        exc = '192.168.0.1'
        fin = []

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

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

        p.kbdb.add_scan_preferences.assert_called_with(
            p._openvas_scan_id,
            ['exclude_hosts|||192.168.0.1'],
        )
Exemple #12
0
    def test_set_alive_only(self, mock_kb):
        w = DummyDaemon()

        t_opt = {'alive_test': 16}
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)

        ov_setting = {'test_alive_hosts_only': 'yes'}

        Openvas.get_settings = MagicMock(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_alive_test_option_for_openvas()

        p.kbdb.add_scan_preferences.assert_called_with(
            p._openvas_scan_id,
            ['ALIVE_TEST|||16'],
        )
Exemple #13
0
    def test_prepare_nvt_prefs(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"
        ]

        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
        p._nvts_params = {
            "1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping": "no"
        }
        p._openvas_scan_id = '456-789'
        p.kbdb.add_scan_preferences = MagicMock()
        p.prepare_nvt_preferences()

        p.kbdb.add_scan_preferences.assert_called_with(
            p._openvas_scan_id,
            alive_test_out,
        )
Exemple #14
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._openvas_scan_id = '456-789'
        p.kbdb.add_scan_preferences = MagicMock()
        r = p.prepare_credentials_for_openvas()

        self.assertFalse(r)
Exemple #15
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._openvas_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)
Exemple #16
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._openvas_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._openvas_scan_id, ['drop_privileges|||yes'])
Exemple #17
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)