Eggstravaganza

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

Weak Randomness in the `EggHuntGame::searchForEgg` Function

[H-01] Weak Randomness in the EggHuntGame::searchForEgg Function

Summary

The EggHuntGame::searchForEgg function in the EggHuntGame contract uses a pseudo-random number generator based on keccak256 and various block parameters, including block.timestamp and block.prevrandao, to determine if a player finds an egg. This method of randomness is weak and can be manipulated by miners or participants, which introduces a potential vulnerability in the game.

Vulnerability Details

The vulnerability occurs in the following function:

/// @notice Participants call this function to search for an egg.
/// A pseudo-random number is generated and, if below the threshold, an egg is found.
function searchForEgg() external {
require(gameActive, "Game not active");
require(block.timestamp >= startTime, "Game not started yet");
require(block.timestamp <= endTime, "Game ended");
// 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) {
eggCounter++;
eggsFound[msg.sender] += 1;
eggNFT.mintEgg(msg.sender, eggCounter);
emit EggFound(msg.sender, eggCounter, eggsFound[msg.sender]);
}
}

In this function, the randomness is generated using keccak256 on block.timestamp, block.prevrandao, msg.sender, and eggCounter. However, both block.timestamp and block.prevrandao can be influenced by miners, making the randomness predictable and potentially exploitable.

Impact

Miners or participants can potentially predict or control the random number generation by manipulating block parameters like block.timestamp or block.prevrandao, which could lead to unfair gameplay.

Tools Used

Manual code review

Recommendations

It is recommended to replace the current pseudo-random number generation with a more secure method, such as Chainlink VRF (Verifiable Random Function). Chainlink VRF offers a secure, tamper-proof source of randomness, ensuring that the random values used in the game cannot be manipulated.

Updates

Lead Judging Commences

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