Dillema缓冲区溢出

Rus*_*kov 6 c assembly buffer-overflow

我正在玩一个堆栈溢出示例.此示例如下所示:

 void return_input (void){ 
    char array[30];    
    gets (array); 
    printf("%s\n", array); 

 }

 main() { 
    return_input();    
    return 0;    
 }
Run Code Online (Sandbox Code Playgroud)

所有这些代码都在名为overflow.c的文件中.我们有一个名为return_input的易受攻击的函数,特别是它的30字节char数组.我编译它并在gdb中打开易受攻击的函数并得到以下输出:

 (gdb) disas return_input
 0x08048464 <+0>:   push   %ebp
 0x08048465 <+1>:   mov    %esp,%ebp
 0x08048467 <+3>:   sub    $0x48,%esp
 0x0804846a <+6>:   mov    %gs:0x14,%eax
 0x08048470 <+12>:  mov    %eax,-0xc(%ebp)
 0x08048473 <+15>:  xor    %eax,%eax
 0x08048475 <+17>:  lea    -0x2a(%ebp),%eax
 0x08048478 <+20>:  mov    %eax,(%esp)
 0x0804847b <+23>:  call   0x8048360 <gets@plt>
 0x08048480 <+28>:  lea    -0x2a(%ebp),%eax
 0x08048483 <+31>:  mov    %eax,(%esp)
 0x08048486 <+34>:  call   0x8048380 <puts@plt>
 0x0804848b <+39>:  mov    -0xc(%ebp),%eax
 0x0804848e <+42>:  xor    %gs:0x14,%eax
 0x08048495 <+49>:  je     0x804849c <return_input+56>
 0x08048497 <+51>:  call   0x8048370 <__stack_chk_fail@plt>
 0x0804849c <+56>:  leave  
 0x0804849d <+57>:  ret    
 End of assembler dump.
Run Code Online (Sandbox Code Playgroud)

正如您在函数序言中看到的那样,我们在堆栈上为局部变量保留了hex48(dec 72)字节.首先,我试图找到易受攻击的数组在堆栈上启动的地址.我认为这是-0x2a(%ebp),我是对的吗?Hex2a是小数点后42位.据我所知,这意味着我们可以在开始覆盖保存在堆栈上的EBP之前安全地写入42个字节.但是当我运行这个例子时,只需要37个字节即可获得分段错误:

rustam@rustam-laptop:~/temp/ELF_reader$ ./overflow
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)

如何将37个字节足以溢出缓冲区?如果我们的本地字符数组是保存的EBP中的-42字节

rum*_*pel 6

没有看到函数的整个反汇编就很难分辨.

但是,我的猜测是存储在-0xc(%ebp)的%gs:0x14可能是你的堆栈canary,如果检测到堆栈coruption会导致干净的退出.所以这个值存储在-0xc(%ebp),这意味着你的缓冲区实际上只有30个字节大,后面跟着是什么.