Skip to main content
Be Bitwise

Practical Binary Modification

Binary Patching & Modification · Advanced Reverse Engineering track

5 min read

In this module

Learning by Doing

The best way to develop binary patching skills is through repetition on real targets. Crackme challenges, CTF reverse engineering problems, and purpose-built practice binaries give you safe, legal things to break. This lesson walks through common patching scenarios and the reasoning behind each one.

Crackme Walkthrough: The Serial Check

A classic crackme presents a dialogue asking for a serial number. The program checks your input and displays either "Correct!" or "Wrong!" The goal is to make it accept any input.

Step 1: Find the decision point. Open the binary in Ghidra. Search for the string "Correct" or "Wrong" in the Strings window. Double-click to navigate to where the string is referenced. You will find both strings referenced near a conditional branch.

nasm
call validate_serial ; returns 0 or 1 in eax
test eax, eax
jz .wrong ; if eax == 0, show "Wrong!"
; ... show "Correct!" ...
.wrong:
; ... show "Wrong!" ...

Step 2: Choose your patch. You have several options:

  • NOP the test and jz instructions, so execution always falls through to "Correct!"
  • Change jz (0x74) to jnz (0x75), inverting the condition
  • Patch validate_serial to always return 1: mov eax, 1; ret

Step 3: Apply and verify. The simplest patch is changing the jz to jnz - a single byte change. Apply it in Ghidra (Patch Instruction) or a hex editor, save the binary, and run it. Every wrong serial now shows "Correct!" (and, since the condition is inverted, the one real serial now shows "Wrong!"). If that matters, NOP the jump instead.

Understanding Before Patching

Resist the urge to immediately patch the first conditional jump you see. Take time to understand the validation logic. Sometimes there are multiple checks (length validation, checksum verification, character set validation) and you need to patch all of them. Other times, the program re-validates later. Map the complete validation flow before deciding where and how to patch.

Removing Anti-Debug Checks

Many programs include anti-debugging techniques to make analysis harder. Common checks include:

IsDebuggerPresent (Windows): reads the BeingDebugged flag from the Process Environment Block. Patch: NOP the call and ensure EAX is 0, or patch the conditional jump that acts on the result.

ptrace(PTRACE_TRACEME) (Linux): a process can only be traced once. If the program traces itself, a debugger cannot attach. Patch: NOP the ptrace call, or change the return value check.

Timing checks: the program measures elapsed time between points. Under a debugger, execution is slower, so the time exceeds a threshold. Patch: NOP the timing comparison, or change the threshold to a very large value.

nasm
; ptrace anti-debug check
mov edi, PTRACE_TRACEME ; 0
xor esi, esi
xor edx, edx
xor ecx, ecx
call ptrace ; returns 0 if not being debugged, -1 if already traced
test eax, eax
js .debugger_detected ; jump if negative (being debugged)
; Patch: NOP the js instruction (2 bytes) to always continue

Both patches from this lesson, as bytes. The serial patch changes one byte; the anti-debug patch replaces the 2-byte js with two 1-byte NOPs, so nothing after it moves. The jump displacements are illustrative.

Serial and ptrace patches

x86-64
Original
1; serial check
285 c0 test eax, eax
374 1a jz .wrong
4
5; ptrace check
685 c0 test eax, eax
778 0e js .debugger_detected
Patched
1; serial check
285 c0 test eax, eax
375 1a jnz .wrong
4
5; ptrace check
685 c0 test eax, eax
790 nop
890 nop

Hover over code lines or mapping badges to see correspondences

Modifying Program Behaviour

Beyond bypassing checks, you can modify how a program operates:

Changing hardcoded limits: a trial version that limits documents to 10 pages might have a cmp eax, 0x0A followed by a conditional jump. Changing 0x0A to 0xFF raises the limit to 255. Changing the comparison to cmp eax, eax (which always equals) and the jump to NOP removes the limit entirely.

Redirecting network connections: if a program connects to a hardcoded server address, you can find the string in .rodata and replace it with your own server's address (same length or shorter, null-padded).

Enabling hidden features: software sometimes includes disabled features behind compile-time flags. If the code is compiled in but gated by a check, patching the check enables the feature.

Ethical Considerations

Binary patching is a genuinely dual-use skill. The same technique that strips a licence check also adds accessibility features to software that ships without them, patches security holes in abandoned codebases, or enables interoperability with proprietary formats. The tool doesn't care about intent.

The ethical lines are straightforward:

Do: practice on crackmes and CTF challenges designed for learning, patch software you own for personal use where legally permitted, use patching in authorised security research, contribute patches upstream when possible.

Do not: distribute patched copies of copyrighted software, circumvent protections for piracy, patch software in violation of licence agreements, use patching to harm others.

Many jurisdictions have specific laws about circumvention of technological protection measures. In the US, the DMCA's Section 1201 prohibits circumvention, with exceptions for security research, interoperability, and accessibility. The EU's Software Directive allows decompilation and modification for interoperability. Always understand the legal framework in your jurisdiction.

Building Your Skills

Start with simple crackmes from sites dedicated to reverse engineering practice. Work through single-check serial validators first, then multi-stage challenges with anti-debugging, integrity checks, and obfuscation layered on top. Each one teaches you to recognise new patterns.

Once the basics click, build your own tooling. Write scripts that automate the patches you keep doing by hand. Build analysis pipelines that flag vulnerability patterns automatically. Contribute to the open-source binary analysis ecosystem. The patching itself matters less than the mental model it builds of how software behaves at the instruction level.