Anyone can invoke the MysteryBox::changeOwner function to assume ownership of the contract, which would then allow them to execute the MysteryBox::withdrawFunds function and drain the contract’s balance.
A malicious user could exploit the contract to steal all funds.
Here is how to exploit the contract:
A attacker takes ownership of the contract through passing their own address in MysteryBox::changeOwner function.
Then the attacker can call MysteryBox::withdrawFunds function bypassing the require(msg.sender == owner, "Only owner can withdraw"); and withdrawing the funds.
Add the code below to the test suit.
Before the attack there is 0.3 ether in the contract from the seed value and two mystery box purchases. After the attack there is 0 ether in contract.
There is another exploit scenario where a maliciuos user can take ownership of contract and set price to zero and buy mystery boxes for free.
A attacker takes ownership of the contract through passing their own address in MysteryBox::changeOwner function.
Then the attacker sets the mystery box price to zero by calling setBoxPrice with 0 as parameter.
After that anybody can buy as many boxes as they want for free.
Manual code review
A way to mitigate this vulnerability is to have proper access control on the MysteryBox::changeOwner function.
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.