forked from SunWeb3Sec/DeFiVulnLabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Storage-collision.sol
47 lines (36 loc) · 1.13 KB
/
Storage-collision.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
import "forge-std/Test.sol";
contract ContractTest is Test {
Logic LogicContract;
Proxy ProxyContract;
function testStorageCollision() public {
LogicContract = new Logic();
ProxyContract = new Proxy(address(LogicContract));
console.log("Current implementation contract address:",ProxyContract.implementation());
ProxyContract.testcollision();
console.log("Overwrited slot0 implementation contract address:",ProxyContract.implementation());
console.log("Exploit completed");
}
receive() payable external{}
}
contract Proxy {
address public implementation; //slot0
constructor (address _implementation) public {
implementation = _implementation;
}
function testcollision() public {
implementation.delegatecall(
abi.encodeWithSignature("foo(address)",address(this))
);
}
}
contract Logic {
address public GuestAddress; //slot0
constructor () public {
GuestAddress = address(0x0);
}
function foo(address _addr) public {
GuestAddress = _addr;
}
}