Esempio n. 1
0
class WebInterfaceTest(unittest.TestCase):
    def setUp(self):
        self.mocked_interface = DatabaseMock()

        self.enter_patch = unittest.mock.patch(
            target='helperFunctions.web_interface.ConnectTo.__enter__',
            new=lambda _: self.mocked_interface)
        self.enter_patch.start()

        self.exit_patch = unittest.mock.patch(
            target='helperFunctions.web_interface.ConnectTo.__exit__',
            new=fake_exit)
        self.exit_patch.start()

        self.config = get_config_for_testing(TMP_DIR)
        self.frontend = WebFrontEnd(config=self.config)
        self.frontend.app.config['TESTING'] = True
        self.test_client = self.frontend.app.test_client()

    def tearDown(self):
        self.enter_patch.stop()
        self.exit_patch.stop()

        self.mocked_interface.shutdown()
        gc.collect()
Esempio n. 2
0
class AnalysisSchedulerTest(TestCase):

    def setUp(self):
        self.mocked_interface = DatabaseMock()
        self.enter_patch = mock.patch(target='helperFunctions.web_interface.ConnectTo.__enter__', new=lambda _: self.mocked_interface)
        self.enter_patch.start()
        self.exit_patch = mock.patch(target='helperFunctions.web_interface.ConnectTo.__exit__', new=fake_exit)
        self.exit_patch.start()

        config = get_config_for_testing()
        config.add_section('ip_and_uri_finder')
        config.set('ip_and_uri_finder', 'signature_directory', 'analysis/signatures/ip_and_uri_finder/')
        config.set('default_plugins', 'default', 'file_hashes')
        self.tmp_queue = Queue()
        self.sched = AnalysisScheduler(config=config, pre_analysis=lambda *_: None, post_analysis=self.dummy_callback, db_interface=self.mocked_interface)

    def tearDown(self):
        self.sched.shutdown()
        self.tmp_queue.close()

        self.enter_patch.stop()
        self.exit_patch.stop()
        self.mocked_interface.shutdown()
        gc.collect()

    def dummy_callback(self, fw):
        self.tmp_queue.put(fw)
Esempio n. 3
0
class AnalysisPluginTest(unittest.TestCase):
    '''
    This is the base class for analysis plugin test.unit
    '''

    PLUGIN_NAME = 'plugin_test'

    def setUp(self):
        self.mocked_interface = DatabaseMock()

        self.enter_patch = unittest.mock.patch(
            target='helperFunctions.database.ConnectTo.__enter__',
            new=lambda _: self.mocked_interface)
        self.enter_patch.start()

        self.exit_patch = unittest.mock.patch(
            target='helperFunctions.database.ConnectTo.__exit__',
            new=fake_exit)
        self.exit_patch.start()

    def tearDown(self):
        self.analysis_plugin.shutdown()  # pylint: disable=no-member

        self.enter_patch.stop()
        self.exit_patch.stop()

        self.mocked_interface.shutdown()
        gc.collect()

    def init_basic_config(self):
        config = ConfigParser()
        config.add_section(self.PLUGIN_NAME)
        config.set(self.PLUGIN_NAME, 'threads', '1')
        config.add_section('ExpertSettings')
        config.set('ExpertSettings', 'block_delay', '0.1')
        config.add_section('data_storage')
        load_users_from_main_config(config)
        config.set('data_storage', 'mongo_server', 'localhost')
        config.set('data_storage', 'mongo_port', '54321')
        config.set('data_storage', 'view_storage', 'tmp_view')
        return config

    def register_plugin(self, name, plugin_object):
        '''
        This is a mock checking if the plugin registers correctly
        '''
        self.assertEqual(name, self.PLUGIN_NAME,
                         'plugin registers with wrong name')
        self.assertEqual(plugin_object.NAME, self.PLUGIN_NAME,
                         'plugin object has wrong name')
        self.assertIsInstance(plugin_object.DESCRIPTION, str)
        self.assertIsInstance(plugin_object.VERSION, str)
        self.assertNotEqual(plugin_object.VERSION, 'not set',
                            'Plug-in version not set')
Esempio n. 4
0
    def setUp(self):
        self.mocked_interface = DatabaseMock()

        self.enter_patch = unittest.mock.patch(
            target='helperFunctions.database.ConnectTo.__enter__',
            new=lambda _: self.mocked_interface)
        self.enter_patch.start()

        self.exit_patch = unittest.mock.patch(
            target='helperFunctions.database.ConnectTo.__exit__',
            new=fake_exit)
        self.exit_patch.start()
    def setUp(self):
        self.mocked_interface = DatabaseMock()
        self.enter_patch = unittest.mock.patch(target='helperFunctions.web_interface.ConnectTo.__enter__', new=lambda _: self.mocked_interface)
        self.enter_patch.start()
        self.exit_patch = unittest.mock.patch(target='helperFunctions.web_interface.ConnectTo.__exit__', new=fake_exit)
        self.exit_patch.start()

        self._config = initialize_config(None)
        self._tmp_queue = Queue()

        self._analysis_scheduler = AnalysisScheduler(config=self._config, post_analysis=self._dummy_callback, db_interface=MockDbInterface(None))
        self._unpack_scheduler = UnpackingScheduler(config=self._config, post_unpack=self._analysis_scheduler.add_task)
