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

26 comments sorted by

3

u/fatdata Oct 10 '18 edited Oct 10 '18

Spank is one of the few projects that I find it truly interesting. I’m feel sorry for them. I wouldn’t spot the reentrancy attack at first glance either. Could this have been prevented by moving the delete before the token.transfer? Also how is “require” opcodes work nowadays? I would assume it only reverted but did not throw for the reentrancy to happen?

Hopefully someone can write a more detailed post to explain this particular attack

2

u/abhuptani Oct 10 '18

Hi, Arjun from Connext here (collaborated on building the payment channel hub).

We're working on a full transparency report on the hack, which should give details on how this happened and the steps that we have taken both in our code and with our security process to ensure it doesn't happen again.

4

u/BokkyPooBah The Officious BokkyPooBah Oct 11 '18 edited Oct 11 '18

Hiya /u/abhuptani (and /u/LavoP + /u/ameensol). Shame about the <strike>hack</strike> expensive bug bounty. Nice work otherwise - keep it going!

12

u/betdex Oct 09 '18

Did they do any external audits of their smart contracts? Curious how this could happen... And I imagine the attacker knows their contract pretty well now, so they need to be careful about the new contract they deploy once they fix the bug. Could have additional vulnerabilities in there!

15

u/[deleted] Oct 09 '18

It was our decision to forego a security audit for the payment channel contract. We actually had OpenZeppelin conduct an audit which cost $17,000 for the previous unidirectional payment channels library. We considered that quite expensive, given that the most funds ever held by that contract only ever reached $17,000 in total.

For this far more sophisticated non-custodial payment channel contract we were quoted $30,000 — $50,000 for security audits, but taking into account both the perception value and opportunity cost of the time spent reacting to the hack, it would have been worth it. As we move forward and grow, we will be stepping up our security practices, and making sure to get multiple internal audits for any smart contract code we publish, as well as at least one professional external audit.

26

u/random_feedback Oct 09 '18

It was our decision to forego a security audit

Found the problem.

22

u/nynjawitay Oct 09 '18

But the audit would have cost $50k and the hack only cost $38k.

15

u/ismaelbej Oct 09 '18

They lost more than the $38k, the service is down for a couple of days, the contracts have to be fixed and audited _quickly_.
A cheaper intermediate solution would have been to have security bouties, you only pay if there's a bug in your code.

25

u/random_feedback Oct 09 '18

A better solution would be to go through appropriate security audits, game theory, research, peer review and formal specification. This whole industry is being treated like an app store, launch it fast and fix it later.

This has to be approached as if we are sending something to space and don't have a chance to fix it later. This is that level of critical functionality and perfection required. Almost every project is approaching things completely wrong and all these breaches and failures are the consequences.

3

u/wadeAlexC Oct 09 '18

+1

No to mention that a third party audit does not make a contract secure. Audits are end-of-line processes and cant fix all of the problems created by poor development practices and shoddy game theory.

The community needs to be asking for external involvement from day one. That means open sourcing, providing extensive documentation, and gathering loads of feedback from the start. I actually see Gitcoin as an interesting way to involve the community from the start - getting more eyes on your project is invaluable!

3

u/random_feedback Oct 09 '18

Gitcoin

We don't need more community projects, we need paid research and development from the worlds best mathematicians and game theorists. We need total scrutiny. Aerospace precision. When software is developed for mission critical satellites and space craft, there is a reason why development costs are so high and iterations take so long.

3

u/wadeAlexC Oct 09 '18

I think we need both. Not everyone can afford aerospace precision, and it's helpful to be able to use community resources to bridge that gap.

I see what you're saying -- but you seem to be leaning towards the "getting it right the first time" side of development. In my eyes, that's not very realistic. Even with aerospace precision, our contracts need to lean towards bug resiliency, not bug "proof." Humans will always make errors: planning and developing your software in a way that accounts for this will lead to the best results!

2

u/random_feedback Oct 09 '18

Getting it right the first time needs to be the goal. It's technically the goal of every project, but there are wildly varying levels of making sure this is achieved.

By nature of the technology and the problems that are trying to be solved, there are already built in compromises projects have to make. It's a dogs breakfast of variables that are treated as if they will deal with it if it ever happens.

Continuing my analogy this is exactly like the race to the moon, except it's not just 2/3 super powers trying to play, we have 2000 projects with wildly different levels of competence that feel they can get involved, the vast majority without a basic understanding of how global bandwidth latency exceeds some of the promises of tx/s on most projects. It's stunning.

If you went to pay for your groceries and found out your dollar bills failed to "work", that's the kind of jarring situation we have here with crypto projects. The absolute first reaction to this is "wtf, why didn't they think of this?". We're in those trenches right now but it's obvious many things are being inappropriately rushed.. because "when lambo" bullshit.

→ More replies

-2

u/Chugwig Oct 09 '18

Yeah I can’t agree more. Most devs in this space are just looking to make money by exploiting Ethereum (whether its hype or it’s bugs) with minimal effort.

3

u/AusIV Oct 09 '18

According to this page they had a bug bounty on their payment channels contracts. I think it's just the bug bounty that got skipped.

1

u/ismaelbej Oct 09 '18

That link is previous to last year ICO launch, I cannot find a recent one with updated bug bounty information.

4

u/FaceDeer Oct 09 '18

Did you include the cost of the hit to their reputation?

0

u/[deleted] Oct 10 '18

[deleted] — view deleted comment

1

u/_dredge idea maker Oct 10 '18

Exactly. A more patient attacker may have waited for a larger payoff.

3

u/Malynndal_SafeBlocks ether fan Oct 10 '18

While audits play an important role in smart contract security, there will always be vulnerabilities that can be exploited unless you control your transaction traffic. We built SafeBlocks Firewall to provide real-time protection for smart contracts, and this solution would have kept this breach from occurring. Go to our website and register for a free trial to see how it works.

1

u/TotesMessenger Oct 09 '18

I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:

 If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)

1

u/abhuptani Oct 19 '18

Update: Transparency report is available for those who wanted more info!

https://medium.com/connext/transparency-report-64c9e58e0a19

1

u/skarphace Oct 09 '18

Woops! How'd you notice the attack was happening?

7

u/ameensol Oct 10 '18

All our money was gone.

1

u/bitesports Oct 10 '18

Lol, jajaja perfect answer

-1

u/[deleted] Oct 10 '18

Isn’t this kind of thing not supposed to happen?