diff --git a/capa/helpers.py b/capa/helpers.py index 38f94b028..a9fd3f8dc 100644 --- a/capa/helpers.py +++ b/capa/helpers.py @@ -126,33 +126,28 @@ def new_print(*args, **kwargs): inspect.builtins.print = old_print # type: ignore -def log_unsupported_format_error(): - logger.error("-" * 80) - logger.error(" Input file does not appear to be a PE or ELF file.") - logger.error(" ") - logger.error( - " capa currently only supports analyzing PE and ELF files (or shellcode, when using --format sc32|sc64)." - ) - logger.error(" If you don't know the input file type, you can try using the `file` utility to guess it.") - logger.error("-" * 80) - - -def log_unsupported_os_error(): - logger.error("-" * 80) - logger.error(" Input file does not appear to target a supported OS.") - logger.error(" ") - logger.error( - " capa currently only supports analyzing executables for some operating systems (including Windows and Linux)." - ) - logger.error("-" * 80) - - -def log_unsupported_arch_error(): - logger.error("-" * 80) - logger.error(" Input file does not appear to target a supported architecture.") - logger.error(" ") - logger.error(" capa currently only supports analyzing x86 (32- and 64-bit).") - logger.error("-" * 80) +def raise_log_unsupported_error(e): + if e = UnsupportedFormatError: + logger.error("-" * 80) + logger.error(" Input file does not appear to be a PE or ELF file.") + logger.error(" ") + logger.error(" capa currently only supports analyzing PE and ELF files (or shellcode, when using --format sc32|sc64).") + logger.error(" If you don't know the input file type, you can try using the `file` utility to guess it.") + return E_INVALID_FILE_TYPE + + if e = UnsupportedArchError: + logger.error("-" * 80) + logger.error(" Input file does not appear to target a supported architecture.") + logger.error(" ") + logger.error(" capa currently only supports analyzing x86 (32- and 64-bit).") + return E_INVALID_FILE_ARCH + + if e = UnsupportedOSError: + logger.error("-" * 80) + logger.error(" Input file does not appear to target a supported OS.") + logger.error(" ") + logger.error(" capa currently only supports analyzing executables for some operating systems (including Windows and Linux).") + return E_INVALID_FILE_OS def log_unsupported_runtime_error(): diff --git a/capa/main.py b/capa/main.py index ae8421560..d94ddad19 100644 --- a/capa/main.py +++ b/capa/main.py @@ -57,10 +57,8 @@ get_format, get_file_taste, get_auto_format, - log_unsupported_os_error, redirecting_print_to_tqdm, - log_unsupported_arch_error, - log_unsupported_format_error, + raise_log_unsupported_error, ) from capa.exceptions import UnsupportedOSError, UnsupportedArchError, UnsupportedFormatError, UnsupportedRuntimeError from capa.features.common import ( @@ -602,6 +600,21 @@ def get_extractor( raise ValueError("unexpected backend: " + backend) +def get_extractor_log_raise_errors( + path: Path, + format_: str, + os_: str, + backend: str, + sigpaths: List[Path], + should_save_workspace=False, + disable_progress=False, +) -> FeatureExtractor: + try: + return get_extractor(path, format_, os_, backend, sigpaths, should_save_workspace, disable_progress) + except (UnsupportedFormatError, UnsupportedArchError, UnsupportedOSError) as e: + raise_log_unsupported_error(e) + + def get_file_extractors(sample: Path, format_: str) -> List[FeatureExtractor]: file_extractors: List[FeatureExtractor] = [] @@ -1148,9 +1161,8 @@ def main(argv: Optional[List[str]] = None): except PEFormatError as e: logger.error("Input file '%s' is not a valid PE file: %s", args.sample, str(e)) return E_CORRUPT_FILE - except UnsupportedFormatError: - log_unsupported_format_error() - return E_INVALID_FILE_TYPE + except UnsupportedFormatError as e: + raise_log_unsupported_error(e) try: if is_running_standalone() and args.is_default_rules: @@ -1257,25 +1269,15 @@ def main(argv: Optional[List[str]] = None): should_save_workspace = os.environ.get("CAPA_SAVE_WORKSPACE") not in ("0", "no", "NO", "n", None) - try: - extractor = get_extractor( - args.sample, - format_, - args.os, - args.backend, - sig_paths, - should_save_workspace, - disable_progress=args.quiet or args.debug, - ) - except UnsupportedFormatError: - log_unsupported_format_error() - return E_INVALID_FILE_TYPE - except UnsupportedArchError: - log_unsupported_arch_error() - return E_INVALID_FILE_ARCH - except UnsupportedOSError: - log_unsupported_os_error() - return E_INVALID_FILE_OS + extractor = get_extractor_log_raise_errors( + args.sample, + format_, + args.os, + args.backend, + sig_paths, + should_save_workspace, + disable_progress=args.quiet or args.debug, + ) meta = collect_metadata(argv, args.sample, args.format, args.os, args.rules, extractor)