Mystery Box

First Flight #25
Beginner FriendlyFoundry
100 EXP
View results
Submission Details
Severity: medium
Valid

Insecure Random Number Generation

Summary

The openBox function uses an insecure method for generating random numbers by relying on block.timestamp and msg.sender. This method is predictable and can be manipulated, compromising the fairness and security of the random reward distribution.

Vulnerability Details

In the openBox function, randomness is generated as follows:

uint256 randomValue = uint256(keccak256(abi.encodePacked(block.timestamp, msg.sender))) % 100;

This approach is vulnerable because:

  • Predictability: Both block.timestamp and msg.sender can be known or influenced by users or miners.

  • Manipulation by Miners: Miners can adjust block.timestamp within a permissible range to alter the outcome.

  • User Exploitation: Users can repeatedly call the function, attempting to get a favorable randomValue.

Impact

  • Unfair Advantage: Malicious users or miners can manipulate the random number to increase their chances of obtaining high-value rewards.

  • Financial Loss: The contract may lose funds if high-value rewards are claimed more frequently than intended.

  • Erosion of Trust: Users may lose confidence in the platform's fairness and integrity.

Tools Used

  • Manual Code Review: Examining the random number generation logic.

Recommendations

Implement a secure randomness source, such as Chainlink VRF (Verifiable Random Function), which provides tamper-proof randomness:

  1. Integrate Chainlink VRF:

    • Import Chainlink VRF contracts.

    • Inherit from VRFConsumerBase.

    • Set up the necessary variables (keyHash, fee).

  2. Modify openBox Function:

    • Request randomness from Chainlink VRF.

    • Handle the asynchronous nature of the randomness request.

  3. Implement fulfillRandomness Function:

    • Receive the random number.

    • Use it to determine the reward.

  4. Ensure Sufficient LINK Balance:

    • Fund the contract with LINK tokens to pay for randomness requests.

  5. Handle Failures Gracefully:

    • Implement fallback mechanisms in case the randomness request fails.

Updates

Appeal created

inallhonesty Lead Judge about 1 year ago
Submission Judgement Published
Validated
Assigned finding tags:

Weak Randomness

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!