如何将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

其他回答

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

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

手动方式

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

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

自动方式

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

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

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

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

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

下载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.

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

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