Bounty Program α — pwnable.tw (500 pts)
← Back to pwnable.tw Challenge Index
nc chall.pwnable.tw 10208Flag:
FLAG{s0m3_3nv_Ar3_pow3rful_it_h3lp_you_lif3_mor3_e4sy}
Challenge Overview
A bug bounty platform binary (x86-64, PIE, Full RELRO, NX, Stack Canary) built against glibc 2.27. Users can register/login, manage products, submit/edit/evaluate bug reports, and manage vulnerability types. A wrapper binary prompts for an environment variable name/value pair before execve-ing the main binary — this is the key hint: GLIBC_TUNABLES can control malloc behavior.
The program features a user system (linked list), product/type/report management with complex heap interactions, and an evaluation flow that links reports to users. All string fields use calloc/strdup/strtok internally.
checksec:
Arch: amd64-64-little
RELRO: Full RELRO # GOT read-only
Stack: Canary found
NX: NX enabled
PIE: PIE enabledVulnerabilities
V1 — strtok Continuation Leak in add_type (Primary)
Root Cause: The add_type function uses strtok(buf, ",") to parse comma-separated type names from a calloc'd buffer. When size is passed as -1 (or 0xFFFFFFFF unsigned → huge), the calloc(1, size) fails and returns NULL, but the code calls strtok(NULL, ",") which continues from the previous strtok state — reading whatever heap data lies beyond the last parsed token.
// add_type (simplified)
buf = calloc(1, size); // size = -1 → calloc fails → buf = NULL
read(0, buf, size); // read to NULL = no-op or ignored
tok = strtok(buf, ","); // strtok(NULL, ",") → continues from previous state!
printf("type: %s\n", tok); // leaks heap/libc pointers from adjacent memoryImpact: By carefully positioning the strtok internal state pointer (via prior string operations), the continuation reads arbitrary heap data. This is used to:
- Leak heap addresses — strtok walks into freed chunk metadata or adjacent allocations
- Leak libc addresses — if strtok state points near an unsorted bin chunk's fd/bk pointers
- Tcache poisoning — the
strtok(NULL, ",")call can write a NUL byte at a comma position in existing heap data, and the subsequent price parsing writes attacker-controlled values into tcache metadata
V2 — Password Overflow into Contact Pointer
Root Cause: The user struct stores username (0x1e bytes), password (0xf bytes), a contact pointer, and other fields. The change_password function reads up to 0x3f bytes into the password field, but the password buffer is only 0xf bytes — the overflow reaches the contact pointer at offset +0x38.
// change_password
read(0, user->password, 0x3f); // 0x3f bytes into a 0xf-byte field
// overflow overwrites: price(+0x30), uid(+0x34), contact_ptr(+0x38)Impact: Overwriting contact_ptr gives:
- Arbitrary read —
change_contactwrites to the address incontact_ptr;show_products/user_inforeads from pointed-to memory (via company pointer in product struct) - Arbitrary write —
change_contactwrites attacker data to the overwritten pointer
V3 — User Struct / Evaluate Fake User
Root Cause: The evaluate function iterates the user linked list and modifies user structs based on report data. By crafting heap layouts where freed user chunks overlap with type name data, an attacker can forge fake user structs with controlled prev/next pointers, username, and password — enabling login to a "fake" user that overlaps critical heap metadata.
V4 — Heap Address Leak via Username Overflow
Root Cause: When registering with a 0x1e-byte username and logging in with a 0x1f-byte username (with a garbage byte appended), the user_info display prints the name field which, due to missing NUL termination at the boundary, leaks heap pointers from adjacent struct fields (the next pointer of the evaluated report list).
// user_info display
printf("Name > %s\n", user->name); // 0x1e bytes, no NUL → leaks next 6 bytesV5 — MD5 Contact Oracle
Root Cause: The user_info command displays the MD5 hash of the contact buffer (20 bytes). By pointing the contact pointer to a target address (via V2) and brute-forcing byte-by-byte against the MD5 hash, an attacker can read arbitrary memory one byte at a time.
Key Data Structures
User Struct (~0x60 bytes, heap-allocated)
| Offset | Field | Size |
|---|---|---|
0x00 |
username |
0x1e bytes |
0x1e |
password |
0x0f bytes |
0x30 |
price |
uint32_t |
0x34 |
uid |
uint32_t |
0x38 |
contact_ptr |
char * |
0x40 |
report_head |
void * |
0x48 |
prev |
user * |
0x50 |
next |
user * |
Global State
- User linked list — doubly-linked list of registered users
- Product array — up to 8 products, each with name/company/comment pointers and a report linked list
- Type array — up to 8 vulnerability types, each with a name (
strdup'd) and a price - Reports — linked to products, contain title, description (
calloc), type ID, reporter
Exploit Paths
Path A — strtok Continuation + Password Overflow → Arbitrary R/W → __free_hook + setcontext
Used by: Solutions 821, 9251, 6748, 6923, 8153, 18331, 26957
- Set
GLIBC_TUNABLES— Use the wrapper to setglibc.malloc.perturb=Nwhich fills freed chunks with a known byte, making heap layout more predictable. - Heap leak — Register a user with 0x1e-byte username. After evaluation,
user_infoleaks heap pointers past the username boundary. Alternatively, use strtok continuation (V1) withsize=-1to leak a freed chunk's fd pointer. - Libc leak — Use V2 to overwrite the contact pointer to a product's company field. Change the company pointer to point at a heap location containing an unsorted bin fd/bk (libc main_arena pointer). Read it via
show_products. Alternatively, use strtok continuation after positioning an unsorted bin chunk's metadata in the strtok path. - Arbitrary R/W primitive — Via V2:
change_passwordoverwritescontact_ptrto target address;change_contactwrites to it. Chain to overwriteproduct.companyfor reads. - Write
setcontext+53to__free_hook— Whenfree()is called,rdipoints to the freed chunk.setcontext+53reads register values from[rdi+offset], enabling a full register-controlled context switch. - Trigger — Place a ROP chain /
mprotect+ shellcode payload in a heap allocation, then free it.setcontextpivotsrspto the payload →mprotect(heap, 0x1000, RWX)→read(0, heap, 0x200)→ jump to shellcode →open("/home/bounty_program/flag")+read+write.
Path B — MD5 Oracle Byte-by-Byte Leak → Stack Leak → Stack ROP
Used by: Solutions 1351, 25916
- Heap leak — Same as Path A.
- Libc leak via MD5 oracle — Use V2 to point
contact_ptrnear a libc pointer. Readuser_infoto get the MD5 of the 20-byte contact buffer. Brute-force each byte (256 attempts per byte, 6 bytes needed for a full pointer) by comparing computed MD5 hashes. Slow but reliable. - Stack leak — Point contact to
libc.sym['environ'], read via MD5 oracle (or direct read once arbitrary read is established). - Stack ROP — Use V2 arbitrary write to write a ROP chain onto the stack (
pop rdi; /bin/sh; systemormprotect+ shellcode). Overwrite saved RIP to pivot.
Path C — strtok Continuation Tcache Poisoning → __malloc_hook
Used by: Solutions 31599, 32010
- Leak heap + libc via strtok continuation (V1) and heap feng shui.
- Tcache poisoning — Arrange heap so that
strtok(NULL, ",")walks into a tcache chunk'sfdpointer. The NUL byte written by strtok at the comma position, combined with price writes, corrupts the fd to point to__malloc_hook. - Allocate from poisoned tcache — Next
strdup/ type allocation returns a chunk at__malloc_hook. Writeadd_rsp_0x48gadget (stack pivot) orone_gadget. - Trigger — Next
malloccall jumps to the gadget → pivot to ROP chain on the heap →open/read/writeflag.
Path D — Heap Feng Shui + Fake User + Evaluate Exploitation
Used by: Solutions exp.py (main), 3148, 6247
- Complex heap feng shui — Use large report descriptions and type allocations to create precise heap layouts where freed user chunks can be reused.
- Fake user creation — Craft a type name that, when placed in a freed user slot, forms a valid user struct with controlled username, password, and pointers.
- Login as fake user — The fake user's contact pointer or report head points to a target (e.g., an unsorted bin chunk for libc leak).
- Establish R/W — Through the fake user's overlapping fields, gain read/write primitives.
__free_hook+setcontext— Same finish as Path A.
Path E — Tcache thread_per_struct Overwrite → environ Leak → Stack ROP
Used by: Solution 32858
- Leak heap + libc via strtok continuation.
- Overwrite
tcache_perthread_struct— Use a large report description to overwrite the tcache metadata structure, redirecting a specific bin's entry tolibc.sym['environ'] - offset. - Allocate from corrupted tcache —
callocreturns a chunk overlappingenviron. Read the stack pointer from the bug report's description output. - Stack ROP — Use tcache poisoning to allocate at the stack return address. Write ROP chain directly.
Exploit Primitive Summary
| Primitive | Source | Reliability |
|---|---|---|
| Heap address leak | V4: username overflow / V1: strtok continuation | Deterministic / ~1/256 |
| Libc address leak | V1: strtok near unsorted bin / V2: arbitrary read | Deterministic |
| Stack address leak | V5: MD5 oracle on environ / V2: arb read |
Deterministic |
| Arbitrary read | V2: password overflow → contact ptr → show | Deterministic |
| Arbitrary write | V2: password overflow → contact ptr → change contact | Deterministic |
| Tcache poisoning | V1: strtok NUL + price writes | Heap-layout dependent |
| Code execution | __free_hook = setcontext+53 or __malloc_hook = gadget |
Deterministic after arb W |
Offsets (pwnable.tw libc-2.27.so)
__free_hook = 0x3ed8e8
__malloc_hook = 0x3ebc30
setcontext_53 = 0x520a5
mprotect = 0x11bae0
_IO_2_1_stderr_ = 0x3ec680
main_arena+96 = 0x3ebca0
environ = 0x3ee098
pop_rdi = 0x2155f
pop_rsi = 0x23e6a
pop_rdx = 0x1b96
pop_rax = 0x439c8
leave_ret = 0x54803
syscall_ret = 0xd2975Solution Write-ups
| File | Primary Path | Key Technique | Notes |
|---|---|---|---|
exp.py |
D | strtok + feng shui + fake user + __free_hook → setcontext |
Most complex; full heap manipulation |
821.md |
A | strtok leak + password overflow → setcontext + mprotect + shellcode |
Clean setcontext usage |
1351.md |
B | MD5 oracle byte-by-byte → stack leak → mprotect + shellcode ROP |
GLIBC_TUNABLES perturb=1 |
2972.md |
D | strtok + fake user via register → libc leak via show_vuln → setcontext |
Minimal feng shui |
3148.md |
D | strtok + heap spray + fake user → __free_hook → setcontext |
Detailed heap layout comments |
6247.md |
D | tcache_max tunable + unsorted bin consolidation → __free_hook |
Uses glibc.malloc.tcache_max |
6748.md |
A | password overflow → arb read/write → tcache fd poison → setcontext |
Well-structured OOP exploit |
6923.md |
A | strtok -1 leak × 2 → password overflow → __free_hook → setcontext |
Direct strtok libc leak |
8153.md |
A + B | password overflow → __environ → stack ROP |
Stack-based finish |
9251.md |
A | password overflow → arb R/W → leave; ret + malloc_hook → ROP |
fopen + fgets for flag read |
18331.md |
A | strtok size=-1 → password overflow → arb R/W → stack return overwrite |
No setcontext; direct stack smash |
25916.md |
B + E | MD5 oracle + _dl_open_hook chain → setcontext |
Most complex; multi-stage via _dl_open_hook |
26957.md |
C | strtok tcache poison → __malloc_hook + setcontext |
Direct tcache FD corruption |
31599.md |
C | strtok continuation → tcache poison → __malloc_hook = add_rsp_0x48 → ROP |
Cleanest tcache poison approach |
32010.md |
D | strtok leak + report/type overlap → __malloc_hook = leave; ret |
leave; ret pivot |
32858.md |
E | tcache_perthread_struct overwrite → environ leak → stack ROP |
Unique tcache struct approach |
34817.md |
D | strtok + __free_hook → setcontext → open/read/write ROP |
Two script variants |
36134.md |
B | MD5 oracle + password overflow + heap feng shui → leave; ret → ROP |
Byte-by-byte MD5 for all leaks |
Notes
- No
system("/bin/sh")— The flag is at/home/bounty_program/flagand seccomp or sandbox restrictions prevent direct shell. All solutions useopen/read/writeshellcode or ROP to read the flag file. GLIBC_TUNABLES— The wrapper's env var prompt is the intended hint. Settingglibc.malloc.perturb=Nfills freed heap with byte N, making heap layout deterministic and leaks reliable. Some solutions also useglibc.malloc.tcache_maxorglibc.malloc.check.- Full RELRO — GOT is read-only, so all exploits target
__free_hookor__malloc_hookinstead.
Files
| File | Description |
|---|---|
exp.py |
Working exploit script (feng shui + fake user approach) |
desc.txt |
Challenge description |
artifacts/bounty_program_alpha.tar.gz |
Original challenge binary + wrapper + libc |
solution/*.md |
Community write-ups (24 solutions) |