如何将Android DEX(虚拟机字节码)文件反编译成相应的Java源代码?


我建议大家去这里看看: https://github.com/JesusFreke/smali

它提供了BAKSMALI,这是用于DEX文件的最优秀的逆向工程工具。 它是由JesusFreke制作的,他为Android创造了著名的rom。

使用Dedexer,您可以将.dex文件分解为dalvik字节码(.ddx)。

据我所知,向Java反编译是不可能的。 你可以在这里阅读dalvik字节码。

很容易

获取这些工具:

Dex2jar将dex文件转换为jar文件 以查看jar中的Java文件

源代码可读性很强,因为dex2jar做了一些优化。

过程:

下面是如何反编译的过程:

步骤1:

将test_apk-debug.apk中的classes.dex转换为test_apk-debug_dex2jar.jar

d2j-dex2jar.sh -f -o output_jar.jar apk_to_decompile.apk
d2j-dex2jar.sh -f -o output_jar.jar dex_to_decompile.dex

注1:在Windows机器中,所有的.sh脚本都被.bat脚本取代

注2:在linux/mac上不要忘记sh或bash。完整的命令应该是:

sh d2j-dex2jar.sh -f -o output_jar.jar apk_to_decompile.apk 

注意3:另外,请记住向dex2jar-X添加执行权限。例如sudo chmod -R + X dex2jar-2.0

dex2jar文档

步骤2:

在JD-GUI中打开罐子

由于没有人提到这一点,还有一个工具:DED主页

安装指南和一些说明:安装。

它被用于一个非常有趣的关于顶级市场应用程序安全性的研究(不是真的相关,如果你好奇的话):Android应用程序安全性调查

需要澄清的是,根据你想要实现的目标,你可以采取两种主要的方法:

将Dalvik字节码(dex)反编译为可读的Java源代码。正如fred提到的,使用dex2jar和jd-gui可以很容易地做到这一点。得到的源代码对于阅读和理解应用程序的功能是有用的,但可能不会产生100%可用的代码。换句话说,您可以读取源代码,但不能真正地修改和重新打包它。请注意,如果源代码使用proguard进行了混淆,那么最终的源代码将更加难以理清。

The other major alternative is to disassemble the bytecode to smali, an assembly language designed for precisely this purpose. I've found that the easiest way to do this is with apktool. Once you've got apktool installed, you can just point it at an apk file, and you'll get back a smali file for each class contained in the application. You can read and modify the smali or even replace classes entirely by generating smali from new Java source (to do this, you could compile your .java source to .class files with javac, then convert your .class files to .dex files with Android's dx compiler, and then use baksmali (smali disassembler) to convert the .dex to .smali files, as described in this question. There might be a shortcut here). Once you're done, you can easily package the apk back up with apktool again. Note that apktool does not sign the resulting apk, so you'll need to take care of that just like any other Android application.

如果你走的是一条小路线,你可能想尝试APK Studio,它是一个IDE,可以自动执行上面的一些步骤,帮助你反编译和重新编译APK,并将其安装到设备上。

简而言之,你的选择是要么反编译成Java,后者可读性更强,但可能不可逆;要么反汇编成smalli,后者更难阅读,但更灵活地进行更改和重新打包修改后的应用。你选择哪种方法取决于你想要实现的目标。

最后,大胆的建议也是值得注意的。这是一个重新定位的工具,可以将.dex和.apk文件转换为java .class文件,这样就可以使用典型的java静态分析工具来分析它们。

弗雷德回答的更完整版本:

手动方式

首先,您需要一个工具将DEX上的所有(已编译的)类提取到JAR中。 有一个叫dex2jar的,是一个中国学生做的。

然后,您可以使用jd-gui将JAR上的类反编译为源代码。 由于dex2jar应用了一些优化,因此得到的源代码应该非常易读。

自动方式

你可以使用APKTool。它将自动提取所有的类(.dex)、资源(.asrc),然后它将二进制XML转换为人类可读的XML,它还将为您分解类。 反汇编总是比反汇编更健壮,特别是使用 jar与Pro Guard混淆!

只需告诉APKTool解码APK到一个目录,然后修改你想要的, 最后编码回APK。这是所有。

重点:APKTool可以分解。它不会反编译。 生成的代码不是Java源代码。 但是如果您熟悉jasmin,您应该能够阅读它,甚至可以编辑它。 如果你想要Java源码,请参照手动方式。

Android逆向工程是可能的 . 按照以下步骤从apk文件中获取.java文件。

步骤1。使用dex2jar

从。apk文件生成。jar文件 命令:dex2jar sampleApp.apk

步骤2。使用JD-GUI反编译.jar

它会反编译。class文件,也就是说,我们会从apk中得到模糊的。java文件。

下载APK文件后,您需要执行以下步骤以获得可编辑的java代码/文档。

