Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
AmazingAng committed Nov 10, 2022
1 parent b00a561 commit 1aa0680
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@

**S06:签名重放**[代码](https://github.com/AmazingAng/WTFSolidity/blob/main/S06_SignatureReplay) | [文章](https://github.com/AmazingAng/WTFSolidity/blob/main/S06_SignatureReplay/readme.md) | [Mirror](https://mirror.xyz/ninjak.eth/hHQO1EGe0vapiejyIrP8bcuPm50Djlse2MareeHLDwE)

**S07:坏随机数**[代码](https://github.com/AmazingAng/WTFSolidity/blob/main/S07_BadRandomness) | [文章](https://github.com/AmazingAng/WTFSolidity/blob/main/S07_BadRandomness/readme.md) | [Mirror](https://mirror.xyz/ninjak.eth/5NQbE-url7o8OV5UtpTPEkMEVwTZqdDIRYDMiG1bECw)

## 主题
### `工具`
**第1讲:Remix, 最易用的Solidity IDE**[代码](https://github.com/AmazingAng/WTFSolidity/tree/main/Topics/Tools/TOOL01_Remix)】 【[文章](https://mirror.xyz/ninjak.eth/dSYXG9zF_Vclw58Bgcvsv6HSA0SU6pmBoYLFwLAgVbU)
Expand Down
4 changes: 3 additions & 1 deletion S07_BadRandomness/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contract BadRandomness is ERC721 {

它有一个主要的铸造函数 `luckyMint()`,用户调用时输入一个 `0-99` 的数字,如果和链上生成的伪随机数 `randomNumber` 相等,即可铸造幸运 NFT。伪随机数使用 `blockhash``block.timestamp` 声称。这个漏洞在于用户可以完美预测生成的随机数并铸造NFT。

下面我们写个攻击合约 `Attack.sol`在攻击函数 `attackMint()`中的参数为 `BadRandomness`合约地址。在函数中,我们计算了随机数 `luckyNumber`,然后将它作为参数输入到 `luckyMint()` 函数完成攻击。由于`attackMint()``luckyMint()`将在同一个区块中调用,`blockhash``block.timestamp`是相同的,利用他们生成的随机数也相同。
下面我们写个攻击合约 `Attack.sol`

```solidity
contract Attack {
Expand All @@ -67,6 +67,8 @@ contract Attack {
}
```

攻击函数 `attackMint()`中的参数为 `BadRandomness`合约地址。在其中,我们计算了随机数 `luckyNumber`,然后将它作为参数输入到 `luckyMint()` 函数完成攻击。由于`attackMint()``luckyMint()`将在同一个区块中调用,`blockhash``block.timestamp`是相同的,利用他们生成的随机数也相同。

## `Remix` 复现

由于 Remix 自带的 Remix VM不支持 `blockhash`函数,因此你需要将合约部署到以太坊测试链上进行复现。
Expand Down

0 comments on commit 1aa0680

Please sign in to comment.