Binary patching changes what a compiled program does by editing its machine code: flipping a conditional jump so a check always passes, replacing instructions with NOPs to remove a call, or rewriting a comparison. In defensive work it is how you neutralize a malware sample's anti-analysis check in your own lab so the sandbox can observe it, how you test a patch's effect between two builds, and how you verify a license validation routine during authorized software assessment. This skill covers finding the right bytes, making minimal patches, and verifying the result. Scope: patch only binaries you are authorized to modify, in an isolated lab, for authorized analysis, testing, or research. Never use patching to circumvent licensing, DRM, or protections on software you are not authorized to alter.
Quick Workflow
- Define the goal precisely: which check to neutralize or which behavior to change, and why the analysis needs it.
- Locate the target in disassembly: find the comparison or call behind the behavior (string references and cross-references are fastest).
- Understand the branch: which jump implements the decision, and what the fall-through vs taken paths do.
- Choose the minimal patch: flip one jump, NOP one call, or patch one comparison.
- Apply the patch in the debugger first (non-destructive), test the behavior change.
- If the change is correct, write it to a patched copy of the file (never overwrite the original sample).
- Verify: hash both files, diff behavior, and document exactly which bytes changed and why.
1. Finding the target: from behavior to bytes
Start from something observable: a string, an API call, or a dialog.
x64dbg workflow:
1. Search strings for the check's message ("Trial expired", "Debugger detected")
2. Follow the cross-reference to the code that uses it
3. Read upward: the comparison and conditional jump above the message
is your target
Example disassembly shape:
00401230 cmp eax, 1 ; is the license valid? (1 = yes)
00401233 jne 00401250 ; no -> show "Trial expired"
00401235 ...licensed path...
In Ghidra, use the decompiler view to confirm your reading: the branch
you plan to flip should correspond to a clean if (!licensed) in the
pseudocode. If the decompiler shows something convoluted, you may be
looking at the wrong branch; re-verify with string references before
patching.
2. NOP and jump patching
Two minimal primitives cover most lab needs:
Flip the jump. A conditional jump becomes its opposite or an unconditional one:
Before: 74 1B je 00401250 ; jump if equal (licensed path skipped)
After: EB 1B jmp 00401250 ; always take the licensed path
Common flips: 74 (je) to 75 (jne), 74 to EB (jmp), 75 to EB.
One byte changes, everything else stays aligned.
NOP the call. Replace a call with NOPs to remove its effect:
Before: E8 3A 10 00 00 call anti_debug_check
After: 90 90 90 90 90 nop x5 (one NOP per byte of the call)
In x64dbg: select the instruction, press Space, type nop, and the
assembler fills the right count. Then File, Patch file, save as a copy.
Always patch a copy: keep the original hash for your records and for
reproducibility.
3. Patching comparisons and return values
Sometimes the cleanest patch is at the source: the function that returns the verdict.
Target: check_license() returns 0 (invalid) or 1 (valid)
Patch: at function start, insert:
B8 01 00 00 00 mov eax, 1
C3 ret
(overwrites the first 6 bytes; pad the rest with NOPs if the
original prologue was longer)
This is more robust than flipping every caller: one patch covers all call sites. Verify the function has no other side effects you need (some checks also initialize state; NOPing them can crash the program). When in doubt, flip the caller's jump instead of gutting the callee.
4. Binary diffing: what changed between versions
# Diaphora-style workflow: export both binaries, diff the databases
# In Ghidra: Version Tracking can also correlate functions across versions
# Quick byte-level first pass
cmp -l v1.exe v2.exe | head -20
Example output shape:
12345 74 75 ; byte 0x12345: 0x74 (je) -> 0x75 (jne)
12346 1B 1B
Diffing answers: did the vendor fix the vulnerability (compare patched vs unpatched to find the fix location, a standard defensive technique for writing detections), did the malware author change the C2 routine between variants, or which functions are new. Function-level diffing (Diaphora, Ghidra Version Tracking) shows added/removed/modified functions with similarity scores; focus manual review on modified functions with low similarity.
5. Keygenning concepts for license analysis
During authorized assessment of license validation (your own product, a client engagement with written permission), the analysis pattern is:
- Find the validation routine (string references to "invalid key", "activation").
- Read the algorithm: what transformation turns a key into a verdict (checksum, RSA signature check, server round-trip).
- Reimplement the check in Python to understand it, not to produce working keys for unlicensed use.
# Example: understanding a checksum-based check (analysis only)
def check(key: str) -> bool:
digits = [int(c) for c in key if c.isdigit()]
return sum(digits) % 7 == 0 and len(digits) == 16
The deliverable of authorized license analysis is a report on the scheme's strength (is it a local checksum an attacker trivially replicates, or a server-side signature), not a key generator. If the assessment scope does not explicitly include key generation, stop at the analysis.
6. Verifying patches
sha256sum original.exe patched.exe
# record both hashes
# Behavior check in the isolated lab:
# run the patched copy, confirm the target behavior changed and nothing
# else broke (smoke-test the main workflows)
Verification checklist: the intended behavior changed (the check no longer triggers), the program still starts and its main functions work, no new crashes in a 5-minute smoke test, and the patch is documented with file offsets, before/bytes, and rationale. If the program has integrity checks (checksums its own code), the patch will be detected at runtime; that is itself a finding about the program's tamper resistance.
Edge cases
- Patches that shift alignment: x86 is variable-length, so replacing a 5-byte call with a 2-byte jump leaves 3 bytes that must be NOP-filled; never leave partial instructions.
- Relative jumps: flipping je to jmp keeps the same displacement bytes, but rewriting a jump to a new target requires recalculating the relative offset.
- Integrity checks: packers and protectors often checksum their code. Patch after unpacking, or patch the checksum routine first (document the chain).
- ASLR and rebasing: file offsets are stable, virtual addresses are not. Record patches as file offsets (or RVAs), never as absolute addresses.
- Patches in shared code: a function used by both the check and normal logic cannot be gutted safely; patch the caller instead.
- Signed binaries: patching breaks the signature. That is expected in a lab; never redistribute a patched signed binary.
- Multi-architecture: 64-bit calling conventions and instruction encodings differ; verify each patch in the target bitness.
Escalation
- If the target is protected by a commercial protector (VMProtect, Themida) and patching requires devirtualization, escalate scope: that is a project, not a patch session.
- If patching is needed to analyze malware but the anti-analysis is layered (debugger checks plus timing plus VM checks), patch one layer at a time and re-test; escalate to the anti-analysis-bypass skill for the systematic approach.
- If a patch causes crashes you cannot explain after reasonable effort, revert to the debugger-only patch and analyze dynamically instead of producing a patched file.
- If the engagement scope is ambiguous about whether patching is allowed, stop and get written clarification before proceeding.
Detection and defender view
Patch-diffing is a defensive superpower: comparing a patched and unpatched binary locates the exact vulnerability fix, which tells you what to hunt for in your environment (did anyone exploit it before the patch?). For malware, the patches you apply in the lab (neutralized anti-analysis) should be documented so the next analyst reproduces your results; publish the patched hash alongside the original with a clear label. When you find that a license or integrity check is trivially patchable during an authorized assessment, the finding is a hardening recommendation (server-side validation, code signing verification, anti-tamper), not an exploitation guide.
Key references
- x64dbg documentation: https://x64dbg.com/
- Ghidra documentation: https://ghidra-sre.org/
- Diaphora (binary diffing): https://github.com/joxeankoret/diaphora
- Intel 64 and IA-32 Architectures Software Developer Manuals (instruction encoding reference): https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html
- Practical Binary Analysis (Dennis Andriesse), Chapters 6-7 (disassembly and binary instrumentation concepts)
- MITRE ATT&CK: T1622 (Debugger Evasion, for the checks being patched in malware labs)
