NFT
ERC721 and ERC20 are two different token standards on the Ethereum blockchain. Unlike ERC20, ERC721 is a non-fungible token (NFT) that allows each token to have unique properties and value. This makes it suitable for creating digital artwork, game items, and other unique assets. The lifecycle of an NFT typically includes issuance, circulation, and destruction phases. Compared to the issuance mechanism of ERC20 tokens, NFT issuance has some unique considerations, with security issues primarily focused on the issuance process.
To ensure that no one can obtain more valuable NFTs through unfair means and to provide a fair competition environment for all participants, NFTs are typically minted based on random numbers. If pseudo-random numbers are used, it can lead to predictability attacks, compromising the fairness of NFT minting.
The purchase of NFTs usually involves pre-sales and official sales. During the pre-sale phase, project teams often set up whitelists for users who can participate. However, even with such measures in place, there can still be issues related to signature reuse and impersonation.
The following are examples of common and more significant vulnerabilities in NFT projects.
Minting NFTs Based on Weak Randomness
Vulnerability Category: Weak Randomness.
In the NFT minting function "luckyMint", users input a number called "luckyNumber", and if it matches the pseudo-random number "randomNumber" generated on the chain, they can mint an NFT. The pseudo-random number "randomNumber" is generated based on the blockhash and block.timestamp. However, since blockchain properties are public, for transactions within the same block, the obtained blockhash and block.timestamp will be the same. Malicious users can exploit smart contracts to deterministically obtain the "randomNumber", thereby fulfilling the condition for minting an NFT.
Suggestion: To mitigate this attack, it is recommended to consider using a more secure random number generation mechanism. One viable solution is to implement Chainlink VRF (Verifiable Random Function).
Flash Loan Attacks in NFTs
The following function implements the token claim function. The second check condition uses alpha.balanceOf() and beta.balanceOf() to determine the caller's ownership of BAYC/MAYC NFTs. However, this approach only provides the instantaneous state of the user's ownership of the NFT, which can be manipulated using flash loans. Flash loans allow users to borrow a large amount of funds and manipulate market prices within the same transaction without providing any collateral. Attackers can exploit flash loans to borrow funds, manipulate prices, and create slippage issues. They then repay the borrowed funds and interest in the same transaction to ensure there is no loan balance at the end of the transaction.
For example, let's consider the APE Coin airdrop incident that occurred on March 17, 2022. The attack transaction involved the following steps: The attacker purchased a BAYC NFT with the ID 1060 and transferred it to the attack contract. The attacker borrowed a large amount of BYAC tokens using a flash loan. During this process, the attacker used the redeem function to obtain 5 additional BYAC NFTs with the IDs 7594, 8214, 9915, 8167, and 4755. The attacker used a total of 6 NFTs to claim the airdrop. The NFT with ID 1060 was their initial purchase, and the remaining 5 were acquired in the previous step. Through the airdrop, the attacker received a total of 60,564 APE tokens as a reward. The attacker needed to repay the borrowed BYAC tokens, so they minted BYAC NFTs to obtain the necessary BYAC tokens. In this process, they also minted their own NFT with ID 1060 because additional BYAC tokens were required to pay the flash loan fee. They then sold the remaining BYAC tokens after repaying the fee, generating 14 ETH. The attacker gained 60,564 APE tokens worth $500,000, with an attack cost of the 1060 NFT (purchased for 106 ETH) minus the 14 ETH obtained from selling the BYAC tokens.
The APE Coin airdrop mechanism has a vulnerability. Specifically, the eligibility for claiming the airdrop depends on the instantaneous state of a user's ownership of BYAC NFTs, which the attacker can manipulate by borrowing BYAC tokens through a flash loan and then redeeming them for BYAC NFTs. The attacker first borrows BYAC tokens through a flash loan, redeems them for BYAC NFTs, uses these NFTs to claim the APE airdrop, and finally mints BYAC tokens using the BYAC NFTs to repay the flash loan.
Suggestion: There is a more secure way to validate a user's ownership of an NFT, which involves using an off-chain snapshot based on a Merkle tree. This approach stores the whitelist on the project's central server off the blockchain. When a user clicks on "mint" on the frontend website, the server generates a Merkle proof based on the user's wallet address. The user then sends a transaction to the smart contract carrying the Merkle proof, which is then verified on-chain by the smart contract. However, this method also has potential security risks, including signature reuse and impersonation issues.
Signature Reuse and Forgery Issues in Whitelist Verification
The following code is used to verify authorization information and issue presale NFTs. This code has two security issues: signature reuse and signature forgery.
Signature reuse refers to the situation where the same signature can only be used once. In general, the project party will set up a mapping structure in the contract to store whether the signature has been used or not. The code is shown below. In this code, "bytes" represents the hash signature data, and the boolean value represents whether the signature has been used. However, the mint_approved function does not update the storage of this value, thus causing the issue of signature reuse.
Suggestion: To address the issue of signature reuse, it is recommended to add this mapping structure in the code.
Signature forgery: The signature can be forged due to the lack of msg.sender verification when passing the vData type info as a parameter. The specific whitelist verification function "verify" is shown below. The whitelist is stored on a centralized server. When users click on "mint" on the frontend NFT website, the server first verifies if the user is on the whitelist. If they are, the server uses the project party's private key to sign the data and returns the signature. Finally, the user sends the transaction with the signature to the chain for verification. Therefore, the ecrecover() verification here only verifies the project party's address, which can only prove that the signature data was issued by the project party. However, since there is no verification of the caller, i.e., info.from, in the signature data, it allows for signature forgery.
Suggestion: To address the issue of signature forgery, it is recommended to add verification of the signature data caller.
Last updated