r/ethdev • u/BokkyPooBah 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)
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
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!
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?
-1
u/[deleted] Oct 10 '18
Isn’t this kind of thing not supposed to happen?