Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Practice library / Exploit development and reversing

Lab · Hard · 3 hoursFree account

Windows Heap Exploitation

Heap spray, use-after-free, and Windows heap internals for exploitation.

  • heap
  • use-after-free
  • heap-spray
  • windows
  • exploit-development
On this page 8 sections
  1. ·Lab Setup
  2. 1Understand heap internals
  3. 2Groom the heap
  4. Locked: Trigger a use-after-free
  5. Locked: Spray the heap
  6. Locked: Examine modern mitigations
  7. Locked: Defence and Detection
  8. Locked: Check Your Work

Before you start

The Windows NT heap stores objects in segmented free lists and the Low Fragmentation Heap. A stale pointer, a double free or a careless size calculation lets an attacker reclaim freed memory with a controlled object and hijack a virtual call. Browser and kernel advisories still describe this exact bug class year after year. The reader grooms the heap under WinDbg, triggers a use-after-free in a practice binary and chains the primitive into control of execution.

What You'll Learn

  • Describe the Windows NT heap structure: segments, chunks, free lists and the LFH
  • Inspect heap state with WinDbg and the !heap extension
  • Groom the heap into a predictable layout by controlling allocation sizes
  • Trigger a use-after-free in a practice program and observe the dangling pointer
  • Reclaim freed memory with a controlled object to hijack a virtual function call
  • Name the mitigations that defend the modern Windows heap and what each one stops

Lab Setup

Everything runs on a single Windows VM that you own. You compile the vulnerable programs yourself.

ItemRequirement
Lab VMWindows 10 or 11 x64, with Visual Studio 2022 (Community is fine)
DebuggerWinDbg (the modern version from the Microsoft Store, or WinDbg Preview)
CompilerMSVC (cl.exe) from a Developer Command Prompt
PythonPython 3.10+ for helper scripts

Compile all practice programs from a Developer Command Prompt for VS 2022.

Authorised scope only. Isolated lab network. Never on production.

Step 1: Understand heap internals

1

Heap exploits depend on the allocator's bookkeeping. You cannot shape what you do not understand. This step maps the structures you will manipulate in later steps.

Create heapview.c. It makes a few allocations so you can see the heap in WinDbg.

/* heapview.c — observe heap layout */
#include <windows.h>
#include <stdio.h>

int main(void) {
    HANDLE h = GetProcessHeap();
    void *a = HeapAlloc(h, 0, 64);
    void *b = HeapAlloc(h, 0, 64);
    void *c = HeapAlloc(h, 0, 64);
    printf("a=%p  b=%p  c=%p\n", a, b, c);
    __debugbreak();      /* trigger a breakpoint for WinDbg */
    HeapFree(h, 0, b);
    __debugbreak();
    return 0;
}

Compile without optimisations and without the security cookie check for clarity.

cl /Zi /Od /GS- heapview.c /link /DEBUG

Launch under WinDbg: File → Open Executable → heapview.exe. Press g to run to the first break. Then inspect the heap.

0:000> !heap -a
...
    Heap entries for Segment00
        address   size   flags
        00d10000  00588  busy (58)   internal
        00d10590  00050  busy (40)   <- a
        00d105e0  00050  busy (40)   <- b
        00d10630  00050  busy (40)   <- c

Each allocation is a chunk. The column size is the full chunk size including the 16-byte header. busy means allocated. The chunks sit next to each other in memory — a, then b, then c — with no gap. This adjacency is what makes heap overflows dangerous: writing past a corrupts the header of b.

Press g to continue past the HeapFree(b) call. Inspect again.

0:000> !heap -a
        00d10590  00050  busy (40)   <- a
        00d105e0  00050  free        <- b (now free)
        00d10630  00050  busy (40)   <- c

The chunk that was b is now marked free. The allocator added it to a free list. The next allocation of the same size class will likely reuse this slot. That reuse is the core of both grooming and use-after-free exploitation.

Step 2: Groom the heap

2

Grooming means making allocations in a pattern so that a target object lands at a predictable place. Reliable exploitation needs the heap to look the way you expect, not the way the application left it.

Create groom.c. It allocates many same-sized blocks, frees every other one, then makes one more allocation. The new block lands in a gap you created.

/* groom.c — heap grooming demonstration */
#include <windows.h>
#include <stdio.h>
#define SLOTS 20
#define SIZE  48

int main(void) {
    HANDLE h = GetProcessHeap();
    void *blocks[SLOTS];

    /* phase 1: fill the heap with same-sized chunks */
    for (int i = 0; i < SLOTS; i++)
        blocks[i] = HeapAlloc(h, HEAP_ZERO_MEMORY, SIZE);

    /* phase 2: free the even-numbered slots to create holes */
    for (int i = 0; i < SLOTS; i += 2)
        HeapFree(h, 0, blocks[i]);

    /* phase 3: the next allocation lands in one of the holes */
    void *target = HeapAlloc(h, HEAP_ZERO_MEMORY, SIZE);
    printf("target = %p\n", target);

    /* check: target should equal one of the freed addresses */
    for (int i = 0; i < SLOTS; i += 2) {
        if (target == blocks[i]) {
            printf("target reused slot %d (%p)\n", i, blocks[i]);
            break;
        }
    }
    __debugbreak();
    return 0;
}
cl /Zi /Od groom.c /link /DEBUG
groom.exe
target = 00E10590
target reused slot 0 (00E10590)

The allocator reused a freed slot of the same size. In a real exploit, the freed slot was held by a victim object (for example a C++ object with a vtable pointer). The attacker's controlled data now sits where the victim was. When the program later calls a virtual method on the stale pointer, it jumps to whatever address the attacker wrote.

Sign in to continue

5 more sections are for signed-in members. It is free: sign in with Google.

Related