Trick or Treat

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

Possible Frontrunning Attacks Allowing Manipulation of Pricing Outcomes

The contract's random pricing mechanism in the trickOrTreat function is susceptible to frontrunning attacks. Since the randomness relies on predictable block variables, attackers can manipulate transaction ordering or timing to increase their chances of receiving favorable pricing, such as consistently obtaining half-price treats or avoiding double-price tricks.

Vulnerability Details

Randomness is generated using the following code:

uint256 random = uint256(
keccak256(
abi.encodePacked(block.timestamp, msg.sender, nextTokenId, block.prevrandao)
)
) % 1000 + 1;

Issues:

  • Predictable Variables: block.timestamp and block.prevrandao are semi-predictable and can be influenced by miners or validators.

  • Transaction Ordering: Attackers can monitor the mempool and adjust gas prices to influence when their transactions are mined.

  • Manipulation by Validators: Validators can alter block.timestamp within a permissible range and may control block.prevrandao, affecting the outcome of the randomness.

Impact

  • Economic Exploit: Attackers can game the system to consistently receive treats at half price or avoid paying double price, leading to financial losses for the contract owner.

  • Unfair Advantage: Honest users are disadvantaged as they cannot manipulate transaction timing or block variables.

  • Reduced Trust: Perception of unfairness may lead to loss of user confidence in the platform.

Proof of Concept

  1. Monitoring Mempool:

    An attacker observes pending transactions and predicts upcoming block variables.

  2. Adjusting Transaction Timing:

    By setting a higher gas price, the attacker ensures their transaction is included in the next block.

  3. Influencing Block Variables:

    If the attacker is a validator or colludes with one, they can manipulate block.timestamp and block.prevrandao to produce a random number that results in favorable pricing.

  4. Consistent Exploitation:

    Repeating this process allows the attacker to consistently benefit from the most advantageous pricing.

Recommendations

  • Implement Secure Randomness:

    Use a reliable source of randomness like Chainlink VRF (Verifiable Random Function) which provides verifiable and tamper-proof randomness.

    import "@chainlink/contracts/src/v0.8/VRFConsumerBase.sol";
    contract SpookySwap is VRFConsumerBase {
    // Implement VRF logic here
    }
  • Commit-Reveal Scheme:

    Introduce a two-step process where users commit to a purchase with a hash of a secret value and later reveal the secret. This reduces the chance of manipulation.

  • Randomness Delayed Usage:

    Use randomness from future blocks, making it harder for attackers to predict or influence the outcome.

  • Limit Transaction Ordering Influence:

    Encourage users to include a standard gas price, reducing the ability of attackers to front-run by paying higher gas fees.

Updates

Appeal created

bube Lead Judge 10 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.