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.

address<strong>of</strong>.c<br />

#include <br />

int main() {<br />

int int_var = 5;<br />

int *int_ptr;<br />

}<br />

int_ptr = &int_var; // put <strong>the</strong> address <strong>of</strong> int_var into int_ptr<br />

The program itself doesn’t actually output anything, but you can probably<br />

guess what happens, even before debugging with GDB.<br />

reader@<strong>hacking</strong>:~/booksrc $ gcc -g address<strong>of</strong>.c<br />

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

Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".<br />

(gdb) list<br />

1 #include <br />

2<br />

3 int main() {<br />

4 int int_var = 5;<br />

5 int *int_ptr;<br />

6<br />

7 int_ptr = &int_var; // Put <strong>the</strong> address <strong>of</strong> int_var into int_ptr.<br />

8 }<br />

(gdb) break 8<br />

Breakpoint 1 at 0x8048361: file address<strong>of</strong>.c, line 8.<br />

(gdb) run<br />

St<strong>art</strong>ing program: /home/reader/booksrc/a.out<br />

Breakpoint 1, main () at address<strong>of</strong>.c:8<br />

8 }<br />

(gdb) print int_var<br />

$1 = 5<br />

(gdb) print &int_var<br />

$2 = (int *) 0xbffff804<br />

(gdb) print int_ptr<br />

$3 = (int *) 0xbffff804<br />

(gdb) print &int_ptr<br />

$4 = (int **) 0xbffff800<br />

(gdb)<br />

As usual, a breakpoint is set and <strong>the</strong> program is executed in <strong>the</strong><br />

debugger. At this point <strong>the</strong> majority <strong>of</strong> <strong>the</strong> program has executed. The first<br />

print command shows <strong>the</strong> value <strong>of</strong> int_var, and <strong>the</strong> second shows its address<br />

using <strong>the</strong> address-<strong>of</strong> operator. The next two print commands show that<br />

int_ptr contains <strong>the</strong> address <strong>of</strong> int_var, and <strong>the</strong>y also show <strong>the</strong> address <strong>of</strong><br />

<strong>the</strong> int_ptr for good measure.<br />

46 0x200

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

Saved successfully!

Ooh no, something went wrong!