Trick or Treat

First Flight #27
Beginner FriendlyFoundry
100 EXP
View results
Submission Details
Severity: low
Invalid

Weak Source of Randomness

Summary

The SpookySwap contract is an ERC721 NFT contract that allows users to participate in a "Trick or Treat" feature. Users can call the trickOrTreat function to purchase a "treat" NFT at a cost in ETH.

The random number generation relies on insecure and manipulable sources — specifically block.timestamp, msg.sender, nextTokenId, and block.prevrandao. These values can be predicted or influenced by miners and users, allowing an attacker to manipulate the outcome of the random number generation.

Vulnerability Details

The contract's method of generating randomness is insecure and can be manipulated by attackers. The random number generation relies on predictable and controllable variables, allowing malicious users to influence the outcome in their favor. This vulnerability enables attackers to consistently obtain NFTs at half price or avoid paying double price, leading to unfair advantages and potential financial losses for the contract owner.

The random number is generated using the following code snippet in the trickOrTreat function:

// Generate a pseudo-random number
uint256 random =
uint256(keccak256(abi.encodePacked(block.timestamp, msg.sender, nextTokenId, block.prevrandao))) % 1000 + 1;

Impact

The vulnerability allows attackers to manipulate the randomness in the SpookySwap contract, giving them an unfair advantage over other users. By manipulating the random number, attackers can increase their chances of getting the treat at half price, reducing revenue for the contract owner. Attackers can prevent scenarios where they have to pay double price, ensuring they never overpay. Contract owner will get reduced revenue from sales due to attackers paying less than intended.

Tools Used

Manual review

Recommendations

To securely generate randomness, it is recommended to use an external oracle or a verifiable random function (VRF), such as Chainlink VRF. This provides a tamper-proof source of randomness that cannot be predicted or manipulated by users or miners.

Updates

Appeal created

bube Lead Judge 9 months ago
Submission Judgement Published
Invalidated
Reason: Known issue
Assigned finding tags:

[invalid] Weak randomness

It's written in the README: "We're aware of the pseudorandom nature of the current implementation. This will be replaced with Chainlink VRF in later builds." This is a known issue.

Support

FAQs

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