将 DEX 反编译为 Java 源代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1249973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
decompiling DEX into Java sourcecode
提问by Will
How can one decompile Android DEX (VM bytecode) files into corresponding Java sourcecode?
如何将Android DEX(VM字节码)文件反编译成对应的Java源代码?
采纳答案by fred
It's easy
这很简单
Get these tools:
获取这些工具:
1) dex2jarto translate dex files to jar files
1) dex2jar将dex文件翻译成jar文件
2) jd-guito view the java files in the jar
2)jd-gui查看jar中的java文件
The source code is quite readable as dex2jar makes some optimizations.
源代码非常易读,因为 dex2jar 做了一些优化。
Procedure:
程序:
And here's the procedure on how to decompile:
这是有关如何反编译的过程:
Step 1:
第1步:
Convert classes.dex in test_apk-debug.apk to test_apk-debug_dex2jar.jar
将 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
Note 1:In the Windows machines all the
.sh
scripts are replaced by.bat
scriptsNote 2:On linux/mac don't forget about
sh
orbash
. The full command should be:
注 1:在 Windows 机器中,所有
.sh
脚本都被.bat
脚本替换注 2:在 linux/mac 上不要忘记
sh
或bash
。完整的命令应该是:
sh d2j-dex2jar.sh -f -o output_jar.jar apk_to_decompile.apk
Note 3:Also, remember to add execute permission to
dex2jar-X.X
directory e.g.sudo chmod -R +x dex2jar-2.0
注意3:另外,记得给
dex2jar-X.X
目录添加执行权限,例如sudo chmod -R +x dex2jar-2.0
Step 2:
第2步:
Open the jar in JD-GUI
在 JD-GUI 中打开 jar
回答by reflog
I'd actually recommend going here: https://github.com/JesusFreke/smali
我实际上建议去这里:https: //github.com/JesusFreke/smali
It provides BAKSMALI, which is a most excellent reverse-engineering tool for DEX files. It's made by JesusFreke, the guy who created the fameous ROMs for Android.
它提供了 BAKSMALI,这是一个最优秀的 DEX 文件逆向工程工具。它是由 JessFreke 制作的,他为 Android 创建了著名的 ROM。
回答by hcpl
回答by phaethon
Since no one mentioned this, there's one more tool: DED homepage
由于没有人提到这一点,所以还有一个工具:DED主页
Install how-to and some explanations: Installation.
安装方法和一些解释:安装。
It was used in a quite interesting study of the security of top market apps(not really related, just if you're curious): A Survey of Android Application Security
它被用于一项关于顶级市场应用程序安全性的非常有趣的研究(不是真正相关,只是如果你很好奇):Android 应用程序安全性调查
回答by Zach Lipton
To clarify somewhat, there are two major paths you might take here depending on what you want to accomplish:
为了稍微澄清一下,根据您要完成的任务,您可以在此处采用两条主要路径:
Decompile the Dalvik bytecode (dex) into readable Java source. You can do this easily with dex2jarand jd-gui, as fred mentions. The resulting source is useful to read and understand the functionality of an app, but will likely not produce 100% usable code. In other words, you can read the source, but you can't really modify and repackage it. Note that if the source has been obfuscated with proguard, the resulting source code will be substantially more difficult to untangle.
将 Dalvik 字节码 (dex) 反编译为可读的 Java 源代码。正如 fred 所提到的,您可以使用dex2jar和jd-gui轻松完成此操作。生成的源代码对于阅读和理解应用程序的功能很有用,但可能不会生成 100% 可用的代码。换句话说,您可以阅读源代码,但您无法真正修改和重新打包它。请注意,如果源代码已被混淆器混淆,则生成的源代码将更难以解开。
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.
另一个主要的替代方法是将字节码反汇编为smali,这是一种专为此目的而设计的汇编语言。我发现最简单的方法是使用apktool。一旦你安装了 apktool,你只需将它指向一个 apk 文件,你就会为应用程序中包含的每个类返回一个 smali 文件。您可以通过从新的 Java 源代码生成 smali 来读取和修改 smali 甚至完全替换类(为此,您可以使用 javac 将您的 .java 源代码编译为 .class 文件,然后将您的 .class 文件转换为使用 Android 的 .dex 文件dx 编译器,然后使用 baksmali(smali 反汇编程序)将 .dex 转换为 .smali 文件,如本问题所述. 这里可能有捷径)。完成后,您可以轻松地再次使用 apktool 将 apk 打包备份。请注意,apktool 不会对生成的 apk 进行签名,因此您需要像处理任何其他 Android 应用程序一样处理它。
If you go the smali route, you might want to try APK Studio, an IDE that automates some of the above steps to assist you with decompiling and recompiling an apk and installing it on a device.
如果您走 smali 路线,您可能想尝试APK Studio,这是一个自动执行上述某些步骤的 IDE,以帮助您反编译和重新编译 apk 并将其安装到设备上。
In short, your choices are pretty much either to decompile into Java, which is more readable but likely irreversible, or to disassemble to smali, which is harder to read but much more flexible to make changes and repackage a modified app. Which approach you choose would depend on what you're looking to achieve.
简而言之,您的选择几乎是反编译为 Java,后者更具可读性但可能不可逆,或者反汇编为 smali,后者更难阅读但更灵活地进行更改和重新打包修改后的应用程序。您选择哪种方法取决于您要实现的目标。
Lastly, the suggestion of dareis also of note. It's a retargeting tool to convert .dex and .apk files to java .class files, so that they can be analyzed using typical java static analysis tools.
最后,大胆的建议也值得注意。它是一个重定向工具,用于将 .dex 和 .apk 文件转换为 java .class 文件,以便使用典型的 java 静态分析工具对其进行分析。
回答by Alba Mendez
A more complete version of fred's answer:
Manual way
手动方式
First you need a tool to extractall the (compiled) classes on the DEX to a JAR.
There's one called dex2jar, which is made by a chinese student.
首先,您需要一个工具将 DEX 上的所有(已编译)类提取到 JAR 中。
有一个叫做dex2jar,它是由中国学生制作的。
Then, you can use jd-guito decompilethe classes on the JAR to source code.
The resulting source should be quite readable, as dex2jar applies some optimizations.
然后,您可以使用JD-GUI来反编译的JAR源代码的类。
生成的源代码应该非常易读,因为 dex2jar 应用了一些优化。
Automatic way
自动方式
You can use APKTool. It will automaticallyextract all the classes (.dex
), resources (.asrc
), then it will convert binary XMLto human-readable XML, and it will also dissassemblethe classes for you.
Disassembly will always be more robust than decompiling, especiallywith
JARs obfuscated with Pro Guard!
您可以使用APKTool。它将自动提取所有类 ( .dex
)、资源 ( .asrc
),然后将二进制 XML转换为人类可读的 XML,并且还会 为您分解这些类。
拆卸总是比反编译更稳健,特别是与
同亲卫队混淆的JAR!
Just tell APKTool to decodethe APK into a directory, then modifywhat you want,
and finally encodeit back to an APK. That's all.
只需告诉 APKTool将 APK解码到一个目录中,然后修改您想要的内容,
最后将其编码回 APK。就这样。
Important:APKTool dissassembles. It doesn't decompile.
The generated code won't be Java source.
But you should be able to read it, and even edit it if you're familiar with jasmin.
If you want Java source, please go over the Manual way.
重要提示:APKTool反汇编. 它不会反编译。
生成的代码不会是 Java 源代码。
但是如果您熟悉jasmin,您应该能够阅读它,甚至可以编辑它。
如果您需要 Java 源代码,请查看手动方式。
回答by gtiwari333
Android Reverse Engineering is possible. Follow these steps to get .java file from apk file.
Android 逆向工程是可能的。按照以下步骤从 apk 文件中获取 .java 文件。
Step1 . Using dex2jar
第1步 。使用 dex2jar
- Generate .jar file from .apk file
- command :
dex2jar sampleApp.apk
- 从 .apk 文件生成 .jar 文件
- 命令 :
dex2jar sampleApp.apk
Step2 . Decompiling .jar using JD-GUI
第2步 。使用 JD-GUI 反编译 .jar
- it decompiles the .class files i.e., we'll get obfuscated.java back from the apk.
- 它反编译 .class 文件,即我们将从apk 中得到混淆的.java。
回答by Kamal
Once you downloaded your APK file , You need to do the following steps to get a editable java code/document.
下载 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.zipand 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.
- 将您的 apk 文件转换为 zip(在开始下载时不要使用“保存”选项,只需使用“另存为”并将您的扩展名提及为 .zip),这样做您可以避免使用 APKTOOL...
- 解压 zip 文件,在那里你可以找到 somefilename.dex。所以现在我们需要转换 dex -> .class
- 要做到这一点,你需要“dex2jar”(你可以从http://code.google.com/p/dex2jar/下载它,解压后,在命令提示符下你必须提到,[D:\dex2jar-0.09> dex2jar somefilename.dex](请记住,您的 somefilename.dex 必须位于保存 dex2jar 的同一文件夹中。)
- 从http://www.viralpatel.net/blogs/download/jad/jad.zip下载 jad并解压。解压后,您可以看到两个文件,例如“jad.exe”和“Readme.txt”(有时可能是“jad.txt”而不是“jad.exe”,因此只需将其扩展名重命名为.exe 即可运行)
- 最后,在命令提示符下你必须提到像[D:\jad>jad -sjava yourfilename.class]它将把你的类文件解析成可编辑的 java 文档。
回答by polym
Sometimes you get broken code, when using dex2jar
/apktool
, most notably in loops. To avoid this, use jadx, which decompiles dalvik bytecode into java source code, without creating a .jar
/.class
file first as dex2jar
does (apktool uses dex2jar I think). It is also open-source and in active development. It even has a GUI, for GUI-fanatics. Try it!
有时您会在使用dex2jar
/时遇到损坏的代码,apktool
尤其是在循环中。为避免这种情况,请使用jadx,它将 dalvik 字节码反编译为 java 源代码,而无需像这样先创建.jar
/.class
文件dex2jar
(我认为 apktool 使用 dex2jar)。它也是开源的并且正在积极开发中。对于 GUI 狂热者来说,它甚至还有一个 GUI。尝试一下!
回答by Jeff Brateman
If you're not looking to download dex2jar, then just use the apk_grabber
python scriptto decompile any apk into jar files. Then you read them with jd-gui.
如果您不打算下载 dex2jar,那么只需使用apk_grabber
python 脚本将任何 apk 反编译为 jar 文件。然后你用 jd-gui 阅读它们。