2
#include<stdio.h>
#include<string.h>
int main(int argc, char ** argv)
{
    char buffer[500];
    strcpy(buffer, argv[1]);

    return 0;
}

我可以使用编译这个程序gcc -m32 -fno-stack-protector -z execstack -fno-pie -no-pie -g -o vuln vuln.c

在使用调试器反汇编主要功能时,我将其作为输出:

Dump of assembler code for function main:
   0x0804840b <+0>:     lea    0x4(%esp),%ecx
   0x0804840f <+4>:     and    $0xfffffff0,%esp
   0x08048412 <+7>:     pushl  -0x4(%ecx)
   0x08048415 <+10>:    push   %ebp
   0x08048416 <+11>:    mov    %esp,%ebp
   0x08048418 <+13>:    push   %ecx
   0x08048419 <+14>:    sub    $0x204,%esp
   0x0804841f <+20>:    mov    %ecx,%eax
   0x08048421 <+22>:    mov    0x4(%eax),%eax
   0x08048424 <+25>:    add    $0x4,%eax
   0x08048427 <+28>:    mov    (%eax),%eax
   0x08048429 <+30>:    sub    $0x8,%esp
   0x0804842c <+33>:    push   %eax
   0x0804842d <+34>:    lea    -0x1fc(%ebp),%eax
   0x08048433 <+40>:    push   %eax
   0x08048434 <+41>:    call   0x80482e0 <strcpy@plt>
   0x08048439 <+46>:    add    $0x10,%esp
   0x0804843c <+49>:    mov    $0x0,%eax
   0x08048441 <+54>:    mov    -0x4(%ebp),%ecx
   0x08048444 <+57>:    leave  
   0x08048445 <+58>:    lea    -0x4(%ecx),%esp
   0x08048448 <+61>:    ret
End of assembler dump.

GCC 版本:6.5.0
操作系统:Ubuntu 16.04
GDB 版本:7.11.1

我所指的教程显示了这个汇编代码:

Dump of assembler code for function main:
   0x080483fb <+0>:     push   %ebp
   0x080483fc <+1>:     mov    %esp,%ebp
   0x080483fe <+3>:     sub    $0x1f4,%esp
   0x08048404 <+9>:     mov    0xc(%ebp),%eax
   0x08048407 <+12>:    add    $0x4,%eax
   0x0804840a <+15>:    mov    (%eax),%eax
   0x0804840c <+17>:    push   %eax
   0x0804840d <+18>:    lea    -0x1f4(%ebp),%eax
   0x08048413 <+24>:    push   %eax
   0x08048414 <+25>:    call   0x80482d0 <strcpy@plt>
   0x08048419 <+30>:    add    $0x8,%esp
   0x0804841c <+33>:    mov    $0x0,%eax
   0x08048421 <+38>:    leave
   0x08048422 <+39>:    ret
End of assembler dump.

我有以下问题:
如何获得教程中提到的完全相同的汇编代码转储?
输出的差异似乎是因为ecx寄存器。该寄存器有什么作用,为什么它不是教程汇编代码的一部分?在 main 函数中,我构造了大小为十六进制的
缓冲区数组,这就是为什么教程的汇编代码是从 esp 寄存器中减去的原因,但我的汇编代码是减去十进制的。我无法理解这一点。5001f41f4204516

编辑:如评论中所述,如果我添加-mpreferred-stack-boundary=2到编译器标志,那么我会得到与教程相同的汇编代码。为什么?

4

0 回答 0