c0mpos3r

[Ethernaut] 00. Hello Ethernaut WriteUp 본문

Web3/Hacking

[Ethernaut] 00. Hello Ethernaut WriteUp

음대생 2025. 8. 24. 20:52

1. 문제 분석

Congratulations! You have completed the tutorial. Have a look at the Solidity code for the contract you just interacted with below.
You are now ready to complete all the levels of the game, and as of now, you're on your own.
Godspeed!!

 

축하해요! 튜토리얼을 완료했습니다. 아래에서 방금 상호 작용 한 계약의 견고성 코드를 살펴보십시오.

이제 게임의 모든 레벨을 완료 할 준비가되었으며 현재는 자신이 혼자입니다.

1-1. code

// 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;
    }
}

1-2. Denial Contract 분석

  • 목표: authenticate()를 성공시켜 cleared = true 만들기
  • 취약점: 온체인에 비밀(password)을 저장해 두었고, 더구나 public이라 누구나 읽을 수 있음
  • 공격 벡터: password()로 비밀번호 조회 → authenticate(password) 호출

상태 변수

  • string public password → 자동 getter로 그대로 노출
  • uint8 public infoNum = 42 → 힌트용 값
  • theMethodName: string public = "The method name is method7123949." → 힌트용 문자열
  • bool private cleared → 인증 성공 시 true

2. Solving

  • password() 호출로 평문 비밀번호 획득
  • authenticate(<획득한_비밀번호>) 호출
  • getCleared()true인지 확인
await contract.authenticate(contract.password());
await contract.getCleared();

3. 결론

접근 제어자를 잘 관리해야 한다.

'Web3 > Hacking' 카테고리의 다른 글

[Ethernaut] 02. Fallout WriteUp  (1) 2025.08.24
[Ethernaut] 01. Fallback WriteUp  (0) 2025.08.24
[Ethernaut] 20. Denial WriteUp  (0) 2025.08.24
[Ethernaut] 21. Shop WriteUp  (0) 2025.08.20
[Ethernaut] 19. Alien Codex WriteUp  (2) 2025.08.09