
逆向x86/x64从汇编分析函数调用约定详解引言函数调用约定是程序运行时函数间传递参数、控制权和返回值的一套规则。在逆向工程中理解调用约定是分析二进制代码的基础。x86和x64架构的调用约定存在显著差异本文将深入剖析从汇编层面分析这些约定的原理并提供可运行的代码示例。## 1. 调用约定的核心要素调用约定主要解决三个问题- 参数如何传递寄存器/栈- 栈的清理责任调用者/被调用者- 返回值的存放位置常见的调用约定包括cdecl、stdcall、fastcallx86以及Microsoft x64和System V AMD64x64。## 2. x86架构调用约定分析### 2.1 cdecl约定C语言默认在cdecl中参数从右向左压入栈调用者负责清理栈。让我们通过汇编分析c// 示例代码cdecl_test.c#include stdio.hint __attribute__((cdecl)) add(int a, int b, int c) { return a b c;}int main() { int result add(1, 2, 3); printf(Result: %d\n, result); return 0;}编译并查看汇编使用gcc -S或objdump反汇编assembly; add函数的汇编ATT语法add: pushl %ebp movl %esp, %ebp movl 12(%ebp), %eax ; 加载b参数2 movl 8(%ebp), %edx ; 加载a参数1 addl %edx, %eax ; a b addl 16(%ebp), %eax ; c popl %ebp ret; main函数的汇编main: pushl $3 ; 参数c最后压栈 pushl $2 ; 参数b pushl $1 ; 参数a先压栈 call add addl $12, %esp ; 调用者清理栈3个int 12字节关键点- 参数从右向左压栈pushl $3-pushl $2-pushl $1- 调用者清理栈addl $12, %esp恢复栈指针- 返回值通过eax传递### 2.2 stdcall约定Windows API常见stdcall与cdecl类似但被调用者负责清理栈assembly; stdcall约定下的add函数add_std: pushl %ebp movl %esp, %ebp movl 12(%ebp), %eax addl 8(%ebp), %eax addl 16(%ebp), %eax popl %ebp ret $12 ; 被调用者清理ret指令后加12字节; 调用方式 pushl $3 pushl $2 pushl $1 call add_std ; 无需清理栈因为add内部已经ret $12区别ret $12指令在返回前自动弹出12字节3个参数调用者无需addl。### 2.3 fastcall约定寄存器传参fastcall使用ecx和edx传递前两个参数其余通过栈传递c// fastcall_test.c#include stdio.hint __attribute__((fastcall)) fast_add(int a, int b, int c) { return a b c;}int main() { int result fast_add(10, 20, 30); printf(Result: %d\n, result); return 0;}汇编分析assemblyfast_add: pushl %ebp movl %esp, %ebp pushl %ebx movl %ecx, %ebx ; 参数a在ecx movl %edx, %eax ; 参数b在edx addl %ebx, %eax ; a b addl 8(%ebp), %eax ; 参数c在栈上 popl %ebx popl %ebp retmain: pushl $30 ; 第三个参数压栈 movl $20, %edx ; 第二个参数在edx movl $10, %ecx ; 第一个参数在ecx call fast_add addl $4, %esp ; 只清理栈上的一个参数## 3. x64架构调用约定### 3.1 Microsoft x64约定Windowsx64下参数通过寄存器传递rcx, rdx, r8, r9前4个整数剩余通过栈传递。调用者负责预留影子空间shadow space。c// x64_test.c#include stdio.hint add_x64(int a, int b, int c, int d, int e) { return a b c d e;}int main() { int result add_x64(1, 2, 3, 4, 5); printf(Result: %d\n, result); return 0;}x64汇编Windows环境assemblyadd_x64: push rbp mov rbp, rsp ; rcx a, rdx b, r8 c, r9 d ; 第五个参数e在栈上rbp0x30因为影子空间占用32字节 mov eax, ecx add eax, edx add eax, r8d add eax, r9d add eax, [rbp0x30] ; 从栈加载e pop rbp retmain: sub rsp, 40 ; 预留影子空间32字节 对齐 mov dword [rsp32], 5 ; 第五个参数在栈上 mov r9d, 4 ; 第四个参数 mov r8d, 3 ; 第三个参数 mov edx, 2 ; 第二个参数 mov ecx, 1 ; 第一个参数 call add_x64 add rsp, 40 ; 返回值在eax### 3.2 System V AMD64约定Linux/macOS前6个整数参数通过rdi, rsi, rdx, rcx, r8, r9传递无需影子空间assembly; Linux x64汇编add_linux: ; rdi a, rsi b, rdx c, rcx d ; 第五个参数e在栈上 mov eax, edi add eax, esi add eax, edx add eax, ecx mov rcx, [rsp8] ; 第五个参数在rsp8返回地址上方 add eax, ecx retmain: push 5 ; 第五个参数 mov ecx, 4 ; 第四个参数 mov edx, 3 ; 第三个参数 mov esi, 2 ; 第二个参数 mov edi, 1 ; 第一个参数 call add_linux add rsp, 8 ; 清理栈上的参数## 4. 逆向分析实战识别调用约定在逆向二进制代码时可以通过以下特征识别调用约定### 4.1 分析栈清理方式python# 模拟逆向分析脚本def analyze_calling_convention(assembly_lines): 分析调用约定 insns [line.strip() for line in assembly_lines] # 检查call指令后是否有add esp, X has_caller_cleanup False for line in insns: if call in line and ret in line.split(,)[-1]: # 被调用者清理stdcall return stdcall if add in line and esp in line and call in insns[insns.index(line)-1]: has_caller_cleanup True if has_caller_cleanup: return cdecl return unknown# 测试cdecl_code [ push 3, push 2, push 1, call add, add esp, 12 # 调用者清理]print(analyze_calling_convention(cdecl_code)) # 输出: cdecl### 4.2 识别寄存器传参pythondef detect_fastcall(assembly): 检测fastcall检查ecx/edx是否用于传参 ecx_used False edx_used False for line in assembly: if mov in line and ecx in line and push not in line: ecx_used True if mov in line and edx in line and push not in line: edx_used True return ecx_used and edx_used# x86 fastcall示例fastcall_code [ push 30, # 第三个参数 mov edx, 20, # 第二个参数 mov ecx, 10, # 第一个参数 call fast_add]print(detect_fastcall(fastcall_code)) # 输出: True## 5. 调用约定对比总结| 特性 | cdecl (x86) | stdcall (x86) | fastcall (x86) | MS x64 | SysV x64 ||------|-------------|---------------|----------------|--------|----------|| 参数传递 | 栈右→左 | 栈右→左 | ecx/edx 栈 | rcx,rdx,r8,r9 栈 | rdi,rsi,rdx,rcx,r8,r9 栈 || 栈清理 | 调用者 | 被调用者 | 调用者栈部分 | 调用者 | 调用者 || 返回值 | eax | eax | eax | rax | rax || 影子空间 | 无 | 无 | 无 | 32字节 | 无 |## 总结函数调用约定是逆向工程中必须掌握的基础知识。通过分析汇编代码我们可以从三个维度识别调用约定参数传递方式寄存器vs栈、栈清理责任方调用者vs被调用者、以及影子空间的使用仅x64 Windows。在实际逆向中cdecl和stdcall通过ret指令后是否带立即数区分fastcall通过检查ecx/edx的初始赋值识别x64下则需观察寄存器的使用顺序和栈布局。掌握这些技术能够帮助我们准确理解二进制代码的函数交互逻辑为后续的逆向分析打下坚实基础。