N-day patch diffing to exploit
When it applies
A vendor shipped a fix but no details: a CVE advisory says "out-of-bounds write in component
X" with no PoC, a Patch Tuesday drop needs triage, or a Linux distro backport may be
incomplete on some branch. The goal is to recover what the patch fixed and prove the bug
against the unpatched build. Weaponizing an N-day is only legitimate against in-scope,
authorized targets (a program that accepts it, an engagement, your own lab) — confirm with
tradecraft-scope-roe and record it in scope.txt.
Why it works
A security patch is a confession. The added bounds check, lock, zeroing, or refcount tells you
exactly which invariant the old code violated — and the unpatched binary still violates it.
Diff the two builds, filter to functions that changed moderately (identical = untouched,
completely different = new feature), and the fix pattern maps almost one-to-one onto a bug
class you can then trigger.
| The patch adds... |
Likely bug class |
if (a + b < a) / __builtin_add_overflow |
integer overflow |
KeAcquireSpinLock / mutex_lock |
race condition (TOCTOU / double-free) |
if (idx >= MAX) / if (len > buf_size) |
OOB read / write |
RtlZeroMemory / memset(struct, 0, ...) |
uninitialized-memory info leak |
InterlockedDecrement + refcount check |
UAF / refcount error |
ProbeForRead / ProbeForWrite / access_ok |
unvalidated user-mode pointer |
SeAccessCheck / capability check |
missing authorization |
| removed / tightened IOCTL codes |
attack-surface reduction — study the old interface |
Method
- Get the before/after binaries. Windows: download the MSU for build N (patched) and N-1
from the Microsoft Update Catalog, unpack with
expand.exe. Linux: apt download the two
kernel/package versions and dpkg-deb -x / rpm2cpio them. Third-party software: grab the
N-1 and N installers. Exact commands per platform in cheatsheet.md.
- Align symbols. Windows: pull PDBs from the Microsoft symbol server with
symchk.
Linux: matching dbgsym / debuginfo, and extract-vmlinux to turn vmlinuz back into an
ELF. No symbols for one side → migrate them from the nearest version before diffing.
- Diff. Feed both binaries to BinDiff, ghidriff, or Diaphora:
ghidriff ntoskrnl_old.exe ntoskrnl_new.exe -o diff_out/
bindiff --primary=old.BinExport --secondary=new.BinExport --output_dir=./bindiff_out/
- Locate the change. Filter to functions with similarity ≈ 0.5–0.95. Read what was
added: new
if guards, new loop bounds, new locks — and what was deleted (removed code
is a clue too). Map the pattern through the table above to a bug class; before/after
pseudocode pairs are ideal LLM input for a root-cause hypothesis (patterns in
cheatsheet.md).
- Write and verify the PoC against the unpatched build:
- integer overflow → boundary values (
0xFFFFFFF0 + 0x100) so the wrap yields a small
allocation but a large copy;
- race → threads hammering two syscalls on one object (close + IOCTL concurrently);
- UAF → spray → free → reclaim → use;
- OOB → drive length/index just past the boundary the new check now guards.
Success criteria are symmetric: the PoC crashes the unpatched build reliably and runs clean
on the patched one.
Gotchas
- Mitigation ≠ fix. Added CFG/XFG instrumentation (
_guard_xfg_dispatch_icall_fptr) is
hardening, not the bug fix — keep looking.
- Compiler noise fakes changes. Inlining decisions, switch-table reordering, and PGO make
unchanged source look different — diff N against N-1 (same toolchain), never across major
versions, and read control/data flow rather than token-level diffs.
- Alignment failure. If overall matched ratio < ~90%, stop: wrong pairing, different
compiler, or rebase mismatch — fix the inputs before reading results.
- A patch may shrink the blast radius, not fix the bug — the same root cause may still be
reachable via another path (one bug, multiple harvests).
- A crash on the unpatched build alone proves nothing — environment faults look identical;
you need the patched build clean and a stated root cause before claiming the CVE
(
reporting-triage-validation).
- Redact in write-ups — target hostnames, internal IPs, usernames become placeholders.
Verify success
You can name the function, the added check, and the violated invariant; your PoC crashes the
unpatched build (BSOD/panic/KASAN naming the expected class) within a predictable window and
exits cleanly on the patched build. That pairing — plus the root cause — is the reproducible
N-day.
References
Microsoft Update Catalog & MSRC CVRF API; BinDiff / ghidriff / Diaphora docs;
reverse-eng-binary-triage, exploit-poc-development in this library.
Portions adapted from reverse-skill by zhaoxuya520, MIT License.
1---2name: reverse-eng-patch-diff3description: >4 Turn a vendor security patch into a working N-day: diff the patched and unpatched binaries,5 read the newly added safety checks back to a bug class, then write a PoC that crashes the6 unpatched build. Load for a CVE with a patch but no public PoC, Patch Tuesday triage7 (ntoskrnl / win32k / afd.sys / clfs.sys), Linux LTS backport analysis, bindiff / ghidriff /8 Diaphora workflows, patch diff, binary diffing to find what a patch fixed.9domain: reverse-engineering10type: technique11stability: learning12modes: [pentest, bugbounty]13severity: high14cwe: [CWE-787, CWE-362, CWE-416, CWE-190, CWE-200]15tools: [bindiff, ghidriff, diaphora, radiff2, ghidra, ida, symchk]16schema_version: 117metadata:18 source: https://skillmd.com/skills/noorqureshi/reverse-eng-patch-diff19---2021# N-day patch diffing to exploit2223## When it applies24A vendor shipped a fix but no details: a CVE advisory says "out-of-bounds write in component25X" with no PoC, a Patch Tuesday drop needs triage, or a Linux distro backport may be26incomplete on some branch. The goal is to recover *what the patch fixed* and prove the bug27against the unpatched build. Weaponizing an N-day is only legitimate against in-scope,28authorized targets (a program that accepts it, an engagement, your own lab) — confirm with29`tradecraft-scope-roe` and record it in `scope.txt`.3031## Why it works32A security patch is a confession. The added bounds check, lock, zeroing, or refcount tells you33exactly which invariant the old code violated — and the unpatched binary still violates it.34Diff the two builds, filter to functions that changed *moderately* (identical = untouched,35completely different = new feature), and the fix pattern maps almost one-to-one onto a bug36class you can then trigger.3738| The patch adds... | Likely bug class |39|---|---|40| `if (a + b < a)` / `__builtin_add_overflow` | integer overflow |41| `KeAcquireSpinLock` / `mutex_lock` | race condition (TOCTOU / double-free) |42| `if (idx >= MAX)` / `if (len > buf_size)` | OOB read / write |43| `RtlZeroMemory` / `memset(struct, 0, ...)` | uninitialized-memory info leak |44| `InterlockedDecrement` + refcount check | UAF / refcount error |45| `ProbeForRead` / `ProbeForWrite` / `access_ok` | unvalidated user-mode pointer |46| `SeAccessCheck` / capability check | missing authorization |47| removed / tightened IOCTL codes | attack-surface reduction — study the old interface |4849## Method501. **Get the before/after binaries.** Windows: download the MSU for build N (patched) and N-151 from the Microsoft Update Catalog, unpack with `expand.exe`. Linux: `apt download` the two52 kernel/package versions and `dpkg-deb -x` / `rpm2cpio` them. Third-party software: grab the53 N-1 and N installers. Exact commands per platform in [`cheatsheet.md`](cheatsheet.md).542. **Align symbols.** Windows: pull PDBs from the Microsoft symbol server with `symchk`.55 Linux: matching dbgsym / debuginfo, and `extract-vmlinux` to turn `vmlinuz` back into an56 ELF. No symbols for one side → migrate them from the nearest version before diffing.573. **Diff.** Feed both binaries to BinDiff, ghidriff, or Diaphora:58 ```bash59 ghidriff ntoskrnl_old.exe ntoskrnl_new.exe -o diff_out/60 bindiff --primary=old.BinExport --secondary=new.BinExport --output_dir=./bindiff_out/61 ```624. **Locate the change.** Filter to functions with similarity ≈ 0.5–0.95. Read what was63 *added*: new `if` guards, new loop bounds, new locks — and what was *deleted* (removed code64 is a clue too). Map the pattern through the table above to a bug class; before/after65 pseudocode pairs are ideal LLM input for a root-cause hypothesis (patterns in66 `cheatsheet.md`).675. **Write and verify the PoC** against the *unpatched* build:68 - integer overflow → boundary values (`0xFFFFFFF0 + 0x100`) so the wrap yields a small69 allocation but a large copy;70 - race → threads hammering two syscalls on one object (close + IOCTL concurrently);71 - UAF → spray → free → reclaim → use;72 - OOB → drive length/index just past the boundary the new check now guards.73 Success criteria are symmetric: the PoC crashes the unpatched build reliably and runs clean74 on the patched one.7576## Gotchas77- **Mitigation ≠ fix.** Added CFG/XFG instrumentation (`_guard_xfg_dispatch_icall_fptr`) is78 hardening, not the bug fix — keep looking.79- **Compiler noise fakes changes.** Inlining decisions, switch-table reordering, and PGO make80 unchanged source look different — diff N against N-1 (same toolchain), never across major81 versions, and read control/data flow rather than token-level diffs.82- **Alignment failure.** If overall matched ratio < ~90%, stop: wrong pairing, different83 compiler, or rebase mismatch — fix the inputs before reading results.84- **A patch may shrink the blast radius, not fix the bug** — the same root cause may still be85 reachable via another path (one bug, multiple harvests).86- **A crash on the unpatched build alone proves nothing** — environment faults look identical;87 you need the patched build clean *and* a stated root cause before claiming the CVE88 (`reporting-triage-validation`).89- **Redact in write-ups** — target hostnames, internal IPs, usernames become placeholders.9091## Verify success92You can name the function, the added check, and the violated invariant; your PoC crashes the93unpatched build (BSOD/panic/KASAN naming the expected class) within a predictable window and94exits cleanly on the patched build. That pairing — plus the root cause — is the reproducible95N-day.9697## References98Microsoft Update Catalog & MSRC CVRF API; BinDiff / ghidriff / Diaphora docs;99`reverse-eng-binary-triage`, `exploit-poc-development` in this library.100101---102_Portions adapted from [reverse-skill](https://github.com/zhaoxuya520/reverse-skill) by zhaoxuya520, MIT License._