The TrickOrTreat::trickOrTreat function uses predictable variables (block.timestamp, msg.sender, nextTokenId, block.prevrandao) for its RNG implementation, allowing malicious users to calculate the exact random number in advance and consistently obtain NFTs at half-price.
Critical RNG vulnerability in TrickOrTreat::trickOrTreat function allows users to predict the generated number and force NFT purchases at half price. This exploit undermines the intended pricing mechanism and could result in significant protocol losses. By correctly guessing the random number, attackers can repeatedly trigger discounted purchases, effectively creating a persistent price manipulation vector.
Here is the malicious code TrickOrTreat::TrickOrTreat function:
Malicious users can exploit predictable RNG to purchase unlimited NFTs at 50% discount, leading to substantial financial losses and undermining the protocol's pricing mechanism.
To create a test folder and a file named TestTrickOrTreat.t.sol for your Foundry project, you can follow these steps:
Create the test folder: Navigate to your project directory and create a test folder.
Create the TestTrickOrTreat.t.sol file: Inside the test folder, create a file named TestTrickOrTreat.t.sol.
Add the Foundry test code: Here’s an example of what you might include in your TestTrickOrTreat.t.sol file:
Then run the test on your terminal:
Foundry test, Manual Review
Consider generating random numbers off-chain, either using services like Chainlink VRF or using your own internal server.
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.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.