新聞中心
[[400055]]
本文轉(zhuǎn)載自微信公眾號(hào)「未讀代碼」,作者達(dá)西呀。轉(zhuǎn)載本文請(qǐng)聯(lián)系未讀代碼公眾號(hào)。

為大柴旦等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及大柴旦網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)、大柴旦網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
前言
Java 反編譯,一聽可能覺得高深莫測(cè),其實(shí)反編譯并不是什么特別高級(jí)的操作,Java 對(duì)于 Class 字節(jié)碼文件的生成有著嚴(yán)格的要求,如果你非常熟悉 Java 虛擬機(jī)規(guī)范,了解 Class 字節(jié)碼文件中一些字節(jié)的作用,那么理解反編譯的原理并不是什么問題。甚至像下面這樣的 Class 文件你都能看懂一二。
一般在逆向研究和代碼分析中,反編譯用到的比較多。不過在日常開發(fā)中,有時(shí)候只是簡(jiǎn)單的看一下所用依賴類的反編譯,也是十分重要的。
恰好最近工作中也需要用到 Java 反編譯,所以這篇文章介紹目前常見的的幾種 Java 反編譯工具的使用,在文章的最后也會(huì)通過編譯速度、語(yǔ)法支持以及代碼可讀性三個(gè)維度,對(duì)它們進(jìn)行測(cè)試,分析幾款工具的優(yōu)缺點(diǎn)。
Procyon
Github 鏈接:https://github.com/mstrobel/procyon
Procyon 不僅僅是反編譯工具,它其實(shí)是專注于 Java 代碼的生成和分析的一整套的 Java 元編程工具。主要包括下面幾個(gè)部分:
- Core Framework
- Reflection Framework
- Expressions Framework
- Compiler Toolset (Experimental)
- Java Decompiler (Experimental)
可以看到反編譯只是 Procyon 的其中一個(gè)模塊,Procyon 原來(lái)托管于 bitbucket,后來(lái)遷移到了 GitHub,根據(jù) GitHub 的提交記錄來(lái)看,也有將近兩年沒有更新了。不過也有依賴 Procyon 的其他的開源反編譯工具如** decompiler-procyon**,更新頻率還是很高的,下面也會(huì)選擇這個(gè)工具進(jìn)行反編譯測(cè)試。
使用 Procyon
org.jboss.windup.decompiler decompiler-procyon 5.1.4.Final
寫一個(gè)簡(jiǎn)單的反編譯測(cè)試。
- package com.wdbyte.decompiler;
- import java.io.IOException;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.Iterator;
- import java.util.List;
- import org.jboss.windup.decompiler.api.DecompilationFailure;
- import org.jboss.windup.decompiler.api.DecompilationListener;
- import org.jboss.windup.decompiler.api.DecompilationResult;
- import org.jboss.windup.decompiler.api.Decompiler;
- import org.jboss.windup.decompiler.procyon.ProcyonDecompiler;
- /**
- * Procyon 反編譯測(cè)試
- *
- * @author https://github.com/niumoo
- * @date 2021/05/15
- */
- public class ProcyonTest {
- public static void main(String[] args) throws IOException {
- Long time = procyon("decompiler.jar", "procyon_output_jar");
- System.out.println(String.format("decompiler time: %dms", time));
- }
- public static Long procyon(String source,String targetPath) throws IOException {
- long start = System.currentTimeMillis();
- Path outDir = Paths.get(targetPath);
- Path archive = Paths.get(source);
- Decompiler dec = new ProcyonDecompiler();
- DecompilationResult res = dec.decompileArchive(archive, outDir, new DecompilationListener() {
- public void decompilationProcessComplete() {
- System.out.println("decompilationProcessComplete");
- }
- public void decompilationFailed(List
inputPath, String message) { - System.out.println("decompilationFailed");
- }
- public void fileDecompiled(List
inputPath, String outputPath) { - }
- public boolean isCancelled() {
- return false;
- }
- });
- if (!res.getFailures().isEmpty()) {
- StringBuilder sb = new StringBuilder();
- sb.append("Failed decompilation of " + res.getFailures().size() + " classes: ");
- Iterator failureIterator = res.getFailures().iterator();
- while (failureIterator.hasNext()) {
- DecompilationFailure dex = (DecompilationFailure)failureIterator.next();
- sb.append(System.lineSeparator() + " ").append(dex.getMessage());
- }
- System.out.println(sb.toString());
- }
- System.out.println("Compilation results: " + res.getDecompiledFiles().size() + " succeeded, " + res.getFailures().size() + " failed.");
- dec.close();
- Long end = System.currentTimeMillis();
- return end - start;
- }
- }
Procyon 在反編譯時(shí)會(huì)實(shí)時(shí)輸出反編譯文件數(shù)量的進(jìn)度情況,最后還會(huì)統(tǒng)計(jì)反編譯成功和失敗的 Class 文件數(shù)量。
- ....
- 五月 15, 2021 10:58:28 下午 org.jboss.windup.decompiler.procyon.ProcyonDecompiler$3 call
- 信息: Decompiling 650 / 783
- 五月 15, 2021 10:58:30 下午 org.jboss.windup.decompiler.procyon.ProcyonDecompiler$3 call
- 信息: Decompiling 700 / 783
- 五月 15, 2021 10:58:37 下午 org.jboss.windup.decompiler.procyon.ProcyonDecompiler$3 call
- 信息: Decompiling 750 / 783
- decompilationProcessComplete
- Compilation results: 783 succeeded, 0 failed.
- decompiler time: 40599ms
Procyon GUI
對(duì)于 Procyon 反編譯來(lái)說,在 GitHub 上也有基于此實(shí)現(xiàn)的開源 GUI 界面,感興趣的可以下載嘗試。
Github 地址:https://github.com/deathmarine/Luyten
CFR
GitHub 地址:https://github.com/leibnitz27/cfr
CFR 官方網(wǎng)站:http://www.benf.org/other/cfr/(可能需要FQ)
Maven 倉(cāng)庫(kù):https://mvnrepository.com/artifact/org.benf/cfr
CFR(Class File Reader) 可以支持 Java 9、Java 12、Java 14 以及其他的最新版 Java 代碼的反編譯工作。而且 CFR 本身的代碼是由 Java 6 編寫,所以基本可以使用 CFR 在任何版本的 Java 程序中。值得一提的是,使用 CFR 甚至可以將使用其他語(yǔ)言編寫的的 JVM 類文件反編譯回 Java 文件。
CFR 命令行使用
使用 CFR 反編譯時(shí),你可以下載已經(jīng)發(fā)布的 JAR 包,進(jìn)行命令行反編譯,也可以使用 Maven 引入的方式,在代碼中使用。下面先說命令行運(yùn)行的方式。
直接在 GitHub Tags 下載已發(fā)布的最新版 JAR. 可以直接運(yùn)行查看幫助。
- # 查看幫助
- java -jar cfr-0.151.jar --help
如果只是反編譯某個(gè) class.
- # 反編譯 class 文件,結(jié)果輸出到控制臺(tái)
- java -jar cfr-0.151.jar WindupClasspathTypeLoader.class
- # 反編譯 class 文件,結(jié)果輸出到 out 文件夾
- java -jar cfr-0.151.jar WindupClasspathTypeLoader.class --outputpath ./out
反編譯某個(gè) JAR.
- # 反編譯 jar 文件,結(jié)果輸出到 output_jar 文件夾
- Desktop java -jar cfr-0.151.jar decompiler.jar --outputdir ./output_jar
- Processing decompiler.jar (use silent to silence)
- Processing com.strobel.assembler.metadata.ArrayTypeLoader
- Processing com.strobel.assembler.metadata.ParameterDefinition
- Processing com.strobel.assembler.metadata.MethodHandle
- Processing com.strobel.assembler.metadata.signatures.FloatSignature
反編譯結(jié)果會(huì)按照 class 的包路徑寫入到指定文件夾中。
CFR 代碼中使用
添加依賴這里不提。
org.benf cfr 0.151
實(shí)際上我在官方網(wǎng)站和 GitHub 上都沒有看到具體的單元測(cè)試示例。不過沒有關(guān)系,既然能在命令行運(yùn)行,那么直接在 IDEA 中查看反編譯后的 Main 方法入口,看下命令行是怎么執(zhí)行的,就可以寫出自己的單元測(cè)試了。
- package com.wdbyte.decompiler;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import org.benf.cfr.reader.api.CfrDriver;
- import org.benf.cfr.reader.util.getopt.OptionsImpl;
- /**
- * CFR Test
- *
- * @author https://github.com/niumoo
- * @date 2021/05/15
- */
- public class CFRTest {
- public static void main(String[] args) throws IOException {
- Long time = cfr("decompiler.jar", "./cfr_output_jar");
- System.out.println(String.format("decompiler time: %dms", time));
- // decompiler time: 11655ms
- }
- public static Long cfr(String source, String targetPath) throws IOException {
- Long start = System.currentTimeMillis();
- // source jar
- List
files = new ArrayList<>(); - files.add(source);
- // target dir
- HashMap
outputMap = new HashMap<>(); - outputMap.put("outputdir", targetPath);
- OptionsImpl options = new OptionsImpl(outputMap);
- CfrDriver cfrDriver = new CfrDriver.Builder().withBuiltOptions(options).build();
- cfrDriver.analyse(files);
- Long end = System.currentTimeMillis();
- return (end - start);
- }
- }
JD-Core
GiHub 地址:https://github.com/java-decompiler/jd-core
JD-core 官方網(wǎng)址:https://java-decompiler.github.io/
JD-core 是一個(gè)的獨(dú)立的 Java 庫(kù),可以用于 Java 的反編譯,支持從 Java 1 至 Java 12 的字節(jié)碼反編譯,包括 Lambda 表達(dá)式、方式引用、默認(rèn)方法等。知名的 JD-GUI 和 Eclipse 無(wú)縫集成反編譯引擎就是 JD-core。JD-core 提供了一些反編譯的核心功能,也提供了單獨(dú)的 Class 反編譯方法,但是如果你想在自己的代碼中去直接反編譯整個(gè) JAR 包,還是需要一些改造的,如果是代碼中有匿名函數(shù),Lambda 等,雖然可以直接反編譯,不過也需要額外考慮。
使用 JD-core
org.jd jd-core 1.1.3
為了可以反編譯整個(gè) JAR 包,使用的代碼我做了一些簡(jiǎn)單改造,以便于最后一部分的對(duì)比測(cè)試,但是這個(gè)示例中沒有考慮內(nèi)部類,Lambda 等會(huì)編譯出多個(gè) Class 文件的情況,所以不能直接使用在生產(chǎn)中。
- package com.wdbyte.decompiler;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.Enumeration;
- import java.util.HashMap;
- import java.util.jar.JarFile;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipFile;
- import org.apache.commons.io.IOUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.jd.core.v1.ClassFileToJavaSourceDecompiler;
- import org.jd.core.v1.api.loader.Loader;
- import org.jd.core.v1.api.printer.Printer;
- /**
- * @author https://github.com/niumoo
- * @date 2021/05/15
- */
- public class JDCoreTest {
- public static void main(String[] args) throws Exception {
- JDCoreDecompiler jdCoreDecompiler = new JDCoreDecompiler();
- Long time = jdCoreDecompiler.decompiler("decompiler.jar","jd_output_jar");
- System.out.println(String.format("decompiler time: %dms", time));
- }
- }
- class JDCoreDecompiler{
- private ClassFileToJavaSourceDecompiler decompiler = new ClassFileToJavaSourceDecompiler();
- // 存放字節(jié)碼
- private HashMap
classByteMap = new HashMap<>(); - /**
- * 注意:沒有考慮一個(gè) Java 類編譯出多個(gè) Class 文件的情況。
- *
- * @param source
- * @param target
- * @return
- * @throws Exception
- */
- public Long decompiler(String source,String target) throws Exception {
- long start = System.currentTimeMillis();
- // 解壓
- archive(source);
- for (String className : classByteMap.keySet()) {
- String path = StringUtils.substringBeforeLast(className, "/");
- String name = StringUtils.substringAfterLast(className, "/");
- if (StringUtils.contains(name, "$")) {
- name = StringUtils.substringAfterLast(name, "$");
- }
- name = StringUtils.replace(name, ".class", ".java");
- decompiler.decompile(loader, printer, className);
- String context = printer.toString();
- Path targetPath = Paths.get(target + "/" + path + "/" + name);
- if (!Files.exists(Paths.get(target + "/" + path))) {
- Files.createDirectories(Paths.get(target + "/" + path));
- }
- Files.deleteIfExists(targetPath);
- Files.createFile(targetPath);
- Files.write(targetPath, context.getBytes());
- }
- return System.currentTimeMillis() - start;
- }
- private void archive(String path) throws IOException {
- try (ZipFile archive = new JarFile(new File(path))) {
- Enumeration extends ZipEntry> entries = archive.entries();
- while (entries.hasMoreElements()) {
- ZipEntry entry = entries.nextElement();
- if (!entry.isDirectory()) {
- String name = entry.getName();
- if (name.endsWith(".class")) {
- byte[] bytes = null;
- try (InputStream stream = archive.getInputStream(entry)) {
- bytes = IOUtils.toByteArray(stream);
- }
- classByteMap.put(name, bytes);
- }
- }
- }
- }
- }
- private Loader loader = new Loader() {
- @Override
- public byte[] load(String internalName) {
- return classByteMap.get(internalName);
- }
- @Override
- public boolean canLoad(String internalName) {
- return classByteMap.containsKey(internalName);
- }
- };
- private Printer printer = new Printer() {
- protected static final String TAB = " ";
- protected static final String NEWLINE = "\n";
- protected int indentationCount = 0;
- protected StringBuilder sb = new StringBuilder();
- @Override public String toString() {
- String toString = sb.toString();
- sb = new StringBuilder();
- return toString;
- }
- @Override public void start(int maxLineNumber, int majorVersion, int minorVersion) {}
- @Override public void end() {}
- @Override public void printText(String text) { sb.append(text); }
- @Override public void printNumericConstant(String constant) { sb.append(constant); }
- @Override public void printStringConstant(String constant, String ownerInternalName) { sb.append(constant); }
- @Override public void printKeyword(String keyword) { sb.append(keyword); }
- @Override public void printDeclaration(int type, String internalTypeName, String name, String descriptor) { sb.append(name); }
- @Override public void printReference(int type, String internalTypeName, String name, String descriptor, String ownerInternalName) { sb.append(name); }
- @Override public void indent() { this.indentationCount++; }
- @Override public void unindent() { this.indentationCount--; }
- @Override public void startLine(int lineNumber) { for (int i=0; i
- @Override public void endLine() { sb.append(NEWLINE); }
- @Override public void extraLine(int count) { while (count-- > 0) sb.append(NEWLINE); }
- @Override public void startMarker(int type) {}
- @Override public void endMarker(int type) {}
- };
- }
JD-GUI
GitHub 地址:https://github.com/java-decompiler/jd-gui
JD-core 也提供了官方的 GUI 界面,需要的也可以直接下載嘗試。
Jadx
GitHub 地址:https://github.com/skylot/jadx
Jadx 是一款可以反編譯 JAR、APK、DEX、AAR、AAB、ZIP 文件的反編譯工具,并且也配有 Jadx-gui 用于界面操作。Jadx 使用 Grade 進(jìn)行依賴管理,可以自行克隆倉(cāng)庫(kù)打包運(yùn)行。
- git clone https://github.com/skylot/jadx.git
- cd jadx
- ./gradlew dist
- # 查看幫助
- ./build/jadx/bin/jadx --help
- jadx - dex to java decompiler, version: dev
- usage: jadx [options] (.apk, .dex, .jar, .class, .smali, .zip, .aar, .arsc, .aab)
- options:
- -d, --output-dir - output directory
- -ds, --output-dir-src - output directory for sources
- -dr, --output-dir-res - output directory for resources
- -r, --no-res - do not decode resources
- -s, --no-src - do not decompile source code
- --single-class - decompile a single class
- --output-format - can be 'java' or 'json', default: java
- -e, --export-gradle - save as android gradle project
- -j, --threads-count - processing threads count, default: 6
- --show-bad-code - show inconsistent code (incorrectly decompiled)
- --no-imports - disable use of imports, always write entire package name
- --no-debug-info - disable debug info
- --add-debug-lines - add comments with debug line numbers if available
- --no-inline-anonymous - disable anonymous classes inline
- --no-replace-consts - don't replace constant value with matching constant field
- --escape-unicode - escape non latin characters in strings (with \u)
- --respect-bytecode-access-modifiers - don't change original access modifiers
- --deobf - activate deobfuscation
- --deobf-min - min length of name, renamed if shorter, default: 3
- --deobf-max - max length of name, renamed if longer, default: 64
- --deobf-cfg-file - deobfuscation map file, default: same dir and name as input file with '.jobf' extension
- --deobf-rewrite-cfg - force to save deobfuscation map
- --deobf-use-sourcename - use source file name as class name alias
- --deobf-parse-kotlin-metadata - parse kotlin metadata to class and package names
- --rename-flags - what to rename, comma-separated, 'case' for system case sensitivity, 'valid' for java identifiers, 'printable' characters, 'none' or 'all' (default)
- --fs-case-sensitive - treat filesystem as case sensitive, false by default
- --cfg - save methods control flow graph to dot file
- --raw-cfg - save methods control flow graph (use raw instructions)
- -f, --fallback - make simple dump (using goto instead of 'if', 'for', etc)
- -v, --verbose - verbose output (set --log-level to DEBUG)
- -q, --quiet - turn off output (set --log-level to QUIET)
- --log-level - set log level, values: QUIET, PROGRESS, ERROR, WARN, INFO, DEBUG, default: PROGRESS
- --version - print jadx version
- -h, --help - print this help
- Example:
- jadx -d out classes.dex
根據(jù) HELP 信息,如果想要反編譯 decompiler.jar 到 out 文件夾。
- ./build/jadx/bin/jadx -d ./out ~/Desktop/decompiler.jar
- INFO - loading ...
- INFO - processing ...
- INFO - doneress: 1143 of 1217 (93%)
Fernflower
GitHub 地址:https://github.com/fesh0r/fernflower
Fernflower 和 Jadx 一樣使用 Grade 進(jìn)行依賴管理,可以自行克隆倉(cāng)庫(kù)打包運(yùn)行。
- fernflower-master ./gradlew build
- BUILD SUCCESSFUL in 32s
- 4 actionable tasks: 4 executed
- fernflower-master java -jar build/libs/fernflower.jar
- Usage: java -jar fernflower.jar [-
- Example: java -jar fernflower.jar -dgs=true c:\my\source\ c:\my.jar d:\decompiled\
- fernflower-master mkdir out
- fernflower-master java -jar build/libs/fernflower.jar ~/Desktop/decompiler.jar ./out
- INFO: Decompiling class com/strobel/assembler/metadata/ArrayTypeLoader
- INFO: ... done
- INFO: Decompiling class com/strobel/assembler/metadata/ParameterDefinition
- INFO: ... done
- INFO: Decompiling class com/strobel/assembler/metadata/MethodHandle
- ...
- fernflower-master ll out
- total 1288
- -rw-r--r-- 1 darcy staff 595K 5 16 17:47 decompiler.jar
- fernflower-master
Fernflower 在反編譯 JAR 包時(shí),默認(rèn)反編譯的結(jié)果也是一個(gè) JAR 包。Jad
反編譯速度
到這里已經(jīng)介紹了五款 Java 反編譯工具了,那么在日常開發(fā)中我們應(yīng)該使用哪一個(gè)呢?又或者在代碼分析時(shí)我們又該選擇哪一個(gè)呢?我想這兩種情況的不同,使用時(shí)的關(guān)注點(diǎn)也是不同的。如果是日常使用,讀讀代碼,我想應(yīng)該是對(duì)可讀性要求更高些,如果是大量的代碼分析工作,那么可能反編譯的速度和語(yǔ)法的支持上要求更高些。為了能有一個(gè)簡(jiǎn)單的參考數(shù)據(jù),我使用 JMH 微基準(zhǔn)測(cè)試工具分別對(duì)這五款反編譯工具進(jìn)行了簡(jiǎn)單的測(cè)試,下面是一些測(cè)試結(jié)果。
測(cè)試環(huán)境
| 環(huán)境變量 | 描述 |
|---|---|
| 處理器 | 2.6 GHz 六核Intel Core i7 |
| 內(nèi)存 | 16 GB 2667 MHz DDR4 |
| Java 版本 | JDK 14.0.2 |
| 測(cè)試方式 | JMH 基準(zhǔn)測(cè)試。 |
| 待反編譯 JAR 1 | procyon-compilertools-0.5.33.jar (1.5 MB) |
| 待反編譯 JAR 2 | python2java4common-1.0.0-20180706.084921-1.jar (42 MB) |
反編譯 JAR 1:procyon-compilertools-0.5.33.jar (1.5 MB)
| Benchmark | Mode | Cnt | Score | Units |
|---|---|---|---|---|
| cfr | avgt | 10 | 6548.642 ± 363.502 | ms/op |
| fernflower | avgt | 10 | 12699.147 ± 1081.539 | ms/op |
| jdcore | avgt | 10 | 5728.621 ± 310.645 | ms/op |
| procyon | avgt | 10 | 26776.125 ± 2651.081 | ms/op |
| jadx | avgt | 10 | 7059.354 ± 323.351 | ms/op |
JAR 2 這個(gè)包是比較大的,是拿很多代碼倉(cāng)庫(kù)合并到一起的,同時(shí)還有很多 Python 轉(zhuǎn) Java 生成的代碼,理論上代碼的復(fù)雜度會(huì)更高。
| Benchmark | Cnt | Score |
|---|---|---|
| Cfr | 1 | 413838.826ms |
| fernflower | 1 | 246819.168ms |
| jdcore | 1 | Error |
| procyon | 1 | 487647.181ms |
| jadx | 1 | 505600.231ms |
語(yǔ)法支持和可讀性
如果反編譯后的代碼需要自己看的話,那么可讀性更好的代碼更占優(yōu)勢(shì),下面我寫了一些代碼,主要是 Java 8 及以下的代碼語(yǔ)法和一些嵌套的流程控制,看看反編譯后的效果如何。
- package com.wdbyte.decompiler;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.IntStream;
- import org.benf.cfr.reader.util.functors.UnaryFunction;
- /**
- * @author https://www.wdbyte.com
- * @date 2021/05/16
- */
- public class HardCode {
- public HardCode(A a, B b) { }
- public static void test(int... args) { }
- public
文章題目:Java反編譯工具的使用與對(duì)比分析
網(wǎng)頁(yè)網(wǎng)址:http://www.fisionsoft.com.cn/article/cdcgoee.html


咨詢
建站咨詢
