当前位置:网站首页>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/
边栏推荐
猜你喜欢
随机推荐
Coordinate knowledge in digital twin campus scenarios
DSP-ADAU1452输入通道配置
MySQL常见函数
锁定和并发控制(四)
编写一个油猴脚本
小程序实现手写左右翻页和动态修改横向滚动条位置
Numpy those things
金仓数据库KingbaseES安全指南--6.13. 关于身份验证的常见问题
常用软件静默安装参数
融云「 IM 进阶实战高手课」系列直播上线
Nacos面试题
【电子器件笔记7】MOS管参数和选型
Inconsistency between oracle and mysql statement results
如何为项目匹配资源技能和要求?
一文搞懂│php 中的 DI 依赖注入
总结:不同语言比较总结
金仓数据库KingbaseES安全指南--6.12. BSD身份验证
AI智能剪辑,仅需2秒一键提取精彩片段
Locking and concurrency control (a)
数字孪生园区场景中的坐标知识







![牛客的课程订单分析[分组统计时如何取指定行字段?]](/img/9d/d27b8372ae5e5f1c7b9e6f5e9bb1f6.png)

