The claimAllRewards and claimSingleReward functions send Ether to users before updating the contract’s state, exposing the contract to potential reentrancy attacks. An attacker could exploit this to repeatedly claim rewards before the state is updated, draining funds or manipulating the contract state.
In the MysteryBox.sol contract, the reward claiming functions transfer Ether before updating the user's reward state:
By transferring Ether before updating the user's reward state (rewardsOwned), the contract allows the possibility of reentrant calls where an attacker can re-enter the function during the Ether transfer and manipulate the contract's state before it has been properly updated.
Exploiting this vulnerability allows an attacker to repetitively call the reward claiming functions before the rewards are deleted, potentially draining the contract's funds or manipulating the rewardsOwned mapping. While the contract may have limited attack vectors due to ownership restrictions, the vulnerability poses a significant risk to the contract’s financial integrity.
An attacker can create a malicious contract that re-enters the claimAllRewards or claimSingleReward function during the Ether transfer:
By deploying this attack contract and initiating the attack function, the attacker can recursively call claimAllRewards before the rewards are deleted, allowing multiple withdrawals.
Manual code review
Slither static analysis tool
Adhere to the Checks-Effects-Interactions pattern by updating the contract's state before making external calls. Additionally, implement reentrancy guards to prevent recursive calls during Ether transfers.
Revised claimAllRewards 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.