Eggstravaganza

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

On-chain randomness can be exploited

Summary

The searchForEgg function in EggHuntGame.sol uses Pseudo-random number generation to get a random number. Which can be manipulated.

Vulnerability Details

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

Pseudo-random number generation can be manipulated easily by a malicious miner or validator. They can slightly tweak values like block.timestamp and block.prevrandao is easily predictable by them to favor certain outcomes. Also the Pseudo-random number generation uses msg.sender as a value. The msg.sender can spam call the function until they get an eggNFT.

Impact

A malicious miner or validator can tweak values like block.timestamp and predict block.prevrandao. They can see what timestamp causes if (random < eggFindThreshold) to return true, which if it does you get an eggNFT. They can choose to mine a block with the timestamp that triggers the out come they want (which is them getting an eggNFT). Also a msg.sender can make multiple wallets and call searchForEgg until they get a eggNFT. This is due to the fact that Pseudo-random number generation uses msg.sender as a value.

Tools Used

Manual Review

Recommendations

Use a decentralized oracle network for randomness such as Chainlink VRF.

Updates

Lead Judging Commences

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