Ethernaut - Hello Ethernaut

| Ethernaut | 1749 | 5分钟 | 以太坊智能合约

初见 Ethernaut。

做毕设的时候查到这个小平台,来玩一玩。

前置条件

Hello Ethernaut

在控制台里按照步骤提示调用智能合约,看结果。

image-20241221150139692

最后一个,密码是什么意思……

发现:

image-20241221152006587

有一个成员变量!

所以:

await contract.authenticate("ethernaut0")

然后点击 submit instance

image-20241221152315112

太有花活了哈哈哈哈,然后显示了智能合约的源码:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Instance {
    string public password;
    uint8 public infoNum = 42;
    string public theMethodName = "The method name is method7123949.";
    bool private cleared = false;

    // constructor
    constructor(string memory _password) {
        password = _password;
    }

    function info() public pure returns (string memory) {
        return "You will find what you need in info1().";
    }

    function info1() public pure returns (string memory) {
        return 'Try info2(), but with "hello" as a parameter.';
    }

    function info2(string memory param) public pure returns (string memory) {
        if (keccak256(abi.encodePacked(param)) == keccak256(abi.encodePacked("hello"))) {
            return "The property infoNum holds the number of the next info method to call.";
        }
        return "Wrong parameter.";
    }

    function info42() public pure returns (string memory) {
        return "theMethodName is the name of the next method.";
    }

    function method7123949() public pure returns (string memory) {
        return "If you know the password, submit it to authenticate().";
    }

    function authenticate(string memory passkey) public {
        if (keccak256(abi.encodePacked(passkey)) == keccak256(abi.encodePacked(password))) {
            cleared = true;
        }
    }

    function getCleared() public view returns (bool) {
        return cleared;
    }
}

好玩。再玩玩。