Eggstravaganza

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

Weak PRNG in `EggHuntGame::searchForEgg` can allow player to always mint an NFT.

Summary

Egg NFTs are intended to be awarded to players randomly when they call the searchForEgg function. However, the psuedo-random number is generated using predictable values that can let a player always mint an NFT.

Vulnerability Details

In EggHuntGame.sol,

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]);
}
}

the code uses block.timestamp, block.prevrandao, msg.sender and eggCounter as sources of randomness to determine minting an egg NFT. These are values that can be easily controlled/manipulated by a player to ensure the generated value is less than eggFindThreshold.

Impact

Malicious players are able to reliably mint NFTs, which breaks the intent of the protocol and results in unfair asset earnings.

Tools Used

  • Manual Review

  • Aderyn

Recommendations

The use of keccak256 hash functions on predictable values like block.timestamp, block.number, or similar data, including modulo operations on these values, should be avoided for generating randomness, as they are easily predictable and manipulable. The PREVRANDAO opcode also should not be used as a source of randomness. Instead, utilize Chainlink VRF for cryptographically secure and provably random values to ensure protocol integrity.

Updates

Lead Judging Commences

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