Ejemplo n.º 1
0
    def test_scan_with_vts_and_param(self, mock_create_process):
        daemon = DummyWrapper([])
        cmd = StartScan(daemon)

        # No error
        request = et.fromstring('<start_scan>'
                                '<targets>'
                                '<target>'
                                '<hosts>localhost</hosts>'
                                '<ports>80, 443</ports>'
                                '</target>'
                                '</targets>'
                                '<scanner_params />'
                                '<vt_selection>'
                                '<vt_single id="1234">'
                                '<vt_value id="ABC">200</vt_value>'
                                '</vt_single>'
                                '</vt_selection>'
                                '</start_scan>')
        response = et.fromstring(cmd.handle_xml(request))
        scan_id = response.findtext('id')

        self.assertEqual(
            daemon.get_scan_vts(scan_id),
            {
                '1234': {
                    'ABC': '200'
                },
                'vt_groups': []
            },
        )

        assert_called(mock_create_process)
Ejemplo n.º 2
0
    def test_scan_with_vts(self, mock_create_process):
        daemon = DummyWrapper([])
        cmd = StartScan(daemon)

        request = et.fromstring('<start_scan>'
                                '<targets>'
                                '<target>'
                                '<hosts>localhost</hosts>'
                                '<ports>80, 443</ports>'
                                '</target>'
                                '</targets>'
                                '<scanner_params />'
                                '<vt_selection>'
                                '<vt_single id="1.2.3.4" />'
                                '</vt_selection>'
                                '</start_scan>')

        # With one vt, without params
        response = et.fromstring(cmd.handle_xml(request))
        scan_id = response.findtext('id')

        self.assertEqual(daemon.get_scan_vts(scan_id), {
            '1.2.3.4': {},
            'vt_groups': []
        })
        self.assertNotEqual(daemon.get_scan_vts(scan_id), {'1.2.3.6': {}})

        assert_called(mock_create_process)
Ejemplo n.º 3
0
    def test_scan_with_vts_and_param_with_vt_group_filter(
            self, mock_create_process):
        daemon = DummyWrapper([])
        cmd = StartScan(daemon)

        # No error
        request = et.fromstring('<start_scan>'
                                '<targets>'
                                '<target>'
                                '<hosts>localhost</hosts>'
                                '<ports>80, 443</ports>'
                                '</target>'
                                '</targets>'
                                '<scanner_params />'
                                '<vt_selection>'
                                '<vt_group filter="a"/>'
                                '</vt_selection>'
                                '</start_scan>')
        response = et.fromstring(cmd.handle_xml(request))
        daemon.start_queued_scans()
        scan_id = response.findtext('id')

        self.assertEqual(daemon.get_scan_vts(scan_id), {'vt_groups': ['a']})

        assert_called_once(mock_create_process)
Ejemplo n.º 4
0
    def test_scan_pop_ports(self):
        daemon = DummyWrapper([])
        cmd = StartScan(daemon)

        request = et.fromstring('<start_scan>'
                                '<targets>'
                                '<target>'
                                '<hosts>localhost</hosts>'
                                '<ports>80, 443</ports>'
                                '</target>'
                                '</targets>'
                                '<scanner_params />'
                                '<vt_selection>'
                                '<vt_single id="1.2.3.4" />'
                                '</vt_selection>'
                                '</start_scan>')

        # With one vt, without params
        response = et.fromstring(cmd.handle_xml(request))
        daemon.start_queued_scans()
        scan_id = response.findtext('id')

        ports = daemon.scan_collection.get_ports(scan_id)
        self.assertEqual(ports, '80, 443')
        self.assertRaises(KeyError, daemon.scan_collection.get_ports, scan_id)
Ejemplo n.º 5
0
    def test_is_new_scan_allowed_true(self):
        daemon = DummyWrapper([])
        cmd = StartScan(daemon)

        cmd._daemon.scan_processes = {  # pylint: disable=protected-access
            'a': 1,
            'b': 2,
        }
        daemon.max_scans = 3

        self.assertTrue(cmd.is_new_scan_allowed())
