A reentrancy vulnerability was identified in MysteryBox.sol. By exploiting this vulnerability, an attacker can repeatedly claim rewards and drain the contract's funds. This issue was demonstrated using a structured exploit within a test case, ultimately leading to the unauthorized transfer of funds from the contract to the attacker.
Function affected:
This vulnerability occurs because the function allows external calls (i.e., to the attacker's contract) before updating the contract’s state. As a result, an attacker can reenter the function within the same transaction, leading to multiple withdrawals and depleting the contract's funds.
The core issue is that the function does not adhere to the "Checks-Effects-Interactions" pattern and is missing a reentrancy guard, which allows the attacker's fallback function to repeatedly execute the vulnerable function.
Copy the code to a new test file: MysteryBox/test/ReentrancyExploit.sol
Run test: forge test --match-contract ReentrancyExploit -vvv
The following logs were captured during the execution of the test:
Exploit Output Explanation:
Rewards Information:
The attacker received three "Silver Coin" rewards, each valued at 0.5 ether (500000000000000000 wei).
Initial Balance: The initial balance of the attacker was 0.7 ether.
Successful Execution: Indicates that the claimAllRewardsfunction executed successfully.
Final Balance: The final balance of the attacker increased to 2.2 ether, confirming a successful exploit where an additional 1.5 ether was withdrawn unauthorizedly due to the reentrancy attack.
Financial Loss: A reentrancy vulnerability allows an attacker to drain significant funds from the contract, leading to severe financial loss for the contract owner and stakeholders.
Reputation Damage: The presence of such a critical vulnerability can erode the trust of users and investors, damaging the project’s reputation.
Operational Risk: Continued exploitation without patching the vulnerability can deplete contract funds, causing disruptions in normal operations and potentially rendering the contract unusable.
Foundry
Implement Reentrancy Guard: Utilize OpenZeppelin's ReentrancyGuard to protect critical functions vulnerable to reentrancy attacks.
Follow Checks-Effects-Interactions Pattern: Ensure that state changes are performed before executing 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.