Binary Patching Basics
Binary Patching & Modification · Advanced Reverse Engineering track
6 min read
In this module
Modifying Compiled Code
Binary patching is the craft of changing a compiled program when you don't have the source. You work straight in machine code, swapping instructions, rewriting data, redirecting control flow one byte at a time. Security researchers patch out anti-debug routines. Engineers fix bugs in third-party software they can't recompile. Anyone curious about how programs really work ends up here eventually.
NOPing Out Instructions
The crudest patch in the book: paint over instructions with NOP (No Operation, 0x90 on x86). The CPU sees a NOP, shrugs, and moves on. Bury a sequence of instructions under NOPs and you've erased the code without shifting anything else around — every other byte stays at its original address.
; Original: licence checkcall check_license ; 5 bytes (E8 xx xx xx xx)test eax, eax ; 2 bytes (85 C0)jz license_invalid ; 2 bytes (74 xx)
; Patched: NOPs replace the check and conditional jumpnop ; 0x90nop ; 0x90nop ; 0x90nop ; 0x90nop ; 0x90nop ; 0x90nop ; 0x90nop ; 0x90nop ; 0x90For longer regions, x86 has multi-byte NOPs that decode in one fetch instead of nine. 0x0F 0x1F 0x44 0x00 0x00 is a 5-byte NOP. Ghidra and most assemblers emit these on your behalf.
Changing Conditional Jumps
Sometimes you want to flip a decision rather than delete it. Conditional jumps come in pairs: JE / JNE, JL / JGE, JLE / JG. Flip the opcode and the branch goes the other way.
; Original: jump to error handler if check failstest eax, eaxjz error_handler ; 74 xx (JZ = jump if zero)
; Patched: jump to error handler if check succeeds (inverted)test eax, eaxjnz error_handler ; 75 xx (JNZ = jump if not zero)JZ and JNZ short forms (0x74, 0x75) differ by one bit. Same goes for JE/JNE, JL/JGE, JLE/JG. A jump-flip is the cleanest patch you'll ever write — one byte, in and out.
To force a branch to always be taken, replace the conditional jump with an unconditional JMP. A short JZ (0x74 rel8) is 2 bytes; a short JMP (0xEB rel8) is also 2 bytes with the same relative offset encoding, so you just change 0x74 to 0xEB. For near jumps, a near JZ (0x0F 0x84 rel32) is 6 bytes but a near JMP (0xE9 rel32) is only 5 bytes. You must NOP the leftover byte.
Here are the three one-instruction patches side by side, bytes and disassembly together. Hover a highlighted line to pair it with its replacement. The addresses are illustrative; the encodings aren't. The 0c displacement survives the first two patches untouched, because it counts from the end of the 2-byte instruction either way.
Patching a jz three ways
x86-64Hover over code lines or mapping badges to see correspondences
Hex Editing
For small patches a hex editor will do nicely. You just need the file offset of the bytes. Disassemblers display virtual addresses, which aren't the same thing. The conversion depends on section layout:
file_offset = virtual_address - section_virtual_address + section_file_offsetGhidra, IDA, and Binary Ninja all happily display both virtual address and file offset. Editors like HxD on Windows, hex on the CLI, or the built-in hex view in your disassembler will jump straight to an offset and let you overwrite bytes.
Standard workflow: spot the target instruction in the disassembler, note its file offset and current bytes, work out the replacement, open the binary in a hex editor, jump to the offset, overwrite, save, test.
Using Ghidra's Patch Instruction Feature
Ghidra has a friendlier path. Right-click an instruction in the Listing view, pick "Patch Instruction," and type the new mnemonic — Ghidra handles the encoding. No looking up REX prefixes by hand.
When you're done, File > Export Program can write the patched file back out. Pick the Original File format: it rewrites the bytes you changed into a copy of the input, so the headers and sections stay as they were. The other export formats (raw binary, C/C++, XML and so on) don't produce a runnable executable, and patches that land in memory Ghidra created itself, rather than bytes loaded from the file, have nowhere to go on export.
For data patches — strings, constants, flag values — use the byte editor instead. Navigate to the data in the Listing view, flip to the hex pane, edit. Useful for hardcoded timeouts, server URLs, or feature flags.
Common Patching Patterns
A handful of patches show up over and over:
Return early: drop a ret (0xC3) at the function's first byte. Function exits immediately without running any of its body. If it has to return a value, use xor eax, eax; ret for zero or mov eax, 1; ret for one.
Always-true / always-false: for a boolean predicate, replace the body with mov eax, 1; ret for true, or xor eax, eax; ret for false.
String replacement: edit a string constant in .rodata. The replacement has to fit in the same space — same length or shorter, padded with nulls. Need a longer string? Park it somewhere else in the binary and update the pointer.
Constant modification: change hardcoded values. Find where the constant is used (typically cmp eax, <value> or mov eax, <value>) and rewrite the immediate operand in the encoding.
Verifying Patches
Verify every patch. Three checks: the binary still loads and runs (you haven't corrupted the format), the patch does what you wanted, and nothing else broke in the process. Disassemble the patched file and confirm the new instructions are right. Run it under a debugger to follow the flow. If the binary signs or checksums itself, your patch just nuked the signature — you'll have to deal with that on its own. The operating system checks signatures too: on Apple Silicon Macs every executable needs a valid signature, so re-sign a patched Mach-O (codesign --force --sign - ./program gives it an ad-hoc one), and a patched Windows binary loses its Authenticode signature, which matters for drivers and anything checked by SmartScreen or App Control.