示例#1
0
def _handle_empty_project(proj_class, *args, **options):
    if not options['no_target']:
        raise CommandError('No target binary specified. Use the -m option to '
                           'create an empty project')

    if not options['image']:
        raise CommandError('An empty project requires a VM image. Use the -i '
                           'option to specify the image')

    if not options['name']:
        raise CommandError('An empty project requires a name. Use the -n '
                           'option to specify one')

    # If the project class wasn't explicitly overridden programmatically, get
    # one of the default project classes from the command line
    if not proj_class:
        project_types = PROJECT_TYPES.keys()
        if options['type'] not in project_types:
            raise CommandError('An empty project requires a type. Use the -t '
                               'option and specify one from %s' %
                               project_types)
        proj_class = PROJECT_TYPES[options['type']]

    target = Target.empty(proj_class)
    options['target'] = target

    return call_command(target.initialize_project(), *args, **options)
示例#2
0
    def test_empty_x86_project_config(self):
        """Test empty Linux x86 project creation."""
        target = Target.empty()
        project = monkey_patch_project(LinuxProject(), LINUX_IMAGE_DESC)

        options = {
            'image': 'debian-9.2.1-i386',
            'name': 'test',
        }

        config = project._configure(target, **options)

        # Assert that we have actually created a Linux project
        self.assertEqual(config['project_type'], 'linux')

        # Assert that the project has no target
        self.assertIsNone(config['target'].path)
        self.assertIsNone(config['target'].arch)
        self.assertFalse(config['target'].files)

        # Should be empty when no target is specified
        self.assertFalse(config['processes'])
        self.assertFalse(config['modules'])

        # An empty project with no target will have no arguments
        self.assertFalse(config['target'].args.get_resolved_args(''))
        self.assertFalse(config['target'].args.symbolic_files)
        self.assertFalse(config['sym_args'])

        # Disabled by default
        self.assertFalse(config['enable_pov_generation'])
        self.assertFalse(config['use_seeds'])
        self.assertFalse(config['use_recipes'])
        self.assertFalse(config['use_fault_injection'])
示例#3
0
    def test_empty_xpsp3pro_project_config(self):
        """Test empty Windows XP SP3 project creation."""
        target = Target.empty(WindowsProject)
        project = monkey_patch_project(target.initialize_project(),
                                       WINDOWS_XPSP3_X86_IMAGE_DESC)
        options = {
            'image': 'windows-xpsp3pro-i386',
            'name': 'test',
        }

        config = project._configure(target, **options)

        # Assert that we have actually created a Windows project
        self.assertEqual(config['project_type'], 'windows')

        # Assert that the project has no target
        self.assertIsNone(config['target_path'])
        self.assertIsNone(config['target_arch'])
        self.assertFalse(config['target_files'])

        # Should be empty when no target is specified
        self.assertFalse(config['processes'])
        self.assertFalse(config['modules'])

        # An empty project with no target will have no arguments
        self.assertFalse(config['target_args'])
        self.assertFalse(config['sym_args'])
        self.assertFalse(config['use_symb_input_file'])

        # Disabled by default
        self.assertFalse(config['enable_pov_generation'])
        self.assertFalse(config['use_seeds'])
        self.assertFalse(config['use_recipes'])
        self.assertFalse(config['use_fault_injection'])
示例#4
0
    def test_scanner_driver_7sp1ent_x64_project_config(self):
        """Test x64 driver project creation."""
        target = Target.from_file(SCANNER_INF_PATH)
        project = monkey_patch_project(target.initialize_project(),
                                       WINDOWS_7SP1_X64_IMAGE_DESC)

        config = project._configure(target)

        # Assert that we've actually created a Windows project
        self.assertEqual(config['project_type'], 'windows')

        # Assert that the target is the one given
        self.assertEqual(config['target_path'], SCANNER_INF_PATH)
        self.assertEqual(config['target_arch'], 'x86_64')
        self.assertItemsEqual(config['target_files'],
                              [SCANNER_INF_PATH, SCANNER_SYS_PATH,
                               SCANNER_USER_EXE_PATH])
        self.assertItemsEqual(config['modules'], [(SCANNER_SYS, True)])

        # Assert that the x86_64 Windows 7 image was selected
        self.assertDictEqual(config['image'], WINDOWS_7SP1_X64_IMAGE_DESC)

        # Enabled by default for drivers
        self.assertTrue(config['use_fault_injection'])

        # Disabled by default
        self.assertFalse(config['enable_pov_generation'])
        self.assertFalse(config['use_seeds'])
        self.assertFalse(config['use_recipes'])
        self.assertFalse(config['processes'])
