The MysteryBox contract uses an insecure method to generate random numbers for reward distribution, relying on block.timestamp
and msg.sender
. This approach is predictable and can be manipulated by miners or users, potentially allowing them to influence or predict the outcomes of box openings.
The vulnerable code is in the openBox()
function:
The issue lies in the random number generation (indicated by @>
):
block.timestamp
can be manipulated by miners within a small range.
msg.sender
is known and controlled by the user calling the function.
The combination of these values is predictable and can be calculated in advance.
The impact of this vulnerability is high:
Reward Manipulation: Users or miners could potentially predict or influence the outcome of box openings.
Economic Imbalance: Exploiters could consistently obtain high-value rewards, draining the contract's funds.
Unfair Advantage: Knowledgeable users could have a significant advantage over others, undermining the fairness of the game.
Loss of Trust: If exploited, users may lose trust in the fairness of the system, potentially leading to a loss of user base.
Manual code review.
To address this vulnerability, consider the following recommendations:
Use a verifiable random function (VRF) provided by a trusted oracle service, such as Chainlink VRF. This provides cryptographically guaranteed randomness that can't be manipulated by miners or users.
Example implementation using Chainlink VRF:
If using an oracle is not feasible, implement a commit-reveal scheme where users submit a hash of their secret and the reveal happens in a separate transaction.
Use block difficulty (block.difficulty
) and a future block hash as additional sources of entropy, although this is still not truly random and can be manipulated to some extent.
By implementing one of these solutions, particularly the Chainlink VRF, the contract can ensure fair and unpredictable reward distribution, maintaining the integrity and excitement of the mystery box concept.
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.