r/ethdev • The Officious BokkyPooBah • Oct 09 '18

Information We Got Spanked: What We Know So Far

From https://medium.com/spankchain/we-got-spanked-what-we-know-so-far-d5ed3a0f38fe :

In short, the attack capitalized on a “reentrancy” bug, much like the one exploited in The DAO. The attacker created a malicious contract masquerading as an ERC20 token, where the “transfer” function called back into the payment channel contract multiple times, draining some ETH each time.

The malicious contract first called createChannel to set up the channel, then called LCOpenTimeout repeatedly via reentrancy. The LCOpenTimeout is there to allow users to quickly exit payment channels which have not yet been joined by the counter-party.

The LCOpenTimeout transfers the user their initial ETH deposit balance and their token deposit balance, both initially set in the createChannel function. Critically, the LCOpenTimeout function only deletes the on-chain channel data (which zeroes out channel balances) after the token transfer function. This allows the malicious contract’s transfer function to call LCOpenTimeout in a loop, each time sending the attacker ETH equivalent to their channel balance.

The reentrancy internal transactions - https://etherscan.io/txsInternal?a=0xc5918a927c4fb83fe99e30d6f66707f4b396900e

From https://etherscan.io/address/0xf91546835f756da0c10cfa0cda95b15577b84aa7#code , the vulnerable code:

function LCOpenTimeout(bytes32 _lcID) public {
    require(msg.sender == Channels[_lcID].partyAddresses[0] && Channels[_lcID].isOpen == false);
    require(now > Channels[_lcID].LCopenTimeout);

    if(Channels[_lcID].initialDeposit[0] != 0) {
        Channels[_lcID].partyAddresses[0].transfer(Channels[_lcID].ethBalances[0]);
    }
    if(Channels[_lcID].initialDeposit[1] != 0) {
        require(Channels[_lcID].token.transfer(Channels[_lcID].partyAddresses[0], Channels[_lcID].erc20Balances[0]),"CreateChannel: token transfer failure");
    }

    emit DidLCClose(_lcID, 0, Channels[_lcID].ethBalances[0], Channels[_lcID].erc20Balances[0], 0, 0);

    // only safe to delete since no action was taken on this channel
    delete Channels[_lcID];
}

The DAO reentrancy exploitation occurred with the call.value(...) call, with all remaining gas passed to the ETH receiving contract - http://hackingdistributed.com/2016/06/18/analysis-of-the-dao-exploit/.

The Channels[_lcID].partyAddresses[0].transfer(Channels[_lcID].ethBalances[0]) above limits gas to 2,300 gas to prevent reentrancy.

The exploiter's Channels[_lcID].token.transfer(...) was instead used in this reentrancy exploit.

(I'm not associated with the SpankChain project, but posted this as it highlights a vector of attack when executing code from untrusted token contracts)

68 Upvotes

Duplicates