如何将Android DEX(虚拟机字节码)文件反编译成相应的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
其他回答
由于没有人提到这一点,还有一个工具:DED主页
安装指南和一些说明:安装。
它被用于一个非常有趣的关于顶级市场应用程序安全性的研究(不是真的相关,如果你好奇的话):Android应用程序安全性调查
自从这些答案被发布以来,很多事情都发生了变化。现在有很多简单的GUI工具,比如:
APK Easy Tool for Windows (GUI tool, friendly)
Bytecode Viewer - APK/Java Reverse Engineering Suite
URET Android Reverser Toolkit
找到他们最好的地方是在XDA开发者论坛上。
下载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.
因为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
很容易
获取这些工具:
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中打开罐子
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 碎片中的onCreateOptionsMenu
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?