Ejemplo n.º 6
0
    def test_scan_with_vts_empty_vt_list(self):
        daemon = DummyWrapper([])
        cmd = StartScan(daemon)
        request = et.fromstring('<start_scan>'
                                '<targets>'
                                '<target>'
                                '<hosts>localhost</hosts>'
                                '<ports>80, 443</ports>'
                                '</target>'
                                '</targets>'
                                '<scanner_params /><vt_selection />'
                                '</start_scan>')

        with self.assertRaises(OspdCommandError):
            cmd.handle_xml(request)
Ejemplo n.º 7
0
    def test_scan_ignore_multi_target(self, mock_logger, mock_create_process):
        daemon = DummyWrapper([])
        cmd = StartScan(daemon)
        request = et.fromstring('<start_scan parallel="100a">'
                                '<targets>'
                                '<target>'
                                '<hosts>localhosts</hosts>'
                                '<ports>22</ports>'
                                '</target>'
                                '</targets>'
                                '<scanner_params />'
                                '</start_scan>')

        cmd.handle_xml(request)

        assert_called(mock_logger.warning)
        assert_called(mock_create_process)
Ejemplo n.º 8
0
    def test_scan_use_legacy_target_and_port(self, mock_logger,
                                             mock_create_process):
        daemon = DummyWrapper([])
        cmd = StartScan(daemon)
        request = et.fromstring('<start_scan target="localhost" ports="22">'
                                '<scanner_params />'
                                '</start_scan>')

        response = et.fromstring(cmd.handle_xml(request))
        scan_id = response.findtext('id')

        self.assertIsNotNone(scan_id)

        self.assertEqual(daemon.get_scan_host(scan_id), 'localhost')
        self.assertEqual(daemon.get_scan_ports(scan_id), '22')

        assert_called(mock_logger.warning)
        assert_called(mock_create_process)
Ejemplo n.º 9
0
    def test_scan_with_vts_and_param_missing_vt_group_filter(self):
        daemon = DummyWrapper([])
        cmd = StartScan(daemon)

        # Raise because no vtgroup filter attribute
        request = et.fromstring('<start_scan>'
                                '<targets>'
                                '<target>'
                                '<hosts>localhost</hosts>'
                                '<ports>80, 443</ports>'
                                '</target>'
                                '</targets>'
                                '<scanner_params />'
                                '<vt_selection><vt_group/></vt_selection>'
                                '</start_scan>')

        with self.assertRaises(OspdError):
            cmd.handle_xml(request)
Ejemplo n.º 10
0
    def test_scan_without_vts(self, mock_create_process):
        daemon = DummyWrapper([])
        cmd = StartScan(daemon)

        # With out vts
        request = et.fromstring('<start_scan>'
                                '<targets>'
                                '<target>'
                                '<hosts>localhost</hosts>'
                                '<ports>80, 443</ports>'
                                '</target>'
                                '</targets>'
                                '<scanner_params />'
                                '</start_scan>')
        response = et.fromstring(cmd.handle_xml(request))

        scan_id = response.findtext('id')

        self.assertEqual(daemon.get_scan_vts(scan_id), {})

        assert_called(mock_create_process)
Ejemplo n.º 11
0
    def test_max_queued_scans_reached(self):
        daemon = DummyWrapper([])
        daemon.max_queued_scans = 1
        cmd = StartScan(daemon)
        request = et.fromstring('<start_scan parallel="100a">'
                                '<targets>'
                                '<target>'
                                '<hosts>localhosts</hosts>'
                                '<ports>22</ports>'
                                '</target>'
                                '</targets>'
                                '<scanner_params />'
                                '</start_scan>')

        # create first scan
        response = et.fromstring(cmd.handle_xml(request))
        scan_id_1 = response.findtext('id')

        with self.assertRaises(OspdCommandError):
            cmd.handle_xml(request)

        daemon.scan_collection.remove_file_pickled_scan_info(scan_id_1)