Binary Patch¶
Binary patches are used to modify files or programs on a device. Using hexadecimal wildcards for search and replace, it supports high and low bit wildcards, such as ??
representing any byte, B?
representing any byte starting with B
, like BA
, B1
, B9
. For example, the wildcard 49
BA
??
?C
will match any segment in the file that matches 49
BA
..
.C
. The following call will replace all AA
BB
CC
D[0-9A-F]
bytes in test.bin with AA
BB
CC
DD
. The interface will return the number of replacements and the offsets of the replacements.
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
The demonstration only shows four-byte matching and replacement, but the interface supports matching and replacing any number of bytes, though at least two valid matching digits are required.
To limit the maximum number of replacements, use the maxreplace parameter. By default, all matches are replaced.
d.hex_patch("AA BB CC D?", "AA BB CC DD", "/data/test.bin", maxreplace=2)
There’s also a test mode that only finds matching positions in the file without performing the replacement operation.
d.hex_patch("AA BB ?? ??", "AA BB 00 00", "/data/test.bin", dryrun=True)
Hint
File paths support wildcard matching (glob). For example, /data/app/*/test.bin will find and match the test.bin file in any first-level directory under the /data/app directory.