The MysteryBox::changeOwner function lacks access control, allowing any user to claim ownership of the contract. This vulnerability gives unauthorized users full control over the protocol’s funds and settings.
The vulnerability exists in the MysteryBox::changeOwner function:
https://github.com/Cyfrin/2024-09-mystery-box/blob/281a3e35761a171ba134e574473565a1afb56b68/src/MysteryBox.sol#L111-L113
This function can be called by anyone, making it possible for any address to become the owner of the contract. As a result, the attacker gains full control over the funds in the protocol, as well as all functions restricted to the owner.
To run the tests successfuly we need a little adjustment in the setUp function:
To demonstrate this vulnerability, modify the setUp function in TestMysteryBox.t.sol to deal ETH to the owner and send the needed 0.1 ether during contract creation:
Next, add the following test to verify that any user can become the owner by calling changeOwner:
Running the test with the following command:
Produces these results:
The test demonstrates that user1 can successfully change ownership of the contract, highlighting the critical nature of this vulnerability.
By exploiting the changeOwner vulnerability, an attacker can:
Steal all funds : Call the MysteryBox::withdrawFunds function and transfer the contract balance to their own address.
Set arbitrary box prices: Call the MysteryBox::setBoxPrice function and adjust the price of boxes to any value.
This represents a critical vulnerability due to the complete control granted to the attacker, impacting both financial and operational aspects of the contract.
Manual Review
MysteryBox::changeOwner function:OR
Ownable.solInstead of implementing custom ownership logic, consider the OpenZeppelin's Ownable.sol. This contract includes built-in ownership transfer functionality and access control, reducing the risk of introducing security vulnerabilities.
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.