示例#5
0
    def test_myputs_dll_7sp1ent_x64_project_config(self):
        """Test x64 DLL project creation."""
        target = Target.from_file(MYPUTS_DLL_PATH)
        project = monkey_patch_project(target.initialize_project(),
                                       WINDOWS_7SP1_X64_IMAGE_DESC)
        options = {
            'target_args': ['MyPuts'],
        }

        config = project._configure(target, **options)

        # Assert that we have actually created a Windows project
        self.assertEqual(config['project_type'], 'windows')

        # Assert that the target is the one given
        self.assertEqual(config['target_path'], MYPUTS_DLL_PATH)
        self.assertEqual(config['target_arch'], 'x86_64')
        self.assertItemsEqual(config['target_files'], [MYPUTS_DLL_PATH])
        self.assertListEqual(config['modules'], [(MYPUTS_DLL, False)])

        # Assert that the x86_64 Windows 7 image was selected
        self.assertDictEqual(config['image'], WINDOWS_7SP1_X64_IMAGE_DESC)

        # Assert that the DLL entry point is the one that we provided
        self.assertEqual(config['target_args'], ['MyPuts'])

        # Verify static analysis results
        self.assertItemsEqual(config['dll_exports'], [u'MyPuts'])

        # Disabled by default
        self.assertFalse(config['processes'])
        self.assertFalse(config['use_seeds'])
示例#6
0
    def test_cat_x86_symbolic_project_config(self):
        """
        Test Linux project creation given a x86 binary (``cat``) and a symbolic
        file argument.
        """
        target = Target.from_file(CAT_X86_PATH)
        project = monkey_patch_project(target.initialize_project(),
                                       LINUX_IMAGE_DESC)
        options = {
            'target_args': ['-T', '@@'],
        }

        config = project._configure(target, **options)

        self._assert_cat_x86_common(config)

        # Verify symbolic arguments
        self.assertListEqual(config['target_args'], ['-T', '@@'])
        self.assertFalse(config['sym_args'])
        self.assertTrue(config['use_symb_input_file'])

        # Disabled by default
        self.assertFalse(config['enable_pov_generation'])
        self.assertFalse(config['use_seeds'])
        self.assertFalse(config['use_recipes'])
        self.assertFalse(config['use_fault_injection'])
示例#7
0
    def test_empty_cgc_target(self):
        """Test empty CGC target."""
        target = Target.empty()

        self.assertFalse(target.path)
        self.assertFalse(target.arch)
        self.assertFalse(target.operating_system)
        self.assertFalse(target.aux_files)
        self.assertTrue(target.is_empty())
示例#8
0
    def test_empty_cgc_target(self):
        """Test empty CGC target."""
        target = Target.empty(CGCProject)

        self.assertFalse(target.path)
        self.assertFalse(target.arch)
        self.assertFalse(target.operating_system)
        self.assertFalse(target.aux_files)
        self.assertIsInstance(target.initialize_project(), CGCProject)
        self.assertTrue(target.is_empty())
示例#9
0
    def test_windows_x86_64_dll_target(self):
        """Test Windows x86_64 DLL target."""
        target_path = os.path.join(DATA_DIR, 'myputs.dll')
        target = Target.from_file(target_path)

        self.assertEqual(target.path, target_path)
        self.assertEqual(target.arch, 'x86_64')
        self.assertEqual(target.operating_system, 'windows')
        self.assertFalse(target.aux_files)
        self.assertIsInstance(target.initialize_project(), WindowsDLLProject)
        self.assertFalse(target.is_empty())
