Eggstravaganza

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

Weak pseudo-random number generation in `EggHuntGame::searchForEgg` function

Summary

The searchForEgg function utilizes a pseudo-random number generator (PRNG) based on predictable blockchain variables (block.timestamp, block.prevrandao, msg.sender, eggCounter). This makes the outcome potentially predictable or manipulatable, undermining the fairness of the egg discovery mechanism.

Vulnerability Details

The randomness used to determine if a player finds an egg is derived from hashing a combination of block.timestamp, block.prevrandao, msg.sender, and the current eggCounter:

// Pseudo-random number generation (for demonstration purposes only)
uint256 random = uint256(
keccak256(abi.encodePacked(block.timestamp, block.prevrandao, msg.sender, eggCounter))
) % 100;
if (random < eggFindThreshold) {
// ... egg found logic ...
}

All these inputs are either known or can be influenced/predicted by users or miners.

A malicious user could potentially time their transactions or observe the mempool to increase their chances of finding an egg when random < eggFindThreshold evaluates to true. Miners have even greater control over block.timestamp and transaction ordering, allowing potential manipulation.

Impact

The weak PRNG compromises the integrity and fairness of the game's core mechanic. Players might be able to predict or influence when they find an egg, deviating from the intended probability set by eggFindThreshold. This could lead to an unfair distribution of NFTs. While exploitation might require some effort or specific conditions, the predictability itself is a vulnerability.

Tools Used

Manual Review

Recommendations

Avoid using on-chain variables like block.timestamp, block.prevrandao, msg.sender, or internal counters for generating randomness in smart contracts, as they are generally predictable or manipulatable. Instead, consider using more secure and reliable methods for randomness generation. Options include:

  • Commit-Reveal Schemes: Where users commit to a hash of a secret value and reveal it later.

  • Verifiable Random Functions (VRFs): Cryptographic functions that produce verifiable pseudo-random outputs. Services like Chainlink VRF provide this on-chain.

  • Randomness Oracles: External services that provide random numbers to the smart contract, often secured through decentralization and cryptographic proofs.

These approaches provide stronger guarantees against prediction and manipulation compared to relying solely on on-chain data.

Updates

Lead Judging Commences

m3dython Lead Judge 3 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.