Esempio n. 6
0
class TestFileAddition(unittest.TestCase):
    @patch('unpacker.unpack.FS_Organizer', MockFSOrganizer)
    def setUp(self):
        self.mocked_interface = DatabaseMock()
        self.enter_patch = unittest.mock.patch(
            target='helperFunctions.web_interface.ConnectTo.__enter__',
            new=lambda _: self.mocked_interface)
        self.enter_patch.start()
        self.exit_patch = unittest.mock.patch(
            target='helperFunctions.web_interface.ConnectTo.__exit__',
            new=fake_exit)
        self.exit_patch.start()

        self._config = initialize_config(None)
        self._tmp_queue = Queue()

        self._analysis_scheduler = AnalysisScheduler(
            config=self._config,
            pre_analysis=lambda *_: None,
            post_analysis=self._dummy_callback,
            db_interface=MockDbInterface(None))
        self._unpack_scheduler = UnpackingScheduler(
            config=self._config,
            post_unpack=self._analysis_scheduler.start_analysis_of_object,
            db_interface=self.mocked_interface)

    def tearDown(self):
        self._unpack_scheduler.shutdown()
        self._analysis_scheduler.shutdown()
        self._tmp_queue.close()

        self.enter_patch.stop()
        self.exit_patch.stop()
        self.mocked_interface.shutdown()
        gc.collect()

    def test_unpack_and_analyse(self):
        test_fw = Firmware(
            file_path='{}/container/test.zip'.format(get_test_data_dir()))

        self._unpack_scheduler.add_task(test_fw)

        for _ in range(
                4 * 2
        ):  # container with 3 included files times 2 mandatory plugins run
            processed_container = self._tmp_queue.get(timeout=10)

        self.assertGreaterEqual(len(processed_container.processed_analysis), 3,
                                'at least one analysis not done')

    def _dummy_callback(self, fw):
        self._tmp_queue.put(fw)
Esempio n. 7
0
    def setUp(self):
        self.mocked_interface = DatabaseMock()
        self.enter_patch = unittest.mock.patch(target='helperFunctions.web_interface.ConnectTo.__enter__', new=lambda _: self.mocked_interface)
        self.enter_patch.start()
        self.exit_patch = unittest.mock.patch(target='helperFunctions.web_interface.ConnectTo.__exit__', new=fake_exit)
        self.exit_patch.start()

        config = get_config_for_testing()
        config.add_section('ip_and_uri_finder')
        config.set('ip_and_uri_finder', 'signature_directory', 'analysis/signatures/ip_and_uri_finder/')
        config.add_section('default_plugins')
        config.set('default_plugins', 'plugins', 'file_hashes')
        self.tmp_queue = Queue()
        self.sched = AnalysisScheduler(config=config, post_analysis=self.dummy_callback, db_interface=DatabaseMock())
Esempio n. 8
0
 def setUp(self):
     self._config = initialize_config(tmp_dir=None)
     self._tmp_queue = Queue()
     self._unpack_scheduler = UnpackingScheduler(
         config=self._config,
         post_unpack=self._dummy_callback,
         db_interface=DatabaseMock())
Esempio n. 9
0
def mocking_the_database(monkeypatch):
    monkeypatch.setattr('helperFunctions.web_interface.ConnectTo.__enter__',
                        lambda _: DatabaseMock())
    monkeypatch.setattr('helperFunctions.web_interface.ConnectTo.__exit__',
                        fake_exit)
    monkeypatch.setattr(
        'intercom.common_mongo_binding.InterComListener.__init__',
        lambda self, config: None)
    monkeypatch.setattr('logging.info', set_output)
    monkeypatch.setattr('logging.debug', set_output)
Esempio n. 10
0
 def setUp(self):
     config = ConfigParser()
     self.ds_tmp_dir = TemporaryDirectory(prefix='faf_tests_')
     config.add_section('data_storage')
     config.set('data_storage', 'firmware_file_storage_directory', self.ds_tmp_dir.name)
     config.add_section('unpack')
     config.set('unpack', 'max_depth', '3')
     config.set('unpack', 'whitelist', 'text/plain, image/png')
     config.add_section('ExpertSettings')
     self.unpacker = Unpacker(config=config, db_interface=DatabaseMock())
     self.tmp_dir = TemporaryDirectory(prefix='faf_tests_')
     self.test_fo = create_test_file_object()
