Exemplo n.º 1
0
    def test_execute__no_results(self, mock_upload, mock_scan):
        mock_scan.return_value = []

        scanner = NmapperService()

        # mocking out _get_target_ips
        scanner.api = MagicMock()
        mock_response = MagicMock()
        mock_response.json.return_value = {
            'meta': {
                'total_count': 1
            },
            'objects': [{
                'id': 1,
                'is_all': True,
                'is_enabled': True,
                'scan_targets': ['192.0.2.1', '192.0.2.2']
            }]
        }
        scanner.api.get_data.return_value = mock_response

        scanner.execute()

        mock_scan.assert_any_call(['192.0.2.1'], None)
        mock_scan.assert_any_call(['192.0.2.2'], None)
        self.assertEquals(mock_upload.call_count, 0)
Exemplo n.º 2
0
    def test_execute__no_results(self, mock_upload, mock_scan):
        mock_scan.return_value = []

        scanner = NmapperService()

        # mocking out _get_target_ips
        scanner.api = MagicMock()
        mock_response = MagicMock()
        mock_response.json.return_value = {
            'meta': {'total_count': 1},
            'objects': [
                {
                    'id': 1,
                    'is_all': True,
                    'is_enabled': True,
                    'scan_targets': ['192.0.2.1', '192.0.2.2']
                }
            ]
        }
        scanner.api.get_data.return_value = mock_response

        scanner.execute()

        mock_scan.assert_any_call(['192.0.2.1'], None)
        mock_scan.assert_any_call(['192.0.2.2'], None)
        self.assertEquals(mock_upload.call_count, 0)
Exemplo n.º 3
0
    def test__upload_results(self):
        scanner = NmapperService()
        scanner.api = MagicMock()
        scanner.api.send_file.return_value = 's3://path'
        now = datetime(2015, 11, 4)

        scanner._upload_results('myfile', now)

        scanner.api.send_file.assert_called_once_with(
            'logs', 'myfile', now, suffix='nmap')
        scanner.api.send_signal.assert_called_once_with(
            'logs',
            {
                'path': 's3://path',
                'log_type': 'observations',
            },
        )
Exemplo n.º 4
0
    def test__upload_results(self):
        scanner = NmapperService()
        scanner.api = MagicMock()
        scanner.api.send_file.return_value = 's3://path'
        now = datetime(2015, 11, 4)

        scanner._upload_results('myfile', now)

        scanner.api.send_file.assert_called_once_with('logs',
                                                      'myfile',
                                                      now,
                                                      suffix='nmap')
        scanner.api.send_signal.assert_called_once_with(
            'logs',
            {
                'path': 's3://path',
                'log_type': 'observations',
            },
        )
Exemplo n.º 5
0
    def test_execute(self, mock_upload, mock_scan):
        mock_scan.return_value = [{'some': 'json'}, {'more': 'json'}]
        actual_results = []

        def _dummy_upload(self, filename, now):
            with open(filename) as f:
                actual_results.append(f.read())

        mock_upload.side_effect = _dummy_upload

        scanner = NmapperService()

        # mocking out _get_target_ips
        scanner.api = MagicMock()
        mock_response = MagicMock()
        mock_response.json.return_value = {
            'meta': {
                'total_count': 1
            },
            'objects': [{
                'id': 1,
                'is_all': True,
                'is_enabled': True,
                'scan_targets': ['192.0.2.1', '192.0.2.2']
            }]
        }
        scanner.api.get_data.return_value = mock_response

        now = datetime(2015, 11, 4)
        scanner.execute(now)

        utcnow = now.replace(tzinfo=utc)

        mock_scan.assert_any_call(['192.0.2.1'], utcnow)
        mock_scan.assert_any_call(['192.0.2.2'], utcnow)
        self.assertEquals(mock_upload.call_count, 2)

        expected_contents = ''.join(
            [json.dumps(s) + '\n' for s in mock_scan.return_value])
        self.assertEquals(len(actual_results), 2)
        self.assertEquals(actual_results[0], expected_contents)
        self.assertEquals(actual_results[1], expected_contents)
Exemplo n.º 6
0
    def test_execute(self, mock_upload, mock_scan):
        mock_scan.return_value = [{'some': 'json'}, {'more': 'json'}]
        actual_results = []

        def _dummy_upload(self, filename, now):
            with open(filename) as f:
                actual_results.append(f.read())
        mock_upload.side_effect = _dummy_upload

        scanner = NmapperService()

        # mocking out _get_target_ips
        scanner.api = MagicMock()
        mock_response = MagicMock()
        mock_response.json.return_value = {
            'meta': {'total_count': 1},
            'objects': [
                {
                    'id': 1,
                    'is_all': True,
                    'is_enabled': True,
                    'scan_targets': ['192.0.2.1', '192.0.2.2']
                }
            ]
        }
        scanner.api.get_data.return_value = mock_response

        now = datetime(2015, 11, 4)
        scanner.execute(now)

        utcnow = now.replace(tzinfo=utc)

        mock_scan.assert_any_call(['192.0.2.1'], utcnow)
        mock_scan.assert_any_call(['192.0.2.2'], utcnow)
        self.assertEquals(mock_upload.call_count, 2)

        expected_contents = ''.join([json.dumps(s) + '\n'
                                     for s in mock_scan.return_value])
        self.assertEquals(len(actual_results), 2)
        self.assertEquals(actual_results[0], expected_contents)
        self.assertEquals(actual_results[1], expected_contents)
