The MysteryBox contract lacks proper access control on the changeOwner function, allowing any user to assume ownership of the contract. An attacker can exploit this vulnerability to change the owner to their own address and subsequently withdraw all funds from the contract, leading to a total loss of assets and compromising the integrity of the platform.
The changeOwner function lacks access control, allowing anyone to change the owner of the contract:
In standard smart contract practices, functions that modify ownership or other sensitive state variables should be restricted to authorized accounts, typically the current owner or an admin role.
An attacker can exploit this function to:
Assume Ownership: Change the owner of the contract to their own or any other address.
Withdraw All Funds: Call the withdrawFunds function to transfer all Ether from the contract to themselves.
Disrupt Contract Operations: Potentially alter other functions restricted to the owner, affecting the contract's functionality.
The following forge test demonstrates how an attacker can change the owner of the contract to themselves and withdraw all of its funds.
It can be run with the following command:
forge test --match-contract AttackChangeOwner -vv
Resulting in:
[⠢] Compiling...
[⠢] Compiling 1 files with Solc 0.8.24
[⠰] Solc 0.8.24 finished in 20.97s
Compiler run successful!Ran 1 test for test/TestMysteryBox.t.sol:AttackChangeOwner
[PASS] testChangeOwnerAndWithdraw() (gas: 37778)
Logs:
MysteryBox owner before attack: 0x0000000000000000000000000000000000000001
MysteryBox owner after attack: 0x0000000000000000000000000000000000000002
MysteryBox initial balance: 5000000000000000000
MysteryBox final balance: 0
Attacker initial balance: 10000000000000000000
Attacker final balance: 15000000000000000000Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 6.24ms (1.51ms CPU time)
Ran 1 test suite in 163.98ms (6.24ms CPU time): 1 tests passed, 0 failed, 0 skipped (1 total tests)
Foundry forge
Implement Access Control on changeOwner Function
Add a requirement that only the current owner can change ownership:
Utilize Standardized Access Control Patterns
Incorporate established and battle-tested contracts like OpenZeppelin's Ownable to manage ownership securely:
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.