728x90
Description
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
Code
// 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;
}
}
}
Scenario
delegatecall은 proxy 컨트랙트의 함수를 실행하되, 내 스토리지 위에서 실행되는 것이다. 쉽게 말해, proxy 컨트랙트의 코드를 빌리는 것이다. 따라서 Delgation 컨트랙트의 fallback을 일으켜 Delegate 컨트랙트의 pwn()을 실행하면 된다. 이 때, msg.sender는 pwn 함수를 delegatecall한 나(player)이다.
Exploit
delegatecall을 할 때는 msg.data에 function selector를 넣어주면 된다. 이것은 foundry의 cast sig 명령어를 쓰면 알 수 있다.
728x90