[问题]对一个C程序的讨论

软件和网站开发以及相关技术探讨
回复
hui hui
帖子: 38
注册时间: 2006-12-21 19:22
来自: Jiang Su China

[问题]对一个C程序的讨论

#1

帖子 hui hui » 2007-07-22 10:56

学校科协上学期讲了缓冲区溢出,后来我照着例子编了一个程序:
#include <stdio.h>
#include <string.h>
int main(void)
{ char largebuff[]="1234512345123451234512345===ABCD";
char smallbuff[16];
strcpy (smallbuff,largebuff);
printf(smallbuff);
}

然后用GCC编译,运行后显示缓冲区溢出,但后来用GDB分析时却出了问题,
(gdb) r
Starting program: /home/hui/a.out
*** stack smashing detected ***: /home/hui/a.out terminated
1234512345123451234512345===ABCD
Program received signal SIGABRT, Aborted.
0xffffe410 in __kernel_vsyscall ()
(gdb) i reg
eax 0x0 0
ecx 0x1ab1 6833
edx 0x6 6
ebx 0x1ab1 6833
esp 0xbff19ec8 0xbff19ec8
ebp 0xbff19ee0 0xbff19ee0
esi 0xbff19f80 -1074684032
edi 0xb7f54ff4 -1208659980
eip 0xffffe410 0xffffe410 <__kernel_vsyscall+16>
eflags 0x246 [ PF ZF IF ]
cs 0x73 115
ss 0x7b 123
ds 0x7b 123
es 0x7b 123
fs 0x0 0
gs 0x33 51
本来此时的EIP应该为0x44434241可这里不是,
后来怀疑是GCC选项的问题,又用了-mpreferred-stack-boudery=4(按16字节对齐)还是没用。不知道是什么原因。
难道现在的GCC加了防溢出机制?请教个位高人。
头像
xhy
帖子: 3916
注册时间: 2005-12-28 1:16
系统: Ubuntu 12.10 X64
来自: 火星

#2

帖子 xhy » 2007-07-23 0:50

缓冲区溢出通常是通过覆盖函数的返回地址完成的
目前负债150多万
lifurong
帖子: 32
注册时间: 2006-10-24 0:00
联系:

#3

帖子 lifurong » 2007-07-23 12:05

这个问题估计这个社区很少人能回答,建议发到chinaunix
头像
laborer
帖子: 1016
注册时间: 2005-10-25 11:15
联系:

#4

帖子 laborer » 2007-07-23 13:17

给楼主一些提示

代码: 全选

$ man gcc
......
       -fstack-protector
           Emit extra code to check for buffer overflows, such as
           stack smashing attacks.  This is done by adding a guard
           variable to functions with vulnerable objects.  This
           includes functions that call alloca, and functions with
           buffers larger than 8 bytes.  The guards are initialized
           when a function is entered and then checked when the func‐
           tion exits.  If a guard check fails, an error message is
           printed and the program exits.

           NOTE: In Ubuntu 6.10 and 7.04 this option is enabled by
           default for C, C++, ObjC, ObjC++.
......

代码: 全选

$ cat test.c
#include <stdio.h> 
#include <string.h> 

int main(void) {
        char largebuff[]="1234512345123451234512345===ABCD"; 
        char smallbuff[16]; 
        strcpy(smallbuff,largebuff); 
        printf("smallbuff=\"%s\"\n", smallbuff);
        printf("largebuff=\"%s\"\n", largebuff);
}

代码: 全选

$ gcc -g -fno-stack-protector test.c; ./a.out
smallbuff="1234512345123451234512345===ABCD"
largebuff="234512345===ABCD"
hreiser@oakland:~$ killall -9 wife
police@oakland:~$ sudo find / -user hreiser
court@oakland:~$ sudo mv /home/hreiser /jail/
court@oakland:~$ sudo usermod -d /jail/hreiser -s "/usr/sbin/chroot /jail/" hreiser
ojjou
帖子: 16
注册时间: 2007-07-25 16:36

#5

帖子 ojjou » 2007-10-05 19:43

我最近也在弄这溢出....楼上的办法可行...可以去掉栈保护..但是还是有其它问题...我在7.04下发觉28个字节大了..好像是20还是24个...已经能够覆盖ebp了...但是还是不能改转向地址....原因是他在push ebp后还 push了ecx 结果这ecx里的地址还用来计算esp的指向....意思是连esp都覆盖了...代码走向乱了...我这么认为..结果就不能call (ebp+8)了..现在还在想解决方案.不知怎样才能不变ecx覆盖掉ebp..应该还是gcc的参数问题..郁闷中..不知楼主解决没有...解决也请教教我吧
回复