Esempio n. 11
0
def mocking_the_database(monkeypatch):
    monkeypatch.setattr('helperFunctions.database.ConnectTo.__enter__',
                        lambda _: DatabaseMock())
    monkeypatch.setattr('helperFunctions.database.ConnectTo.__exit__',
                        fake_exit)
Esempio n. 12
0
 def __init__(self):
     self.tag_queue = Queue()
     self.config = get_config_for_testing()
     self.db_backend_service = DatabaseMock(None)
Esempio n. 13
0
class TestScheduleInitialAnalysis(unittest.TestCase):
    def setUp(self):
        self.mocked_interface = DatabaseMock()
        self.enter_patch = unittest.mock.patch(
            target='helperFunctions.web_interface.ConnectTo.__enter__',
            new=lambda _: self.mocked_interface)
        self.enter_patch.start()
        self.exit_patch = unittest.mock.patch(
            target='helperFunctions.web_interface.ConnectTo.__exit__',
            new=fake_exit)
        self.exit_patch.start()

        config = get_config_for_testing()
        config.add_section('ip_and_uri_finder')
        config.set('ip_and_uri_finder', 'signature_directory',
                   'analysis/signatures/ip_and_uri_finder/')
        config.add_section('default_plugins')
        config.set('default_plugins', 'plugins', 'file_hashes')
        self.tmp_queue = Queue()
        self.sched = AnalysisScheduler(config=config,
                                       post_analysis=self.dummy_callback,
                                       db_interface=DatabaseMock())

    def tearDown(self):
        self.sched.shutdown()
        self.tmp_queue.close()

        self.enter_patch.stop()
        self.exit_patch.stop()
        self.mocked_interface.shutdown()
        gc.collect()

    def test_plugin_registration(self):
        self.assertIn('dummy_plugin_for_testing_only',
                      self.sched.analysis_plugins, 'Dummy plugin not found')

    def test_schedule_firmware_init_no_analysis_selected(self):
        self.sched.shutdown()
        self.sched.process_queue = Queue()
        test_fw = Firmware(binary=b'test')
        self.sched.add_task(test_fw)
        test_fw = self.sched.process_queue.get(timeout=5)
        self.assertEqual(len(test_fw.scheduled_analysis),
                         len(MANDATORY_PLUGINS),
                         'Mandatory Plugins not selected')
        for item in MANDATORY_PLUGINS:
            self.assertIn(item, test_fw.scheduled_analysis)

    def test_whole_run_analyis_selected(self):
        test_fw = Firmware(file_path=os.path.join(get_test_data_dir(),
                                                  'get_files_test/testfile1'))
        test_fw.scheduled_analysis = ['dummy_plugin_for_testing_only']
        self.sched.add_task(test_fw)
        test_fw = self.tmp_queue.get(timeout=10)
        self.assertEqual(len(test_fw.processed_analysis), 3,
                         'analysis not done')
        self.assertEqual(
            test_fw.processed_analysis['dummy_plugin_for_testing_only']['1'],
            'first result', 'result not correct')
        self.assertEqual(
            test_fw.processed_analysis['dummy_plugin_for_testing_only']
            ['summary'], ['first result', 'second result'])
        self.assertIn('file_hashes', test_fw.processed_analysis.keys(),
                      'Mandatory plug-in not executed')
        self.assertIn('file_type', test_fw.processed_analysis.keys(),
                      'Mandatory plug-in not executed')

    def test_get_plugin_dict(self):
        result = self.sched.get_plugin_dict()
        self.assertIn('file_hashes', result.keys(),
                      'file hashes plugin not found')
        self.assertTrue(result['file_hashes'][1], 'mandatory flag not set')
        self.assertTrue(result['file_hashes'][2], 'default flag not set')
        self.assertIn('file_type', result.keys(), 'file type plugin not found')
        self.assertFalse(result['file_type'][2],
                         'default flag set but should not')
        self.assertEqual(result['file_type'][0],
                         self.sched.analysis_plugins['file_type'].DESCRIPTION,
                         'description not correct')
        self.assertTrue(result['unpacker'][1],
                        'unpacker plugin not marked as mandatory')
        self.assertNotIn('dummy_plug_in_for_testing_only', result.keys(),
                         'dummy plug-in not removed')

    def dummy_callback(self, fw):
        self.tmp_queue.put(fw)
Esempio n. 14
0
 def _start_scheduler(self):
     self.scheduler = UnpackingScheduler(config=self.config, post_unpack=self._mock_callback, analysis_workload=self._mock_get_analysis_workload, db_interface=DatabaseMock())