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.

Example <strong>of</strong> printing with different format string<br />

printf("[A] Dec: %d, Hex: %x, Unsigned: %u\n", A, A, A);<br />

printf("[B] Dec: %d, Hex: %x, Unsigned: %u\n", B, B, B);<br />

printf("[field width on B] 3: '%3u', 10: '%10u', '%08u'\n", B, B, B);<br />

printf("[string] %s Address %08x\n", string, string);<br />

}<br />

// Example <strong>of</strong> unary address operator (dereferencing) and a %x format string<br />

printf("variable A is at address: %08x\n", &A);<br />

In <strong>the</strong> preceding code, additional variable arguments are passed to each<br />

printf() call for every format parameter in <strong>the</strong> format string. The final printf()<br />

call uses <strong>the</strong> argument &A, which will provide <strong>the</strong> address <strong>of</strong> <strong>the</strong> variable A.<br />

The program’s compilation and execution are as follows.<br />

reader@<strong>hacking</strong>:~/booksrc $ gcc -o fmt_strings fmt_strings.c<br />

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

[A] Dec: -73, Hex: ffffffb7, Unsigned: 4294967223<br />

[B] Dec: 31337, Hex: 7a69, Unsigned: 31337<br />

[field width on B] 3: '31337', 10: ' 31337', '00031337'<br />

[string] sample Address bffff870<br />

variable A is at address: bffff86c<br />

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

The first two calls to printf() demonstrate <strong>the</strong> printing <strong>of</strong> variables A and B,<br />

using different format parameters. Since <strong>the</strong>re are three format parameters<br />

in each line, <strong>the</strong> variables A and B need to be supplied three times each. The<br />

%d format parameter allows for negative values, while %u does not, since it is<br />

expecting unsigned values.<br />

When <strong>the</strong> variable A is printed using <strong>the</strong> %u format parameter, it appears<br />

as a very high value. This is because A is a negative number stored in two’s<br />

complement, and <strong>the</strong> format parameter is trying to print it as if it were an<br />

unsigned value. Since two’s complement flips all <strong>the</strong> bits and adds one, <strong>the</strong><br />

very high bits that used to be zero are now one.<br />

The third line in <strong>the</strong> example, labeled [field width on B], shows <strong>the</strong> use<br />

<strong>of</strong> <strong>the</strong> field-width option in a format parameter. This is just an integer that<br />

designates <strong>the</strong> minimum field width for that format parameter. However,<br />

this is not a maximum field width—if <strong>the</strong> value to be outputted is greater<br />

than <strong>the</strong> field width, <strong>the</strong> field width will be exceeded. This happens when 3 is<br />

used, since <strong>the</strong> output data needs 5 bytes. When 10 is used as <strong>the</strong> field width,<br />

5 bytes <strong>of</strong> blank space are outputted before <strong>the</strong> output data. Additionally, if a<br />

field width value begins with a 0, this means <strong>the</strong> field should be padded with<br />

zeros. When 08 is used, for example, <strong>the</strong> output is 00031337.<br />

The fourth line, labeled [string], simply shows <strong>the</strong> use <strong>of</strong> <strong>the</strong> %s format<br />

parameter. Remember that <strong>the</strong> variable string is actually a pointer containing<br />

<strong>the</strong> address <strong>of</strong> <strong>the</strong> string, which works out wonderfully, since <strong>the</strong> %s format<br />

parameter expects its data to be passed by reference.<br />

Programming 49

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

Saved successfully!

Ooh no, something went wrong!