26.07.2018 Views

hacking-the-art-of-exploitation

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

aslr_demo.c<br />

#include <br />

int main(int argc, char *argv[]) {<br />

char buffer[50];<br />

printf("buffer is at %p\n", &buffer);<br />

if(argc > 1)<br />

strcpy(buffer, argv[1]);<br />

}<br />

return 1;<br />

This program has an obvious buffer overflow vulnerability in it. However,<br />

with ASLR turned on, <strong>exploitation</strong> isn’t that easy.<br />

reader@<strong>hacking</strong>:~/booksrc $ gcc -g -o aslr_demo aslr_demo.c<br />

reader@<strong>hacking</strong>:~/booksrc $ ./aslr_demo<br />

buffer is at 0xbffbbf90<br />

reader@<strong>hacking</strong>:~/booksrc $ ./aslr_demo<br />

buffer is at 0xbfe4de20<br />

reader@<strong>hacking</strong>:~/booksrc $ ./aslr_demo<br />

buffer is at 0xbfc7ac50<br />

reader@<strong>hacking</strong>:~/booksrc $ ./aslr_demo $(perl -e 'print "ABCD"x20')<br />

buffer is at 0xbf9a4920<br />

Segmentation fault<br />

reader@<strong>hacking</strong>:~/booksrc $<br />

Notice how <strong>the</strong> location <strong>of</strong> <strong>the</strong> buffer on <strong>the</strong> stack changes with every<br />

run. We can still inject <strong>the</strong> shellcode and corrupt memory to overwrite <strong>the</strong><br />

return address, but we don’t know where <strong>the</strong> shellcode is in memory. The<br />

randomization changes <strong>the</strong> location <strong>of</strong> everything on <strong>the</strong> stack, including<br />

environment variables.<br />

reader@<strong>hacking</strong>:~/booksrc $ export SHELLCODE=$(cat shellcode.bin)<br />

reader@<strong>hacking</strong>:~/booksrc $ ./getenvaddr SHELLCODE ./aslr_demo<br />

SHELLCODE will be at 0xbfd919c3<br />

reader@<strong>hacking</strong>:~/booksrc $ ./getenvaddr SHELLCODE ./aslr_demo<br />

SHELLCODE will be at 0xbfe499c3<br />

reader@<strong>hacking</strong>:~/booksrc $ ./getenvaddr SHELLCODE ./aslr_demo<br />

SHELLCODE will be at 0xbfcae9c3<br />

reader@<strong>hacking</strong>:~/booksrc $<br />

This type <strong>of</strong> protection can be very effective in stopping exploits by <strong>the</strong><br />

average attacker, but it isn’t always enough to stop a determined hacker. Can<br />

you think <strong>of</strong> a way to successfully exploit this program under <strong>the</strong>se conditions?<br />

0x6c1<br />

Investigations with BASH and GDB<br />

Since ASLR doesn’t stop <strong>the</strong> memory corruption, we can still use a bruteforcing<br />

BASH script to figure out <strong>the</strong> <strong>of</strong>fset to <strong>the</strong> return address from <strong>the</strong><br />

380 0x600

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!