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.

address operator is used to write this data into <strong>the</strong> variables count_one and<br />

count_two, respectively. The values are <strong>the</strong>n outputted, revealing that 46 bytes<br />

are found before <strong>the</strong> first %n and 113 before <strong>the</strong> second.<br />

The stack example at <strong>the</strong> end is a convenient segue into an explanation<br />

<strong>of</strong> <strong>the</strong> stack’s role with format strings:<br />

printf("A is %d and is at %08x. B is %x.\n", A, &A, B);<br />

When this printf() function is called (as with any function), <strong>the</strong> arguments<br />

are pushed to <strong>the</strong> stack in reverse order. First <strong>the</strong> value <strong>of</strong> B, <strong>the</strong>n <strong>the</strong><br />

address <strong>of</strong> A, <strong>the</strong>n <strong>the</strong> value <strong>of</strong> A, and finally <strong>the</strong> address <strong>of</strong> <strong>the</strong> format string.<br />

The stack will look like <strong>the</strong> diagram here.<br />

The format function iterates through <strong>the</strong><br />

Top <strong>of</strong> <strong>the</strong> Stack<br />

format string one character at a time. If <strong>the</strong><br />

Address <strong>of</strong> format string<br />

character isn’t <strong>the</strong> beginning <strong>of</strong> a format<br />

parameter (which is designated by <strong>the</strong> percent<br />

sign), <strong>the</strong> character is copied to <strong>the</strong><br />

Value <strong>of</strong> A<br />

Address <strong>of</strong> A<br />

output. If a format parameter is encountered,<br />

<strong>the</strong> appropriate action is taken, using <strong>the</strong><br />

Value <strong>of</strong> B<br />

argument in <strong>the</strong> stack corresponding to that<br />

Bottom <strong>of</strong> <strong>the</strong> Stack<br />

parameter.<br />

But what if only two arguments are pushed<br />

to <strong>the</strong> stack with a format string that uses three<br />

format parameters? Try removing <strong>the</strong> last argument from <strong>the</strong> printf()<br />

line for <strong>the</strong> stack example so it matches <strong>the</strong> line shown below.<br />

printf("A is %d and is at %08x. B is %x.\n", A, &A);<br />

This can be done in an editor or with a little bit <strong>of</strong> sed magic.<br />

reader@<strong>hacking</strong>:~/booksrc $ sed -e 's/, B)/)/' fmt_uncommon.c > fmt_uncommon2.c<br />

reader@<strong>hacking</strong>:~/booksrc $ diff fmt_uncommon.c fmt_uncommon2.c<br />

14c14<br />

< printf("A is %d and is at %08x. B is %x.\n", A, &A, B);<br />

---<br />

> printf("A is %d and is at %08x. B is %x.\n", A, &A);<br />

reader@<strong>hacking</strong>:~/booksrc $ gcc fmt_uncommon2.c<br />

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

The number <strong>of</strong> bytes written up to this point X is being stored in count_one, and <strong>the</strong> number <strong>of</strong><br />

bytes up to here X is being stored in count_two.<br />

count_one: 46<br />

count_two: 113<br />

A is 5 and is at bffffc24. B is b7fd6ff4.<br />

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

The result is b7fd6ff4. What <strong>the</strong> hell is b7fd6ff4? It turns out that since<br />

<strong>the</strong>re wasn’t a value pushed to <strong>the</strong> stack, <strong>the</strong> format function just pulled data<br />

from where <strong>the</strong> third argument should have been (by adding to <strong>the</strong> current<br />

frame pointer). This means 0xb7fd6ff4 is <strong>the</strong> first value found below <strong>the</strong><br />

stack frame for <strong>the</strong> format function.<br />

Exploitation 169

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

Saved successfully!

Ooh no, something went wrong!