Eggstravaganza

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

Title Insecure Pseudo-Randomness in Egg Hunting Mechanism Allows Predictable Egg Discovery

Summary

The EggHuntGame contract uses an insecure pseudo-random number generator to determine egg discovery, relying on predictable blockchain variables (block.timestamp, block.prevrandao, msg.sender, eggCounter). This allows attackers to manipulate or predict the outcome, guaranteeing egg finds and NFT mints. The flaw undermines the game’s fairness and could lead to excessive NFT issuance, devaluing the project. A secure randomness solution, such as Chainlink VRF, is recommended to fix this high-severity issue.

Vulnerability Details

Code Affected

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

Description

The contract generates a pseudo-random number (random) using keccak256 with inputs from:

  • block.timestamp: Miners can adjust slightly (e.g., within seconds).

  • block.prevrandao: Known after the previous block is mined.

  • msg.sender: Controlled by the caller.

  • eggCounter: Publicly readable state variable.

These inputs are transparent and manipulable, making random predictable. Attackers can:

  • Simulate random off-chain using current blockchain state.

  • Submit transactions only when random < eggFindThreshold, ensuring success.

  • Miners can tweak block.timestamp to favor themselves.

This breaks the intended probabilistic egg-finding mechanic (e.g., a 50% chance if eggFindThreshold = 50), allowing attackers to mint NFTs at will.

Exploit Example

An attacker contract:

contract EggHuntExploit {
EggHuntGame public game = EggHuntGame(GAME_ADDRESS);
function attack() external {
uint256 random = uint256(
keccak256(abi.encodePacked(block.timestamp, block.prevrandao, address(this), game.eggCounter()))
) % 100;
if (random < game.eggFindThreshold()) {
game.hunt(); // Assumes function name
}
}
}
  • Steps: Call attack() repeatedly, only executing when random favors the attacker.

Impact

  • Fairness Compromised: Attackers can find eggs with near-100% success, leaving honest players at a disadvantage.

  • NFT Over-Minting: Uncontrolled minting could exhaust an NFT supply cap or flood the market, devaluing Egg NFTs.

  • Economic Loss: If rewards or fees are tied to minting, attackers could drain contract resources.

  • Trust Erosion: Players may abandon the game if its core mechanic is exploitable, damaging the project’s reputation.

Tools Used

No specific tools were employed to identify this vulnerability.

Recommendations

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.