Skip to content

injector.py

ofrak.core.injector

BinaryInjectorModifier (Modifier)

Injects arbitrary data at a specified virtual memory address within a memory region, placing the data at the target address in the binary's memory space. The injection is address-targeted rather than offset-targeted. Use for address-specific injection when you know exact memory locations, patching memory-mapped resources, injecting at known virtual addresses from analysis, modifying specific memory locations, or implementing address-based modifications. Requires understanding of the binary's memory layout and virtual addressing.

modify(self, resource, config) async

Modify the given resource.

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

Parameters:

Name Type Description Default
resource Resource required
config BinaryInjectorModifierConfig

Optional config for modification. 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
Source code in ofrak/core/injector.py
async def modify(self, resource: Resource, config: BinaryInjectorModifierConfig):
    assert config is not None
    memory_region = await resource.view_as(MemoryRegion)
    for vm_address, patch in config.binary_patch_configs:
        offset = memory_region.get_offset_in_self(vm_address)
        region_resource_data_id = memory_region.resource.get_data_id()
        if region_resource_data_id is None:
            raise ValueError(
                "Cannot create DataPatch for a memory region resource with no " "data ID"
            )
        memory_region.resource.queue_patch(Range(offset, offset + len(patch)), patch)

BinaryInjectorModifierConfig (ComponentConfig) dataclass

BinaryInjectorModifierConfig(binary_patch_configs: List[Tuple[int, bytes]])