Section outline

  • 要了解如何进行 Return-to-libc 攻击,我们需要理解堆栈的工作原理。 我们使用一个小型 C 程序来理解函数调用对堆栈的影响。更详细的解释可以在SEED 书籍和 SEED 课程中找到。

    #include<stdio.h>
    void foo(int x)
    {
      printf("Hello world: %d\n", x);
    }
    
    int main()
    {
      foo(1);
      return 0;
    }

    我们可以使用 "gcc -m32 -S foobar.c" 将这个程序编译成汇编代码。 生成的文件 foobar.s 将如下所示:

    ......
      8 foo:
      9         pushl   %ebp
     10         movl    %esp, %ebp
     11         subl    $8, %esp
     12         movl    8(%ebp), %eax   
     13         movl    %eax, 4(%esp)
     14         movl    $.LC0, (%esp)
     15         call    printf
     16         leave
     17         ret
        ......
     21 main:
     22         leal    4(%esp), %ecx
     23         andl    $-16, %esp
     24         pushl   -4(%ecx)
     25         pushl   %ebp
     26         movl    %esp, %ebp
     27         pushl   %ecx
     28         subl    $4, %esp
     29         movl    $1, (%esp)
     30         call    foo
     31         movl    $0, %eax
     32         addl    $4, %esp
     33         popl    %ecx
     34         popl    %ebp
     35         leal    -4(%ecx), %esp
     36         ret