【问题标题】:Buffer overflow/overrun explanation?缓冲区溢出/溢出解释?
【发布时间】:2013-11-06 00:59:57
【问题描述】:

在给定的 url 中给出了这个函数: http://insecure.org/stf/smashstack.html

void function(int a, int b, int c) {
   char buffer1[5];
   char buffer2[10];
   int *ret;

   ret = buffer1 + 12;
   (*ret) += 8;
}

void main() {
  int x;

  x = 0;
  function(1,2,3);
  x = 1;
  printf("%d\n",x);
}

main函数对应的汇编代码为:

Dump of assembler code for function main:
0x8000490 <main>:       pushl  %ebp
0x8000491 <main+1>:     movl   %esp,%ebp
0x8000493 <main+3>:     subl   $0x4,%esp
0x8000496 <main+6>:     movl   $0x0,0xfffffffc(%ebp)
0x800049d <main+13>:    pushl  $0x3
0x800049f <main+15>:    pushl  $0x2
0x80004a1 <main+17>:    pushl  $0x1
0x80004a3 <main+19>:    call   0x8000470 <function>
0x80004a8 <main+24>:    addl   $0xc,%esp
0x80004ab <main+27>:    movl   $0x1,0xfffffffc(%ebp)
0x80004b2 <main+34>:    movl   0xfffffffc(%ebp),%eax
0x80004b5 <main+37>:    pushl  %eax
0x80004b6 <main+38>:    pushl  $0x80004f8
0x80004bb <main+43>:    call   0x8000378 <printf>
0x80004c0 <main+48>:    addl   $0x8,%esp
0x80004c3 <main+51>:    movl   %ebp,%esp
0x80004c5 <main+53>:    popl   %ebp
0x80004c6 <main+54>:    ret
0x80004c7 <main+55>:    nop

在变量ret 中,它们将ret 指向要运行的下一条指令的地址。我无法理解仅将下一条指令保留在ret 变量中,程序将如何跳转到下一个位置? 我知道缓冲区溢出是如何工作的,但是通过更改ret 变量,这是如何进行缓冲区溢出的? 即使考虑到这是一个虚拟程序,只是为了让我们了解缓冲区溢出的工作原理,更改 ret 变量似乎也是错误的。

【问题讨论】:

    标签: c assembly x86 buffer-overflow


    【解决方案1】:

    解释这是一个缓冲区溢出的例子:

    function 的局部变量,包括buffer1,连同返回地址一起在栈上,计算为比buffer1 多12 个字节。这是缓冲区溢出的示例,因为写入超出buffer1 12 个字节的地址超出了buffer1 的正确边界。当function完成时,通过将返回地址替换为比原来大的数字8,而不是像往常一样弹出返回到函数调用之后的语句(在这种情况下为x = 1;),返回地址将是8 个字节后(在这种情况下,在 printf 语句中)。

    跳过x = 1; 语句不是缓冲区溢出,而是缓冲区溢出修改了返回地址。

    注意计算 8 作为跳过x = 1; 语句的正确偏移量

    另见FrankH's careful reevaluation 的计算 8 作为适当的偏移量添加到返回地址以实现跳过x = 1; 的意图。他的发现与基于 GDB 的insecure.org source article 分析相矛盾。 不管这个细节如何,关于如何使用缓冲区溢出来更改返回地址的解释保持不变——只是在溢出中写入什么的问题。

    为了完整起见,这里是对insecure.org source article基于GDB的分析

    我们所做的是将 12 添加到 buffer1[] 的地址。这个新 address 是存储返回地址的位置。我们想跳过通行证 printf 调用的分配。我们怎么知道要加 8 退货地址?我们先用了一个测试值(例如1),编译 程序,然后启动 gdb:

    [aleph1]$ gdb example3
    GDB is free software and you are welcome to distribute copies of it
     under certain conditions; type "show copying" to see the conditions.
    There is absolutely no warranty for GDB; type "show warranty" for details.
    GDB 4.15 (i586-unknown-linux), Copyright 1995 Free Software Foundation, Inc...
    (no debugging symbols found)...
    (gdb) disassemble main
    Dump of assembler code for function main:
    0x8000490 <main>:       pushl  %ebp
    0x8000491 <main+1>:     movl   %esp,%ebp
    0x8000493 <main+3>:     subl   $0x4,%esp
    0x8000496 <main+6>:     movl   $0x0,0xfffffffc(%ebp)
    0x800049d <main+13>:    pushl  $0x3
    0x800049f <main+15>:    pushl  $0x2
    0x80004a1 <main+17>:    pushl  $0x1
    0x80004a3 <main+19>:    call   0x8000470 <function>
    0x80004a8 <main+24>:    addl   $0xc,%esp
    0x80004ab <main+27>:    movl   $0x1,0xfffffffc(%ebp)
    0x80004b2 <main+34>:    movl   0xfffffffc(%ebp),%eax
    0x80004b5 <main+37>:    pushl  %eax
    0x80004b6 <main+38>:    pushl  $0x80004f8
    0x80004bb <main+43>:    call   0x8000378 <printf>
    0x80004c0 <main+48>:    addl   $0x8,%esp
    0x80004c3 <main+51>:    movl   %ebp,%esp
    0x80004c5 <main+53>:    popl   %ebp
    0x80004c6 <main+54>:    ret
    0x80004c7 <main+55>:    nop
    

    我们可以看到调用function()的时候RET是0x8004a8, 我们想跳过 0x80004ab 处的分配。下一个 我们要执行的指令是 at 0x8004b2。一点数学 告诉我们距离是 8 个字节。

    稍微好一点的数学告诉我们,距离是 0x8004a8 - 0x8004b2 = 0xA 或 10 个字节,而不是 8 个字节。

    【讨论】:

      【解决方案2】:

      堆栈上的布局是这样的(地址向下 - 随着堆栈的增长):

      buffer + ...       value found       description
      =================================================================================
      +24                3                 # from main,     pushl $0x3
      +20                2                 # from main,     pushl $0x2
      +16                1                 # from main,     pushl $0x1
      +12                <main+24>         # from main,     call  0x8000470 <function>
      +8                 <frameptr main>   # from function, pushl %ebp
      +4  %ebp(function) padding (3 bytes) # ABI - compiler will not _pack_ vars
      +0                 buffer[5];
      ...                buffer1[12];      # might be optimized out (unused)
      ...                int *ret          # might be optimized out (reg used instead)
      

      棘手的是buffer 从一个四字节对齐的地址开始,即使它的大小不是四字节的倍数。 “有效大小”是 8 个字节,所以如果你在它的开头添加 8 个字节,你会找到保存的帧指针,如果你再往下 4 个字节,保存的返回地址(根据你的反汇编,它是 @ 987654323@ / 0x80004a8。添加 8 会跳转到两个指令的“中间”,结果是垃圾 - 您不会跳过 x = 1 语句。

      【讨论】:

      • 不错的收获。更新了我的答案以记录您发现的错误。 +1
      猜你喜欢
      • 1970-01-01
      • 2010-11-11
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      • 2015-07-07
      • 2012-02-05
      相关资源
      最近更新 更多