当前位置:网站首页>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/
边栏推荐
- RAID存储级别分类
- navicat创建连接 2002-can‘t connect to server on localhost(10061)且mysql服务已启动问题
- 【电子器件笔记6】三极管(BJT)参数和选型
- 小心 transmittable-thread-local 的这个坑
- In the idea to create a web project _idea deployment of the web project
- CNN经典模型汇总[通俗易懂]
- julia系列6:并行计算
- 【无标题】
- 在idea中创建web项目_idea部署web项目
- Locking and Concurrency Control (4)
猜你喜欢
持续集成(五)Jenkins配置父子job
[300+ selected big factory interview questions continue to share] Big data operation and maintenance sharp knife interview questions column (10)
Real-time data warehouse architecture evolution and selection
Red and blue against experience sharing: CS from kill posture
uniapp引入vantUI库
Mysql开启binlog
Navicat for mysql破解版安装
Mysql应用安装后找不到my.ini文件
开始使用 NVIDIA Jetson Orin 上的深度学习加速器
Redis进阶之路:深度解析Redis单线程架构,图文并茂不能再清晰了
随机推荐
双指针法 | leecode刷题笔记
互联网刚需岗位 前景一片大好?
1.NVIDIA Deepstream开发指南中文版--欢迎使用 DeepStream 文档
SQL语句基础
Inconsistency between oracle and mysql statement results
图像质量评价指标
Locking and concurrency control (a)
js实现改变原来对象中的键值对对应的值
Oracle 11 g rac finished patch, dbca new patches of SQL database also needs to perform?
Mysql——字符串函数
Nacos的基本配置
numpy的学习笔记
技术分享| 融合调度系统中的电子围栏功能说明
Navicat premium download and install 15 detailed tutorial
领导无线边缘AI的联合神经形态学习,具有较高的识别精度以及较低的能耗
什么是实时流引擎?
RAID存储级别分类
【电子器件笔记6】三极管(BJT)参数和选型
exness:欧元区经济意外向好,欧元震荡蓄势等待突破
CWE4.8:2022年危害最大的25种软件安全问题