c - 在汇编级别的这个极简 C 代码中到底发生了什么?

标签 c assembly

我目前正在努力理解 Writing buffer overflow exploits - a tutorial for beginners .

C 代码,使用 cc -ggdb exploitable.c -o exploitable 编译

#include <stdio.h>

void exploitableFunction (void) {
    char small[30];
    gets (small);
    printf("%s\n", small);
}

main() {
    exploitableFunction();
    return 0;
}

好像有汇编代码

0x000000000040063b <+0>:    push   %rbp
0x000000000040063c <+1>:    mov    %rsp,%rbp
0x000000000040063f <+4>:    callq  0x4005f6 <exploitableFunction>
0x0000000000400644 <+9>:    mov    $0x0,%eax
0x0000000000400649 <+14>:   pop    %rbp
0x000000000040064a <+15>:   retq

我认为它执行以下操作,但我真的不确定,如果我是对的/什么是对的,我想听听有汇编代码经验的人的意见。

  • 40063b:将当前基指针寄存器中的地址放入栈段(这个寄存器是怎么初始化的?为什么要这样?)
  • 40063c:将栈指针寄存器中的值复制到基址指针寄存器中(为什么?)
  • 40063f:调用 exploitableFunction(在汇编中“调用”一个函数到底是什么意思?这里发生了什么?)
  • 400644:将地址$0x0的值复制到EAX寄存器
  • 400649:将栈顶的值(由%rsp中的值决定)复制到基址指针寄存器中(似乎由Assembler: Push / pop registers?确认)
  • 40064a:返回(操作系统使用 %EAX 中的内容作为返回码 - 所以我猜地址 $0x0 包含常量 0?或者那不是地址而是常量?)

最佳答案

40063b: Put the address which is currently in the base pointer register into the stack segment (How is this register initialized? Why is that done?)

您想保存基指针,因为它可能被调用函数使用。

40063c: Copy the value from the stack pointer register into the base pointer register (why?)

这为您提供了堆栈中的固定位置,其中可能包含函数的参数。它也可以用作任何局部变量的基地址。

40063f: Call exploitableFunction (What exactly does it mean to "call" a function in assembly? What happens here?)

“调用”就是将返回地址(下一条指令的地址)压栈,然后跳转到被调用函数的开头。

400644: Copy the value from the address $0x0 to the EAX register

它实际上是 return 语句中的值 0。

400649: Copy the value from the top of the stack (determined by the value in %rsp) into the base pointer register (seems to be confirmed by Assembler: Push / pop registers?)

这将恢复我们保存在顶部的基指针。调用函数可能会假设我们这样做。

40064a: Return (the OS uses what is in %EAX as return code - so I guess the address $0x0 contains the constant 0? Or is that not an address but the constant?)

它是 return 0 的常量。将 EAX 用于较小的返回值是一种常见的约定。

关于c - 在汇编级别的这个极简 C 代码中到底发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32357328/

相关文章:

c - 以编程方式检索 infiniband 设备 ip 地址

用于 C、C#、C++ 和 Java 的 C# 代码分析器。

c - C中的公式P(n) = (n!)(6^n)产生了一个很大的值

c++ - 如何在 C/C++ 中关闭特定客户端的套接字?

c - 我的 malloc 声明有什么问题?

assembly - JL 组装后测试

assembly - ISR 后程序不断返回同一行。 (总成 8086)

assembly - 在MIPS中,有符号加法、无符号加法、有符号减法和无符号减法有什么区别?

assembly - 什么是 MTMSREE PowerPC 操作?

c++ - 函数地址不是实际的代码地址