Documentation Index
Fetch the complete documentation index at: https://mintlify.com/shellphish/how2heap/llms.txt
Use this file to discover all available pages before exploring further.
Overview
The House of Spirit is a heap exploitation technique that allows an attacker to trick malloc into returning a nearly-arbitrary pointer by freeing a fake chunk. This technique adds a non-heap pointer into the fastbin, leading to (nearly) arbitrary write capabilities.This technique works on the latest glibc versions and is still relevant in modern exploitation.
Glibc Version Compatibility
| Version | Status | Notes |
|---|---|---|
| glibc 2.23+ | ✅ Working | Original technique |
| glibc 2.26+ | ✅ Working | Requires tcache bypass |
| Latest | ✅ Working | Must fill tcache first |
Ret2 Wargames Practice: Try this technique hands-on at House of Spirit Interactive Challenge
What This Technique Achieves
The House of Spirit enables:- Nearly-arbitrary pointer allocation: Force malloc to return a pointer to attacker-controlled memory
- Stack exploitation: Allocate chunks on the stack for control flow hijacking
- Memory region control: Write to specific memory regions without direct write primitives
Prerequisites and Constraints
How It Works
Fill the tcache (modern glibc only)
On glibc 2.26+, allocate and free 7 chunks to fill the tcache for the target size. This forces subsequent frees to go to fastbins.
Create the fake chunk
Set up fake chunk metadata in your target memory region. The fake chunk must have:
- Valid size field (in fastbin range, typically 0x40 for 0x30 request)
- Size of next chunk must pass sanity checks (> 16 bytes and < 128KB)
Complete Source Code
Technical Deep Dive
Chunk Size Constraints
The fake chunk size must satisfy multiple conditions:- Fastbin range: Size must be ≤ 128 bytes on x64 (≤ 64 bytes on x86)
- Alignment: Size must be aligned to 16 bytes on x64 (8 bytes on x86)
- Flag bits:
- PREV_INUSE bit (LSB) is ignored for fastbins
- IS_MMAPPED bit (2nd LSB) must be 0
- NON_MAIN_ARENA bit (3rd LSB) must be 0
Next Chunk Validation
When freeing a chunk, glibc validates the next chunk:- Greater than 16 bytes (2 * SIZE_SZ on x64)
- Less than 128KB (default system_mem for main arena)
Tcache vs Fastbin
On glibc 2.26+, the tcache is checked first:- Tcache holds up to 7 chunks per size
- After tcache is full, chunks go to fastbins
- Must fill tcache before House of Spirit works
- Alternative: use tcache_house_of_spirit variant
Related Techniques
Tcache House of Spirit
Modern variant using tcache instead of fastbins
Fastbin Dup Into Stack
Alternative approach using fastbin duplication
CTF Challenges
hack.lu CTF 2014 - OREO
hack.lu CTF 2014 - OREO
Challenge: OREO cookie management system with heap overflowExploitation: Used House of Spirit to allocate a fake chunk on the stack, overwrite return addressWriteup: CTF WriteupKey Points:
- Created fake chunk on stack with proper size fields
- Freed fake chunk to add it to fastbin
- Next allocation returned stack pointer
- Overwrote saved instruction pointer for shell
Common Pitfalls
Mitigations
This technique has not been patched in glibc. Effective mitigations include:- Heap isolation: Prevent mixing heap and stack/data pointers
- Metadata checksums: Some custom allocators verify chunk metadata
- Guard pages: Place guard pages around sensitive memory regions
- Address sanitization: Tools like ASan detect fake chunk frees
See Also
- [Fastbin Attack Overview/techniques/fastbin/fastbin-dup)
- Tcache Exploitation
- Heap Metadata Structure
