当前位置:网站首页>智能合约安全——selfdestruct攻击
智能合约安全——selfdestruct攻击
2022-07-28 00:50:00 【fingernft】
selfdestruct函数(自毁函数)由以太坊智能合约提供,用于销毁区块链上的合约系统。当合约执行自毁操作时,合约账户上剩余的以太币会发送给指定的目标,然后其存储和代码从状态中被移除。selfdestruct函数虽然能在紧急情况下帮助开发人员删除智能合约并将合约内的余额转移到指定的地址,但这一特性也被不法分子利用,使它成为了攻击手段。让我们来看个经典游戏“幸运7”的案例:
合约代码

漏洞分析
在“幸运7”游戏中,玩家每次向 EtherGame 合约中打入一个ETH,第七个成功打入ETH的玩家将成为 winner。winner 可以提取合约中的 7 个ETH。玩家每次玩游戏时都会调用 EtherGame.deposit 函数向合约中先打入一个ETH,随后函数会检查合约中的余额(balance)是否小于等于 7 ,只有合约中的余额小于等于 7 时才能继续否则将回滚。合约中的余额(balance)是通过 address(this).balance 取到的,这就意味着我们只要有办法在产生 winner 之前改变 EtherGame 合约中的余额让他等于 7 就会使该合约瘫痪。这样我们的攻击方向就明确了,只要我们强制给 EtherGame 合约打入一笔ETH让该合约中的余额大于7 这样后面的玩家将无法通过 EtherGame.deposit 的检查,从而使 EtherGame 合约瘫痪,永远无法产生 winner。但是 EtherGame.deposit 函数中存在验证:require(msg.value == 1 ether, "You can only send 1 Ether"),这里要求我们每次只能打一个ETH进去,所以通过正常路径是不可能一次向 EtherGame 打入大于 1 枚的ETH的,但是我们又需要打入大于 1 枚的ETH到 EtherGame 合约中,所以selfdestruct函数就登场了。
攻击合约
攻击者调用攻击函数attack()销毁合约并将合约中的余额强制转账到“幸运7”的游戏合约地址,制造出游戏合约查询余额address(this).balance大于7的情况,导致函数回退,无法正常进行游戏的情况,使得游戏合约瘫痪。
修复建议我们来分析一下攻击者的思路:利用selfdestruct函数强制转账给游戏地址,导致unit balance = address(this).balance查询出的balance的值大于7,诱发require(balance <= targetAmount, "Game is over");无法通过使得游戏合约瘫痪。我们不难发现攻击者是通过影响balance从而达到目的的,那么我们就让balance不受这个影响,在最初先定义balance,再让balance的值只受通过游戏规则内的正常途径的影响,这样就不会出现balance值异常的情况了。
修复的代码:

如果想了解更多的智能合约和区块链知识,欢迎到区块链交流社区CHAINPIP社区,一起交流学习~
边栏推荐
- Two ways for wechat applet to realize dynamic horizontal step bar
- Promise从入门到精通 (第1章 Promise的介绍和基本使用)
- 考研数学一元微分学证明题常见题型方法
- LeetCode 热题 HOT 100 -> 3. 无重复字符的最长子串
- Fiddler mobile packet capturing agent settings (for Huawei glory 60s)
- 53: Chapter 5: develop admin management service: 6: develop [admin administrator exit login, interface]; (one point: when we want to modify a value with a certain coding method, the new value should b
- 软考 --- 数据库(2)关系模型
- Typescript中类的使用
- 二叉树的遍历和性质
- 微信小程序实现动态横向步骤条的两种方式
猜你喜欢

Promise from introduction to mastery (Chapter 2 understanding and use of promise)

云原生爱好者周刊:Prometheus 架构演进之路

They are all talking about Devops. Do you really understand it?

ArcGIS:加载历史遥感影像

Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始

【愚公系列】2022年07月 Go教学课程 019-循环结构之for

Interviewer: are you sure redis is a single threaded process?

Unreal ue4.27 switchboard porting engine process

Sample imbalance - entry 0

产品解读丨MeterSphere UI测试模块的设计与分布式扩展
随机推荐
Zkrollup learning materials summary
53: Chapter 5: develop admin management service: 6: develop [admin administrator exit login, interface]; (one point: when we want to modify a value with a certain coding method, the new value should b
Shell regular and metacharacters
软考 --- 数据库(2)关系模型
Fluorite network, difficult to be a "lone brave"
Clear the cause of floating and six methods (solve the problem that floating affects the parent element and the global)
Software testing interview question: what do you think is the key to good test case design?
Principle and implementation of focal loss
Appium click operation sorting
Five basic data structures of redis
Codeworks round 807 (Div. 2) a-c problem solution
软件测试面试题:常见的 POST 提交数据方式
11-Django-基础篇-数据库操作
考研数学一元微分学证明题常见题型方法
小米网站主页面大模块——小模块+导航(浮动案例)
测试/开发程序员的级别“陷阱“,级别不是衡量单维度的能力......
Common modules of ros2 launch files
每条你收藏的资讯背后,都离不开TA
Unreal ue4.27 switchboard porting engine process
Behind every piece of information you collect, you can't live without TA