当前位置:网站首页>Smart Contract Security - delegatecall (1)
Smart Contract Security - delegatecall (1)
2022-08-02 19:05:00 【fingernft】
In the previous content, you learned that storage uses slots to store data.The delegatecall function has an interesting feature: when using the delegatecall function to make an external call that involves the modification of the storage variable, it is modified according to the slot position rather than the variable name.
For example: Contract A

Contract B

When contract B calls the testDelegatecall() function, the value of address c of contract B will become the address of contract A, while address a will remain unchanged.Because the function test() of contract A changes the value of slot slot1, when running in contract B, it also changes the value of slot slot1, that is, the value of address c.
Target Contract

Vulnerability Analysis
We can see that there are two contracts. There is only one pwn function in the Lib contract to modify the owner of the contract. There is a fallback function in the HackMe contract. The content of the fallback function is to use delegatecall to call the function in the Lib contract.We need to use HackMe.fallback() to trigger the delegatecall function to call Lib.pwn() to change the owner in the HackMe contract to ourselves.
Attack the contract

Now let's take a look at the logic of the entire attack: 1. The attacker calls attack() to launch an attack, and the attack function first calls HackMe.pwn(); 2. There is no pwn function in the HackMe contract, and HackMe is triggered at this time.fallback(); 3.HackMe.fallback() uses deldegatecall to call the function in the Lib contract, the function name is msg.data, which is "pwn()", and the Lib contract happens to have a function named pwn, so it is called in HackMeThe pwn function is run in the contract; 4. The pwn function modifies the value of the slot0 position (that is, the owner of the HackMe contract) to msg.sender (that is, the attacker), which eventually causes the owner of the HackMe contract to become an attacker.Suggested fixes
- When using delegatecall, it should be noted that the address of the called contract cannot be controlled;
- In a more complex contract environment, you need to pay attention to the declaration order and storage location of variables.Because when using delegatecall to make external calls, the data stored in the corresponding slot of the contract will be modified according to the data structure of the called contract, which may cause unexpected variable coverage when the data structure changes.
If you want to know more about smart contracts and blockchain knowledge, welcome to the blockchain exchange community CHAINPIP community to communicate and learn together~Community address: https://www.chainpip.com/
边栏推荐
猜你喜欢

Mysql应用安装后找不到my.ini文件

分类实验报告作业

【电子器件笔记6】三极管(BJT)参数和选型

【Redis】连接报错:Could not connect to Redis at 127.0.0.1:6379: Connection refused

Continuous integration (4) Jenkins configuration alarm mechanism

【genius_platform软件平台开发】第七十五讲:YUY2转RGB24实现源码

持续集成(五)Jenkins配置父子job

【二】TS基本类型

MySQL常见面试题汇总(建议收藏!!!)

开始使用 NVIDIA Jetson Orin 上的深度学习加速器
随机推荐
Locking and Concurrency Control (4)
When Oracle analyzes the archive log content, it finds many nulls?
jar包应用的简单启停脚本
融云「 IM 进阶实战高手课」系列直播上线
CNN经典模型汇总[通俗易懂]
如何为项目匹配资源技能和要求?
Gartner released, annual Challenger!
解析并执行 shell 命令
Default username and password (SQL)
numpy的学习笔记
Redis进阶之路:深度解析Redis单线程架构,图文并茂不能再清晰了
持续交付(一)JenkinsAPI接口调用
Special Variables (SQL)
在idea中创建web项目_idea部署web项目
Coordinate knowledge in digital twin campus scenarios
使用 LaunchPad 上的 TAO 工具包体验轻松创建 AI 模型
关于我用iVX沉浸式体验了一把0代码项目创建
【二】通过props进行传值,子页面多种方式接收
领导无线边缘AI的联合神经形态学习,具有较高的识别精度以及较低的能耗
什么是实时流引擎?