This repository showcases a vulnerability in a Solidity smart contract, VulnerableGame
, and demonstrates how it can be exploited using a SelfDestructExploit
contract.
- VulnerableGame: A game where players deposit 1 ether. The first player to make the contract balance reach 10 ether wins and can withdraw all funds. The game is then reset and can start over.
- SelfDestructExploit: A malicious contract that uses
selfdestruct
to forcibly send ether to theVulnerableGame
contract, bypassing its logic, making the game unplayable and the funds unretrievable.
The VulnerableGame
contract is vulnerable because:
- It relies on
address(this).balance
for game logic. - It doesn’t prevent nor handle unexpected ether transfers.
The SelfDestructExploit
contract can forcibly send ether using selfdestruct
, causing:
- The game to reach the target balance unexpectedly.
- Potentially locking or misdirecting funds.
- Foundry
- Solidity compiler (v0.8.x).
-
Clone the Repository:
git clone https://github.com/nightowlish/selfdestruct-attack.git cd selfdestruct-attack
-
Compile the Contracts: Use Foundry to compile the smart contracts:
forge install forge build
-
Run the Exploit:
forge test --match-test testExploit -vvv