<?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/"
	>

<channel>
	<title>Labix Blog &#187; Security</title>
	<atom:link href="http://blog.labix.org/tag/security/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.labix.org</link>
	<description>by Gustavo Niemeyer</description>
	<lastBuildDate>Mon, 16 Jan 2012 04:02:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Breaking into an Android password manager &#8211; Practice</title>
		<link>http://blog.labix.org/2009/12/05/breaking-into-an-android-password-manager-practice</link>
		<comments>http://blog.labix.org/2009/12/05/breaking-into-an-android-password-manager-practice#comments</comments>
		<pubDate>Sun, 06 Dec 2009 00:38:11 +0000</pubDate>
		<dc:creator>Gustavo Niemeyer</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Puzzle]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=230</guid>
		<description><![CDATA[In the last post, we&#8217;ve seen some security issues which exist in the Android password manager gbaSafe version 1.1.0a, by analyzing the security description provided in its web site. As described there, even though the system depends on a &#8220;master &#8230; <a href="http://blog.labix.org/2009/12/05/breaking-into-an-android-password-manager-practice">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://blog.labix.org/2009/12/01/breaking-into-an-android-password-manager-theory">the last post</a>, we&#8217;ve seen some security issues which exist in the Android password manager <a href="http://www.gbizapps.com/gbasafe.htm">gbaSafe</a> version 1.1.0a, by analyzing the security description provided in its web site.  As described there, even though the system depends on a &#8220;master key&#8221; which might be secure, the security of the system is seriously compromised by the encouragement of very weak keys (a few <i>digits</i> only) in what is named an &#8220;unlock key&#8221;, used to encrypt the master key itself.  All of that in an application which claims to strongly <i>protect</i> people&#8217;s data from unwanted eyes.</p>
<p>In this post, we will play a bit with the Linux-based Android OS to actually explore these security deficiencies, demonstrating that such issues are very real, and that the claims of being hard to unveil the data is unfounded.  Since the most serious weakness lies in the key itself, we&#8217;ll run a simple brute force attack to try to find arbitrary unlock keys.</p>
<p><span id="more-230"></span></p>
<p>This procedure is actually mentioned by the author of gbaSafe himself in the web page, except he overestimates the work involved in producing such a mechanism:</p>
<blockquote><p>
Theoretically, somebody could write a program that tries to decrypt the master key by trying all possible values of the short key (with 4 digits there are only 10000 possibilities), but this would still be much work, as more information about the crypting algorithm is needed (e.g. salt bytes, iteration count).
</p></blockquote>
<p>So let&#8217;s get started.</p>
<p>As a first step, we&#8217;ll need the <a href="http://developer.android.com/sdk/index.html">Android SDK</a> with a working emulator (I&#8217;ve used API level 5, revision 1), and a copy of the application itself.  I got a <a href="http://andappstore.com/AndroidApplications/apps/gbaSafe_Password_Safe">trial version of the application</a> from <a href="http://andappstore.com/">AndAppStore.com</a>.</p>
<p>The application downloaded is bundled within that <i>.apk</i> file, which is really a <i>.zip</i> file that may be opened up normally with any tool which understands this file format.</p>
<p>Once that&#8217;s done, we get access to all the information needed to run the application, including icons, interface layouts, and most importantly in this case, the bytecode which targets the <a href="http://en.wikipedia.org/wiki/Dalvik_virtual_machine">Dalvik VM</a>.   This bytecode is the end result of a sequence of translations which happen when the program&#8217;s Java source code is compiled, so that&#8217;s what we&#8217;ll have to fiddle with to figure details of the application we want to investigate.</p>
<p>The bytecode is located inside the <i>classes.dex</i> file, and as expected it&#8217;s not easy to read in its native format.  Luckily, though, a smart guy has already written a couple of tools, <a href="http://code.google.com/p/smali/">smali and baksmali</a>, which allow people to decompile and recompile that bytecode format to/from something that is much easier to understand.</p>
<p>After downloading these tools, the following command should decompile the file:</p>
<blockquote><p>
$ java -jar baksmali.jar &#8211;output classes classes.dex
</p></blockquote>
<p>We now have a <i>classes/</i> directory full of <i>.smali</i> files.</p>
<p>Before going any further, let&#8217;s ponder for a moment about what we want to do.  A brute force attack is when we attempt sequentially many possible keys, and given the context already presented, what we&#8217;re looking after is to attempt different &#8220;unlock keys&#8221;.  With that in mind, we&#8217;ll introduce a very small modification in the application so that it will attempt to enter the unlock key automatically, rather than reporting an error when the key entered in the unlock dialog is invalid.</p>
<p>With that in mind, after some quick research, it looks like the <i>onClick()</i> method within the <i>DlgUnlock.smali</i> file is a pretty good candidate.  This is supposedly called when the button in the unlock dialog is clicked, so something interesting about the password being correct or not must happen there.</p>
<p>Before doing anything there, I&#8217;ve increased the number of registers in the function to 12, to get some additional registers to play with, and then initialized a register with the value zero, to serve as a monotonically increasing number (our keys!):</p>
<blockquote><p>
 .method public onClick(Landroid/view/View;)V<br />
-    .registers 9<br />
+    .registers 12<br />
     .parameter &#8220;view&#8221;<br />
+    const/16 v9, 0&#215;0
</p></blockquote>
<p>Then, we have to instruct the program to use these keys rather than whatever is typed in the dialog box.  Some lines down, we&#8217;ll see a call to the <i>checkUnlockKey()</i> method, which is certainly what we&#8217;re looking after.  Let&#8217;s do this now:</p>
<blockquote><p>
+    :mykey<br />
+    invoke-static {v9}, Ljava/lang/String;->valueOf(I)Ljava/lang/String;<br />
+    move-result-object v2<br />
     invoke-static {v2}, Lcom/gbizapps/safeA/Crypt;->checkUnlockKey(Ljava/lang/String;)I
</p></blockquote>
<p>Now, what if this key is wrong?  We don&#8217;t want the master key to be removed as mentioned in the software description.  We want to simply attempt the next key.  With some analysis, we see that in case of errors, the next couple of lines below the above code will instruct the VM to jump to an error branch.  Rather than following up with the normal error logic, we&#8217;ll increment the key, and jump back to the above code:</p>
<blockquote><p>
     :cond_6c<br />
+    add-int/lit8 v9, v9, 0&#215;1<br />
+    goto :mykey
</p></blockquote>
<p>Now we just have to rebundle this and put it into the emulator.  I won&#8217;t go over it in too much detail here, since there&#8217;s plenty of information available online, but the steps to do that are:</p>
<ol>
<li>Recreate a modified classes.dex with <i>smali</i>
<li>Recreate a modified <i>.apk</i> file by just zipping the modified content
<li>Sign and zipalign the new <i>.apk</i> file
<li>Install it
</ol>
<p>And that&#8217;s it, seriously!  This would be enough to break the software security if it was working <i>correctly</i>.</p>
<p>Interestingly, though, the software <i>wasn&#8217;t</i> working correctly with this change.  Instead, it was <i>Force Closing</i> on certain keys.   To test it out, use the master key &#8220;master key&#8221;, and the unlock key &#8220;999999&#8243;, and then once you close and open the application again, try to unlock it with the key &#8220;1175&#8243;.  Instead of showing an error message, it will break badly.</p>
<p>Now, for the proof of concept to work, I actually had to <i>fix</i> the bug, which felt a bit funny to do given the context.</p>
<p>Looking at the traceback trough <i>adb logcat</i>, I found out that there was a null being dereferenced in the file <i>Crypt.smali</i>, so I fixed the problem by injecting some error checking at this position and jumping the flow into an existing error branch:</p>
<blockquote><p>
+    if-eqz v3, :cond_5a<br />
     const-string v4, &#8220;ucpmhkexov85MDKhdfdfFGQPYxywq7209fcrqhghjkuiopy&#8221;
</p></blockquote>
<p>With this in place came the biggest surprise of the experiment. The keys which were crashing the application were special, in the sense that they actually <i>decode the master key successfully</i>!  That&#8217;s right: whatever the algorithm is doing, that six-digit &#8220;999999&#8243; encrypts the master key in such a way that attempting the &#8220;1175&#8243; key works, so even big keys are rendered extremely weak with the logic used to encrypt the master key.</p>
<p>At this point, I added some trivial logic to display the key found with a <a href="http://developer.android.com/reference/android/widget/Toast.html">Toast</a>, just to ensure the whole thing was working correctly:</p>
<p><img src="http://blog.labix.org/wp-content/uploads/2009/12/device.png" alt="Toast displaying unlock key found" title="Toast displaying unlock key found" width="320" height="480" class="aligncenter size-full wp-image-242" /></p>
<p>Note that the key generation implemented above is a bit simplistic, in the sense that it doesn&#8217;t attempt keys with leading zeros, but this would be trivial to implement, and my intention here isn&#8217;t to actually break any keys for real, but just to show how the promised security in this application is not to be trusted at all.  Just the logic above will already be enough for a brute force attack against the application, and has broken all the keys I&#8217;ve tried in mere seconds, in a <i>slow emulator</i>.</p>
<p>As a conclusion, if you want to put your data in a secure place, rather than picking an application which promises security because <i>the salt is hidden somewhere</i> or because it&#8217;s <i>too much work to figure its logic</i>, pick an open source application with logic which is publicly verifiable and has already had many eyes over it.  Chances are that doing something like what was described in this post won&#8217;t be so trivial.  Then, <i>choose your keys wisely</i>!  The most secure application won&#8217;t be enough if you pick a bad key.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2009/12/05/breaking-into-an-android-password-manager-practice/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Breaking into an Android password manager &#8211; Theory</title>
		<link>http://blog.labix.org/2009/12/01/breaking-into-an-android-password-manager-theory</link>
		<comments>http://blog.labix.org/2009/12/01/breaking-into-an-android-password-manager-theory#comments</comments>
		<pubDate>Wed, 02 Dec 2009 01:33:16 +0000</pubDate>
		<dc:creator>Gustavo Niemeyer</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Puzzle]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=203</guid>
		<description><![CDATA[For some time now I&#8217;ve been wanting to research more deeply about the internals of Android. Until now, though, this was just a sentiment. Then, a couple of weeks ago I&#8217;ve finally managed to replace my iPhone for an Android &#8230; <a href="http://blog.labix.org/2009/12/01/breaking-into-an-android-password-manager-theory">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>For some time now I&#8217;ve been wanting to research more deeply about the internals of <a href="http://www.android.com">Android</a>.  Until now, though, this was just a sentiment.  Then, a couple of weeks ago I&#8217;ve finally managed to replace <a href="http://blog.labix.org/2009/06/30/my-iphone-for-an-android">my iPhone for an Android</a> phone, and that was the final motivator for me to actually get into learning more about the inner workings of the Linux-based OS.</p>
<p>Now, I just had to pick an actual task for digging into.  The Dalvik VM is certainly one of the most innovative and advertised technical details about the OS, so something around it would be a nice start.. some kind of bytecode fiddling perhaps, but what?  Luckily, even without trying too hard, I eventually stumbled upon an interesting case for researching upon.</p>
<p>The &#8220;victim&#8221; of this research is the application <a href="http://www.gbizapps.com/gbasafe.htm">gbaSafe</a> version 1.1.0a, which claims to protect user passwords using <i>unbreakable algorithms</i> (how&#8217;s that for a hint of a <a href="http://en.wikipedia.org/wiki/Snake_oil_(cryptography)">Snake oil</a> case?).</p>
<p><span id="more-203"></span></p>
<p>Before we get into some hacking, let&#8217;s see some words on the software security by the author himself, and then render some analysis on conceptual issues on it:</p>
<blockquote><p>
The confidential data can only be decrypted if the master key is known. You should choose a long key (at least 16 characters) with mixed case and unreadable text. Of course you cannot enter this key each time you want to access the confidential data, so it is stored in the user settings encrypted with a shorter key (4 to 6 digits) and normally you only have to enter this unlock key. Theoretically it is possible to try all possible values (brute force attack), but then you must use another program, since gbaSafe deletes the encrypted master key from the user settings when you enter the unlock key wrong three times repeatedly, and then you must enter the master key. If you wrote a program to decrypt the master key, you would have to know the algorithm used, the salt bytes and iteration count (used to augment the short unlock key), which are very hard to extract from the binary program module gbaSafe.
</p></blockquote>
<p>If you have some security background, I&#8217;m sure that by now you&#8217;re already counting the issues on this single paragraph.</p>
<p>The most obvious issue is the fact that there&#8217;s a &#8220;strong key&#8221; and a &#8220;weak key&#8221;, and the strong key is encrypted with the weak one.  This is a very common cryptography sin, as would say my friend and coworker Andreas Hasenack (a security researcher himself).  A security system is only as secure as its weakest spot.  It obviously makes little difference for an attacker if he has to attempt decrypting a master key or the actual data, since decrypting the master key will give access to the data.</p>
<p>Then, it mentions en passant that the software <i>enforces</i> the use of <i>digits</i> for the weak key.  This ensures that the weak key is <i>really</i> weak!  Four digits is basically ten thousand attempts, which is absolutely nothing for nowadays&#8217;s hardware.  This number would move up to about 15 million by simply allowing upper and lowercase letters as well (which isn&#8217;t great either, but a few orders of magnitude never hurt in this scenario).</p>
<p>It follows up encouraging people to think that it&#8217;s actually hard to figure the algorithm and other implementation details.  Considering that there&#8217;s absolutely nothing preventing people from getting their hands in the implementation itself, this is in fact asserting that the security mechanism is based on the ignorance of the attacker.  Counting on the ignorance of people is bad at all times, and in a security context it&#8217;s a major error.</p>
<p>There&#8217;s a final security issue in this description which is a bit more subtle, but further analysis on the logic used leaves no doubt.  In cryptography, the <a href="http://en.wikipedia.org/wiki/Salt_(cryptography)">salt</a> is supposed to increase the work needed in a brute force attack by strengthening the number of bits of the actual passphrase, in a case where the salt is <i>actually</i> unavailable, or at least prevent that a single large word dictionary can be used to attack several encryptions or hashes at once, in a case where the salt is known but variable.  In the latter case, it helps because encrypting a single key with two different salts must be done twice, rather than once, so it increases the computational task when attacking multiple items.  A salt which is known and does not change across all processed items is worth pretty close to nothing.</p>
<p>So, indeed, considering the many security issues here, this isn&#8217;t something I&#8217;d store my passwords or credit card numbers on, and I suggest you don&#8217;t do it either.</p>
<p>In my next post on this topic I&#8217;ll actually implement a trivial brute force attack to prove that these issues are <i>very</i> real, and that, actually, it&#8217;s not even hard to break into a security system like this.</p>
<p>The application author has been contacted about this blog post, since he&#8217;ll likely want to fix some of these issues.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2009/12/01/breaking-into-an-android-password-manager-theory/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

