The EggHuntGame
smart contract uses a weak pseudo-random number generation mechanism in its searchForEgg
function. This vulnerability arises from the use of predictable blockchain variables (block.timestamp
, block.prevrandao
, msg.sender
, and eggCounter
) as inputs to the keccak256
hash function. Attackers can potentially predict or manipulate these values, undermining the fairness of the egg-finding mechanism and allowing exploitation of the game's randomness.
The searchForEgg
function generates a pseudo-random number using the following code:
The approach is insecure because:
Predictable inputs
block.timestamp
is the timestamp of the current block, which miners can manipulate within a small range (typically ±15 seconds).
block.prevrandao
(introduced in Ethereum's PoS upgrade) provides a randomness value, but it is still predictable once the block is proposed.
msg.sender
is the address calling the function, which is known to the caller.
eggCounter
is a public state variable that increments predictably with each egg found.
Lack of Cryptographic Security: The keccak256 hash function, while collision-resistant, does not provide sufficient entropy when fed with predictable inputs. An attacker can precompute or simulate outcomes based on these values.
Modulus Operation: The result is reduced modulo 100 to compare against eggFindThreshold. Since the inputs are not sufficiently random, the distribution of outcomes can be skewed or manipulated.
This weak randomness allows an players to:
Predict when they are likely to find an egg by simulating the random number off-chain.
Time their transaction submission to align with favorable block.timestamp or block.prevrandao values.
Gain an unfair advantage over other players, undermining the game's integrity.
Add this test to EggHuntGameTest.t.sol
Manual review - foundry
Integrate Chainlink VRF (or similar oracle-based randomness service) to provide cryptographically secure randomness.
Insecure methods to generate pseudo-random numbers
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.