Binary Patch¶
Binary patches are used to patch files or programs on a device. It performs find and replace operations using hexadecimal wildcards, supporting both high and low nibble wildcards. For example, ?? represents any byte, and B? represents any byte starting with B, such as BA, B1, or B9. A wildcard pattern like 49 BA ?? ?C will match any sequence in the file corresponding to 49 BA .. .C. The following call will replace all AA BB CC D[0-9A-F] byte sequences in test.bin with AA BB CC DD. The interface returns the number of replacements and their respective offsets.
d.hex_patch("AA BB CC D?", "AA BB CC DD", "/data/test.bin")
>>> result = d.hex_patch("AA BB CC D?", "AA BB CC DD", "/data/test.bin")
>>> print (result.count)
1
>>> print (result.replaces[0].offset)
8123
Note
To limit the maximum number of replacements, use the maxreplace parameter. By default, all occurrences are replaced.
d.hex_patch("AA BB CC D?", "AA BB CC DD", "/data/test.bin", maxreplace=2)
A dry run mode is also available. This will only find matching locations in the file without performing any replacement operations.
d.hex_patch("AA BB ?? ??", "AA BB 00 00", "/data/test.bin", dryrun=True)
Hint