当前位置:网站首页>Smart contract security -- selfdestroy attack
Smart contract security -- selfdestroy attack
2022-07-28 02:26:00 【fingernft】
selfdestruct function ( Self destruct function ) Provided by Ethereum smart contract , Used to destroy the contract system on the blockchain . When the contract performs self destruction , The remaining etheric currency in the contract account will be sent to the designated target , Then its storage and code are removed from the State .selfdestruct Function can help developers delete smart contracts and transfer the balance in the contract to the specified address in case of emergency , But this feature is also used by criminals , Make it a means of attack . Let's watch a classic game “ lucky 7” The case of :
Contract code

Vulnerability analysis
stay “ lucky 7” In the game , Every time a player EtherGame Enter a ETH, The seventh one successfully entered ETH Of players will become winner.winner You can withdraw 7 individual ETH. Players will call every time they play a game EtherGame.deposit Function first enters a ETH, The function then checks the balance in the contract (balance) Less than or equal to 7 , Only the balance in the contract is less than or equal to 7 Can continue, otherwise it will be rolled back . The balance in the contract (balance) It's through address(this).balance Pick up to , This means that as long as we have a way to produce winner Change before EtherGame The balance in the contract makes him equal to 7 It will paralyze the contract . So our attack direction is clear , As long as we force EtherGame A sum of money was entered into the contract ETH Let the balance in the contract be greater than 7 In this way, the players behind will not be able to pass EtherGame.deposit The inspection of , So that EtherGame Contract paralysis , Never produce winner. however EtherGame.deposit There is validation in the function :require(msg.value == 1 ether, "You can only send 1 Ether"), It is required that we can only play one at a time ETH go in , Therefore, it is impossible to go to EtherGame Drive greater than 1 Of ETH Of , But we need to break into more than 1 Of ETH To EtherGame In contract , therefore selfdestruct The function comes on stage .
Attack contracts
The attacker calls the attack function attack() Destroy the contract and forcibly transfer the balance in the contract to “ lucky 7” The address of the game contract , Create a game contract to query the balance address(this).balance Greater than 7 The situation of , Cause the function to fall back , Unable to play normally , Paralyze the game contract .
Repair suggestions Let's analyze the attacker's thinking : utilize selfdestruct Function to force the transfer to the game address , Lead to unit balance = address(this).balance Found out balance The value is greater than 7, induce require(balance <= targetAmount, "Game is over"); Unable to paralyze the game contract through . It is not difficult for us to find that the attacker affects balance So as to achieve the goal , Then let's balance Not affected by this , First define balance, let balance The value of is only affected by the normal way through the rules of the game , So it won't show up balance The value is abnormal .
Fixed code :

If you want to know more about smart contracts and blockchain , Welcome to blockchain communication community CHAINPIP Community , Exchange and study together ~
Community address :https://www.chainpip.com/
边栏推荐
- Talk to ye Yanxiu, an atlassian certification expert: where should Chinese users go when atlassian products enter the post server era?
- 产品解读丨MeterSphere UI测试模块的设计与分布式扩展
- cn+dt
- go 学习02 基础知识
- Promise from introduction to mastery (Chapter 2 understanding and use of promise)
- Promise from getting started to mastering (Chapter 3: customize (handwriting) promise)
- Leetcode hot topic Hot 100 - > 2. Add two numbers
- 54: Chapter 5: develop admin management services: 7: face warehousing process; Face login process; The browser turns on the video debugging mode (so that the camera can also be turned on in the case o
- [Yugong series] July 2022 go teaching course 019 - for circular structure
- 【愚公系列】2022年07月 Go教学课程 019-循环结构之for
猜你喜欢

【愚公系列】2022年07月 Go教学课程 019-循环结构之for
![[Yugong series] use of tabby integrated terminal in July 2022](/img/df/bf01fc77ae019200d1bf57be783cb9.png)
[Yugong series] use of tabby integrated terminal in July 2022

使用BigDecimal类型应该避免哪些问题?(荣耀典藏版)

产品解读丨MeterSphere UI测试模块的设计与分布式扩展

The cooperation between starfish OS and metabell is just the beginning

新零售业态下,零售电商RPA助力重塑增长

Ceresdao: new endorsement of ventures Dao

【数据库数据恢复】SQL Server数据库磁盘空间不足的数据恢复案例

What problems should be avoided when using BigDecimal type? (glory Collection Edition)

A lock faster than read-write lock. Don't get to know it quickly
随机推荐
Codeworks round 807 (Div. 2) a-c problem solution
WMS you don't know
【网站搭建】使用acme.sh更新ssl证书:将zerossl改为letsencrypt
Class notes (5) (1) - 593. Binary search
Record a production deadlock
MYSQL解决死锁之路 - 常见 SQL 语句的加锁分析
了解加密行业的“下一个大趋势”——Ventures DAO
【HCIP】路由策略、策略路由
[advanced ROS chapter] Lecture 10 gadf integrated simulation process and examples based on gazebo
mysql创建存储过程---------[HY000][1418] This function has none of DETERMINISTIC, NO SQL
Codeworks round 810 (Div. 2) a~c problem solution
Flex layout learning completed on PC side
Flume(5个demo轻松入门)
新零售业态下,零售电商RPA助力重塑增长
"Risking your life to upload" proe/creo product structure design - seam and buckle
MySQL的pymysql操作
Digital empowerment and innovation in the future: hese eredi appears at the 5th Digital China Construction Summit
[website construction] update SSL certificate with acme.sh: change zerossl to letsencrypt
This operation may not be worth money, but it is worth learning | [batch cutting of pictures]
cn+dt