我正在通過「Shellcoder手冊」中的示例工作。但是,這並不是很好。我正在運行Debian 2.6.32-5-686內核,i386。堆棧溢出 - 奇數返回地址
下面的演練將引導讀者瞭解發生緩衝區溢出時發生的情況。
程序:
include <stdio.h>
include <string.h>
void return_input(void)
{
char array[30];
gets (array);
printf("%s\n", array);
}
int main()
{
return_input();
return 0;
}
遊戲的目的是爲「AAAAAAAAAABBBBBBBBBBCCCCCCCCCCDDDDDDDDDD」傳遞到,這反過來,將覆蓋與過量的「D的返回地址的數組。
我編譯如下所示:
gcc -ggdb -m32 -o test -fno-stack-protector -mpreferred-stack-boundary=2 test.c
我跑gdb test
並開始調查:
(gdb) disas return_input
Dump of assembler code for function return_input:
0x080483f4 <return_input+0>: push %ebp
0x080483f5 <return_input+1>: mov %esp,%ebp
0x080483f7 <return_input+3>: sub $0x24,%esp
0x080483fa <return_input+6>: lea -0x1e(%ebp),%eax
0x080483fd <return_input+9>: mov %eax,(%esp)
0x08048400 <return_input+12>: call 0x804830c <[email protected]>
0x08048405 <return_input+17>: lea -0x1e(%ebp),%eax
0x08048408 <return_input+20>: mov %eax,(%esp)
0x0804840b <return_input+23>: call 0x804832c <[email protected]>
0x08048410 <return_input+28>: leave
0x08048411 <return_input+29>: ret
End of assembler dump.
(gdb) break *0x08048400
Breakpoint 1 at 0x8048400: file test.c, line 7.
(gdb) break *0x08048411
Breakpoint 2 at 0x8048411: file test.c, line 9.
在這一點上,我們推出了兩個破發點。一個在致電gets
之前。另一個就在函數返回之前。現在我們運行它:
(gdb) run
Starting program: ./test
Breakpoint 1, 0x08048400 in return_input() at test.c:7
7 gets (array);
(gdb) x/20x $esp
0xbffff3ac: 0xbffff3b2 0xb7fca304 0xb7fc9ff4 0x08048440
0xbffff3bc: 0xbffff3d8 0xb7eb75a5 0xb7ff1040 0x0804844b
0xbffff3cc: 0xb7fc9ff4 0xbffff3d8 *0x0804841a* 0xbffff458
0xbffff3dc: 0xb7e9ec76 0x00000001 0xbffff484 0xbffff48c
0xbffff3ec: 0xb7fe18c8 0xbffff440 0xffffffff 0xb7ffeff4
這是什麼堆棧看起來像只是調用gets
之前。我用星號標出了返回地址(0x0804841a
)。我們來覆蓋它:
(gdb) continue
Continuing.
AAAAAAAAAABBBBBBBBBBCCCCCCCCCCDDDDDDDDDD
AAAAAAAAAABBBBBBBBBBCCCCCCCCCCDDDDDDDDDD
Breakpoint 2, 0x08048411 in return_input() at test.c:9
9 }
(gdb) x/20x 0xbffff3ac
0xbffff3ac: 0xbffff3b2 0x4141a304 0x41414141 0x41414141
0xbffff3bc: 0x42424242 0x42424242 0x43434242 0x43434343
0xbffff3cc: 0x43434343 0x44444444 *0x44444444* 0xbf004444
0xbffff3dc: 0xb7e9ec76 0x00000001 0xbffff484 0xbffff48c
0xbffff3ec: 0xb7fe18c8 0xbffff440 0xffffffff 0xb7ffeff4
上面是堆棧在從函數返回之前的樣子。正如你所看到的,我們用這些多餘的'D'覆蓋了返回地址。結果。讓我們來完成:
(gdb) x/li $eip
0x8048411 <return_input+29>: ret
(gdb) stepi
Cannot access memory at address 0x44444448
恩,呃?這0x44444448
來自無處不在的屁股。不知何故,gcc在我們返回之前修改了返回地址。謝謝。
任何想法?我正確地假設gcc已經做了自己的內部檢查,返回地址是否有效。如果不是的話,它會阻止我們製造一個討厭的回報地址?
任何方法?我已經嘗試了一切 - http://www.madhur.co.in/blog/2011/08/06/protbufferoverflow.html。同樣的結果。
在調試時,gcc沒有做任何事情。 – halfdan