Eggstravaganza

First Flight #37
Beginner FriendlySolidity
100 EXP
View results
Submission Details
Severity: high
Valid

Randomness Logic Used in searchForEgg Function Allows the Random Number to Be Predictable

Summary

The searchForEgg function in the EggHuntGame contract attempts to introduce game-like randomness by using a pseudo-random number generator based on on-chain parameters. However, the chosen method is insecure, making the random number predictable and therefore, exploitable by malicious users or bots to increase their chances of finding eggs.

Vulnerability Details

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

The above line is responsible for generating a pseudo-random number. The randomness relies on the following parameters:

  • block.timestamp: Can be influenced by miners/validators within a reasonable window (e.g., ±15 seconds).

  • block.prevrandao: Introduced after Ethereum’s Merge to replace block.difficulty, but it’s not cryptographically secure and can be biased under certain conditions.

  • msg.sender: Fully known by the caller.

  • eggCounter: A public or easily inferred state variable.

Since all of these values can be predicted off-chain, an attacker can simulate the exact outcome of searchForEgg() before calling it. If the random < eggFindThreshold condition is not met, they simply skip the transaction. If the result is favorable, they proceed — giving themselves a clear advantage over honest users.

Impact

  • Game Manipulation: Bots or malicious players can consistently win more eggs than intended.

  • Unfair Advantage: Legitimate players are at a disadvantage due to manipulation by those simulating the RNG.

Tools Used

  • Manual Code Review

Recommendations

  1. Avoid using block variables for randomness in on-chain logic unless the outcome has no economic impact or can tolerate manipulation.

  2. Replace the random generation logic with **Chainlink VRF (Verifiable Random Function)**

Updates

Lead Judging Commences

m3dython Lead Judge 2 months ago
Submission Judgement Published
Validated
Assigned finding tags:

Insecure Randomness

Insecure methods to generate pseudo-random numbers

Support

FAQs

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