Skip to content

strings_analyzer.py

ofrak_components.strings_analyzer

StringsAnalyzer (Analyzer)

analyze(self, resource, config) async

Analyze a resource for to extract specific ResourceAttributes.

Users should not call this method directly; rather, they should run Resource.run or Resource.analyze.

Parameters:

Name Type Description Default
resource Resource

The resource that is being analyzed

required
config StringsAnalyzerConfig

Optional config for analyzing. If an implementation provides a default, this default will always be used when config would otherwise be None. Note that a copy of the default config will be passed, so the default config values cannot be modified persistently by a component run.

required

Returns:

Type Description
StringsAttributes

The analysis results

Source code in ofrak_components/strings_analyzer.py
async def analyze(self, resource: Resource, config: StringsAnalyzerConfig) -> StringsAttributes:
    if config is None:
        config = StringsAnalyzerConfig()

    strings = dict()
    with tempfile.NamedTemporaryFile() as temp_file:
        temp_file.write(await resource.get_data())
        temp_file.flush()

        proc = await asyncio.subprocess.create_subprocess_exec(
            "strings",
            "-t",
            "d",
            f"-{config.min_length}",
            temp_file.name,
            stdout=asyncio.subprocess.PIPE,
        )

        line = await proc.stdout.readline()  # type: ignore
        while line:
            line = line.decode("ascii").strip()
            try:
                offset, string = line.split(" ", maxsplit=1)
            except ValueError as e:
                # String consisted entirely of whitespace
                line = await proc.stdout.readline()  # type: ignore
                continue
            strings[int(offset)] = string
            line = await proc.stdout.readline()  # type: ignore
        await proc.wait()

    return StringsAttributes(strings)

StringsAnalyzerConfig (ComponentConfig) dataclass

StringsAnalyzerConfig(min_length: int = 8)

StringsAttributes (ResourceAttributes) dataclass

StringsAttributes(strings: Dict[int, str])