[Advanced] — the other two bug families: the one that gives you an arbitrary read/write for free, and the one that runs the modern exploitation scene.

Format string bugs — the free primitive

The kid version first

printf reads its first argument as a format string full of instructions like %s and %d, and fetches the matching values from the following arguments. If you control that first argument, you control the instructions — you can make printf read and write memory, just by choosing what to type.

The bug

printf(user_input);              // ← BUG. user controls the format string
printf("%s", user_input);        // ← correct

If you type %x %x %x, printf has no matching arguments, so it happily prints whatever’s on the stack where those arguments would be. You just leaked stack memory by typing three characters.

What it gives you

A read primitive. %p %p %p ... walks up the stack printing values — leak the canary, leak a libc address, leak the PIE base. One format string bug defeats ASLR and the canary at once, which is why it’s so prized.

A targeted read. %7$p prints the 7th argument directly (positional). Put a target address in your input and use %s on it to read arbitrary memory.

A write primitive. %n writes the number of bytes printed so far to a pointer argument. Combined with positional access and width specifiers (%100c prints 100 characters), you can write an arbitrary value to an arbitrary address:

%<value>c%<n>$n     ← print `value` chars, then write that count to the nth stack slot

This is fiddly to do a full 8-byte write (you do it a byte or two at a time with %hhn), and pwntools automates it entirely:

payload = fmtstr_payload(offset, {target_addr: value})   # builds the whole thing

Format string is the friendliest bug — one vulnerability often yields both a read (kills ASLR) and a write (take control). Modern compilers warn on it (-Wformat-security) and it’s rarer in new code, but it’s a CTF staple and still appears in the wild.

Heap exploitation — the modern frontier

The kid version first

The heap is where malloc hands out memory on request. Behind the scenes, the allocator keeps bookkeeping data — sizes, and linked lists of freed chunks — mixed in with your data. Corrupt that bookkeeping and you can trick malloc into handing you a pointer to somewhere it shouldn’t, which becomes a write-anywhere primitive.

Heap exploitation is harder than the stack, it’s allocator-specific, and it’s where most real-world exploitation now lives — browsers, kernels, and modern CTF pwn.

The bug families

Heap overflow — write past a chunk into the next chunk’s header or data. Corrupt a size field, or an adjacent object’s function pointer.

Use-after-free (UAF) — the big one:

free(ptr);           // memory returned to the allocator
// ptr still points there — a DANGLING pointer
strcpy(ptr, evil);   // write into freed memory
// the allocator has since handed that memory to something else → you overwrote it

UAF is the dominant real-world memory bug today — the majority of exploited browser and kernel vulnerabilities. The pattern: free an object, get the allocator to reuse that slot for something you control, then use the stale pointer to corrupt the new occupant.

Double free — free the same chunk twice, corrupting the allocator’s free-lists so a later malloc returns a chunk you control.

How glibc’s allocator gets abused

glibc malloc sorts freed chunks into bins (fastbins, tcache, unsorted, small, large). The tcache (thread cache, glibc 2.26+) is a simple singly-linked list per size, and it’s the modern beginner target because its checks were historically weak:

  • tcache poisoning — corrupt the next pointer of a freed tcache chunk so the next malloc of that size returns an arbitrary address you chose. That’s a write-what-where. This is the canonical modern heap primitive
  • fastbin dup, unsafe unlink, House of Force / Spirit / Einherjar — a whole family of named techniques (“the House of…”), each abusing a specific allocator invariant

how2heap (by Shellphish) is the canonical annotated catalogue — every technique as a runnable, commented C file. It’s the resource → practice.

Why the heap is harder

  • Non-determinism — allocation layout depends on prior allocations, so you must groom the heap into a known state first
  • Allocator-specific — glibc, musl, Windows, macOS, and kernel allocators all differ. A technique for one doesn’t transfer
  • Version-specific — glibc adds hardening most releases, so an exploit is often pinned to a glibc version. Know your target’s version

The defences

Modern glibc added safe-linking (mangles free-list pointers with the chunk’s address, so you must leak a heap address first), pointer alignment checks, and tcache double-free detection. Each raised the bar; each has known workarounds in the how2heap catalogue.

Key insight

Format strings are the friendliest bug — one gives you both a read that defeats ASLR and a write that takes control — while the heap is the hardest and the most important, because use-after-free is what actually gets exploited in browsers and kernels today. The heap’s difficulty is that the allocator’s bookkeeping lives next to your data, exploitation is allocator- and version-specific, and you must groom the heap into a known state first. Start format strings early; treat the heap as the deep end you grow into with how2heap.

Source: [reference] — Aug 2026. CTF/authorised-testing material.

↑ Back to Binary Exploitation