- Introduction
- Synopsis
- Options
- File Offsets vs. Virtual Addresses (
-ovs-va) - Cross-Architecture Disassembly & ARM Thumb Support
- Hex Input Flexibility
- Find Limits & Terminal Flooding
- Heuristic Find (
-fh) – Advanced Search - Entry Point & Main Resolution (
-e,-m,-r) - Exit Codes
- Dependencies
- Installation
- Examples
- License
binpatch is a lightweight, dependency-free command-line utility designed to patch, find, and disassemble bytes directly inside binary files. It combines features of dd, xxd, and objdump into a single, scriptable interface.
Key capabilities include automatic timestamped backups, flexible hex string parsing, cross-architecture disassembly, an advanced heuristic search engine for surviving byte sequences, and Thumb-state handling for ARM binaries.
binpatch <file> [OPTIONS]| Option | Argument | Description |
|---|---|---|
-o, --offset |
OFFSET | Raw File Offset to patch or disassemble (e.g., 112b or 0x112b). |
-va, --vaddr |
VMA | Virtual Memory Address (from objdump/IDA/Ghidra). Auto-translates to File Offset. |
-e, --entry |
(none) | Automatically parse ELF headers to target the Entry Point (_start). Replaces -o. |
-m, --main |
(none) | Automatically target the main() function in compiled C/C++ binaries. Replaces -o. |
-h, --hex |
HEX_STRING | Hex bytes to write to the file (e.g., "cb 10 00 00 05"). |
-b, --backup |
(none) | Create a timestamped backup of the file before applying the patch. |
-f, --find |
HEX_STRING | Find an exact hex pattern in the file and print an xxd-style hex dump of occurrences. |
-fh, --find-heuristic |
HEX_STRING | Find the largest contiguous substring match (heuristic search). |
-d, --disassemble |
(none) | Disassemble instructions at the target offset. |
-s, --size |
N | Dual-purpose: Number of instructions to disassemble (default: 1), OR max find results (default: 5). |
-r, --return |
(none) | Disassemble until a return instruction is reached (ret, bx lr, pop {pc}, etc.). Cannot be used with -s. |
--thumb |
(none) | Force ARM Thumb mode disassembly on ARM binaries. |
-a, --all |
(none) | Show ALL find results, overriding the -s size limit. |
-q, --quiet |
(none) | Scripting mode. Suppresses formatting and prints only raw hex offsets (0x...). |
--version |
(none) | Show version information. |
--help |
(none) | Show the help message and exit. |
(Note: -h maps to --hex, not help. Use --help for documentation).
Reverse-engineering tools output Virtual Memory Addresses (VMAs) (e.g., 0x4011e0), whereas files on disk are referenced via File Offsets (e.g., 0x11e0).
- When working with an exact file offset, use
-o. - When copying an address from disassembly or debuggers, use
-va.binpatchparses ELF program headers and translates the VMA directly to its physical file offset.
Standard host objdump typically rejects binaries built for foreign architectures (e.g., inspecting an ARM or AArch64 binary on an x86_64 machine). binpatch resolves this by dynamically querying for multi-target tools such as llvm-objdump or cross-binutils toolchains (e.g., aarch64-linux-gnu-objdump, arm-linux-gnueabi-objdump). Installing llvm on your system enables universal cross-disassembly out of the box.
Under the 32-bit ARM ELF specification, the LSB (bit 0) of function pointers and the ELF e_entry address is set to 1 to signal Thumb execution state. binpatch handles this by:
- Masking bit 0 so file offsets and virtual addresses remain aligned to instruction boundaries.
- Automatically forcing Thumb decoding (
-M force-thumb) when disassembling stripped binaries without mapping symbols. - Providing the
--thumbflag for manual overrides.
Hex inputs passed to -h, -f, and -fh are normalized automatically. Whitespace, commas, and 0x prefixes are stripped:
"cb 10 00 00 05""cb10000005""0xcb 0x10 0x00 0x00 0x05""CB 10 00 00 05""cb, 10, 00, 00, 05"
All translate to the same byte array.
To prevent excessive output on common byte sequences, searches default to displaying the first 5 matches.
- Use
-aor--allto print all matches. - Use
-s Nto set a custom match limit.
When patching binaries across minor revisions or recompilations, exact byte sequences often break. -fh generates contiguous substrings of your hex pattern and identifies the largest surviving byte chunks.
-e(Entry Point): Parses ELF program headers natively to find and target_start.-m(Main): Resolves the<main>symbol from the symbol table. (Fails gracefully on stripped binaries).-r(Until Return): Disassembles instructions until a function return instruction is encountered:ret,repz ret(x86/x86-64/AArch64/RISC-V)bx lr,mov pc, lr,pop {..., pc},ldm... {..., pc}(ARM/Thumb)jr ra(MIPS)
When -b (--backup) is specified with -h, a copy of the target file is created prior to modification:
backup_YYYYMMDD_HHMMSS_originalfilename
| Code | Meaning |
|---|---|
0 |
Success. |
1 |
Error (File not found, invalid offset, conflict, or match not found). |
- Python 3.6+
objdump/llvm-objdump(Optional, required only for-dand-m).
Run the installation script:
chmod +x install.sh
./install.shSee INSTALL.md for distribution-specific package instructions and manual installation details.
binpatch c_binary -va 4011cd -h "90 90"binpatch arm_binary -e -d -s 10binpatch arm_binary -o 0x12a0 -d -r --thumbbinpatch my_program -o 112B -h "cb 10 00 00 05" -bOFFSET=$(binpatch my_program -f "cb 10 00 00 05" -q | head -1)
binpatch my_program -o $OFFSET -h "90 90 90 90 90" -bThis project is licensed under the GNU General Public License v3.0 (GPLv3).