Hacktricks-skills slirp-nat-heap-exploitation
Analyze and understand VirtualBox Slirp NAT packet heap exploitation vulnerabilities. Use this skill whenever the user needs to understand Slirp NAT heap corruption, mbuf allocator exploitation, UMA zone hijacking, or similar allocator-based vulnerabilities in network stacks. Also use when analyzing packet buffer overflows, heap grooming techniques, or when creating educational content about VirtualBox security research.
git clone https://github.com/abelrguezr/hacktricks-skills
skills/binary-exploitation/libc-heap/virtualbox-slirp-nat-packet-heap-exploitation/SKILL.MDSlirp NAT Heap Exploitation Analysis
A skill for understanding and analyzing VirtualBox Slirp NAT packet heap exploitation vulnerabilities, including mbuf allocator corruption, UMA zone hijacking, and heap grooming techniques.
When to Use This Skill
Use this skill when:
- Analyzing Slirp NAT or similar network stack vulnerabilities
- Understanding mbuf allocator exploitation patterns
- Studying UMA zone hijacking techniques
- Creating educational content about VirtualBox security
- Analyzing packet buffer overflow vulnerabilities
- Understanding heap grooming for deterministic exploitation
- Researching guest-to-host escape vectors
Core Vulnerability Mechanics
1. Packet Allocator Anatomy
VirtualBox uses a custom zone allocator for Slirp packet buffers (mbufs):
struct item { uint32_t magic; // 0xdead0001 void *zone; // uma_zone_t pointer with callbacks uint32_t ref_count; LIST_ENTRY(item) list; // freelist / used list links };
Key insight: Each 0x800-byte data chunk has an inline header. When freed,
slirp_uma_free() trusts this header and executes zone->pfFini() and zone->pfDtor() callbacks.
2. The m->m_len Override Primitive
At the top of
ip_input():
if (m->m_len != RT_N2H_U16(ip->ip_len)) m->m_len = RT_N2H_U16(ip->ip_len);
Exploitation vector: Guest can advertise any
ip_len up to 0xffff. The assignment happens before IP header verification, destroying all later bounds checks.
3. Infoleak Primitive
Use UDP packets with checksum
0 (no checksum). The NAT fast-path forwards m->m_len bytes without inspecting payload integrity.
What can be leaked:
- Next mbuf's inline
(reveals freelist order, realstruct item
pointer)zone - Heap cookies like
fieldsmagic - Zone structure addresses
- Neighboring chunk metadata
4. Overwrite Primitive via IP Options
Force packets through
ip_stripoptions() by:
- Supplying long
so computed move length extends past current mbufip_len - Including IP options to trigger the stripping path
reads from following mbuf and writes over current mbuf's payload and inline headermemcpy()
Result: Can corrupt
magic, zone, ref_count, and list pointers.
5. UMA Zone Hijacking
Once adjacent
struct item is corruptible:
-
Build fake
structure inside guest-controlled mbuf:uma_zone
→ PLT entry ofpfFinimemcpy()
→ destination pointer (GOT entry, vtable slot, function pointer)pData
→ bytes to copysize
→ optional second stage callpfDtor
-
Overwrite target mbuf's
field with fake structure pointerzone -
Free the mbuf →
executesslirp_uma_free()
with guest-controlled datamemcpy(dest, src, size)
6. Heap Grooming for Deterministic Adjacency
Slirp's per-interface zone is 3072 chunks deep, freelist traversed from high addresses downward.
Grooming technique:
- Flood NAT with
fragments of constant sizeIP_MF - Recycle specific chunks by sending fragments that time out
- Freelist walks in LIFO order, enabling predictable placement
7. From Arbitrary Write to Code Execution
With memcpy-on-free primitive:
- Copy
string and command buffer into stable mbuf/bin/sh - Overwrite GOT entry or indirect callsite with
PLT entrysystem() - Trigger the overwritten call
Alternative: Plant ROP chain in heap memory, copy address into frequently-invoked callback.
Analysis Workflow
Step 1: Understand the Allocator
# Use the analysis script to understand heap layout python scripts/analyze_heap_layout.py --zone-size 0x800 --chunk-count 3072
Step 2: Craft Test Packets
# Generate packets for testing the vulnerability python scripts/craft_slirp_packet.py \ --ip-len 0x1000 \ --udp-checksum 0 \ --ip-options 16 \ --output packets.pcap
Step 3: Analyze Memory Layout
- Identify zone structure addresses from infoleak
- Map freelist order from leaked
datastruct item - Calculate offsets for target corruption
Step 4: Build Exploit Chain
- Infoleak phase: Send oversized UDP packets, capture leaked heap data
- Grooming phase: Allocate predictable mbuf sequences via fragmentation
- Corruption phase: Send IP options packet to overwrite target
struct item - Hijack phase: Free corrupted mbuf to trigger fake
callbacksuma_zone - Payload phase: Execute arbitrary write to GOT/function pointer
Key Concepts to Remember
Why This Works
- Trust boundary violation: Guest-supplied
trusted before validationip_len - Inline metadata: Allocator header lives adjacent to data, overflowable
- Callback trust:
/pfFini
called unconditionally on freepfDtor - Non-PIE binary: Fixed PLT addresses enable direct function targeting
Common Patterns in Similar Vulnerabilities
- Length field override: Anywhere user-controlled length is used before validation
- Inline allocator headers: Metadata adjacent to data is overflow target
- Callback on free: Any allocator that calls user-controlled functions on free
- Freelist traversal: Predictable allocation order enables grooming
Defense Considerations
- Validate all length fields before use
- Separate metadata from data (not inline)
- Use canaries in allocator headers
- Enable PIE/ASLR for address randomization
- Validate zone pointers before callback execution
Test Cases
Test Case 1: Basic Infoleak
Goal: Verify m->m_len override causes infoleak
Steps:
- Send UDP packet with
, checksum=0ip_len=0x1000 - Capture response or observe behavior
- Verify data beyond actual packet is read
Test Case 2: Header Corruption
Goal: Verify IP options overflow corrupts adjacent chunk
Steps:
- Groom heap with fragmentation
- Send packet with IP options and oversized
ip_len - Free the packet
- Observe corruption in adjacent
struct item
Test Case 3: Zone Hijacking
Goal: Verify fake uma_zone triggers controlled callback
Steps:
- Leak real zone address
- Build fake zone with
pfFini=memcpy@plt - Corrupt target mbuf's zone pointer
- Free mbuf and verify memcpy executes
References
- Thinking Outside The Box: Exploiting VirtualBox Slirp NAT Heap Corruption
- UMA allocator documentation
- Slirp source code analysis
- VirtualBox NAT device implementation
Related Skills
- Binary exploitation fundamentals
- Heap exploitation techniques
- Network stack vulnerability analysis
- Guest-to-host escape research