-
Notifications
You must be signed in to change notification settings - Fork 288
/
Immunefi_ch2.sol
69 lines (58 loc) · 2.23 KB
/
Immunefi_ch2.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import "forge-std/Test.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/utils/StorageSlot.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/proxy/utils/Initializable.sol";
//#SpotTheBugChallenge
//https://twitter.com/immunefi/status/1562858386244665348?s=21&t=d7_HtNra5AGuNmzVtv9uKg
interface imp {
function initialize(address) external;
}
contract ContractTest is Test {
Proxy ProxyContract;
Implementation ImplementationContract;
function testChallenge() public {
ImplementationContract = new Implementation();
console.log(
"ImplementationContract addr",
address(ImplementationContract)
);
ProxyContract = new Proxy(address(ImplementationContract));
emit log_named_bytes32(
"Storage slot 0:",
vm.load(address(ProxyContract), bytes32(uint256(0)))
);
}
}
contract Proxy {
//bytes32 constant internal _IMPLEMENTATION_SLOT = keccak256("where.bug.ser"); //correct pattern.
bytes32 internal _IMPLEMENTATION_SLOT = keccak256("where.bug.ser"); // wrong
constructor(address implementation) {
_setImplementation(address(0));
Address.functionDelegateCall(
implementation,
abi.encodeWithSignature("initialize(address)", msg.sender)
);
}
fallback() external payable {
address implementation = _getImplementation();
Address.functionDelegateCall(implementation, msg.data);
}
function _setImplementation(address newImplementation) private {
//require(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract");
StorageSlot
.getAddressSlot(_IMPLEMENTATION_SLOT)
.value = newImplementation;
}
function _getImplementation() public view returns (address) {
return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
}
}
contract Implementation is Ownable, Initializable {
// function initialize(address owner) external { //test purpose
function initialize(address owner) external initializer {
_transferOwnership(owner);
}
}