The EggHuntGame
contract is vulnerable to front-running attacks due to the predictable nature of its randomness in the searchForEgg()
function. The randomness used to determine whether a player finds an egg is derived from publicly accessible data, including:
block.timestamp
(known before the transaction is mined).
block.prevrandao
(known before the transaction is mined).
msg.sender
(publicly accessible).
eggCounter
(a public state variable).
Since these values are observable before the transaction is executed, attackers can predict the outcome of the random number generation (RNG) with high accuracy. This enables them to front-run transactions by submitting their own searchForEgg
transaction when favorable conditions are detected (i.e., when the RNG is likely to result in an egg being found).
Attacker Predictability: An attacker can predict when the RNG will result in a successful egg find and submit their transaction first, maximizing their chances of finding eggs before legitimate players.
Unfair Advantage: This gives attackers an unfair advantage over regular players, as they can achieve a 100% success rate while regular users only have a 50% chance (when the threshold is set to 50%).
Game Manipulation: The attacker can collect more eggs than other players, distorting the game's fair play mechanics and leading to an unfair gaming experience.
Below is a PoC demonstrating the vulnerability of front-running attacks:
Attacker Contract: The AttackerContract
can simulate the outcome of a searchForEgg()
call by calculating the same randomness as the contract would. This allows the attacker to predict when the RNG will be favorable (i.e., when the random value is below the eggFindThreshold
).
Front-Running Attempts: The attacker can front-run their transaction by submitting it when the outcome is favorable, giving them a 100% success rate in finding eggs.
Regular Player Comparison: In contrast, a regular player has a 50% success rate (for a threshold of 50%) and cannot predict the RNG, making them significantly less efficient at finding eggs.
This vulnerability allows attackers to:
Gain an Unfair Advantage: Attackers can reliably and consistently find eggs, making them disproportionately successful compared to regular players, thus undermining the fairness of the game.
Exploit Predictable Randomness: The attack is based on the fact that the randomness used in the game is predictable before execution, making it easy to exploit.
Distort Game Economy: If the game involves rewards or rankings based on the number of eggs found, this could distort the game's economy, create imbalance, and frustrate legitimate players.
Undermine Game Integrity: The integrity of the game is compromised as attackers exploit the predictable nature of the randomness, reducing the overall player experience.
To mitigate the front-running attack, the randomness used for determining whether an egg is found should be made unpredictable. The best approach is to use a secure random number generator (RNG), such as Chainlink VRF (Verifiable Random Function), which provides a verifiable and cryptographically secure source of randomness.
Replace the current randomness generation with Chainlink VRF, which provides a secure and verifiable random number. This ensures that no party can predict the outcome before the transaction is confirmed.
Example:
This will ensure that the randomness is secure and not predictable before execution.
The front-running vulnerability in the EggHuntGame
contract arises from the predictable nature of the random number generation used to determine whether an egg is found. By replacing the current RNG mechanism with a secure and verifiable source like Chainlink VRF, this vulnerability can be mitigated, ensuring fairness and integrity in the game.
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.