本文へ移動
Contact

TECHFUND
Media

Hi AUDIT
2024/5/2

Pike Finance Exploit $1.6 million | Hack Analysis

Pike Finance, a cross-chain lending protocol, was exploited again in less than a week, leading to losses exceeding $1.6 million. The attack primarily targeted the Ethereum, Arbitrum (ARB), and Optimism (OP) blockchains.

Summary

New upgraded contract Introduce new dependency like addition of new variables.

the new variables which altered the storage layout — in particular, the position of the initialized variable.

As a result, the position occupied by the initialized variable was taken over by other variables, leading to a misalignment in storage mapping.

This misalignment caused the contract to behave as if it was uninitialized, since the initialized variable could no longer be accessed variable couldn't be accessed anymore.

 Breakdown

- The attacker executed the initialize function in the original contract ( spoke ) , adding their address to the _isActive variable.

- As you know proxied contracts do not use of a constructor, it’s normal to move constructor logic to an external initializer function, usually called initialize.

- so it important to protect this initializer function so it can only be called once.

- The initialization use a version number. Once a version number is used, it is consumed and cannot be reused.

attacker then executes an upgradeToAndCall, enabling them to upgrade to a malicious implementation of the contract.

Root Causes

so basically attacker perform these operation

1. firstly attacker executed the initialize function, adding their address to the _isActive variable due to misalignment in storage mapping

function initialize(address _console, address_rng, address vault, address _gameProvider, uint16 minPos, uint16 maxPos) public nonPayable {
    require(msg.data.length == 4192);

    vev1 = !stor_b_2_2;
    if (!stor_b_2_2) {
        vev2 = stor_b_1_1;
    }

    if (tv) {
        if (tbool(this.code.size)) {
            v01 = stor_b_1_1;
        }
    }

    vev3 = this.code.size;
    require(ve, Error('Initializable: contract is already initialized'));
    stor_b_1_1 = 11;

    if (!stor_b_2_2) {
        stor_b_2_2 = 1;
    }

    gateway = console;
    owner_2_0_19_rng = _endpoint_vault;
    stor_4_0_19_gameProvider = _gameProvider;
    IsActive = 0x1 | (bytes31(msg.sender) & 0xffffffffffffffffffffffffffffff000000000000000000000000ffffffffee & _maxPos | (0xfffffffffff24424224224224224242242424242244242442442442224224224424242224222200 & minPos & 0xfffffffffffffff44444444424242424444444444444444444444444424424222200));
    nativeAsset;

    if (!stor_b_2_2) {
        stor_b_2_2 = 201;
        emit Initialized(1);
    }
}

2. Then attacker could then perform an upgradeToAndCall and upgrade to a malicious implementation

3. Attackers were then able to upgrade the spoke contracts, then bypass admin access control

4. As a result, he withdraw funds. and drain $1.6 million.

How upgradeToAndCall Works ?

1. Upgrade: The function first updates the address of the implementation contract to which the proxy forwards calls. This address points to the new version of the contract containing updated logic.

2. Initialization Call: After upgrading the implementation address, upgradeToAndCall executes a specified function in the context of the new implementation. This is typically used to set initial states, configurations, or perform necessary migrations that align with the new logic.

Read more  Hedgey Finance Exploit $44.7 million

Author : Piyush shukla

Hi AUDIT

Hi AUDIT is a high speed, high quality, best price security audit service, providing automated audit SaaS and consulting for web3 startups.
View More