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

Manipulation of Rarity Assignment in PuppyRaffle Contract

Summary

The PuppyRaffle contract uses a mapping (tokenIdToRarity) to associate token IDs with their respective rarities. This mapping is modified in the selectWinner function, which is public and can be called by anyone. The logic in this function can be manipulated, leading to incorrect rarity assignments.

Vulnerability Details

The selectWinner function calculates a rarity value based on the msg.sender and block.difficulty, and then assigns this rarity to a token ID in the tokenIdToRarity mapping. An attacker can influence this calculation, they could potentially manipulate the rarity of a token.

PoC

The potential vulnerability lies in the calculation of rarity in the selectWinner function. The rarity is calculated using the keccak256 hash of the msg.sender and block.difficulty.

uint256 rarity = uint256(keccak256(abi.encodePacked(msg.sender, block.difficulty))) % 100;

An attacker could potentially manipulate this calculation in the following ways:

  1. Predicting block.difficulty: The block.difficulty is a value that can be influenced by miners. If an attacker is also a miner, they could potentially manipulate this value when they mine a block.

  2. An attacker could potentially create multiple addresses and call the selectWinner function from these addresses to influence the rarity calculation.

Here's an example of how an attacker might manipulate the rarity:

contract Attack {
PuppyRaffle pr;
address target = address(pr);
function attack() public {
// The attacker creates multiple addresses
for (uint i = 0; i < 100; i++) {
// The attacker calls the selectWinner function from each address
(bool success,) = target.call(abi.encodeWithSignature("selectWinner()"));
require(success, "Attack failed");
}
}
}

In this example, the attacker creates a contract that calls the selectWinner function from multiple addresses. This could potentially influence the rarity calculation.

Impact

The impact of this vulnerability would be that the attacker could potentially manipulate the rarity of the tokens they win, which could disrupt the fairness of the raffle and impact the perceived value of the tokens.

Tools Used

manual code review

Recommendations

  • To mitigate this vulnerability, consider implementing additional checks or using a more secure source of randomness in the selectWinner function. For example as explained in my high severity findings, you could use a commit-reveal scheme or an off-chain oracle to generate random numbers.

  • Additionally, consider making the selectWinner function only callable by the contract owner or a trusted party to prevent potential manipulation by attackers.

Updates

Lead Judging Commences

Hamiltonite Lead Judge about 2 years 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.

Give us feedback!