The SpookySwap
smart contract includes a refund mechanism within the trickOrTreat
and resolveTrick
functions. However, these functions are vulnerable to reentrancy attacks due to their usage of the call
function for issuing refunds to users. This vulnerability allows a malicious actor to re-enter the contract, potentially draining funds or minting multiple NFTs.
In SpookySwap
, when excess funds are sent by a user to trickOrTreat
or resolveTrick
, the contract attempts to refund the surplus Ether by calling:
Although this code is standard for issuing refunds, it is vulnerable to reentrancy attacks. Here’s how an attacker could exploit this:
The call
function can be hijacked by a malicious contract, which can initiate a fallback function that re-enters trickOrTreat
or resolveTrick
before the original function call completes.
By repeatedly re-entering and calling trickOrTreat
or resolveTrick
, the attacker can drain the contract’s funds through repeated refunds.
The attacker could also trigger multiple NFT mints by re-entering the contract functions and manipulating the state of nextTokenId
, leading to unauthorized minting.
Below is the specific portion of the contract code where the refund mechanism is implemented:
The same approach is used in the resolveTrick
function:
A malicious contract can repeatedly call trickOrTreat
or resolveTrick
, receiving a refund each time. By implementing a fallback function that recursively re-enters these functions, the attacker can drain funds or mint multiple NFTs.
Deploy SpookySwap
contract in Hardhat.
Create a malicious contract with a fallback function that re-enters trickOrTreat
to trigger repeated refunds.
Here’s the Hardhat test simulating the reentrancy attack:
The malicious contract below re-enters trickOrTreat
upon receiving a refund:
Output indicates that the attacker has drained the contract’s funds through repeated refunds.
A reentrancy attack of this nature allows malicious users to:
Drain Contract Funds: Repeatedly trigger refunds until all funds in the contract are depleted.
Mint Unauthorized NFTs: If the contract allows minting NFTs as part of the trickOrTreat
function, the attacker could also mint numerous unauthorized NFTs.
Damage User Trust: Exploiting this vulnerability could lead to severe financial losses and erode user trust, especially in high-value contracts.
Manual review.
Using transfer
or send
instead of call
limits the gas provided to the receiving address to 2300, which is insufficient to perform a reentrant call.
Here’s an example with these mitigations:
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.