skills/binary-exploitation/arbitrary-write-2-exec/www2exec-atexit/SKILL.md
Guide for exploiting arbitrary write vulnerabilities through atexit handlers, link_map manipulation, and TLS dtor_list overwrites. Use this skill whenever the user mentions atexit, exit handlers, link_map, TLS destructors, PTR_MANGLE, __run_exit_handlers, or needs to convert an arbitrary write primitive into code execution via program exit. Also use when analyzing binaries for exit handler vulnerabilities or crafting exploits that trigger code execution on program termination.
npx skillsauth add abelrguezr/hacktricks-skills atexit-exploitationInstall 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.
This skill helps you exploit arbitrary write vulnerabilities by hijacking exit handlers to achieve code execution when a program terminates via return or exit().
Use this skill when:
return or exit() (not _exit())When a program exits via return or exit():
__run_exit_handlers() is called__call_tls_dtors()Critical: If the program exits via _exit() syscall, exit handlers are NOT executed. Always verify with a breakpoint on __run_exit_handlers().
On x86/x64, function pointers in exit handlers are obfuscated:
PTR_MANGLE cookiemov rax, QWORD PTR [rbx] ; load mangled ptr
ror rax, 0x11 ; rotate right 17 bits
xor rax, QWORD PTR fs:0x30 ; XOR with PTR_MANGLE cookie
Other architectures (m68k, mips32, mips64, aarch64, arm, hppa) do NOT implement mangling - the pointer is used as-is.
The link_map structure contains l_info[DT_FINI_ARRAY] which points to an array of destructor functions.
Attack vectors:
Fake fini_array: Overwrite l_info[DT_FINI_ARRAY] to point to a fake Elf64_Dyn structure in controlled memory (e.g., .bss)
d_un.d_ptr should point to your one_gadget addressmap->l_addr offset in calculationsStack pointer overwrite: ld.so leaves a pointer to link_map on the stack. Overwrite it to point to a fake fini_array containing your one_gadget.
Structure layout:
struct Elf64_Dyn {
Elf64_Sxval d_tag; // DT_FINI_ARRAY = 0x1e
union {
Elf64_Xword d_val; // function address (one_gadget)
Elf64_Addr d_ptr; // offset from l_addr
} d_un;
};
The tls_dtor_list is a linked list of destructor functions stored near the stack canary.
Structure:
struct dtor_list {
dtor_func func; // function pointer (mangled)
void *obj; // argument to function
struct link_map *map;
struct dtor_list *next;
};
Exploitation steps:
Mangled pointer calculation:
def mangle_ptr(addr, cookie):
# Rotate right 17 bits, then XOR with cookie
rotated = ((addr >> 17) | (addr << (64 - 17))) & 0xffffffffffffffff
return rotated ^ cookie
def demangle_ptr(mangled, cookie):
# XOR with cookie, then rotate left 17 bits
xored = mangled ^ cookie
return ((xored << 17) | (xored >> (64 - 17))) & 0xffffffffffffffff
The initial structure contains an array of exit functions with different flavors:
struct exit_function {
enum exit_function_flavor flavor;
union {
void (*at) (void); // ef_at
void (*on) (int, void *); // ef_on (with arg)
void (*cxa) (void *, int); // ef_cxa (with arg)
} func;
};
Flavors:
ef_at: atexit() registered function, no argumentsef_on: on_exit() registered function, takes (status, arg)ef_cxa: C++ destructor, takes (arg, status)Exploitation:
cxa or on entry with system and /bin/sh as argument# Set breakpoint on exit handler
break __run_exit_handlers
# Run program and verify breakpoint is hit
run
If breakpoint is NOT hit, the program uses _exit() and these techniques won't work.
# Find link_map (usually in ld.so)
info proc mappings | grep ld.so
# Find TLS dtor_list
gef> tls
# Find initial structure
gef> p initial
Use the calculate_mangled_pointer.py script (see scripts/) to compute the correct mangled values for your target addresses.
Ensure the program exits via return or exit(), not _exit() or abort().
| Architecture | PTR_MANGLE | Exploitation Difficulty | |--------------|------------|------------------------| | x86/x64 | Yes | Harder (need cookie) | | aarch64 | No | Easier (direct ptr) | | arm | No | Easier (direct ptr) | | mips32/64 | No | Easier (direct ptr) | | m68k | No | Easier (direct ptr) | | hppa | No | Easier (direct ptr) |
See scripts/ directory for helper tools:
calculate_mangled_pointer.py - Compute mangled/demangled pointersgenerate_fake_structures.py - Create fake Elf64_Dyn and dtor_list structuresfind_exit_handlers.py - Analyze binaries for exit handler vulnerabilitiestesting
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.