Hacking - The Art of Exploitation, 2nd Edition

(Romina) #1
Programming 45

7 char *pointer2; // And yet another one
8
9 strcpy(str_a, "Hello, world!\n");
10 pointer = str_a; // Set the first pointer to the start of the array.
(gdb)
11 printf(pointer);
12
13 pointer2 = pointer + 2; // Set the second one 2 bytes further in.
14 printf(pointer2); // Print it.
15 strcpy(pointer2, "y you guys!\n"); // Copy into that spot.
16 printf(pointer); // Print again.
17 }
(gdb) break 11
Breakpoint 1 at 0x80483dd: file pointer.c, line 11.
(gdb) run
Starting program: /home/reader/booksrc/pointer


Breakpoint 1, main () at pointer.c:11
11 printf(pointer);
(gdb) x/xw pointer
0xbffff7e0: 0x6c6c6548
(gdb) x/s pointer
0xbffff7e0: "Hello, world!\n"
(gdb)


When the pointer is examined as a string, it’s apparent that the given


string is there and is located at memory address 0xbffff7e0. Remember that


the string itself isn’t stored in the pointer variable—only the memory address


0xbffff7e0 is stored there.


In order to see the actual data stored in the pointer variable, you must


use the address-of operator. The address-of operator is a unary operator,


which simply means it operates on a single argument. This operator is just


an ampersand (&) prepended to a variable name. When it’s used, the address


of that variable is returned, instead of the variable itself. This operator exists


both in GDB and in the C programming language.


(gdb) x/xw &pointer
0xbffff7dc: 0xbffff7e0
(gdb) print &pointer
$1 = (char **) 0xbffff7dc
(gdb) print pointer
$2 = 0xbffff7e0 "Hello, world!\n"
(gdb)


When the address-of operator is used, the pointer variable is shown to


be located at the address 0xbffff7dc in memory, and it contains the address


0xbffff7e0.


The address-of operator is often used in conjunction with pointers, since


pointers contain memory addresses. The addressof.c program demonstrates


the address-of operator being used to put the address of an integer variable


into a pointer. This line is shown in bold below.

Free download pdf