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


当前回答

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

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

其他回答

最近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

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

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

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

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

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

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

将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静态分析工具来分析它们。

因为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