Convert your apk file to zip (while start your download don't go with "save" option , just go with "save as" and mention your extension as .zip) by doing like this you may avoid APKTOOL... Extract the zip file , there you can find somefilename.dex. so now we need to convert dex -> .class To do that, you need "dex2jar"(you can download it from http://code.google.com/p/dex2jar/ , after extracted, in command prompt you have to mention like, [D:\dex2jar-0.09>dex2jar somefilename.dex] (Keep in mind that your somefilename.dex must be inside the same folder where you have keep your dex2jar.) Download jad from http://www.viralpatel.net/blogs/download/jad/jad.zip and extract it. Once extracted you can see two files like "jad.exe" and "Readme.txt" (sometimes "jad.txt" may there instead of "jad.exe", so just rename its extension as.exe to run) Finally, in command prompt you have to mention like [D:\jad>jad -sjava yourfilename.class] it will parse your class file into editable java document.

在使用dex2jar/apktool时,有时会出现坏代码,最明显的是在循环中。为了避免这种情况,可以使用jadx,它可以将dalvik字节码反编译成java源代码,而不需要像dex2jar那样先创建.jar/.class文件(我认为apktool使用dex2jar)。它也是开源的,正在积极开发中。它甚至有一个GUI,专为GUI爱好者设计。试一试!

如果您不打算下载dex2jar,那么只需使用apk_grabber python脚本将任何apk反编译到jar文件中。然后使用jd-gui读取它们。

您可以尝试JADX (https://bitbucket.org/mstrobel/procyon/wiki/Java%20Decompiler),这是一个完美的DEX反编译工具。

是的,它也可以在线(my:0))新网站:http://www.javadecompilers.com/apk/

这可以通过以下五个步骤来完成:

这个gem自动为您完成这些工作,甚至安装所需的工具

将apk文件转换为zip 解压缩文件 从中提取classes.dex 使用dex to jar将classes.dex转换为jar文件 使用jadx GUI打开jar文件作为Java源代码

最近Debian有Python包androguard:

Description-en: full Python tool to play with Android files
 Androguard is a full Python tool to play with Android files.
  * DEX, ODEX
  * APK
  * Android's binary xml
  * Android resources
  * Disassemble DEX/ODEX bytecodes
  * Decompiler for DEX/ODEX files

安装相应的软件包:

sudo apt-get install androguard python-networkx

反编译DEX文件:

$ androdd -i classes.dex -o ./dir-for-output

从Apk +反编译中提取classes.dex:

$ androdd -i app.apk -o ./dir-for-output

Apk文件就是Java的JAR文件,你可以通过以下方法从JAR文件中提取文件:

$ unzip app.apk -d ./dir-for-output

自从这些答案被发布以来,很多事情都发生了变化。现在有很多简单的GUI工具,比如:

APK Easy Tool for Windows (GUI tool, friendly)
Bytecode Viewer - APK/Java Reverse Engineering Suite
URET Android Reverser Toolkit

找到他们最好的地方是在XDA开发者论坛上。

反编译android应用程序最简单的方法是从playstore下载一个名为ShowJava的应用程序。只需从应用程序列表中选择需要反编译的应用程序。你可以使用三种不同的反编译器来反编译应用程序,即-

CFR 0.110, JaDX 0.6.1或FernFlower(分析反编译器)。

我用过

Dex2jar + jd-gui javadecompilers.com enjarify Apktool

但没有比谷歌自己的工具更好的了

1)Android Studio 2.x: 构建>分析apk

2)Android Studio 3.0: 配置文件或调试APK

因为Dheeraj Bhaskar的答案在很多年前就已经过时了。

以下是我最新的答案(2019年):

主逻辑

从dex到Java源代码,目前有两种解决方案:

一步:直接转换dex到java源代码 两步:首先将dex转换为jar,然后将jar转换为java源代码


一步解决方案:dex直接到java源代码

工具

jadx

过程

下载jadx-0.9.0.zip,解压,在bin文件夹中可以看到命令行jadx或GUI版本jadx- GUI,双击运行GUI版本:jadx- GUI

打开dex文件

然后可以显示Java源代码:

文件->保存为gradle项目

然后得到Java源代码:


两步解决方案

步骤1:dex到jar

工具

dex2jar

过程

下载dex2jar zip,解压得到d2j-dex2jar.sh,然后:

Apk到jar: sh d2j-dex2jar.sh -f ~/path/to/apk_to_decompile.apk .sh Dex to jar: sh d2j-dex2jar.sh -f ~/path/to/dex_to_decompile.dex

例子:

➜  v3.4.8 /Users/crifan/dev/dev_tool/android/reverse_engineering/dex-tools/dex-tools-2.1-SNAPSHOT/d2j-dex2jar.sh -f com.huili.readingclub8825612.dex
dex2jar com.huili.readingclub8825612.dex -> ./com.huili.readingclub8825612-dex2jar.jar
➜  v3.4.8 ll
-rw-------  1 crifan  staff   9.5M  3 21 10:00 com.huili.readingclub8825612-dex2jar.jar
-rw-------  1 crifan  staff   8.4M  3 19 14:04 com.huili.readingclub8825612.dex

Step2: jar到java源代码

工具

Jd-gui:最流行的,但许多代码会反编译错误 CRF:流行,次要代码会反编译错误 Procyon:流行,没有代码反编译错误 基于Procyon的GUI工具 Luyten: 字节码查看器 其他人 喀拉喀托火山 Fernflower 旧的:AndroChef 等。

过程

这里演示Procyon转换jar到java源代码:

下载procyon-decompiler-0.5.34.jar

然后使用语法:

java -jar /path/to/procyon-decompiler-0.5.34.jar -jar your_to_decompile.jar -o outputFolderName .jar

例子:

java -jar /Users/crifan/dev/dev_tool/android/reverse_engineering/Procyon/ Procyon -decompiler-0.5.34.jar -jar com.huili.readingclub8825612-dex2jar.jar -o com.huili.readingclub8825612

使用编辑器VSCode打开导出的源代码,如下所示:

结论

转换正确性:Jadx > Procyon > CRF > JD-GUI

推荐使用:(一步解决方案)Jadx


for more detailed explanation, please refer my online Chinese ebook: Security and Cracking of Android Apps