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.

at <strong>the</strong> end is used as a delimiter character to tell any function that is dealing<br />

with <strong>the</strong> string to stop operations right <strong>the</strong>re. The remaining extra bytes are<br />

just garbage and will be ignored. If a null byte is inserted in <strong>the</strong> fifth element<br />

<strong>of</strong> <strong>the</strong> character array, only <strong>the</strong> characters Hello would be printed by <strong>the</strong><br />

printf() function.<br />

Since setting each character in a character array is painstaking and<br />

strings are used fairly <strong>of</strong>ten, a set <strong>of</strong> standard functions was created for string<br />

manipulation. For example, <strong>the</strong> strcpy() function will copy a string from a<br />

source to a destination, iterating through <strong>the</strong> source string and copying each<br />

byte to <strong>the</strong> destination (and stopping after it copies <strong>the</strong> null termination byte).<br />

The order <strong>of</strong> <strong>the</strong> function’s arguments is similar to Intel assembly syntax:<br />

destination first and <strong>the</strong>n source. The char_array.c program can be rewritten<br />

using strcpy() to accomplish <strong>the</strong> same thing using <strong>the</strong> string library. The<br />

next version <strong>of</strong> <strong>the</strong> char_array program shown below includes string.h since<br />

it uses a string function.<br />

char_array2.c<br />

#include <br />

#include <br />

int main() {<br />

char str_a[20];<br />

}<br />

strcpy(str_a, "Hello, world!\n");<br />

printf(str_a);<br />

Let’s take a look at this program with GDB. In <strong>the</strong> output below, <strong>the</strong><br />

compiled program is opened with GDB and breakpoints are set before, in, and<br />

after <strong>the</strong> strcpy() call shown in bold. The debugger will pause <strong>the</strong> program at<br />

each breakpoint, giving us a chance to examine registers and memory. The<br />

strcpy() function’s code comes from a shared library, so <strong>the</strong> breakpoint in this<br />

function can’t actually be set until <strong>the</strong> program is executed.<br />

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

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

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

(gdb) list<br />

1 #include <br />

2 #include <br />

3<br />

4 int main() {<br />

5 char str_a[20];<br />

6<br />

7 strcpy(str_a, "Hello, world!\n");<br />

8 printf(str_a);<br />

9 }<br />

(gdb) break 6<br />

Breakpoint 1 at 0x80483c4: file char_array2.c, line 6.<br />

(gdb) break strcpy<br />

Programming 39

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

Saved successfully!

Ooh no, something went wrong!