Android Hacking: Part 1 - Decompilation & Source Code
Android Hacking: Part 1 - Decompilation & Source Code
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
In the following tutorial we will take a closer look on how to decompile any Android app and obtain the source code of the application.
We will also learn how decompiling can reveal secret information.
To have a practical example, I have written a demo app, which you can freely download here. Additionally, I have also uploaded the
original source code of the app to GitHub. But as we progress, it should become clear that we do not need the actual source code.
Why decompiling?
Getting the source code of an app – why should we want that anyway? This is best illustrated using our demo app for this tutorial:
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
The app is rather simple and basically just a small vault for your imaginary money. The safe requires the correct password combination
to open its door. Of course, we could start by randomly guessing the combination, but if the password is more complex, we won’t get
anywhere for quite some time.
However, in order to verify whether the entered password is correct or incorrect, the application must somehow know what the actual
password is. Hence, if we manage to inspect the source code of the application, we might nd clues to the actual password of the vault.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Although I wrote the app especially for this tutorial, the context is not that far away from reality. Even in the applications of large
companies, the source code often contains equally sensitive information – whether it is the admin’s password, API tokens or even the
credentials to the database. In addition, the source code allows us to understand the functionality of the app much more precisely
and thus nd vulnerabilities more easily.
Android applications are distributed as APK les, usually through Google’s Play Store. APK is short for Android Package and the le is
essentially similar to a ZIP archive.
This means that if we unpack our demo application with any ZIP tool, we could in theory view the entire contents of the app:
For example, if we take a look at the le AndroidManifest.xml , we see only non-readable binary code. While the app is unpacked, it is
still compiled.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Compiled content of the AndroidManifest.xml
The classes.dex le contains the application’s actual code, currently also in the form of compiled binary code. DEX stands for Dalvik
Executable. The Android operating system creates its own virtualised Dalvik environment when starting the app and then executes the
code of the DEX le inside it.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
If we manage to view the contents of the classes.dex le, we might also have a pretty good chance to nd the password.
It is now necessary to distinguish between disassembling on the one hand and decompiling on the other.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Disassembling
When you disassemble byte code, it gets converted into a low-level and machine-oriented language, which is yet readable for us. With
Android apps, the DEX le is converted into so-called Smali code.
We can then analyze the Smali code and manipulate it as we like. Afterwards, we can rebuild the app and run it on our smartphones
again. This allows us, for example, to unlock restricted functionalities in an app or to have in nite lives in a game. We will learn how to
do this in the second part of the Android Hacking course.
Decompiling
Decompiling converts the byte code, unlike disassembling, into a high-level programming language. The goal is to reverse the
compilation process and restore the source code as close to the original as possible. The quality of the reconstructed code is strongly
dependent on the overall quality of the used decompiler.
Android applications can basically be decompiled in two di erent ways. The most common approach is to decompile the app in two
separate steps. Why this method, despite its popularity, has various disadvantages will become more obvious later on.
In the rst method, the Dalvik binary code of the DEX le is rst converted to Java binary code. As a result, we get a compiled Java
archive in the form of a JAR le. We can then use any Java decompiler to convert the JAR archive back into readable Java les. This
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
method is most popular, because Java is relatively old and many good decompilers are already out there.
In order to convert the DEX le to a JAR le, we will use dex2jar . This open source tool is available for free for all platforms.
d2j-dex2jar vault.apk
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Next we will use JD-GUI to open the JAR archive. The open source Java decompiler is also available for all platforms.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Now we see that the JAR le contains a total of 4 root packages. The rst three packages are not that big of a deal, since they basically
contain utility libraries we don’t care about.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
First, we have the BuildConfig with some meta information about the application, the UI package with the classes controlling the user
interface, and the Data package, which controls the data structure.
After a brief investigation, we discover the class VaultDataSource . There you will nd the parameter vaultCombination with its string
value Subscr1be! . Doesn’t this sound like a promising string we should test in the application?
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
In the app, we now enter Subscr1be! as the password, con rm with a click on “Unlock” and…
…it works! By decompiling and analyzing the source code, we were able to crack the vault and can now access the immense fortune of
1337 euros and 42 cents!
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
A little tip for more complex applications:
In JD-GUI, it is also possible to decompile all classes at once and export the entire source code via “File → Save all Sources”. You can
then open the exported source code in any editor. This makes it easier to browse the files or add comments. In addition, you can give the
individual variables and methods more meaningful names. The latter is particularly useful for making code in foreign languages or
obfuscated code easier to understand.
Now lets take a look on how to decompile DEX les or entire APKs directly into Java code without any additional steps.
In the more modern approach, we convert the APK le directly into the corresponding Java les. The biggest advantage of this method
is, that on one hand it’s less complicated, but on the other hand we also lose much less meta information. This also allows for much
better outcomes. Unfortunately, there are relatively few Android decompilers on the market.
To decompile the app from binary code directly into Java classes, we use the Android decompiler JADX. With JADX, we can simply open
the APK le and view the source code.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
This shows the most signi cant bene t of an Android decompiler compared to a Java decompiler. In addition to the source code from
the DEX le, we also get the decompiled AndroidManifest , information about the certi cate and all kinds of other meta-information
that might help us with our analyses.
Original Code
Below we rst see the original source code of the le, as I have also published it on Github. The string parameter password is passed
to the unlock method, which then veri es it in an if-else comparison.
Original
package digital.basto.vault.data;
import digital.basto.vault.data.model.VaultData;
import java.io.IOException;
import java.security.AccessControlException;
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
public void lock() {
}
}
JD-GUI
package digital.basto.vault.data;
import digital.basto.vault.data.model.VaultData;
import java.io.IOException;
import java.security.AccessControlException;
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
} catch (Exception paramString) {
return new Result.Error(new IOException("Error unlocking view!", paramString));
}
}
}
JADX
package digital.basto.vault.data;
import digital.basto.vault.data.Result.Error;
import digital.basto.vault.data.Result.Success;
import digital.basto.vault.data.model.VaultData;
import java.io.IOException;
import java.security.AccessControlException;
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
} catch (Exception e) {
return new Error(new IOException("Error unlocking view!", e));
}
}
Conclusion
We learned the rst and one of the most important steps in the analysis of Android apps: e ectively decompiling and restoring the
source code of an application.
Choosing the right tools is critical when searching for vulnerabilities. Although the variety of Android decompilers cannot keep up with
the number of Java decompilers, JADX is a great and also free alternative you can use. Apart from additional meta-information, JADX
also o ers the ability to de-obfuscate the code and usually produces better results. The direct comparison of the two methods is
de nitely recommended.
Sources
DEX2JAR: https://github.com/pxb1988/dex2jar
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
JD-GUI: https://java-decompiler.github.io/
JADX: https://github.com/skylot/jadx
INTERESSANTE BEITRÄGE
6. September 2020
L ANG UAG E
English
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
g
AB OU T
T R E N DI NG P O S T S
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
TAG S
I N FO
Basto.digital
Home
Contact
Impressum
Privacy Policy
T R E N DI NG P O S T S
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Android Hacking: Part 1 – Decompilation &…
FOL L OW M E
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD