The MysteryBox.sol contract contains a critical access control vulnerability in the MysteryBox::changeOwner function. This vulnerability allows any user to become the owner of the contract, granting them unrestricted access to sensitive functions such as withdrawing funds, setting box prices, and adding rewards.
Affected code - https://github.com/Cyfrin/2024-09-mystery-box/blob/281a3e35761a171ba134e574473565a1afb56b68/src/MysteryBox.sol#L111-L112
The vulnerability stems from the MysteryBox::changeOwner function lacking proper access control:
This function allows any address to call it and change the owner of the contract. As a result, an attacker can easily take control of the contract by calling this function.
An attacker calls the changeOwner function to become the new owner of the contract.
The attacker, now the owner, withdraws all funds from the contract.
The attacker changes the box price to a new value.
The attacker adds a new reward to the reward pool.
This PoC demonstrates that an attacker can:
Take ownership of the contract
Withdraw all funds from the contract
Change the box price at will
Add new rewards to the reward pool
The console output shows the changes made by the attacker:
These actions should only be possible for the legitimate owner of the contract, highlighting the severity of the access control vulnerability.
The lack of access control in the MysteryBox::changeOwner function presents a critical vulnerability that can lead to catastrophic consequences for the contract and its users:
Complete Contract Takeover: An attacker can easily become the owner of the contract, granting them unrestricted access to all owner-only functions. This allows them to manipulate the contract's core functionality at will.
Fund Theft: As the new owner, the attacker can withdraw all funds stored in the contract using the withdrawFunds function. This could result in significant financial losses for the original owner and users who have invested in mystery boxes.
Price Manipulation: The attacker can arbitrarily change the box price using the MysteryBox::setBoxPrice function. This could be exploited to:
Set extremely low prices to drain the contract's rewards quickly
Set unreasonably high prices to prevent legitimate users from participating
Reward Pool Corruption: With the ability to add new rewards via the addReward function, an attacker could:
Introduce worthless rewards
Oversaturate the reward pool with low-value items, reducing the chances of users receiving valuable rewards
Loss of User Trust: Once users discover that the contract has been compromised, it will likely lead to a complete loss of trust in the platform, potentially causing irreparable damage to the project's reputation.
Disruption of Game Economics: For gaming or NFT projects utilizing this contract, the ability to manipulate prices and rewards could severely disrupt the in-game economy and user experience.
The severity of this vulnerability cannot be overstated. It essentially provides a "master key" to the entire contract, allowing an attacker to completely subvert its intended functionality and potentially cause significant financial and reputational damage to the project and its users.
Manual Review and Foundry Test
Implement proper access control on the MysteryBox::changeOwner function with custom revert errors:
Or consider using OpenZeppelin's Ownable contract to handle ownership-related functionality securely:
Add access control to other sensitive functions like MysteryBox::withdrawFunds, MysteryBox::setBoxPrice, and MysteryBox::addReward using the onlyOwner modifier.
When implementing an Ownable contract consider adding a time-lock mechanism for ownership transfers to provide an additional layer of security.
By implementing these recommendations, the security of the MysteryBox contract can be significantly improved, preventing unauthorized access and protecting both the contract and its users from potential attacks.
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.