The MysteryBox contract contains a critical vulnerability in its random number generation mechanism in MysteryBox::openBox, which can be exploited to predict and manipulate the outcomes of box openings. This weakness allows malicious actors to consistently obtain high-value rewards, potentially draining the contract's funds and undermining the fairness of the game.
Affected code - https://github.com/Cyfrin/2024-09-mystery-box/blob/281a3e35761a171ba134e574473565a1afb56b68/src/MysteryBox.sol#L47
The vulnerability lies in the MysteryBox::openBox function, specifically in the random number generation:
This method of generating randomness is insecure because:
block.timestamp is predictable and can could be manipulated by miners within a small range (for our case it is predictability)
msg.sender is known and constant for a given user.
The modulo operation (% 100) further reduces the randomness, creating a biased distribution.
The attacker checks block.timestamp and calculates the random value that the MysteryBox would have in that transaction.
Once the desired random value has been found, the attacker opens the box.
Attacker verifies that they have obtained the highest-valued box with only one transaction.
This PoC demonstrates that an attacker can:
Predict the outcome of the MysteryBox::openBox function by simulating different timestamps.
Consistently receive the highest value reward (Gold Coin) by executing the transaction at the right moment.
The console output shows:
This output indicates that the attacker successfully received a Gold Coin with only one transaction to MysteryBox::openBox, which has only a 1% chance of being obtained under normal circumstances.
These actions should not be possible in a truly random system, highlighting the severity of the weak randomness vulnerability in the MysteryBox contract.
The impact of this vulnerability is severe:
Users can manipulate the system to consistently obtain high-value rewards, breaking the intended probability distribution. This can lead to the contract may rapidly deplete its funds by disproportionately awarding high-value prizes all the time.
In the end, it will likely lead to a loss of user trust and potentially the abandonment of the platform.
Manual Review and Foundry Test
To address this vulnerability, consider the following recommendations:
Implement more secure sources of entropy or use Verifiable Random Function (VRF) as for example Chainlink VRF, which provides cryptographically guaranteed randomness. This is the industry standard for RNG without compromising security or usability
By implementing the recommendations, the MysteryBox contract can significantly improve its randomness generation, ensuring fair and unpredictable outcomes for all users.
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.