The MysteryBox
contract uses inadequate sources of randomness to determine reward distribution when opening boxes. Specifically, the openBox
function relies on block.timestamp
and msg.sender
to generate a random number, which are predictable and manipulable sources. This vulnerability allows malicious actors to potentially influence or predict the rewards they receive, undermining the fairness and unpredictability of the mystery box system.
The vulnerability lies in the openBox
function, which uses insecure sources to generate randomness:
The main issues with this implementation are:
block.timestamp
is manipulable by miners within a certain range. Miners can adjust the timestamp slightly to influence the outcome.
msg.sender
is known and controlled by the user calling the function, allowing them to potentially influence the result.
The modulo operation % 100
further reduces the randomness by limiting the output to a small range of values.
This combination of predictable inputs and limited output range makes the reward distribution vulnerable to manipulation and prediction.
The inadequate random number generation has several significant impacts on the MysteryBox system:
Manipulation of Rewards: Malicious actors could potentially manipulate the timing and parameters of their transactions to increase their chances of receiving high-value rewards. This undermines the fairness of the system and could lead to economic imbalances.
Predictability: Users with the ability to analyze blockchain data could potentially predict the outcomes of box openings, giving them an unfair advantage. This defeats the purpose of a "mystery" box and could lead to exploitation.
Miner Exploitation: Miners could use their ability to manipulate block timestamps to their advantage when opening boxes, potentially extracting more value from the system than intended.
The severity of this impact is high because it fundamentally undermines the core functionality and fairness of the MysteryBox system, potentially leading to both immediate financial exploitation and long-term damage to the platform's reputation and viability.
The following test function demonstrates the vulnerability in the random number generation of the MysteryBox contract:
This test demonstrates two critical aspects of the vulnerability:
Predictability: The test accurately predicts the reward a user will receive by using the same random number generation logic as the contract. This is shown in the first part of the test where we predict the reward and then verify that the actual reward matches our prediction.
Manipulability: The test shows how a user could manipulate the timestamp to consistently receive the highest value reward (Gold Coin in this case). This is demonstrated in the second part of the test where we manipulate the timestamp to ensure we receive a Gold Coin.
To run this test, use the following command:
the test output is attached here:
The test passing confirms the existence and exploitability of the vulnerability in the random number generation mechanism of the MysteryBox contract.
Manual review of the smart contract code
Foundry for writing and running test cases to validate the vulnerability
To address the vulnerability in random number generation, we recommend to use a secure source of randomness:
Implement a Verifiable Random Function (VRF) from Chainlink, which provides cryptographically secure randomness.
Example implementation:
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.