Example #1
0
def index_file_command(file_path: str, index_as: str, family_name: Optional[str]):
    if not utilities.is_supported_file(file_path):
        click.echo('File is not PE, ELF, DEX or APK')
        return
    try:
        index = Index(index_as=sdk_consts.IndexType.from_str(index_as), file_path=file_path, family_name=family_name)
        index.send(wait=True)
        click.echo('Finish index: {} with status: {}'.format(index.index_id, index.status))
    except sdk_errors.IntezerError as e:
        click.echo('Index error: {}'.format(e))
Example #2
0
def index_directory_command(directory_path: str,
                            index_as: str,
                            family_name: Optional[str],
                            ignore_directory_count_limit: bool):
    indexes_results = []

    for root, dirs, files in os.walk(directory_path):
        files = [f for f in files if not is_hidden(os.path.join(root, f))]
        dirs[:] = [d for d in dirs if not is_hidden(os.path.join(root, d))]

        number_of_files = len(files)
        if not ignore_directory_count_limit:
            utilities.check_should_continue_for_large_dir(number_of_files, default_config.unusual_amount_in_dir)
        with click.progressbar(length=number_of_files,
                               label='Index files',
                               show_pos=True,
                               width=0) as progressbar:
            for file_name in files:
                file_path = os.path.join(root, file_name)

                if not utilities.is_supported_file(file_path):
                    click.echo('Could not open {} because it is not a supported file type'.format(file_name))
                    progressbar.update(1)
                    continue

                try:
                    index = Index(index_as=sdk_consts.IndexType.from_str(index_as),
                                  file_path=file_path,
                                  family_name=family_name)
                    index.send()
                    indexes_results.append({'file_name': file_name, 'index': index})
                except sdk_errors.IntezerError:
                    click.echo('error occurred during indexing of {}'.format(file_name))
                    progressbar.update(1)

            for index_result in indexes_results:
                try:
                    index_result['index'].wait_for_completion()
                    click.echo('Index: {} , File: {} , finished with status: {}'.format(index_result['index'].index_id,
                                                                                        index_result['file_name'],
                                                                                        index_result['index'].status))
                    progressbar.update(1)
                except Exception:
                    click.echo('error occurred during indexing of {}'.format(index_result['file_name']))
                    progressbar.update(1)
Example #3
0
def analyze_file_command(file_path, no_unpacking, no_static_unpacking):
    if not utilities.is_supported_file(file_path):
        click.echo('File is not PE, ELF, DEX or APK')
        return

    try:
        analysis = Analysis(file_path=file_path,
                            dynamic_unpacking=no_unpacking,
                            static_unpacking=no_static_unpacking)
        analysis.send()
        if default_config.is_cloud:
            click.echo(
                'Analysis created. In order to check its result, go to: {}/{}'.format(default_config.analyses_url,
                                                                                      analysis.analysis_id))
        else:
            click.echo('Analysis created. In order to check its result go to Intezer analyze history page')
    except sdk_errors.IntezerError as e:
        click.echo('Analyze error: {}'.format(e))
Example #4
0
def analyze_directory_command(path, no_unpacking, no_static_unpacking):
    success_number = 0
    failed_number = 0
    unsupported_number = 0

    for root, dirs, files in os.walk(path):
        number_of_files = len(files)
        utilities.check_should_continue_for_large_dir(number_of_files, default_config.unusual_amount_in_dir)
        with click.progressbar(length=number_of_files,
                               label='Sending files for analysis',
                               show_pos=True) as progressbar:
            for file_name in files:
                file_path = os.path.join(root, file_name)
                if utilities.is_supported_file(file_path):
                    try:
                        Analysis(file_path=file_path,
                                 dynamic_unpacking=no_unpacking,
                                 static_unpacking=no_static_unpacking).send()
                        success_number += 1
                    except sdk_errors.InsufficientQuota:
                        raise sdk_errors.InsufficientQuota
                    except sdk_errors.IntezerError:
                        failed_number += 1
                else:
                    unsupported_number += 1
                progressbar.update(1)

    if success_number != 0:
        if default_config.is_cloud:
            click.echo('{} analysis created. In order to check their results, go to: {}'.format(success_number,
                                                                                                default_config.analyses_url))
        else:
            click.echo('{} analysis created. In order to check their results '
                       'go to Intezer analyze history page'.format(success_number))

    if failed_number != 0:
        click.echo('{} analysis failed'.format(failed_number))

    if unsupported_number != 0:
        click.echo('{} unsupported files'.format(unsupported_number))
Example #5
0
def analyze_directory_command(path: str,
                              disable_dynamic_unpacking: bool,
                              disable_static_unpacking: bool,
                              code_item_type: str,
                              ignore_directory_count_limit: bool):
    success_number = 0
    failed_number = 0
    unsupported_number = 0

    for root, dirs, files in os.walk(path):
        files = [f for f in files if not is_hidden(os.path.join(root, f))]
        dirs[:] = [d for d in dirs if not is_hidden(os.path.join(root, d))]

        number_of_files = len(files)
        if not ignore_directory_count_limit:
            utilities.check_should_continue_for_large_dir(number_of_files, default_config.unusual_amount_in_dir)
        if not files:
            continue

        with click.progressbar(length=number_of_files,
                               label='Sending files for analysis',
                               show_pos=True) as progressbar:
            for file_name in files:
                file_path = os.path.join(root, file_name)
                if disable_dynamic_unpacking and not utilities.is_supported_file(file_path):
                    unsupported_number += 1
                else:
                    try:
                        Analysis(file_path=file_path,
                                 code_item_type=code_item_type,
                                 disable_dynamic_unpacking=disable_dynamic_unpacking,
                                 disable_static_unpacking=disable_static_unpacking).send()
                        success_number += 1
                    except sdk_errors.IntezerError as ex:
                        # We cannot continue analyzing the directory if the account is out of quota
                        if isinstance(ex, sdk_errors.InsufficientQuota):
                            logger.error('Failed to analyze %s', file_path)
                            raise

                        logger.exception('Error while analyzing directory')
                        failed_number += 1
                    except Exception:
                        logger.exception('Failed to analyze %s', file_path)
                        failed_number += 1

                progressbar.update(1)

    if success_number != 0:
        if default_config.is_cloud:
            click.echo('{} analysis created. In order to check their results, go to: {}'.format(
                success_number,
                default_config.analyses_url)
            )
        else:
            click.echo('{} analysis created. In order to check their results '
                       'go to Intezer Analyze history page'.format(success_number))

    if failed_number != 0:
        click.echo('{} analysis failed'.format(failed_number))

    if unsupported_number != 0:
        click.echo('{} unsupported files'.format(unsupported_number))