Exemplo n.º 7
0
 def setUp(self):
     self.inst = NmapperService()
     self.inst.api = MagicMock()
Exemplo n.º 8
0
class NmapperTest(TestCase):
    def setUp(self):
        self.inst = NmapperService()
        self.inst.api = MagicMock()

    @patch('ona_service.nmapper.NmapProcess', autospec=True)
    def test__run_scan(self, mock_nmap_factory):
        mock_nmap = MagicMock()
        mock_nmap_factory.return_value = mock_nmap

        mock_nmap.run.return_value = 0
        mock_nmap.stdout = MOCK_SCAN_RESULT

        now = datetime(2015, 11, 4)
        res = _run_scan(['1.1.1.1', '2.2.2.2'], now)
        self.assertEqual(res, [
            {
                'time': now.isoformat(),
                'source': '192.168.1.42',
                'ports': '',
                'info_type': 'services',
                'result': '',
            },
            {
                'time': now.isoformat(),
                'source': '192.168.1.1',
                'ports': '22/closed, 80/open, 443/open',
                'info_type': 'services',
                'result': '',
            },
        ])

    @patch('ona_service.nmapper.NmapProcess', autospec=True)
    def test__run_scan__bad_return(self, mock_nmap_factory):
        mock_nmap = MagicMock()
        mock_nmap_factory.return_value = mock_nmap

        mock_nmap.run.return_value = 12
        mock_nmap.stdout = 'foo'
        now = datetime(2015, 11, 4)

        self.assertIsNone(_run_scan(['1.1.1.1', '2.2.2.2'], now))

    @patch('ona_service.nmapper.NmapProcess', autospec=True)
    def test__run_scan__bad_parse(self, mock_nmap_factory):
        mock_nmap = MagicMock()
        mock_nmap_factory.return_value = mock_nmap

        mock_nmap.run.return_value = 0
        mock_nmap.stdout = 'foo'
        now = datetime(2015, 11, 4)

        self.assertIsNone(_run_scan(['1.1.1.1', '2.2.2.2'], now))

    @patch('ona_service.nmapper.MAX_SIMULTANEOUS_TARGETS', 1)
    @patch('ona_service.nmapper._run_scan', autospec=True)
    def test_execute(self, mock_scan):
        mock_scan.return_value = [{'some': 'json'}, {'more': 'json'}]
        actual_results = []

        def _dummy_upload(self, filename, now, suffix=None):
            with open(filename) as f:
                actual_results.append(f.read())

        self.inst.api.send_file.side_effect = _dummy_upload

        # mocking out _get_target_ips
        mock_response = MagicMock()
        mock_response.json.return_value = {
            'meta': {
                'total_count': 1
            },
            'objects': [{
                'id': 1,
                'is_all': True,
                'is_enabled': True,
                'scan_targets': ['192.0.2.1', '192.0.2.2']
            }]
        }
        self.inst.api.get_data.return_value = mock_response

        now = datetime(2015, 11, 4)
        self.inst.execute(now)

        utcnow = now.replace(tzinfo=utc)

        mock_scan.assert_any_call(['192.0.2.1'], utcnow)
        mock_scan.assert_any_call(['192.0.2.2'], utcnow)
        self.assertEqual(self.inst.api.send_file.call_count, 2)

        expected_contents = [
            dumps(s, sort_keys=True) + '\n' for s in mock_scan.return_value
        ]
        expected_contents = ''.join(expected_contents)
        self.assertEqual(len(actual_results), 2)
        self.assertEqual(actual_results[0], expected_contents)
        self.assertEqual(actual_results[1], expected_contents)

    @patch('ona_service.nmapper.MAX_SIMULTANEOUS_TARGETS', 1)
    @patch('ona_service.nmapper._run_scan', autospec=True)
    def test_execute__no_results(self, mock_scan):
        mock_scan.return_value = []

        # mocking out _get_target_ips
        mock_response = MagicMock()
        mock_response.json.return_value = {
            'meta': {
                'total_count': 1
            },
            'objects': [{
                'id': 1,
                'is_all': True,
                'is_enabled': True,
                'scan_targets': ['192.0.2.1', '192.0.2.2']
            }]
        }
        self.inst.api.get_data.return_value = mock_response

        self.inst.execute()

        mock_scan.assert_any_call(['192.0.2.1'], None)
        mock_scan.assert_any_call(['192.0.2.2'], None)
        self.assertEqual(self.inst.api.call_count, 0)