26.07.2018 Views

hacking-the-art-of-exploitation

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

eader@<strong>hacking</strong>:~/booksrc $ gcc pointer_types4.c<br />

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

[char pointer] points to 0xbffff810, which contains <strong>the</strong> char 'a'<br />

[char pointer] points to 0xbffff811, which contains <strong>the</strong> char 'b'<br />

[char pointer] points to 0xbffff812, which contains <strong>the</strong> char 'c'<br />

[char pointer] points to 0xbffff813, which contains <strong>the</strong> char 'd'<br />

[char pointer] points to 0xbffff814, which contains <strong>the</strong> char 'e'<br />

[integer pointer] points to 0xbffff7f0, which contains <strong>the</strong> integer 1<br />

[integer pointer] points to 0xbffff7f4, which contains <strong>the</strong> integer 2<br />

[integer pointer] points to 0xbffff7f8, which contains <strong>the</strong> integer 3<br />

[integer pointer] points to 0xbffff7fc, which contains <strong>the</strong> integer 4<br />

[integer pointer] points to 0xbffff800, which contains <strong>the</strong> integer 5<br />

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

The compilation and output <strong>of</strong> this pointer_types4.c is basically <strong>the</strong> same<br />

as that for pointer_types3.c. The void pointer is really just holding <strong>the</strong> memory<br />

addresses, while <strong>the</strong> hard-coded typecasting is telling <strong>the</strong> compiler to use <strong>the</strong><br />

proper types whenever <strong>the</strong> pointer is used.<br />

Since <strong>the</strong> type is taken care <strong>of</strong> by <strong>the</strong> typecasts, <strong>the</strong> void pointer is truly<br />

nothing more than a memory address. With <strong>the</strong> data types defined by typecasting,<br />

anything that is big enough to hold a four-byte value can work <strong>the</strong><br />

same way as a void pointer. In pointer_types5.c, an unsigned integer is used<br />

to store this address.<br />

pointer_types5.c<br />

#include <br />

int main() {<br />

int i;<br />

char char_array[5] = {'a', 'b', 'c', 'd', 'e'};<br />

int int_array[5] = {1, 2, 3, 4, 5};<br />

unsigned int hacky_nonpointer;<br />

hacky_nonpointer = (unsigned int) char_array;<br />

for(i=0; i < 5; i++) { // Iterate through <strong>the</strong> int array with <strong>the</strong> int_pointer.<br />

printf("[hacky_nonpointer] points to %p, which contains <strong>the</strong> char '%c'\n",<br />

hacky_nonpointer, *((char *) hacky_nonpointer));<br />

hacky_nonpointer = hacky_nonpointer + size<strong>of</strong>(char);<br />

}<br />

hacky_nonpointer = (unsigned int) int_array;<br />

}<br />

for(i=0; i < 5; i++) { // Iterate through <strong>the</strong> int array with <strong>the</strong> int_pointer.<br />

printf("[hacky_nonpointer] points to %p, which contains <strong>the</strong> integer %d\n",<br />

hacky_nonpointer, *((int *) hacky_nonpointer));<br />

hacky_nonpointer = hacky_nonpointer + size<strong>of</strong>(int);<br />

}<br />

Programming 57

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

Saved successfully!

Ooh no, something went wrong!