示例#10
0
    def test_linux_i386_target(self):
        """Test Linux i386 executable target."""
        target_path = os.path.join(DATA_DIR, 'cat')
        target = Target.from_file(target_path)

        self.assertEqual(target.path, target_path)
        self.assertEqual(target.arch, 'i386')
        self.assertEqual(target.operating_system, 'linux')
        self.assertFalse(target.aux_files)
        self.assertIsInstance(target.initialize_project(), LinuxProject)
        self.assertFalse(target.is_empty())
示例#11
0
    def test_cgc_target(self):
        """Test CGC executable target."""
        target_path = os.path.join(DATA_DIR, 'CADET_00001')
        target = Target.from_file(target_path)

        self.assertEqual(target.path, target_path)
        self.assertEqual(target.arch, 'i386')
        self.assertEqual(target.operating_system, 'decree')
        self.assertFalse(target.aux_files)
        self.assertIsInstance(target.initialize_project(), CGCProject)
        self.assertFalse(target.is_empty())
示例#12
0
    def _handle_test(self, test_root, test, test_config, img_templates):
        ts_dir = self.source_path('testsuite')

        if os.path.exists(os.path.join(test_root, 'Makefile')):
            _build_test(self._config, self.source_path('s2e'), test_root)

        blacklisted_images = set(test_config.get('blacklisted-images', []))
        target_images = set(test_config.get('target-images', []))

        for target_name in test_config['targets']:
            target_path = os.path.join(test_root, target_name)
            target = Target.from_file(target_path)
            project = target.initialize_project()
            images = project.get_usable_images(target, img_templates)

            for image_name in images:
                if image_name in blacklisted_images:
                    logger.warn(
                        '%s is blacklisted, skipping tests for that image',
                        image_name)
                    continue

                if target_images and image_name not in target_images:
                    logger.debug('%s is not in target-images, skipping',
                                 image_name)
                    continue

                options = {
                    'image':
                    image_name,
                    'name':
                    'testsuite_%s_%s_%s' %
                    (test, os.path.basename(target_name), image_name),
                    'target':
                    target,
                    'target_args':
                    test_config.get('target_arguments', []),
                    'force':
                    True
                }
                options.update(test_config.get('options', []))

                call_command(project, *[], **options)

                scripts = test_config.get('scripts', {})
                run_tests_template = scripts.get('run_tests', 'run-tests.tpl')
                self._generate_run_tests(ts_dir, test, run_tests_template,
                                         options)
                _call_post_project_gen_script(
                    test_root, test_config,
                    self.projects_path(options['name']))
示例#13
0
    def test_windows_x86_64_inf_target(self):
        """Test Windows x86_64 INF driver target."""
        target_path = os.path.join(DATA_DIR, 'scanner.inf')
        target = Target.from_file(target_path)

        self.assertEqual(target.path, target_path)
        self.assertEqual(target.arch, 'x86_64')
        self.assertEqual(target.operating_system, 'windows')
        self.assertItemsEqual(target.aux_files, [
            os.path.join(DATA_DIR, 'scanner.sys'),
            os.path.join(DATA_DIR, 'scanuser.exe')
        ])
        self.assertIsInstance(target.initialize_project(),
                              WindowsDriverProject)
        self.assertFalse(target.is_empty())
示例#14
0
def target_from_file(path, args=None, project_class=None):
    files = _translate_target_to_files(path)
    path_to_analyze = files[0]
    aux_files = files[1:]

    arch, op_sys, proj_class = _determine_arch_and_proj(path_to_analyze)
    if not arch:
        raise Exception(
            f'Could not determine architecture for {path_to_analyze}')

    # Overwrite the automatically-derived project class if one is provided
    if project_class:
        if not issubclass(project_class, AbstractProject):
            raise Exception(
                'Custom projects must be a subclass of AbstractProject')
        proj_class = project_class

    return Target(path, args, arch, op_sys, aux_files), proj_class
