<?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>Apkudo &#187; Hacking</title>
	<atom:link href="http://mspotten.com/wordpress/category/hacking/feed/" rel="self" type="application/rss+xml" />
	<link>http://mspotten.com/wordpress</link>
	<description></description>
	<lastBuildDate>Mon, 05 Jan 2015 21:04:08 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Instrumentation 101 &#8211; Must have tips for Android developers</title>
		<link>http://mspotten.com/wordpress/instrumentation-101-must-have-tips-for-android-developers/</link>
		<comments>http://mspotten.com/wordpress/instrumentation-101-must-have-tips-for-android-developers/#comments</comments>
		<pubDate>Tue, 18 Dec 2012 22:56:01 +0000</pubDate>
		<dc:creator><![CDATA[mspotten]]></dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[Hacking]]></category>
		<category><![CDATA[AnDevCon]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Monkey]]></category>
		<category><![CDATA[MonkeyRunner]]></category>
		<category><![CDATA[Robotium]]></category>
		<category><![CDATA[UIAutomator]]></category>

		<guid isPermaLink="false">http://blog.apkudo.com/?p=841</guid>
		<description><![CDATA[Thanks to everyone who came out to our class! It was great meeting and chatting with all of you Android developers. If you missed your chance to check out our 30 minute primer at AnDevCon IV, you&#8217;ll find the details of our Android Instrumentation 101 talk in this post along with download links for the demos and scripts used in the presentation. You can download the presentation here. Testing can be boring and tedious, but its role is very important. In the long run you&#8217;ll save time fixing bugs in your application. Fortunately, Android supports many tools for testing. The...]]></description>
				<content:encoded><![CDATA[<p>Thanks to everyone who came out to our class! It was great meeting and chatting with all of you Android developers. If you missed your chance to check out our 30 minute primer at <a href="www.andevcon.com" target="_blank">AnDevCon IV</a>, you&#8217;ll find the details of our Android Instrumentation 101 talk in this post along with download links for the demos and scripts used in the presentation. You can <a href="http://www.slideshare.net/Apkudo/apkudo-instrumentation101final" target="_blank">download the presentation here</a>.</p>
<p>Testing can be boring and tedious, but its role is very important. In the long run you&#8217;ll save time fixing bugs in your application. Fortunately, Android supports many tools for testing. The ones we&#8217;ll cover include Monkey, MonkeyRunner, Robotium, and UIAutomator.</p>
<p><strong>Monkey</strong></p>
<p><a href="http://developer.android.com/tools/help/monkey.html" target="_blank">Monkey</a> is a simple and easy to use command line tool that is part of the Android SDK.</p>
<p>Monkey&#8217;s only purpose is stress testing your application. This means testing the things you would normally not test or think to test. Monkey provides a stream of random input events while your application is running with the primary goal of determining how your app preforms under such a load. This will test all kinds of strange input events and I assure you, your app will crash at some point under a corner case that you never imagined.</p>
<p>Monkey is not for testing specific app features and it is not smart (smart being, scriptable &#8211; you don’t tell Monkey what to do). Finally, Monkey is not a “cure-all.” It won&#8217;t find the bugs that other types of testing will find.</p>
<p>Monkey is simple, easy, and while not terribly exciting, it&#8217;s most definitely useful.</p>
<p><a href="http://www.youtube.com/watch?v=HAzvAQU3HpQ" target="_blank">Monkey in action.</a></p>
<p><strong>MonkeyRunner</strong></p>
<p>In addition to stress testing, you may want to test a specific sequence of inputs. <a href="http://developer.android.com/tools/help/monkeyrunner_concepts.html" target="_blank">MonkeyRunner</a> allows you to easily script your tests.</p>
<p>MonkeyRunner is a smarter Monkey in that it allows for better structured tests. You can do functional and regression testing using MonkeyRunner.<br />
Some of its features include support for scripts written in Python, ability to take screenshots of the device, and the MonkeyRunner API is extensible. On the downside, it isn&#8217;t fast.</p>
<p>Things you&#8217;ll need to do:<br />
• Learn Python<br />
• Be aware that MonkeyRunner uses Jython. This means that your Python scripts can interface with java classes.</p>
<p>Understand how to use the three main modules of MonkeyRunner:<br />
• MonkeyRunner – Connect to device<br />
• MonkeyDevice – Call functions on the device (examples below)<br />
• MonkeyImage – Take screenshots</p>
<p>The MonkeyRunner module allows your to connect to your device through a device object (MonkeyDevice). The device object allows you to call various methods, like:<br />
• installPackage<br />
• touch (pass in touch type)<br />
• takeSnapshot<br />
• startActivity</p>
<p><a href="http://www.youtube.com/watch?v=-Oex1ElexTg" target="_blank">MonkeyRunner in action.</a></p>
<p><strong>Robotium</strong></p>
<p><a href="http://code.google.com/p/robotium/" target="_blank">Robotium</a> is a powerful tool and API for testing your Android applications.</p>
<p>Robotium is more in depth and more robust than the tools mentioned above. The basic idea of Robotium is to create a testing APK that will exercise your target app through test cases that you write. In that way, it is very much like any standard testing framework. Robotium’s tag line is, “Like Selenium for Android,” so if you come from a web development background Robotium should be familiar ground for you. One more thing to note, Robotium is very fast which is a great boon to the testing experience.</p>
<p>On the downside, Robotuim requires some boilerplate. You’re going to have to create an APK, build with the Robotium JAR, and in general, be aware of the internals of the app you wish to test. That being said, it is quite powerful and, some might say, the de-facto testing tool for Android apps.</p>
<p>Robotium requires the Robotium JAR which can easily be downloaded from the Robotium website.</p>
<p>You’re going to need to know your APK’s package name and signature, though, you should have both if you are developing the app you wish to test.</p>
<p>Tests are set up by extending the main test class and writing methods for each test case. The Robotium Solo class provides the interface for testing on the device. That is, all of your testing commands will be issued from a solo object. As is the norm with testing, you’ll want to use assert to ensure test results.</p>
<p>If you take a look at the API later you’ll see that Robotium provides a bunch of methods, allowing you to do a lot with the app (switching activities being quite useful).</p>
<p>Finally, Robotium can be run from Eclipse or the command line (your preference) and provides output regarding test case success, failure, and error.</p>
<p><a href="http://www.youtube.com/watch?v=Y9WddhQZKPA" target="_blank">Robotium in action. </a></p>
<p><strong>UIAutomator</strong></p>
<p><a href="http://developer.android.com/tools/help/uiautomator/index.html" target="_blank">UIAutomator</a> is a testing framework that has been added fairly recently.</p>
<p>It is part of the Android SDK revision 21 and up and requires your test device to have Android API version 16 or higher.</p>
<p>UIAutomator consists of:<br />
• A command-line tool found on the device<br />
• A Java library (uiautomator.jar found in the sdk/tools/) which you use in your tests<br />
• The Android SDK tool UIAutomatorViewer which comes in handy for writing your tests<br />
• The ability to reference UI Objects by name</p>
<p>UIAutomator comes with the UIAutomatorViewer tool that captures screenshot of the device and lists all the UI objects (great for identifying which components you want to test).</p>
<p><a href="http://www.youtube.com/watch?v=KLOa4aE1TGs" target="_blank">UIAutomater in action.</a></p>
<p>To see examples on using each tool, here are some of our presentation files:<br />
<a href="http://blog.apkudo.com/2012/12/18/instrumentation-101-must-have-tips-for-android-developers/instrumentation101/" rel="attachment wp-att-850">Instrumentation101.zip</a></p>
<p>So far the tools we’ve seen, with the exception of Robotium, have done black box testing of the UI. However white box testing, whereby you test units internally, is just as important (there are a whole host of other tools that fill this role that we&#8217;ll cover in a future blog). For now, this is a primer on the types of tools available, what they are capable of,  and the roles that they fill. We hope we’ve provided a good launching point for you to get started.</p>
<p>The biggest thing to take away from the presentation and this blog is that you should make a habit of using some combination of these tools to test your apps. Develop a testing framework and stick to it. You are all programmers, so it should be no trouble for you to automate your tests. Don’t forget to test before every pull request!</p>
<p>Have any questions? Let us know in the comments.</p>
<p>Happy hacking,<br />
Alex Bannerjee and Daniel Joyce</p>
]]></content:encoded>
			<wfw:commentRss>http://mspotten.com/wordpress/instrumentation-101-must-have-tips-for-android-developers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reverse Engineering Android: Disassembling Hello World</title>
		<link>http://mspotten.com/wordpress/reverse-engineering-android-disassembling-hello-world/</link>
		<comments>http://mspotten.com/wordpress/reverse-engineering-android-disassembling-hello-world/#comments</comments>
		<pubDate>Tue, 16 Oct 2012 23:16:10 +0000</pubDate>
		<dc:creator><![CDATA[mspotten]]></dc:creator>
				<category><![CDATA[Hacking]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[baksmali]]></category>
		<category><![CDATA[HelloWorld]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[smali]]></category>

		<guid isPermaLink="false">http://blog.apkudo.com/?p=775</guid>
		<description><![CDATA[When it comes to learning Android, it&#8217;s amazing how easy it is to find tutorials, code samples, and documentation to immerse yourself into. Interestingly, I&#8217;ve found the inverse to be true for the, dare I say, way cooler world of hacking Android. Reverse engineering Android applications can be really fun and give you a decent knowledge for the inner workings of the Dalvik Virtual Machine. This post will be an all-out, start-to-finish, beginners* tutorial on the tools and practices of reverse engineering Android through the disassembly and code injection of the Android Hello World application. *Beginner means that you know...]]></description>
				<content:encoded><![CDATA[<p>When it comes to learning Android, it&#8217;s amazing how easy it is to find tutorials, code samples, and documentation to immerse yourself into. Interestingly, I&#8217;ve found the inverse to be true for the, dare I say, way cooler world of <strong><em>hacking</em></strong> Android. Reverse engineering Android applications can be really fun and give you a decent knowledge for the inner workings of the <a href="http://en.wikipedia.org/wiki/Dalvik_(software)" target="_blank">Dalvik Virtual Machine</a>. This post will be an all-out, start-to-finish, beginners* tutorial on the tools and practices of reverse engineering Android through the disassembly and code injection of the <a href="http://developer.android.com/training/basics/firstapp/index.html" target="_blank">Android Hello World application</a>.</p>
<p><em>*Beginner means that you know a bit about Android and Java in general, if not, <a href="http://developer.android.com/training/index.html" target="_blank">learn</a> a bit first and come back. Experience in the terminal environment on your machine is also probably necessary.</em></p>
<h4>The Apk</h4>
<p>In order to start reverse engineering, you must first understand what you&#8217;re working with. So what exactly is an apk? (hint: not <a href="http://www.americanparkour.com/" target="_blank">American Parkour</a>.) An <a href="http://en.wikipedia.org/wiki/APK_(file_format)" target="_blank">Android package</a>, or apk, is the container for an Android app&#8217;s resources and executables. It&#8217;s a zipped file that contains simply:</p>
<ul>
<li>AndroidManifest.xml (serialized, non human readable)</li>
<li>classes.dex</li>
<li>res/</li>
<li>lib/ (sometimes)</li>
<li>META-INF/</li>
</ul>
<p>The meat of the application is the classes.dex file, or the Dalvik executable (get it, dex) that runs on the device. The application&#8217;s resources (i.e. images, sound files) reside in the res directory, and the AndroidManifest.xml is more or less the link between the two, providing some additional information about the application to the OS. The lib directory contains native libraries that the application may use via <a href="http://developer.android.com/tools/sdk/ndk/index.html" target="_blank">NDK</a>, and the META-INF directory contains information regarding the <a href="http://developer.android.com/tools/publishing/app-signing.html" target="_blank">application&#8217;s signature</a>.</p>
<p>You can grab the HelloWorld apk we will be hacking <a href="http://www.davtbaum.com/bin/HelloWorld.apk">here</a>. The source to this apk is available from the <a href="http://developer.android.com/training/basics/firstapp/index.html" target="_blank">developer docs tutorial</a>, and when compiled looks something like this:</p>
<div id="attachment_150" style="width: 178px" class="wp-caption aligncenter"><a href="http://apkudo.files.wordpress.com/2012/10/helloworld.png" target="_blank"><img class="size-medium wp-image-150 " title="HelloWorld" alt="" src="http://apkudo.files.wordpress.com/2012/10/helloworld.png" height="300" width="168" /></a><p class="wp-caption-text">Flashy, huh</p></div>
<h4>The Tools</h4>
<p>In order to complete this tutorial, you&#8217;ll need to download and install the following tools:</p>
<ul>
<li><a href="http://code.google.com/p/android-apktool/downloads/list" target="_blank">apktool</a></li>
<li><a href="http://docs.oracle.com/javase/1.3/docs/tooldocs/win32/jarsigner.html" target="_blank">jarsigner</a></li>
<li><a href="http://docs.oracle.com/javase/6/docs/technotes/tools/windows/keytool.html" target="_blank">keytool</a></li>
</ul>
<div>Apktool does all of the disassembling/reassembling and wraps functionality from a lot of tools in the reverse engineering realm (smali/baksmali assembler, XML deserializers, etc). I&#8217;m not a _huge_ fan of the tool, but it&#8217;s a great way to get started. Jarsigner and keytool allow you to re-sign the application after it&#8217;s been disassembled. We&#8217;ll get into what the signing process does later on.</div>
<div></div>
<h4>Disassembling the Apk</h4>
<p>Once you&#8217;ve installed apktool, go ahead and open up your terminal and change directory into where you&#8217;ve placed the downloaded apk.</p>
<pre>$ cd ~/Desktop/HelloWorld</pre>
<p>Execution of the apktool binary without arguments will give you its usage, but we will only use the &#8216;d&#8217; (dump) and &#8216;b&#8217; (build) commandline options for this tutorial. Dump the apk using the apktool &#8216;d&#8217; option:</p>
<pre>$ apktool d HelloWorld.apk</pre>
<p>This will tell the tool to decode the assets and disassemble the .dex file in the apk. When finished, you will see the ./HelloWorld directory, containing:</p>
<ul>
<li>AndroidManifest.xml (decoded, human readable)</li>
<li>res/ (decoded)</li>
<li>smali/</li>
<li>apktool.yml</li>
</ul>
<p>The AndroidManifest.xml is now readable, the resources have been decoded, and a smali directory has been created (ignore the apktool.yml as it&#8217;s just a configuration for the tool itself). The smali directory is probably the most important of the three, as it contains a set of smali files, or bytecode representation of the application&#8217;s dex file. You can think of it as an intermediate file between the .java and the executable.</p>
<p>So let&#8217;s take a look at what&#8217;s in the smali directory , &#8216;ls&#8217; yields:</p>
<pre> $ ls HelloWorld/smali/com/test/helloworld/
HelloWorldActivity.smali
R$attr.smali
R$drawable.smali
R$layout.smali
R$string.smali
R.smali</pre>
<p>Immediately we notice that the smali directory contains subdirectories defining the application&#8217;s namespace (com.test.helloworld). Additionally, we can see an individual smali file for each java class. There&#8217;s one catch &#8211; any &#8216;$&#8217; in the smali file&#8217;s name means it&#8217;s an<a href="http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html" target="_blank"> inner class </a>in Java. Here we see the bytecode representation of the following classes:</p>
<ul>
<li>HelloWorldActivity.java</li>
<li>R.java</li>
</ul>
<p>Where R.java contains inner classes attr, string, and so on. It&#8217;s evident that HelloWorldActivity is the activity that&#8217;s displayed when the app launches, so what exactly is R?</p>
<p>R.java is an automatically generated file at application build time that maps resources to an associated id. When a developer wants to use anything in the res folder, he/she must use the R class to appropriately reference that resource. Because of this, we&#8217;ll omit the R.java from our investigation, as it really only contains a bunch of constants that no one cares about.</p>
<h4>Reading the Smali</h4>
<p>Now that we&#8217;ve disassembled our apk, let&#8217;s take a look at the java and smali representations of our impressive HelloWorldActivity.</p>
<pre>package com.test.helloworld;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloWorldActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView text = new TextView(this);
        text.setText("Hello World, Android");
        setContentView(text);
    }
}
.class public Lcom/test/helloworld/HelloWorldActivity;
.super Landroid/app/Activity;
.source "HelloWorldActivity.java"

# direct methods
.method public constructor ()V
    .locals 0

    .prologue
    .line 7
    invoke-direct {p0}, Landroid/app/Activity;-&gt;()V

    return-void
.end method

# virtual methods
.method public onCreate(Landroid/os/Bundle;)V
    .locals 2
    .parameter "savedInstanceState"

    .prologue
    .line 11
    invoke-super {p0, p1}, Landroid/app/Activity;-&gt;onCreate(Landroid/os/Bundle;)V

    .line 13
    new-instance v0, Landroid/widget/TextView;

    invoke-direct {v0, p0}, Landroid/widget/TextView;-&gt;(Landroid/content/Context;)V

    .line 14
    .local v0, text:Landroid/widget/TextView;
    const-string v1, "Hello World, Android"

    invoke-virtual {v0, v1}, Landroid/widget/TextView;-&gt;setText(Ljava/lang/CharSequence;)V

    .line 15
    invoke-virtual {p0, v0}, Lcom/test/helloworld/HelloWorldActivity;-&gt;setContentView(Landroid/view/View;)V

    .line 17
    return-void
.end method</pre>
<p>It should be pretty evident which one of these files is written in java, nonetheless, the smali representation shouldn&#8217;t be too intimidating.</p>
<p>Let&#8217;s break down whats going on here in java first.  In <strong>line 07</strong>, we define our HelloWorldActivity class that extends android.app.Activity, and within that class, override the onCreate() method. Inside the method, we create an instance of the TextView class and call the TextView.setText() method with our message. Finally, in <strong>line 15 </strong>we set the view by calling setContentView(), passing in the TextView instance.</p>
<p>In smali, we can see that we have a bit more going on. Let&#8217;s break it up into sections, we have:</p>
<ol>
<li>class declarations from <strong>lines 01-03</strong></li>
<li>a constructor method from <strong>lines 07-15</strong></li>
<li>a bigger onCreate() method from <strong>lines 19-43</strong></li>
</ol>
<div>
<h4>Declarations and Constructor</h4>
<p>The class declarations in smali are essentially the same in java, just in a different syntax. They give the virtual machine their class and superclass name via the .class and .super tags. Additionally, the compiler throws in the source file name for&#8230;shits and gigs? Nope, stack traces.</p>
<p>The constructor has seemingly appeared out of no where, but really was inserted by the compiler because we extended another class. You can see that in <strong>line 12</strong> the virtual machine is to make a direct invokation of the super classes constructor &#8211; this follows the nature of subclasses, they must call their superclasses constructor.</p>
<h4>Data Types</h4>
<p>In the onCreate() method beginning on <strong>line 19</strong>, we can see that the smali method definition isn&#8217;t that far off from its java counterpart. The method&#8217;s parameter types are defined within the parenthesis (semicolon separated) with the return type discreetly placed on the end of the .method line. Object return types are easy to recognize, given they begin with an L and are in full namespace. Java primitives, however, are represented as capital chars and follow the format:</p>
<pre>V	 void
Z	 boolean
B	 byte
S	 short
C	 char
I	 int
J	 long (64 bits)
F	 float
D	 double (64 bits)</pre>
<p>So for our onCreate() definition in smali, we can expect a void return value.</p>
<h4>Registers</h4>
<p>Moving one line down, on <strong>line 20</strong> we see the &#8216;.locals&#8217; directive. This determines how many registers the Dalvik vm will use for this method <strong>_without_</strong> including registers allocated to the parameters of the method. Additionally, the number of parameters for any virtual method <strong>will always be the number of input parameters + 1</strong>. This is due to an implicit reference to the current object that resides in parameter register 0 or p0 (<a href="http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html" target="_blank">in java this is called the &#8220;this&#8221; reference</a>). The registers are essentially references, and can point to both primitive data types and java objects. Given 2 local registers, 1 parameter register, and 1 &#8220;this&#8221; reference, the onCreate() method uses an effective 4 registers.</p>
<p>For convenience, smali uses a &#8216;v&#8217; and &#8216;p&#8217; naming convention for local vs. parameter registers. Essentially, parameter (p) registers can be represented by local (v) registers and will always reside in the highest available registers. For this example, onCreate() has 2 local registers and 2 parameter registers, so the naming scheme will look something like this:</p>
<pre>v0 - local 0
v1 - local 1
v2/p0 - local 2 or parameter 0 (this)
v3/p1 - local 3 or parameter 1 (android/os/Bundle)</pre>
<p><strong><em>Note: You may see the .registers directive as oppose to the .locals directive. The only difference is that the .registers directive includes parameter registers (including &#8220;this&#8221;) into the count. Given the onCreate() example, .locals 2 == .registers 4</em></strong></p>
</div>
<h4>Opcodes</h4>
<p>Dalvik opcodes are relatively straightforward, but there are a <a href="http://pallergabor.uw.hu/androidblog/dalvik_opcodes.html" target="_blank">lot of them</a>. For the sake of this post&#8217;s length, we&#8217;ll only go over the basic (yet important) opcodes found in our example HelloWorldActivity.smali. In the onCreate method in HelloWorldActivity the following opcodes are used:</p>
<ol>
<li><strong>invoke-super vx, vy, &#8230;</strong> invokes the parent classes method in object vx, passing in parameter(s) vy, &#8230;</li>
<li><strong>new-instance vx </strong>creates a new object instance and places its reference in vx</li>
<li><strong>invoke-direct vx, vy, &#8230;</strong> invokes a method in object vx with parameters vy, &#8230; without the virtual method resolution</li>
<li><strong>const-string vx</strong> creates string constant and passes reference into vx</li>
<li><strong>invoke-virtual vx, vy, &#8230;</strong> invokes the virtual method in object vx, passing in parameters vy, &#8230;</li>
<li><strong>return-void</strong> returns void</li>
</ol>
<h4>Hacking the App</h4>
<p>Now that we know what we&#8217;re looking at, lets inject some code and rebuild the app. The code we will inject is only one line in java and presents the user with the toast message &#8220;hacked!&#8221;.</p>
<pre>Toast.makeText(getApplicationContext(), "Hacked!", Toast.LENGTH_SHORT).show();</pre>
<p>How do we do this in smali? Easy, let&#8217;s just compile this into another application and disassemble. The end result is something like this:</p>
<pre>    .line 18
    invoke-virtual {p0}, Lcom/test/helloworld/HelloWorldActivity;-&gt;getApplicationContext()Landroid/content/Context;

    move-result-object v1

    const-string v2, "Hacked!"

    const/4 v3, 0x0

    invoke-static {v1, v2, v3}, Landroid/widget/Toast;-&gt;makeText(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;

    move-result-object v1

    invoke-virtual {v1}, Landroid/widget/Toast;-&gt;show()V</pre>
<p>Now, let&#8217;s ensure we have the right amount of registers in our original onCreate() to support these method calls. We can see that the highest register in the code we want to patch is v3, which we have but will require us to overwrite both of our parameter registers. Given we won&#8217;t be using either of those registers after setContentView(), this number is appropriate. Our final patched HelloWorldActivity.smali should look like:</p>
<pre>.class public Lcom/test/helloworld/HelloWorldActivity;
.super Landroid/app/Activity;
.source "HelloWorldActivity.java"

# direct methods
.method public constructor ()V
    .locals 0

    .prologue
    .line 8
    invoke-direct {p0}, Landroid/app/Activity;-&gt;()V

    return-void
.end method

# virtual methods
.method public onCreate(Landroid/os/Bundle;)V
    .locals 2
    .parameter "savedInstanceState"

    .prologue
    .line 12
    invoke-super {p0, p1}, Landroid/app/Activity;-&gt;onCreate(Landroid/os/Bundle;)V

    .line 14
    new-instance v0, Landroid/widget/TextView;

    invoke-direct {v0, p0}, Landroid/widget/TextView;-&gt;(Landroid/content/Context;)V

    .line 15
    .local v0, text:Landroid/widget/TextView;
    const-string v1, "Hello World, Android"

    invoke-virtual {v0, v1}, Landroid/widget/TextView;-&gt;setText(Ljava/lang/CharSequence;)V

    .line 16
    invoke-virtual {p0, v0}, Lcom/test/helloworld/HelloWorldActivity;-&gt;setContentView(Landroid/view/View;)V

    # Patches Start

    invoke-virtual {p0}, Lcom/test/helloworld/HelloWorldActivity;-&gt;getApplicationContext()Landroid/content/Context;

    move-result-object v1

    const-string v2, "Hacked!"

    const/4 v3, 0x0

    invoke-static {v1, v2, v3}, Landroid/widget/Toast;-&gt;makeText(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;

    move-result-object v1

    invoke-virtual {v1}, Landroid/widget/Toast;-&gt;show()V

    # Patches End

    return-void
.end method</pre>
<p><strong>Lines 40+</strong> contain the injected code.</p>
<h4>Rebuilding the Apk</h4>
<p>Now all that&#8217;s left is to rebuild the app!</p>
<pre>$ apktool b ./HelloWorld</pre>
<p>This will instruct apktool to rebuild the app, however, this rebuilt app will <strong>not</strong> be signed. We will need to sign the app before it can be successfully installed on any device or emulator.</p>
<h4>Signing the Apk</h4>
<p>In order to sign the apk, you&#8217;ll need jarsigner and keytool (or a platform specific alternative, like <a href="http://www.londatiga.net/it/how-to-sign-apk-zip-files/" target="_blank">signapk for windows</a>). With jarsigner and keytool, however, the steps are pretty easy. First create the key:</p>
<pre>$ keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -validity 10000</pre>
<p>Then use jarsigner to sign your apk, referencing that key:</p>
<pre>$ jarsigner -verbose -keystore my-release-key.keystore ./HelloWorld/dist/HelloWorld.apk alias_name</pre>
<p>Then you&#8217;re done! Install the app onto your device or emulator and impress the shit out of yourself!</p>
<div id="attachment_318" style="width: 178px" class="wp-caption aligncenter"><a href="http://apkudo.files.wordpress.com/2012/10/mm.png" target="_blank"><img class="size-medium wp-image-318 " title="Hello World Hacked" alt="" src="http://apkudo.files.wordpress.com/2012/10/mm.png" height="300" width="168" /></a><p class="wp-caption-text">damn&#8230;impressive</p></div>
<p>That&#8217;s it for this tutorial, but stay tuned. There will definitely be more in the future. Feel free to leave any questions in the comment section, or <a href="mailto:david@apkudo.com" target="_blank">contact </a>me with any questions.</p>
<p>Happy hacking,</p>
<p>David Teitelbaum</p>
]]></content:encoded>
			<wfw:commentRss>http://mspotten.com/wordpress/reverse-engineering-android-disassembling-hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python packages and plugins</title>
		<link>http://mspotten.com/wordpress/python-packages-and-plugins/</link>
		<comments>http://mspotten.com/wordpress/python-packages-and-plugins/#comments</comments>
		<pubDate>Sat, 06 Oct 2012 19:47:55 +0000</pubDate>
		<dc:creator><![CDATA[wyattkund]]></dc:creator>
				<category><![CDATA[Hacking]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.apkudo.com/?p=754</guid>
		<description><![CDATA[While we obviously do a lot of work on the innards of Android, putting together a backend infrastructure requires a lot of engineering. We use a number of different technologies for this, and from time to time come across stuff we think could be generally interesting to others (even if it isn&#8217;t Android focused). This post is focused on Python, which we use pretty extensively. We like keeping up with the new language developments, so most of our Python posts will be related to Python 3, not the old Python 2.7. One thing that can be a little confusing with...]]></description>
				<content:encoded><![CDATA[<p>While we obviously do a lot of work on the innards of Android, putting together a backend infrastructure requires a lot of engineering. We use a number of different technologies for this, and from time to time come across stuff we think could be generally interesting to others (even if it isn&#8217;t Android focused). This post is focused on Python, which we use pretty extensively. We like keeping up with the new language developments, so most of our Python posts will be related to Python 3, not the old Python 2.7.</p>
<p>One thing that can be a little confusing with Python is how packages work. Packages let you group your modules together and gives you a nice namespace. You can read all about them in the <a href="http://docs.python.org/tutorial/modules.html#packages" target="_blank">Python docs.</a></p>
<p>Something that&#8217;s pretty confusing is that importing a package <strong>does not</strong> mean that any modules inside that package are loaded.</p>
<p>Imagine a very simple package called <code>testing</code>, with a single <code>foo</code> module. E.g:</p>
<pre>testing/
    __init__.py
    foo.py</pre>
<p>The <code>foo</code> module might look something like:</p>
<pre>def bar():
    return 'bar'</pre>
<p>Now, you might expect to be able to write code such as:</p>
<pre>import testing
print(testing.foo.bar())</pre>
<p>However, trying this won’t work, you end up with an<strong><code>AttributeError</code></strong>:</p>
<pre>Traceback (most recent call last):
  File "t.py", line 2, in 
    testing.foo.bar()
AttributeError: 'module' object has no attribute 'foo'</pre>
<p>So, to fix this you need to actually import the module. There are (at least) two ways you can do this:</p>
<pre>import testing.foo
from testing import foo</pre>
<p>Either of these put <code>testing.foo</code> into <code>sys.modules</code>, and<code>testing.foo.bar()</code> will work fine.</p>
<p>But, what if you want to load all the modules in a package? Well, as far as I know there isn&#8217;t any built-in approach to doing this, so what we’ve come up with is a pretty simple function that, given a package, will load all the modules in the package, and return them as a dictionary keyed by the module name.</p>
<pre>def plugin_load(pkg):
    """Load all the plugin modules in a specific package.

    A dictionary of modules is returned indexed by the module name.

    Note: This assumes packages have a single path, and will only
    find modules with a .py file extension.

    """
    path = pkg.__path__[0]
    pkg_name = pkg.__name__
    module_names = [os.path.splitext(m)[0] for m in
                    os.listdir(path)
                    if os.path.splitext(m)[1] == '.py' and m != '__init__.py']
    imported = __import__(pkg_name, fromlist=module_names)
    return {m: getattr(imported, m) for m in module_names}</pre>
<p>There are plenty of caveats to be aware of here. It only works with modules ending in <code>.py</code>, which may miss out on some cases. Also, at this point it doesn’t support packages that span multiple directories (although that would be relatively simple to add). <strong>Note:</strong> code testing on Python 3.2, probably needs some modification to work on 2.x (in particular I don’t think <a href="http://www.python.org/dev/peps/pep-0274/">dictionary comprehensions</a> work in 2.x).</p>
<p>If you’ve got a better way for achieving this, please let us know in the comments.</p>
<p>Benno Leslie</p>
]]></content:encoded>
			<wfw:commentRss>http://mspotten.com/wordpress/python-packages-and-plugins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>One true adb_usb.ini to rule them all</title>
		<link>http://mspotten.com/wordpress/one-true-adb_usb-ini-to-rule-them-all/</link>
		<comments>http://mspotten.com/wordpress/one-true-adb_usb-ini-to-rule-them-all/#comments</comments>
		<pubDate>Tue, 21 Aug 2012 22:39:05 +0000</pubDate>
		<dc:creator><![CDATA[mspotten]]></dc:creator>
				<category><![CDATA[Hacking]]></category>
		<category><![CDATA[adb]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[user id]]></category>

		<guid isPermaLink="false">http://blog.apkudo.com/?p=676</guid>
		<description><![CDATA[We do a lot of work with adb here at Apkudo &#8211; obtaining and maintaining an adb connection to every Android device is no small task. We could go on and on about some of the more interesting trials and tribulations with adb &#8211; there are lots, but why not start with one of the the more boring (and most common) causes for problem? A missing USB vendor id in adb_usb.ini. You know the drill: you plug in a new Android device, run adb devices, and&#8230; crickets. Nothing. Then it&#8217;s a bit of messing with dmesg or lsusb to get the...]]></description>
				<content:encoded><![CDATA[<p>We do a lot of work with adb here at Apkudo &#8211; obtaining and maintaining an adb connection to every Android device is no small task. We could go on and on about some of the more interesting trials and tribulations with adb &#8211; there are lots, but why not start with one of the the more boring (and most common) causes for problem? A missing USB vendor id in adb_usb.ini. You know the drill: you plug in a new Android device, run adb devices, and&#8230; crickets. Nothing. Then it&#8217;s a bit of messing with dmesg or lsusb to get the vendor id, an edit to your ~/.android/adb_usb.ini to add it in, and you&#8217;re off and running again. A little annoying.</p>
<p>Here&#8217;s a way to ensure you&#8217;ll never have the issue again: we&#8217;ve created an adb_usb.ini containing every known Android vendor id! For good measure, we then went and threw in every other USB vendor on the planet &#8211; perhaps it will inspire them to some day create an Android device <img src="http://mspotten.com/wordpress/wp-includes/images/smilies/icon_smile.gif" alt=":-)" class="wp-smiley" /></p>
<p>To clone into your .android directory (for easy updating via git):</p>
<pre>$ cd ~/.android
$ rm adb_usb.ini
$ git init
$ git remote add origin git@github.com:apkudo/adbusbini.git
$ git pull origin master</pre>
<p>Alternatively, to just copy the latest file directly into your .android:</p>
<pre>$ curl -L http://raw.github.com/apkudo/adbusbini/master/adb_usb.ini 
 --O ~/.android/adb_usb.ini</pre>
<p>A parseable listing, in case you have other uses for the data, is included in VENDORS, using the following format:</p>
<pre>&lt;vendor_id&gt; &lt;is_android_vendor&gt; &lt;vendor_name&gt;</pre>
<p>For data sources, we used every Android device that we have (which is, we believe, all of them), the Linux USB listing at <a href="http://www.linux-usb.org/usb.ids">http://www.linux-usb.org/usb.ids</a>, and the USB listing at <a href="http://www.usb.org/developers/tools/comp_dump">http://www.usb.org/developers/tools/comp_dump</a>. If you&#8217;re interested, the tool used to scrape the web data sources is in adbusbini_scrape.py.</p>
<p>Updates are very much encouraged and appreciated &#8211; please either submit a pull request or email to <a href="mailto:josh@apkudo.com">josh@apkudo.com</a>.</p>
<p>Happy hacking,</p>
<p>Josh</p>
]]></content:encoded>
			<wfw:commentRss>http://mspotten.com/wordpress/one-true-adb_usb-ini-to-rule-them-all/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Run Google Play on an emulator and pretend to be any device</title>
		<link>http://mspotten.com/wordpress/run-google-play-on-an-emulator-and-pretend-to-be-any-device/</link>
		<comments>http://mspotten.com/wordpress/run-google-play-on-an-emulator-and-pretend-to-be-any-device/#comments</comments>
		<pubDate>Wed, 08 Aug 2012 17:23:05 +0000</pubDate>
		<dc:creator><![CDATA[mspotten]]></dc:creator>
				<category><![CDATA[Hacking]]></category>
		<category><![CDATA[adb]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[AVD]]></category>
		<category><![CDATA[baksmali]]></category>
		<category><![CDATA[Emulator]]></category>
		<category><![CDATA[Galaxy S III]]></category>
		<category><![CDATA[Google Play]]></category>
		<category><![CDATA[Jelly Bean]]></category>
		<category><![CDATA[smali]]></category>

		<guid isPermaLink="false">http://apkudo.wordpress.com/?p=510</guid>
		<description><![CDATA[Since the beginning of time man has sought to virtualize the human experience. We go to sleep and have life-like experiences in our dreams, we connect to internet sites like Chatroulette to virtually meet strangers (and inadvertently catch a glimpse of some old man balls from time to time), 1995 brought us the classic Denzel Washington/Russell Crowe sci-fi action thriller Virtuosity, and nerds gather in herds online to play StarCraft (myself included) and become virtual intergalactic heroes. The journey to virtualize reality has been a long time coming. The next logical step&#8230; to run Google Play on a virtual mobile device?...]]></description>
				<content:encoded><![CDATA[<p>Since the beginning of time man has sought to virtualize the human experience. We go to sleep and have life-like experiences in our dreams, we connect to internet sites like <a href="http://chatroulette.com/" target="_blank">Chatroulette</a> to virtually meet strangers (and inadvertently catch a glimpse of some old man balls from time to time), 1995 brought us the classic Denzel Washington/Russell Crowe sci-fi action thriller <a href="http://www.imdb.com/title/tt0114857/" target="_blank">Virtuosity</a>, and nerds gather in herds online to play <a href="http://en.wikipedia.org/wiki/StarCraft" target="_blank">StarCraft</a> (myself included) and become virtual intergalactic heroes. The journey to virtualize reality has been a long time coming. The next logical step&#8230; to run Google Play on a virtual mobile device? &#8220;Of course!&#8221; said no one.</p>
<p>Alas, our journey continues. Our odyssey starts with the awesome blog post &#8220;<a href="http://www.piotrbuda.eu/2012/05/installing-google-play-on-android-emulator.html" target="_blank">Installing Google Play on Android Emulator</a>.&#8221; The author, Piotr Buda, was able to successfully install the Google Play application on an emulator. However, you’ll notice that in the <a href="http://www.piotrbuda.eu/2012/06/trying-to-solve-error-491-in-play-store-on-android-emulator.html" target="_blank">comments and subsequent blog post</a> he (and his readers) were unable to download and install applications: the effects of the dastardly Error 491. Additionally, the applications they were able to view were severely limited. After reading both posts, the Apkudo team attempted to take it a step further and a) get downloads working and b) be able to browse and download any app.</p>
<p>Installing Google Play on an Android emulator requires three separate APKs: GoogleLoginService.apk, GoogleServicesFramework.apk, and Phonesky.apk (older versions will be named ‘Vending.apk’.) All three are located in the /system/app/ folder on your device. You can issue an adb pull command to pull each one off of your device (you generally don&#8217;t need root to pull). We pulled ours off of a Samsung Galaxy S III (the version of the store that we pulled was 3.5.16)</p>
<p>Next we just push the three APKs to install on the emulator (we made a simple modification to the shell script from the original blog post):</p>
<pre>#!/bin/sh
echo "remounting..."
adb remount
echo "pushing login apk..."
adb push GoogleLoginService.apk /system/app/.
echo "pushing framework apk..."
adb push GoogleServicesFramework.apk /system/app/.
echo "pushing vending apk..."
adb push Phonesky.apk /system/app/.
echo "done"</pre>
<p>Once the three APKs are installed, it is trivial to get the market executing. You&#8217;ll need to enter your account information when prompted and accept the market update if required (the update will generally not be successful, but it doesn&#8217;t seem to matter). You should now be able to browse and search the Play Store. Except, of course, you&#8217;ll usually hit an Error 491 when you try to download!</p>
<p><strong>Solving Error 491</strong></p>
<p>If you have created your AVD for ICS or earlier you will undoubtedly encounter Error 491 when attempting to download an application. The reason is that a shared object file, libdrmframework_jni.so is missing from the /system/lib/ folder. You may also find that the DrmProvider.odex and DrmProvider.apk files are missing from the /system/app/ folder. It seems that standard Android AVD images do not include these files. In order to circumvent this, you can build the Android platform from source (see <a href="http://source.android.com/source/initializing.html">http://source.android.com/source/initializing.html</a>); your AVDs should now include the relevant DRM files. (Note: pulling the .so off of your device and pushing them to the emulator will generally not work.) You can also, of course, just create an AVD for 4.1 Jelly Bean: AVDs that target 4.1 appear to include those missing files and should be able to download applications easily.</p>
<div style="width: 410px" class="wp-caption aligncenter"><img src="https://lh4.googleusercontent.com/cIBCcuyK3u9J2kmLq5-iUiZrtTK4O4RjQ2p5V_1zM4ubYYmUJ5sUL4pTXVc51_v_R-kVPECGVDtAfdp3JtEmuQ6xdwuPBA1AcKEm4i4WGEiG07zJEf0" alt="" width="400" height="260" /><p class="wp-caption-text">Google Play on an emulator.</p></div>
<div style="width: 404px" class="wp-caption aligncenter"><a href="http://apkudo.files.wordpress.com/2012/08/games.gif"><img title="games" src="http://apkudo.files.wordpress.com/2012/08/games.gif?w=300" alt="" width="394" height="256" /></a><p class="wp-caption-text">Move along, not too many apps to see here!</p></div>
<div style="width: 410px" class="wp-caption aligncenter"><img class="  " title="Emulator mocking a Samsung Galaxy Nexus" src="https://lh5.googleusercontent.com/sypYD4Vcrq_tBppmyQVqAP7IM0qA1ifytU2CWLwDbSoWj3eyg340JYaVItWod6UzAwELslUTqfaX10No_nFBWmwh58VrwbsutmZVf9CHwyzxR0_YFmY" alt="" width="400" height="260" /><p class="wp-caption-text">Downloading Solitaire. Side note: I&#8217;m pretty good at Solitaire.</p></div>
<p><strong>Circumventing Google Play Filters</strong></p>
<p>As you browse the Play Store you’ll notice a startling lack of available applications. The Google Play Store filters the apps that you can view based on a handful of factors relating to hardware, software, and carrier. You can find most filters listed here: <a href="http://developer.android.com/guide/google/play/filters.html">http://developer.android.com/guide/google/play/filters.html</a></p>
<p>Depending on which features you give your AVD, you will see a variety of available apps. However, you’ll still be unable to view a great deal of what the store has to offer simply because your AVD lacks a great many features. Therefore, we’ll need to get our hands dirty with the innards of the Google Play code (this could probably be accomplished in a few other ways, but this was the most practical.)</p>
<p>The basic idea is to capture and mock all of the device identifying information and metadata that Play compiles before it is sent off on the wire. You&#8217;ll first need to unzip each APK: <code>unzip {apk} -d {out_dir}</code>. Then, remove the META-INF folder within each (we&#8217;ll re-sign each APK later.) Use baksmali on the classes.dex of both Phonesky.apk and GoogleServicesFramework.apk. Now you&#8217;ll have access to the smali code for each APK.</p>
<p>Inside the  decompiled GoogleServicesFramework you&#8217;ll find a file entitled <code>/com/google/android/gsf/checkin/CheckinRequestBuilder.smali</code>. Poke around a bit, and you&#8217;ll notice that it contains a host of device identifying information, all of which we must intercept and mock. Inside the decompiled Phonesky you&#8217;ll find two files entitled <code>/com/google/android/finsky/utils/DeviceConfigurationHelper.smali</code> and <code>/com/google/android/finsky/utils/VendingUtils.smali</code> respectively. Again, each file contains device identifying information that Google Play will eventually serialize, send off into the cloud, and use to filter the apps that you can view on the store.</p>
<p>So now that we know what data is being sent, how do we intercept it and mock it?  First, you&#8217;re going to need data to mock. This is relativley simple, as you can pull most of that information from a real phone using some combination of <code>adb shell getprop</code>, <code>adb shell pm</code>, and <code>adb shell dumpsys</code>. On the other hand, you can build a simple application that pulls all of this data and dumps it out in a neat little file. The next step is to build a framework for mocking data. I built a java class composed entirely of static methods (it is very easy to hand insert into smali code (with some register finagling)) that I compiled and then decompiled back into smali. Inside each of the decompiled Google APKs, make the appropriate directories for your classes package, and copy over the smali file. You should now be able to call any of your static methods from Google code.</p>
<p>Now its a matter of using <code>grep</code> to find the right method calls in the files mentioned above. For each method found, comment it out, and insert your own, mocked method call.  For example, in <code>DeviceConfigurationHelper.smali</code>I&#8217;ve replaced the call</p>
<pre>invoke-virtual {v4}, Landroid/content/pm/PackageManager;-&gt;getSystemAvailableFeatures()[Landroid/content/pm/FeatureInfo;</pre>
<p>with a call to my own <code>getSystemAvailableFeatures()</code> method. Finally, you&#8217;ll need to use <code>smali</code> to re-compile all of the code back into a classes.dex file (replace the old ones in Phonesky and GoogleServicesFramework respectively), re-zip into each into an apk (including GoogleLoginService), and re-sign each using <code>jarsigner</code>. Push them onto the emulator as always and you should be good to go.</p>
<div id="attachment_554" style="width: 410px" class="wp-caption aligncenter"><a href="http://apkudo.files.wordpress.com/2012/08/nexusgames-e1344440682257.gif"><img class=" wp-image-554  " title="nexusgames" src="http://apkudo.files.wordpress.com/2012/08/nexusgames-e1344440682257.gif?w=300" alt="" width="400" height="260" /></a><p class="wp-caption-text">Play thinks we&#8217;re a Galaxy Nexus!</p></div>
<div style="width: 410px" class="wp-caption aligncenter"><img src="https://lh5.googleusercontent.com/TrPelKO4885gwZGPZ8qhcua-ghHimCh-faVBqrvIeiuycdAAxzs76aaE5yGZxAaWnFAmkT6EauDE3zK9jeVMcJPK4MRXQbDsHfkS6oH25u-AnO-gK2k" alt="" width="400" height="260" /><p class="wp-caption-text">Downloading Angry Birds Space.</p></div>
<div style="width: 410px" class="wp-caption aligncenter"><img src="https://lh6.googleusercontent.com/WbVOSLwKS10N6KMcpwdUMJzR5YAJJlWsZdjpGteluftiP4w27XR52VwOLz7HLWv2c07LDCD8TX6UWjcj7Mh1me2uq6IIcwJed2cizdj7Pp2fb_w_Upw" alt="" width="400" height="260" /><p class="wp-caption-text">Playing our downloaded app on the emulator!</p></div>
<p>For now, you can only download and install free apps. As you might imagine, getting paid apps on an emulator is a bit trickier. We’ll save that for a later post.</p>
<p>Happy hacking,</p>
<p>-Daniel Joyce, Software Engineer</p>
<p>You may wish to reference the following:<br />
<a href="http://code.google.com/p/smali/">Smali and Baksmali<br />
</a><a href="http://developer.android.com/reference/packages.html">Android Api<br />
</a><a href="http://developer.android.com/guide/google/play/filters.html">Google Play Filters</a></p>
]]></content:encoded>
			<wfw:commentRss>http://mspotten.com/wordpress/run-google-play-on-an-emulator-and-pretend-to-be-any-device/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
