缓冲区溢出/溢出说明?

时间:2013-10-27 04:53:17

标签: c assembly x86 buffer-overflow

在给定的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);
}

主要功能的相应汇编代码是:

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变量似乎是错误的。

2 个答案:

答案 0 :(得分:4)

说明这是缓冲区溢出的示例:

function的局部变量(包括buffer1)与返回地址一起位于堆栈中,返回地址计算为超出buffer1的12个字节。这是缓冲区溢出的一个示例,因为写入超出buffer1的12个字节的地址是在buffer1的适当范围之外写入的。通过将返回地址替换为大于它的数字8,当function完成时,而不是像往常一样弹出函数调用之后返回语句(在这种情况下为x = 1;),返回地址将在8个字节之后(在printf语句中,在这种情况下)。

跳过x = 1;语句不是缓冲区溢出 - 这是修改返回地址的缓冲区溢出的影响。

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

另请参阅FrankH's careful reevaluation计算8作为添加到返回地址的正确偏移量,以实现跳过x = 1;的意图。他的发现与基于GDB的insecure.org source article分析相矛盾。 无论这个细节如何,关于如何使用缓冲区溢出来更改返回地址的解释仍然是相同的 - 这只是要写入溢出的内容的问题。

为了完整性,这里是基于GDB的insecure.org source article 分析:

  

我们所做的是将buffer1 []的地址加12。这个新的   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的作业。下一个   我们要执行的指令是0x8004b2。一点点数学   告诉我们距离是8个字节。

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

答案 1 :(得分:1)

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

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个字节,则保存的返回地址(根据你的反汇编,它是{ {1}} / main+0x24。添加8到两个intructions“跳到中间”,结果是垃圾 - 你跳过0x80004a8语句。