示例#15
0
    def test_cadet0001_project_config(self):
        """
        Test CGC project creation given a CGC binary and nothing else. No
        image, project name, etc. is provided.
        """
        target = Target.from_file(CADET_00001_PATH)
        project = monkey_patch_project(target.initialize_project(),
                                       CGC_IMAGE_DESC)

        config = project._configure(target)

        # Assert that we have actually created a CGC project
        self.assertEqual(config['project_type'], 'cgc')

        # Assert that the target is the one given (CADET_00001)
        self.assertEqual(config['target_path'], CADET_00001_PATH)
        self.assertEqual(config['target_arch'], 'i386')
        self.assertListEqual(config['target_files'], [CADET_00001_PATH])
        self.assertListEqual(config['processes'], [CADET_00001])
        self.assertListEqual(config['modules'], [(CADET_00001, False)])

        # Assert that the CGC image has been selected
        self.assertDictEqual(config['image'], CGC_IMAGE_DESC)

        # CGC binaries have no input files
        self.assertFalse(config['target_args'])
        self.assertFalse(config['sym_args'])
        self.assertFalse(config['use_symb_input_file'])
        self.assertFalse(config['warn_input_file'])
        self.assertFalse(config['warn_seeds'])

        # CGC projects should always have POV generation, seeds and recipes
        # enabled
        self.assertTrue(config['enable_pov_generation'])
        self.assertTrue(config['use_seeds'])
        self.assertTrue(config['use_recipes'])

        # CGC has its own test case generation system
        self.assertFalse(config['use_test_case_generator'])
示例#16
0
    def test_empty_project_config(self):
        """Test empty CGC project creation."""
        target = Target.empty(CGCProject)
        project = monkey_patch_project(target.initialize_project(),
                                       CGC_IMAGE_DESC)
        options = {
            'image': 'cgc_debian-9.2.1-i386',
            'name': 'test',
        }

        config = project._configure(target, **options)

        # Assert that we have actually created a CGC project
        self.assertEqual(config['project_type'], 'cgc')

        # Assert that the projet has no target
        self.assertIsNone(config['target_path'])
        self.assertIsNone(config['target_arch'])
        self.assertFalse(config['target_files'])

        # Should be empty when no target is specified
        self.assertFalse(config['processes'])
        self.assertFalse(config['modules'])

        # CGC binaries have no input files
        self.assertFalse(config['target_args'])
        self.assertFalse(config['sym_args'])
        self.assertFalse(config['use_symb_input_file'])
        self.assertFalse(config['warn_input_file'])
        self.assertFalse(config['warn_seeds'])

        # CGC projects should always have POV generation, seeds and recipes
        # enabled
        self.assertTrue(config['enable_pov_generation'])
        self.assertTrue(config['use_seeds'])
        self.assertTrue(config['use_recipes'])

        # CGC has its own test case generation system
        self.assertFalse(config['use_test_case_generator'])
示例#17
0
    def test_cat_x86_concrete_project_config(self):
        """
        Test Linux project creation given a x86 binary (``cat``) and nothing
        else. No image, project name, symbolic arguments, etc. are provided.
        """
        target = Target.from_file(CAT_X86_PATH)
        project = monkey_patch_project(target.initialize_project(),
                                       LINUX_IMAGE_DESC)

        config = project._configure(target)

        self._assert_cat_x86_common(config)

        # No target arguments specified
        self.assertFalse(config['target_args'])
        self.assertFalse(config['sym_args'])
        self.assertFalse(config['use_symb_input_file'])

        # Disabled by default
        self.assertFalse(config['enable_pov_generation'])
        self.assertFalse(config['use_seeds'])
        self.assertFalse(config['use_recipes'])
        self.assertFalse(config['use_fault_injection'])
示例#18
0
def _handle_with_file(target_path, proj_class, *args, **options):
    target = Target.from_file(target_path, proj_class)
    options['target'] = target

    return call_command(target.initialize_project(), *args, **options)