1. 문제

 주어진 인스턴스의 소유권을 탈취하라.
 'delegatecall' 이 어떻게 동작하는지 공부 ㄱㄱ
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;
        }
    }
}

2. delegatecall

delegatecall 이란?

delegatecall은 Solidity에서 외부 컨트랙트의 코드를 실행하되, 스토리지(storage)는 자신(this)의 것을 사용하는 저수준 함수입니다. -> 요약 : 코드는 빌리고, 저장소는 내 거 쓴다

3. 코드 분석 및 문제 풀이 과정

fallback() external {
    (bool result,) = address(delegate).delegatecall(msg.data);
    if (result) {
        this;
    }
}
function pwn() public {
    owner = msg.sender;
}
  1. Delegation contract에 정의되지 않은 함수인 pwn() 함수를 호출하여 fallback 을 발생 시켜 deletegatecall 이 동작하도록 한다.
  2. delegatecall 이 호출되면 msg.data 는 호출한 계정의 정보 즉, 나의 정보를 인자로 넘기게 된다.
  3. pwn() 함수에서 owner = msg.sender 에서 내가 owner 권한을 탈취하게 된다.