The claimSingleReward function within MysteryBox.sol s susceptible to a reentrancy attack. This vulnerability arises because the external call to the recipient's address is made before updating the internal state. As a result, an attacker can recursively invoke the claimSingleReward function to drain significant funds from the contract.
An attacker can exploit this vulnerability by creating a malicious contract that re-enters the claimSingleReward function. This allows the attacker to claim rewards multiple times before the contract updates the internal state, leading to a substantial loss of funds.
Copy this code to a new test file: MysteryBox/test/ReentrancyExploit2.sol
Run: forge test --match-contract ReentrancyExploit2 -vvv
Output Logs:
Explanation of the Logs:
Contract Deployment: Logs display the deployment of the MysteryBox contract and the attacker's initial funding.
Box Transactions: Logs confirm that the attacker bought and opened three mystery boxes.
Rewards Confirmation: Logs verify that the attacker received multiple rewards.
Balance Logs: Initial and final balance logs of the attacker's address to show the financial impact.
Reentrancy Occurrence: Logs indicating the
claimSingleReward function was re-entered multiple times, exploiting the vulnerability.
Exploit Confirmation: Confirmation of the exploit's execution, highlighting the significant balance increase due to the reentrancy attack.
Financial Loss: The attacker can drain a substantial amount of Ether from the contract, causing financial loss.
Loss of User Trust: Users might lose trust in the platform due to security vulnerabilities.
Operational Disruption: Draining of contract funds could lead to operational disruptions and potential insolvency of the contract.
Foundry
Implement Reentrancy Guard: Utilize ReentrancyGuard from OpenZeppelin to protect functions susceptible to reentrancy.
Follow Checks-Effects-Interactions Pattern: Ensure state changes are made before performing any external calls..
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.