c - 了解如何将参数传递给 Assembly 中的函数

标签 c assembly

我正在读这篇文章writeup关于如何执行 ret2libc 漏洞利用。它指出传递给函数的参数存储在 ebp+8 中。

现在,如果我编写一个简单的 C 程序

#include <stdlib.h>

int main() {
  system("/bin/sh");
}

并编译它: gcc -m32 -o test_sh test_sh.c

并查看它的反汇编 objdump -d -M intel test_sh

0804840b <main>:
 804840b:       8d 4c 24 04             lea    ecx,[esp+0x4]
 804840f:       83 e4 f0                and    esp,0xfffffff0
 8048412:       ff 71 fc                push   DWORD PTR [ecx-0x4]
 8048415:       55                      push   ebp
 8048416:       89 e5                   mov    ebp,esp
 8048418:       51                      push   ecx
 8048419:       83 ec 04                sub    esp,0x4
 804841c:       83 ec 0c                sub    esp,0xc
 804841f:       68 c4 84 04 08          push   0x80484c4
 8048424:       e8 b7 fe ff ff          call   80482e0 <system@plt>
 8048429:       83 c4 10                add    esp,0x10
 804842c:       b8 00 00 00 00          mov    eax,0x0
 8048431:       8b 4d fc                mov    ecx,DWORD PTR [ebp-0x4]
 8048434:       c9                      leave  
 8048435:       8d 61 fc                lea    esp,[ecx-0x4]
 8048438:       c3                      ret    
 8048439:       66 90                   xchg   ax,ax
 804843b:       66 90                   xchg   ax,ax
 804843d:       66 90                   xchg   ax,ax
 804843f:       90                      nop

线路

804841f:       68 c4 84 04 08          push   0x80484c4

将字符串“/bin/sh”的地址压入堆栈。之后立即调用 system@plt 函数。那么如何从上面的输出到达 ebp+8 呢?

帮助将不胜感激!

最佳答案

The arguments passed to a function are stored at ebp+8.

这是从被调用函数的角度来看的,而不是从调用函数的角度来看的。调用函数在 ebp+8 处有自己的参数,并且您的 main() 不使用它的任何参数,因此,您看不到任何 ebp+8 在你的 main() 中。

您可以看到 ebp+8 的使用方式如下:

  • 尝试编写带有参数的第二个函数,并从 main() 调用它,而不是调用 system()。您仍然不会在 main() 中看到任何 ebp+8 的使用,但您会看到它在第二个函数中使用。

  • 尝试声明您的 main() 以接受其 char** argv 参数,然后尝试将 argv[0] 发送到printf().

关于c - 了解如何将参数传递给 Assembly 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43132933/

相关文章:

c - 键入意外的输入会导致循环失败 (C)

c - fscanf 变量设置不正确。可能是 realloc()

math - 汇编中的算术运算

assembly - 寄存器有默认值吗?

c - 使用 c 和 do-while 循环将值存储在结构对象中

c - 有没有办法告诉C永远不要动态分配内存?

c++ - 将指针传递给结构

assembly - MIPS - assembly BEQ 命令

windows-mobile - 获取 ARM 汇编中的 PC 值

assembly - 为什么我的代码总是在第 2 行出现比较失败,我该如何解决这个问题?