#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void func(const char *str) {
char buf[4];
strcpy(buf,str);
printf("you entered [%s]\n",buf);
}
int main(int argc, char *argv[]) {
if(argc != 2) {
printf("Need an argument\n");
return 0;
}
func(argv[1]);
return 0;
}
我使用以下命令编译了您的代码。
gcc blah.c -o blah
裂开gdb。
[ayrx@localhost ~]$ gdb -q blah
Reading symbols from /home/ayrx/blah...(no debugging symbols found)...done.
(gdb) run AAAAAAAAAAAAAAAABBBB
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/fedora/blah AAAAAAAAAAAAAAAABBBB
you entered [AAAAAAAAAAAAAAAABBBB]
Program received signal SIGSEGV, Segmentation fault.
0x42424242 in ?? ()
这是来自 的寄存器转储gdb。
(gdb) info registers
eax 0x23 35
ecx 0x7fffffde 2147483614
edx 0x0 0
ebx 0x4e735000 1316179968
esp 0xbffff160 0xbffff160
ebp 0x41414141 0x41414141
esi 0x0 0
edi 0x0 0
eip 0x42424242 0x42424242
eflags 0x10286 [ PF SF IF RF ]
cs 0x73 115
ss 0x7b 123
ds 0x7b 123
es 0x7b 123
fs 0x0 0
gs 0x33 51
如您所见,寄存器已被我输入eip的最后 4 秒覆盖。B
最后一步是覆盖eip寄存器以指向您的 shellcode 的位置。由于您的缓冲区很小,您将不得不找到一些其他内存位置来存储 shellcode。覆盖eip寄存器以指向该位置,您就赢了。我会把那部分留给你。