hacktricks/mobile-pentesting/android-app-pentesting/apk-decompilers.md

160 lines
10 KiB
Markdown
Raw Normal View History

2022-04-28 23:27:22 +00:00
# APK decompilers
2022-04-28 16:01:33 +00:00
<details>
2023-04-25 18:35:28 +00:00
<summary><a href="https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology"><strong>☁️ HackTricks Cloud ☁️</strong></a> -<a href="https://twitter.com/hacktricks_live"><strong>🐦 Twitter 🐦</strong></a> - <a href="https://www.twitch.tv/hacktricks_live/schedule"><strong>🎙️ Twitch 🎙️</strong></a> - <a href="https://www.youtube.com/@hacktricks_LIVE"><strong>🎥 Youtube 🎥</strong></a></summary>
2022-04-28 16:01:33 +00:00
2022-09-09 11:57:02 +00:00
* Do you work in a **cybersecurity company**? Do you want to see your **company advertised in HackTricks**? or do you want to have access to the **latest version of the PEASS or download HackTricks in PDF**? Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* **Join the** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** me on **Twitter** [**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks\_live)**.**
2023-01-02 12:00:18 +00:00
* **Share your hacking tricks by submitting PRs to the** [**hacktricks repo**](https://github.com/carlospolop/hacktricks) **and** [**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud).
2022-04-28 16:01:33 +00:00
</details>
2022-05-01 12:41:36 +00:00
## [JD-Gui](https://github.com/java-decompiler/jd-gui)
First famous gui Java decompiler, you could use it to investigate the Java code from the APK once you have obtained it.
2022-05-01 12:41:36 +00:00
## [Jadx](https://github.com/skylot/jadx)
Buildin Java (multi-platform)and at this moment I think it's the recommended one.\
Just **download** the **latest** version and execute it from the _**bin**_ folder:
```
jadx-gui
```
2021-11-30 16:46:07 +00:00
Using the GUI you can perform **text search**, go to the **functions definitions** (_CTRL + left click_ on the function) and cross refs (_right click_ --> _Find Usage_)
2021-11-30 16:46:07 +00:00
If you **only want** the **java code** but without using a GUI a very easy way is to use the jadx cli tool:
```
jadx app.apk
```
Some **interesting options of jadx** (GUI and CLI versions) are:
```
-d <path to output dir>
--no-res #No resources
--no-src #No source code
--no-imports #Always write entire package name (very useful to know where is the function that you might want to hook)
```
2022-05-01 12:41:36 +00:00
## [GDA-android-reversing-Tool](https://github.com/charles2gan/GDA-android-reversing-Tool)
2022-04-06 08:57:29 +00:00
GDA is also a powerful and fast reverse analysis platform. Which does not only supports the basic decompiling operation, but also many excellent functions like **Malicious behavior detection, Privacy leaking detection, Vulnerability detection, Path solving, Packer identification, Variable tracking analysis, Deobfuscation, Python& Java scripts, Device memory extraction, Data decryption and encryption** etc\*\*.\*\*
**Only for Windows.**
![](<../../.gitbook/assets/image (207) (1).png>)
2022-05-01 12:41:36 +00:00
## [Bytecode-Viewer](https://github.com/Konloch/bytecode-viewer/releases)
2021-11-30 16:46:07 +00:00
Another **interesting tool to make a Static analysis is**: [**bytecode-viewer**](https://github.com/Konloch/bytecode-viewer/releases)**.** It allows you to decompile the APK using **several decompilers at the same time**. Then, you can see for example, 2 different Java decompilers and one Smali decompiler. It allows you also to **modify** the code:
![](<../../.gitbook/assets/image (82).png>)
If you modify the code, then you can **export it**.\
2021-11-30 16:46:07 +00:00
One bad thing of bytecode-viewer is that it **doesn't have references** or **cross-references.**
2022-05-01 12:41:36 +00:00
## [**Enjarify**](https://github.com/Storyyeller/enjarify)
2021-03-22 09:38:34 +00:00
Enjarify is a tool for translating Dalvik bytecode to equivalent Java bytecode. This allows Java analysis tools to analyze Android applications.\
2022-04-05 22:24:52 +00:00
Dex2jar is an older tool that also tries to translate Dalvik to Java bytecode. It works reasonably well most of the time, but a lot of obscure features or edge cases will cause it to fail or even silently produce incorrect results. By contrast, Enjarify is designed to work in as many cases as possible, even for code where Dex2jar would fail. Among other things, Enjarify correctly handles unicode class names, constants used as multiple types, implicit casts, exception handlers jumping into normal control flow, classes that reference too many constants, very long methods, exception handlers after a catchall handler, and static initial values of the wrong type.
2021-03-22 09:38:34 +00:00
2022-05-01 12:41:36 +00:00
## [CFR](https://github.com/leibnitz27/cfr)
2021-03-22 09:38:34 +00:00
2021-11-30 16:46:07 +00:00
CFR will decompile modern Java features - [including much of Java ](https://www.benf.org/other/cfr/java9observations.html)[9](https://github.com/leibnitz27/cfr/blob/master/java9stringconcat.html), [12](https://www.benf.org/other/cfr/switch\_expressions.html) & [14](https://www.benf.org/other/cfr/java14instanceof\_pattern), but is written entirely in Java 6, so will work anywhere! ([FAQ](https://www.benf.org/other/cfr/faq.html)) - It'll even make a decent go of turning class files from other JVM languages back into java!
2021-03-22 09:38:34 +00:00
That JAR file can be used as follows:
```
2021-03-22 09:38:34 +00:00
java -jar ./cfr.jar "$JARFILE" --outputdir "$OUTDIR"
```
For larger JAR files I found it to run out of memory. You can simply adapt the size of the memory allocation pool of the JVM if that happens to you, too.
```
2021-03-22 09:38:34 +00:00
java -Xmx4G -jar ./cfr.jar "$JARFILE" --outputdir "$OUTDIR"
```
This example will allow a maximum of 4GB to be allocated.
In the output directory, you will find the decompiled `.java` files, together with a summary of the decompilation
2022-05-01 12:41:36 +00:00
## [Fernflower](https://github.com/JetBrains/intellij-community/tree/master/plugins/java-decompiler/engine)
2021-03-22 09:38:34 +00:00
Next up is [Fernflower](https://github.com/JetBrains/intellij-community/tree/master/plugins/java-decompiler/engine), which is part of [IntelliJ IDEA](https://www.jetbrains.com/idea/). Everyone mentions that it is an _analytical_ decompiler (as stated in their project description), but nobody points out what this actually means. I only found [this Stackoverflow question](https://stackoverflow.com/q/62298929), which unfortunately remains unanswered as of today.
2021-03-22 09:38:34 +00:00
Anyway, since there are no self-contained releases, you need to build it yourself. As a [Gradle](https://gradle.org)-based project, you can clone it and then run the following command given that Gradle is installed on your machine.
2021-03-22 09:38:34 +00:00
```
2021-03-22 09:38:34 +00:00
cd ./plugins/java-decompiler/engine && gradle jar
```
Here, we first switch our working directory to the root directory of Fernflower. Then, we instruct Gradle to build the file `./build/libs/fernflower.jar`.
The invocation of Fernflower is similar to that of CFR.
```
2021-03-22 09:38:34 +00:00
java -jar ./fernflower.jar "$JARFILE" "$OUTDIR"
```
Among the decompilers described here, this is the only one that outputs the generated `.java` files in a JAR file. You can easily extract the source files using `unzip`.
2022-05-01 12:41:36 +00:00
## [Krakatau](https://github.com/Storyyeller/Krakatau)
2021-03-22 09:38:34 +00:00
Remember Enjarify from above? The very same author is also the developer of a decompiler named [Krakatau](https://github.com/Storyyeller/Krakatau).
In contrast to the other projects, this one is written in Python. And I think this is the reason why its a bit different from the others.
Let me cite from [the README of the project](https://github.com/Storyyeller/Krakatau/blob/master/README.md).
> Next, make sure you have jars containing defintions (sic!) for any external classes (i.e. libraries) that might be referenced by the jar you are trying to decompile. This includes the standard library classes (i.e. JRT).
2021-03-22 09:38:34 +00:00
And according to the description, these standard library classes come with up to version 8 of Java in the form of the file `rt.jar`. For later versions, the author provides [jrt-extractor](https://github.com/Storyyeller/jrt-extractor), which can generate this file for us.
So we download that tool and run the following commands.
```
2021-03-22 09:38:34 +00:00
cd ./jrt-extractor
javac JRTExtractor.java
java -ea JRTExtractor
```
This should have written a file `rt.jar` inside the directory.
Given this file, we can run Krakatau as follows.
```
2021-03-22 09:38:34 +00:00
./Krakatau/decompile.py -out "$OUTDIR" -skip -nauto -path ./jrt-extractor/rt.jar "$JARFILE"
```
Let me refer to the projects GitHub for an explanation of the parameters. Just note that for any libraries used by your JAR file, Krakatau will require you to add it as a JAR file to the `-path` flag.
2022-05-01 12:41:36 +00:00
## [procyon](https://github.com/mstrobel/procyon)
2021-03-22 09:38:34 +00:00
Once installed, the usage is straightforward.
```
2021-03-22 09:38:34 +00:00
procyon -jar "$JARFILE" -o "$OUTDIR"
```
2022-05-01 12:41:36 +00:00
## References
2021-04-01 22:13:01 +00:00
{% embed url="https://eiken.dev/blog/2021/02/how-to-break-your-jar-in-2021-decompilation-guide-for-jars-and-apks/#cfr" %}
2022-04-28 16:01:33 +00:00
<details>
2023-04-25 18:35:28 +00:00
<summary><a href="https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology"><strong>☁️ HackTricks Cloud ☁️</strong></a> -<a href="https://twitter.com/hacktricks_live"><strong>🐦 Twitter 🐦</strong></a> - <a href="https://www.twitch.tv/hacktricks_live/schedule"><strong>🎙️ Twitch 🎙️</strong></a> - <a href="https://www.youtube.com/@hacktricks_LIVE"><strong>🎥 Youtube 🎥</strong></a></summary>
2022-04-28 16:01:33 +00:00
2022-09-09 11:57:02 +00:00
* Do you work in a **cybersecurity company**? Do you want to see your **company advertised in HackTricks**? or do you want to have access to the **latest version of the PEASS or download HackTricks in PDF**? Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* **Join the** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** me on **Twitter** [**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks\_live)**.**
2023-01-02 12:00:18 +00:00
* **Share your hacking tricks by submitting PRs to the** [**hacktricks repo**](https://github.com/carlospolop/hacktricks) **and** [**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud).
2022-04-28 16:01:33 +00:00
</details>