Skip to content
TheMaster1127Public

About

binpatch is a powerful, dependency-free command-line utility designed to patch, find, and disassemble bytes directly inside binary files. It simplifies low-level binary editing by combining the best features of dd, xxd, and objdump into a single, scriptable interface.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

10 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

binpatch – Binary Patching Tool


Table of Contents

  1. Introduction
  2. Synopsis
  3. Options
  4. File Offsets vs. Virtual Addresses (-o vs -va)
  5. Cross-Architecture Disassembly & ARM Thumb Support
  6. Hex Input Flexibility
  7. Find Limits & Terminal Flooding
  8. Heuristic Find (-fh) – Advanced Search
  9. Entry Point & Main Resolution (-e, -m, -r)
  10. Exit Codes
  11. Dependencies
  12. Installation
  13. Examples
  14. License

Introduction

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.


Synopsis

binpatch <file> [OPTIONS]

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).


File Offsets vs. Virtual Addresses (-o vs -va)

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. binpatch parses ELF program headers and translates the VMA directly to its physical file offset.

Cross-Architecture Disassembly & ARM Thumb Support

Cross-Architecture Support

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.

ARM Thumb Handling & Stripped Binaries

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 --thumb flag for manual overrides.

Hex Input Flexibility

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.


Find Limits & Terminal Flooding

To prevent excessive output on common byte sequences, searches default to displaying the first 5 matches.

  • Use -a or --all to print all matches.
  • Use -s N to set a custom match limit.

Heuristic Find (-fh) – Advanced Search

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.


Entry Point & Main Resolution (-e, -m, -r)

  • -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)

Backup Format

When -b (--backup) is specified with -h, a copy of the target file is created prior to modification:

backup_YYYYMMDD_HHMMSS_originalfilename


Exit Codes

Code Meaning
0 Success.
1 Error (File not found, invalid offset, conflict, or match not found).

Dependencies

  • Python 3.6+
  • objdump / llvm-objdump (Optional, required only for -d and -m).

Installation

Run the installation script:

chmod +x install.sh
./install.sh

See INSTALL.md for distribution-specific package instructions and manual installation details.


Examples

1. NOP out a Jump Instruction (x86 or ARM)

binpatch c_binary -va 4011cd -h "90 90"

2. Disassemble a Foreign Architecture Binary (ARM on x86_64)

binpatch arm_binary -e -d -s 10

3. Disassemble an ARM Thumb Function Until Return

binpatch arm_binary -o 0x12a0 -d -r --thumb

4. Patch at Raw Offset with Backup

binpatch my_program -o 112B -h "cb 10 00 00 05" -b

5. Scripting Mode (-q)

OFFSET=$(binpatch my_program -f "cb 10 00 00 05" -q | head -1)
binpatch my_program -o $OFFSET -h "90 90 90 90 90" -b

License

This project is licensed under the GNU General Public License v3.0 (GPLv3).

About

binpatch is a powerful, dependency-free command-line utility designed to patch, find, and disassemble bytes directly inside binary files. It simplifies low-level binary editing by combining the best features of dd, xxd, and objdump into a single, scriptable interface.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages