skills/binary-exploitation/libc-heap/heap-memory-functions/heap-memory-functions/SKILL.md
How to analyze and understand heap memory functions (malloc, free, realloc, calloc) in binary exploitation and security research. Use this skill whenever the user mentions heap memory, memory allocation, malloc/free, heap exploitation, glibc heap, tcache, bins, or any heap-related vulnerability analysis. This skill helps with understanding heap internals, debugging heap issues, and identifying heap-based vulnerabilities.
npx skillsauth add abelrguezr/hacktricks-skills heap-memory-functionsInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
A skill for analyzing heap memory functions and understanding heap-based vulnerabilities in binary exploitation.
This skill helps you understand and analyze heap memory management functions, including:
Use this skill when you need to:
In glibc's malloc implementation, each allocated chunk has metadata:
+------------------+
| prev_size | (8 bytes) - size of previous chunk
+------------------+
| size | flags | (8 bytes) - current chunk size + flags
+------------------+
| user data | (variable) - actual allocated memory
+------------------+
Key flags:
PREV_INUSE (bit 0): Previous chunk is in useIS_MMAPPED (bit 1): Chunk was allocated via mmapNON_MAIN_ARENA (bit 2): Chunk belongs to non-main arenaModern glibc (2.26+) uses tcache for small allocations:
setenv("MALLOC_CHECK_", "0", 1)Free chunks are organized into bins:
Use the find_heap_functions.sh script to locate heap-related function calls:
./scripts/find_heap_functions.sh <binary>
This identifies all malloc, free, realloc, and calloc calls in the binary.
Examine how the program allocates memory:
Use the check_heap_vulns.sh script to identify potential issues:
./scripts/check_heap_vulns.sh <source_file.c>
Common vulnerabilities to look for:
Use GDB with the glibc-malloc plugin or heap commands:
(gdb) break main
(gdb) run
(gdb) heap
(gdb) heap chunks
(gdb) heap bins
Or use pwndbg/gef for enhanced heap debugging:
(gdb) heap
(gdb) heap chunks
(gdb) tcache
(gdb) bins
Pattern:
char *ptr = malloc(100);
free(ptr);
// ptr still points to freed memory
strcpy(ptr, "malicious data"); // UAF!
Exploitation:
Pattern:
char *ptr = malloc(100);
free(ptr);
free(ptr); // Double free!
Exploitation:
Pattern:
char *ptr = malloc(100);
strcpy(ptr, large_buffer); // Overflow!
Exploitation:
Pattern:
Requirements:
Locates all heap function calls in a binary:
#!/bin/bash
# Usage: ./find_heap_functions.sh <binary>
# Finds all malloc, free, realloc, calloc calls
if [ -z "$1" ]; then
echo "Usage: $0 <binary>"
exit 1
fi
binary="$1"
echo "=== Heap Function Calls in $binary ==="
echo ""
# Find malloc calls
echo "malloc() calls:"
objdump -d "$binary" | grep -A5 "<malloc>" | head -20
echo ""
echo "free() calls:"
objdump -d "$binary" | grep -A5 "<free>" | head -20
echo ""
echo "realloc() calls:"
objdump -d "$binary" | grep -A5 "<realloc>" | head -20
echo ""
echo "calloc() calls:"
objdump -d "$binary" | grep -A5 "<calloc>" | head -20
Analyzes C source for common heap vulnerabilities:
#!/bin/bash
# Usage: ./check_heap_vulns.sh <source.c>
# Checks for common heap vulnerability patterns
if [ -z "$1" ]; then
echo "Usage: $0 <source.c>"
exit 1
fi
source_file="$1"
echo "=== Heap Vulnerability Analysis ==="
echo "File: $source_file"
echo ""
# Check for potential double frees
echo "Potential double free patterns:"
grep -n "free(" "$source_file" | while read line; do
ptr=$(echo "$line" | grep -oP 'free\(\K[^)]+' | head -1)
if [ -n "$ptr" ]; then
count=$(grep -c "free($ptr)" "$source_file" 2>/dev/null || echo "1")
if [ "$count" -gt 1 ]; then
echo " WARNING: $ptr freed $count times"
fi
fi
done
# Check for use-after-free patterns
echo ""
echo "Potential use-after-free patterns:"
grep -B5 -A5 "free(" "$source_file" | grep -A5 "free(" | grep -E "\*|\[|strcpy|memcpy" | head -10
# Check for unsafe string operations after malloc
echo ""
echo "Unsafe operations after malloc:"
grep -B2 -A2 "malloc(" "$source_file" | grep -E "strcpy|sprintf|gets" | head -10
echo ""
echo "Analysis complete. Review flagged patterns manually."
# Basic heap inspection
heap
heap chunks
heap bins
# With pwndbg/gef
heap
heap chunks
heap bins
tcache
bins
# Inspect specific chunk
x/20gx 0x<chunk_address>
# Follow allocation
break malloc
run
info registers
# Disable tcache for debugging
export MALLOC_CHECK_=0
export MALLOC_TRIM_THRESHOLD_=128
# Enable malloc debugging
export MALLOC_CHECK_=2 # Abort on corruption
export MALLOC_CHECK_=3 # Abort on corruption + check pointers
# Set heap size
export MALLOC_MMAP_THRESHOLD_=131072
find_heap_functions.sh to map allocation patternsAfter analyzing heap functions:
testing
How to perform a House of Lore (small bin attack) heap exploitation. Use this skill whenever the user mentions heap exploitation, small bin attacks, fake chunks, glibc heap vulnerabilities, or needs to insert fake chunks into small bins for arbitrary read/write. Trigger for CTF challenges involving heap corruption, glibc 2.31+ exploitation, or when the user needs to bypass malloc sanity checks using fake chunk linking.
testing
How to perform House of Force heap exploitation attacks. Use this skill whenever the user mentions heap exploitation, House of Force, top chunk manipulation, arbitrary memory allocation, malloc manipulation, or wants to allocate chunks at specific addresses. Also trigger for CTF challenges involving heap overflows, top chunk size overwrites, or when the user needs to calculate evil_size for heap attacks. Make sure to use this skill for any binary exploitation task involving glibc heap manipulation, even if they don't explicitly say "House of Force".
tools
How to perform House of Einherjar heap exploitation to allocate memory at arbitrary addresses. Use this skill whenever the user mentions heap exploitation, glibc heap attacks, arbitrary memory allocation, off-by-one overflow exploitation, tcache poisoning, fast bin attacks, or any CTF challenge involving heap manipulation. This is essential for binary exploitation tasks where you need to control malloc() return addresses.
testing
How to identify, analyze, and exploit heap overflow vulnerabilities in binary exploitation challenges and real-world scenarios. Use this skill whenever the user mentions heap overflows, memory corruption, heap grooming, tcache poisoning, fast-bin attacks, or any heap-related vulnerability in CTF challenges, binary analysis, or security research. This skill covers heap overflow fundamentals, exploitation techniques, heap grooming strategies, and real-world CVE analysis.