x86_32栈的调用结构

1 篇文章 0 订阅
下面将通过一段代码分析x86_32栈的使用方法,程序运行后不停的重新调用main函数重新运行。

原理:
In x86_32, When one function named ‘function_A’
call another function named ‘function_B’
the function_B’s stack look like below:
Parameter #N <—- N*4+4(%ebp)

Parameter #2 <—- 12(%ebp)
Parameter #1 <—- 8(%ebp)
Return Address <—- 4(%ebp)
Old %ebp <—- (%ebp)
Local Variable 1 <—- -4(%ebp)
Local Variable 2 <—- -8(%ebp)

So, We can’t use Paremeter or local variable get the address which store the
Return Address and change it.



#include <stdio.h>
void modify_return_address(int num);

int main(int argc, const char **argv)
{
    printf("%s %d\n", __func__, __LINE__);
    modify_return_address(1);
    printf("%s will return\n", __func__);
    return 0;
}
void modify_return_address(int num)
{
    int *return_address = (int *)((char *)(&num) - 4); 
    *return_address = (int )(main);
    return;     
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值