{"id":1149,"date":"2017-09-28T15:28:06","date_gmt":"2017-09-28T15:28:06","guid":{"rendered":"https:\/\/davidgerard.co.uk\/blockchain\/?p=1149"},"modified":"2017-10-15T14:43:20","modified_gmt":"2017-10-15T14:43:20","slug":"etherdelta-hack-malicious-javascript-in-a-contract-name-stealing-the-users-key","status":"publish","type":"post","link":"https:\/\/davidgerard.co.uk\/blockchain\/2017\/09\/28\/etherdelta-hack-malicious-javascript-in-a-contract-name-stealing-the-users-key\/","title":{"rendered":"The EtherDelta hack: malicious JavaScript in a contract name, stealing the user&#8217;s private key"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1179\" src=\"https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2017\/09\/etherdelta.png\" alt=\"\" width=\"1600\" height=\"400\" srcset=\"https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2017\/09\/etherdelta.png 1600w, https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2017\/09\/etherdelta-300x75.png 300w, https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2017\/09\/etherdelta-768x192.png 768w, https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2017\/09\/etherdelta-1024x256.png 1024w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/p>\n<p><a href=\"https:\/\/etherdelta.com\/\">EtherDelta<\/a> is a somewhat decentralised cryptocurrency exchange. You can&#8217;t cash out to actual money, but you can swap assorted <a href=\"https:\/\/davidgerard.co.uk\/blockchain\/icos-magic-beans-and-bubble-machines\/\">ERC-20 tokens<\/a>, including ones too small-time to get onto an exchange.<\/p>\n<p>(Whenever a critic points out that the gateway between exchanges and actual money is the obvious control point for cryptocurrencies, advocates will cite EtherDelta as a <em>totally decentralised<\/em> counterexample \u2014 even though that doesn&#8217;t answer the objection, unless the corner shop started accepting ICO tokens when I wasn&#8217;t looking.)<\/p>\n<p>EtherDelta is a couple of megabytes of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Minification_(programming)\">minified<\/a> JavaScript \u2014 only the <a href=\"https:\/\/raw.githubusercontent.com\/etherdelta\/etherdelta.github.io\/master\/js\/main.js\">minified version<\/a> is available, not the original source\u00a0\u2014 with a smart contract backing it. To use it, you go to the site, put in your Ethereum address and private key, then deposit some of your ETH into their contract. <a href=\"https:\/\/www.reddit.com\/r\/EtherDelta\/comments\/6hrvwl\/how_fees_work\/\">Fees<\/a> are approximately 0.3% of volume. You can also use <a href=\"https:\/\/www.reddit.com\/r\/EtherDelta\/comments\/6hrgbh\/how_to_get_started_using_metamask\/\">MetaMask<\/a>, a Chrome browser plugin that runs an Ethereum wallet, rather than putting your private key in directly. (MetaMask users apparently weren&#8217;t susceptible to the present hack.)<\/p>\n<p>Decentralisation is always more costly than a centralised approach, and this is <a href=\"http:\/\/hackingdistributed.com\/2017\/08\/13\/cost-of-decent\/\">true here as well<\/a>. Trades happen on-chain, so there&#8217;s opportunities for <a href=\"https:\/\/en.wikipedia.org\/wiki\/Arbitrage\">arbitrage<\/a>, miners <a href=\"https:\/\/en.wikipedia.org\/wiki\/Front_running\">front running<\/a> traders, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Race_condition\">race conditions<\/a> and slow order cancellations. And despite being provably worse than a centralised exchange, the &#8220;decentralised&#8221; exchange is still controlled by a single entity; it&#8217;s not like <a href=\"https:\/\/www.fincen.gov\/\">FinCEN<\/a> can&#8217;t just call <a href=\"https:\/\/zackcoburn.github.io\/\">Zack Coburn<\/a> about EtherDelta&#8217;s KYC\/AML compliance.<\/p>\n<p>You can trade <em>any<\/em> ERC-20 token at EtherDelta, including ones they don&#8217;t know about yet. You just put the contract address into the web page address. So, for example,\u00a0<a href=\"https:\/\/etherdelta.com\/#0x27f706edde3ad952ef647dd67e24e38cd0803dd6-ETH\">https:\/\/etherdelta.com\/#0x27f706edde3ad952ef647dd67e24e38cd0803dd6-ETH<\/a> lets you trade <a href=\"https:\/\/uetoken.com\/\">Useless Ethereum Token<\/a> versus ETH.<\/p>\n<p>It turns out that EtherDelta <a href=\"https:\/\/github.com\/etherdelta\/etherdelta.github.io\/issues\/143\">didn&#8217;t sanitise inputs<\/a>, to make sure program code embedded in the data couldn&#8217;t run. In this case, when it scanned a token&#8217;s contract address for its name, it didn&#8217;t sanitise the name. So you could make it run JavaScript of your choice. With that you could do anything that JavaScript could do, <em>e.g.<\/em>, steal someone&#8217;s Ethereum private key from their browser session.<\/p>\n<p>The thief posted links to what they claimed were blog posts on http:\/\/emotionaltrader.tumblr.com\/ , which immediately redirected to a Google redirect, and then to a link to EtherDelta with the exploit (which has been disabled). They apparently netted several thousand dollars.<\/p>\n<p>Various <a href=\"https:\/\/www.reddit.com\/r\/EtherDelta\/comments\/72vltl\/malicious_urls_redirecting_to_ed_with_embedded\/\">suggestions<\/a> have been posted as to how to trade\u00a0securely on EtherDelta. You can be sure approximately 0% of users will do anything beyond reloading the web page, because &#8220;be your own bank!&#8221; means &#8220;be your own financial system chief security officer and researcher,&#8221; and users just assume that <em>someone else<\/em> has done the security legwork to decompile and inspect a smart contract or a huge pile of minified JavaScript. Meanwhile, they pick up their crypto trading tips at <a href=\"https:\/\/medium.com\/@rleshner\/security-vulnerability-etherdelta-10556d6e72a\">4chan \/biz<\/a>.<\/p>\n<p>Christian Montoya <a href=\"https:\/\/hackernoon.com\/how-one-hacker-stole-thousands-of-dollars-worth-of-cryptocurrency-with-a-classic-code-injection-a3aba5d2bff0\">describes<\/a> the hack, and some of the efforts to <a href=\"https:\/\/medium.com\/@decktonic\/following-the-trail-what-we-know-about-the-hacker-behind-the-etherdelta-attack-9ac6015fc2e1\">track down<\/a> the perpetrator.<\/p>\n<p>Further security problems with EtherDelta should be expected. &#8220;Uptrenda&#8221; at Reddit \/r\/buttcoin <a href=\"https:\/\/www.reddit.com\/r\/Buttcoin\/comments\/72ymk5\/bobby_tables_attack_on_etherdelta_someone_put\/dnmc2yi\/\">describes<\/a> how EtherDelta is fully up to <a href=\"https:\/\/davidgerard.co.uk\/blockchain\/ethereum-smart-contracts-in-practice\/\">previous standards<\/a> for Ethereum smart contracts <a href=\"https:\/\/davidgerard.co.uk\/blockchain\/the-dao\/\">involving money<\/a>:<\/p>\n<div class=\"usertext-body may-blank-within md-container \">\n<div class=\"md\">\n<blockquote><p>I was looking at the EtherDelta code not long ago and concluded it was too terrible to save.<\/p>\n<p>The trade engine is so closely coupled with the transaction code that it&#8217;s impossible to re-use it for anything else; There seems to be no simple way to test the software or indeed any unit tests at all; The UI is literally a cluster f\u2014 of code with no clear separation; The smart contract is a monolithic file where the author has apparently never heard of a module before; The smart contract can only be used for one purpose and the fee logic is so tightly coupled that it reads like spaghetti code; Nothing is documented, there is no documentation at all.<\/p>\n<p>I am glad we abandoned this software. I got as far as installing it and did some tests but I could see that everything would have to be re-written from scratch with solid engineering if we wanted to use it for anything practical.<\/p>\n<p>I almost forgot the best part: the EtherDelta &#8220;smart contract&#8221; has race conditions on every order where a person can race to take the same order. This means that it&#8217;s impossible to calculate the price of an asset reliably (it actually incentivizes <a href=\"https:\/\/en.wikipedia.org\/wiki\/Sybil_attack\">sybil attacks<\/a>) or scale the exchange to any amount of volume. I am honestly surprised that the exchange even works at all given these issues.<\/p>\n<p>Obviously none of its financial code has been audited by anyone to my knowledge. It was thrown online by some &#8220;solidity developer&#8221; who doesn&#8217;t even understand how a trade engine works&#8230; I wonder just how much gas has already been wasted due to race conditions or how many people lost money from its multiple asymmetric pricing vulnerabilities. I think bot authors would love this exchange as there are highly malicious trading strategies that would yield insane profits if you know what you&#8217;re doing.<\/p><\/blockquote>\n<\/div>\n<\/div>\n<p>It turns out that &#8220;I know JavaScript! How hard could running an exchange be?&#8221; is the new <a href=\"https:\/\/davidgerard.co.uk\/blockchain\/2017\/09\/17\/kim-nilsson-of-wizsec-how-the-bitcoins-were-stolen-from-mt-gox\/\">&#8220;I know PHP! How hard could running an exchange be?&#8221;<\/a><\/p>\n<p>The attacker later updated the code of their malicious smart contract:<\/p>\n<blockquote>\n<pre>f`[\u00a4 DATA &lt;script&gt; alert(\"powned\") &lt;\/script&gt;<\/pre>\n<\/blockquote>\n<p>&nbsp;<\/p>\n<br><br><div align=\"center\"><p><a href=\"https:\/\/www.patreon.com\/bePatron?u=8420236\"><img src=\"https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2021\/10\/become_a_patron_button.svg\" alt=\"Become a Patron!\" title=\"Become a Patron!\" width=217 height=51><\/a><br><p style=\"align:center;\" class=\"patreon-badge\"><i>Your subscriptions keep this site going. <a href=\"https:\/\/www.patreon.com\/bePatron?u=8420236\">Sign up today!<\/a><\/i><\/p><\/div>","protected":false},"excerpt":{"rendered":"<p>Plus a review of the EtherDelta smart contract&#8217;s coding and security. Summary: it&#8217;s pretty bad.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[155,113,112,114,115],"class_list":["post-1149","post","type-post","status-publish","format-standard","hentry","category-uncategorised","tag-rbuttcoin","tag-christian-montoya","tag-etherdelta","tag-uptrenda","tag-zack-coburn"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts\/1149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/comments?post=1149"}],"version-history":[{"count":44,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts\/1149\/revisions"}],"predecessor-version":[{"id":1202,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts\/1149\/revisions\/1202"}],"wp:attachment":[{"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/media?parent=1149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/categories?post=1149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/tags?post=1149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}