bcv-vf/src/main/java/the/bytecode/club/bytecodeviewer/CommandLineInput.java

382 lines
18 KiB
Java
Raw Normal View History

package the.bytecode.club.bytecodeviewer;
import java.io.File;
import me.konloch.kontainer.io.DiskWriter;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;
import the.bytecode.club.bytecodeviewer.decompilers.Decompiler;
2021-07-08 09:24:12 +00:00
import the.bytecode.club.bytecodeviewer.translation.Language;
2019-04-17 06:45:15 +00:00
import the.bytecode.club.bytecodeviewer.util.JarUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
import static the.bytecode.club.bytecodeviewer.Constants.*;
/***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
* Copyright (C) 2014 Kalen 'Konloch' Kinloch - http://bytecodeviewer.com *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
/**
* Used to allow BCV to be integrated as CLI instead of GUI.
*
2018-01-31 15:41:24 +00:00
* @author Konloch
*/
public class CommandLineInput {
2018-01-31 15:41:24 +00:00
private static final Options options = new Options();
private static final CommandLineParser parser = new DefaultParser();
/*BECAUSE WHO DOESN'T LOVE MAGIC NUMBERS*/
public static int STOP = -1;
2021-07-08 09:24:12 +00:00
public static int GUI = 0;
2018-01-31 15:41:24 +00:00
public static int CLI = 1;
static {
options.addOption("help", false, "prints the help menu.");
options.addOption("list", false, "lists all the available decompilers for BCV " + VERSION + ".");
2018-01-31 15:41:24 +00:00
options.addOption("decompiler", true, "sets the decompiler, procyon by default.");
options.addOption("i", true, "sets the input.");
options.addOption("o", true, "sets the output.");
options.addOption("t", true, "sets the target class to decompile, append all to decomp all as zip.");
options.addOption("nowait", true, "won't wait the 5 seconds to allow the user to read the CLI.");
}
public static boolean containsCommand(String[] args) {
if (args == null || args.length == 0)
return false;
try {
CommandLine cmd = parser.parse(options, args);
if (
cmd.hasOption("help") ||
2021-07-08 09:24:12 +00:00
cmd.hasOption("clean") ||
cmd.hasOption("english") ||
2018-01-31 15:41:24 +00:00
cmd.hasOption("list") ||
cmd.hasOption("decompiler") ||
cmd.hasOption("i") ||
cmd.hasOption("o") ||
cmd.hasOption("t") ||
cmd.hasOption("nowait")
2021-04-12 20:19:12 +00:00
) {
2018-01-31 15:41:24 +00:00
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static int parseCommandLine(String[] args) {
if (!containsCommand(args))
2021-07-08 09:24:12 +00:00
return GUI;
2018-01-31 15:41:24 +00:00
try {
CommandLine cmd = parser.parse(options, args);
if (cmd.hasOption("list")) {
System.out.println("Procyon");
System.out.println("CFR");
System.out.println("FernFlower");
System.out.println("Krakatau");
System.out.println("Krakatau-Bytecode");
System.out.println("JD-GUI");
System.out.println("Smali");
return STOP;
2021-07-08 09:24:12 +00:00
} else if (cmd.hasOption("clean")) {
new File(Constants.getBCVDirectory()).delete();
if(cmd.getOptionValue("i") == null && cmd.getOptionValue("o") == null
&& cmd.getOptionValue("t") == null)
return GUI;
} else if (cmd.hasOption("english")) {
Configuration.language = Language.ENGLISH;
return GUI;
2018-01-31 15:41:24 +00:00
} else if (cmd.hasOption("help")) {
for (String s : new String[]{
"-help Displays the help menu",
2021-07-11 12:01:32 +00:00
"-clean Deletes the BCV directory",
2021-07-08 09:24:12 +00:00
"-english Forces English language translations",
2018-01-31 15:41:24 +00:00
"-list Displays the available decompilers",
"-decompiler <decompiler> Selects the decompiler, procyon by default",
"-i <input file> Selects the input file",
"-o <output file> Selects the output file",
2021-07-08 09:24:12 +00:00
"-t <target classname> Must either be the fully qualified classname or \"all\" to decompile all as zip",
2018-01-31 15:41:24 +00:00
"-nowait Doesn't wait for the user to read the CLI messages"
})
System.out.println(s);
return STOP;
} else {
if (cmd.getOptionValue("i") == null) {
System.err.println("Set the input with -i");
return STOP;
}
if (cmd.getOptionValue("o") == null) {
System.err.println("Set the output with -o");
return STOP;
}
if (cmd.getOptionValue("t") == null) {
System.err.println("Set the target with -t");
return STOP;
}
File input = new File(cmd.getOptionValue("i"));
File output = new File(cmd.getOptionValue("o"));
String decompiler = cmd.getOptionValue("decompiler");
if (!input.exists()) {
System.err.println(input.getAbsolutePath() + " does not exist.");
return STOP;
}
if (output.exists()) {
System.err.println("WARNING: Deleted old " + output.getAbsolutePath() + ".");
output.delete();
}
//check if zip, jar, apk, dex, or class
//if its zip/jar/apk/dex attempt unzip as whole zip
//if its just class allow any
if (
decompiler != null &&
!decompiler.equalsIgnoreCase("procyon") &&
!decompiler.equalsIgnoreCase("cfr") &&
!decompiler.equalsIgnoreCase("fernflower") &&
!decompiler.equalsIgnoreCase("krakatau") &&
!decompiler.equalsIgnoreCase("krakatau-bytecode") &&
!decompiler.equalsIgnoreCase("jd-gui") &&
!decompiler.equalsIgnoreCase("smali")
2021-04-12 20:19:12 +00:00
) {
System.out.println("Error, no decompiler called '" + decompiler + "' found. Type -decompiler-list"
+ " for the list");
2018-01-31 15:41:24 +00:00
}
if (!cmd.hasOption("nowait"))
Thread.sleep(5 * 1000);
return CLI;
}
} catch (Exception e) {
2021-07-07 02:51:10 +00:00
BytecodeViewer.handleException(e);
2018-01-31 15:41:24 +00:00
}
2021-07-08 09:24:12 +00:00
return GUI;
2018-01-31 15:41:24 +00:00
}
public static void executeCommandLine(String[] args) {
try {
CommandLine cmd = parser.parse(options, args);
String decompiler = cmd.getOptionValue("decompiler");
File input = new File(cmd.getOptionValue("i"));
File output = new File(cmd.getOptionValue("o"));
String target = cmd.getOptionValue("t");
if (cmd.getOptionValue("decompiler") == null) {
2021-04-12 20:19:12 +00:00
System.out.println("You can define another decompiler by appending -decompiler \"name\", by default "
+ "procyon has been set.");
2018-01-31 15:41:24 +00:00
decompiler = "procyon";
}
//check if zip, jar, apk, dex, or class
//if its zip/jar/apk/dex attempt unzip as whole zip
//if its just class allow any
2021-04-12 20:19:12 +00:00
File tempZip =
new File(tempDirectory + fs + "temp_" + MiscUtils.getRandomizedName() + ".jar");
2019-04-14 03:45:40 +00:00
if (tempZip.exists())
tempZip.delete();
JarUtils.saveAsJarClassesOnly(BytecodeViewer.getLoadedClasses(), tempZip.getAbsolutePath());
2018-01-31 15:41:24 +00:00
if (decompiler.equalsIgnoreCase("procyon")) {
System.out.println("Decompiling " + input.getAbsolutePath() + " with Procyon");
BytecodeViewer.openFiles(new File[]{input}, false);
Thread.sleep(5 * 1000);
if (target.equalsIgnoreCase("all")) {
2021-07-04 05:59:42 +00:00
Decompiler.PROCYON_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(), output.getAbsolutePath());
2018-01-31 15:41:24 +00:00
} else {
try {
2021-07-10 16:05:08 +00:00
ClassNode cn = BytecodeViewer.blindlySearchForClassNode(target);
2018-01-31 15:41:24 +00:00
final ClassWriter cw = accept(cn);
2021-07-04 05:59:42 +00:00
String contents = Decompiler.PROCYON_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray());
2018-01-31 15:41:24 +00:00
DiskWriter.replaceFile(output.getAbsolutePath(), contents, false);
} catch (Exception e) {
2021-07-07 02:51:10 +00:00
BytecodeViewer.handleException(e);
2018-01-31 15:41:24 +00:00
}
}
} else if (decompiler.equalsIgnoreCase("cfr")) {
System.out.println("Decompiling " + input.getAbsolutePath() + " with CFR");
BytecodeViewer.openFiles(new File[]{input}, false);
Thread.sleep(5 * 1000);
if (target.equalsIgnoreCase("all")) {
2021-07-04 05:59:42 +00:00
Decompiler.CFR_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(), output.getAbsolutePath());
2018-01-31 15:41:24 +00:00
} else {
try {
2021-07-10 16:05:08 +00:00
ClassNode cn = BytecodeViewer.blindlySearchForClassNode(target);
2018-01-31 15:41:24 +00:00
final ClassWriter cw = accept(cn);
2021-07-04 05:59:42 +00:00
String contents = Decompiler.CFR_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray());
2018-01-31 15:41:24 +00:00
DiskWriter.replaceFile(output.getAbsolutePath(), contents, false);
} catch (Exception e) {
2021-07-07 02:51:10 +00:00
BytecodeViewer.handleException(e);
2018-01-31 15:41:24 +00:00
}
}
} else if (decompiler.equalsIgnoreCase("fernflower")) {
System.out.println("Decompiling " + input.getAbsolutePath() + " with FernFlower");
BytecodeViewer.openFiles(new File[]{input}, false);
Thread.sleep(5 * 1000);
if (target.equalsIgnoreCase("all")) {
2021-07-04 05:59:42 +00:00
Decompiler.FERNFLOWER_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(), output.getAbsolutePath());
2018-01-31 15:41:24 +00:00
} else {
try {
2021-07-10 16:05:08 +00:00
ClassNode cn = BytecodeViewer.blindlySearchForClassNode(target);
2018-01-31 15:41:24 +00:00
final ClassWriter cw = accept(cn);
2021-07-04 05:59:42 +00:00
String contents = Decompiler.FERNFLOWER_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray());
2018-01-31 15:41:24 +00:00
DiskWriter.replaceFile(output.getAbsolutePath(), contents, false);
} catch (Exception e) {
2021-07-07 02:51:10 +00:00
BytecodeViewer.handleException(e);
2018-01-31 15:41:24 +00:00
}
}
} else if (decompiler.equalsIgnoreCase("krakatau")) {
System.out.println("Decompiling " + input.getAbsolutePath() + " with Krakatau");
BytecodeViewer.openFiles(new File[]{input}, false);
Thread.sleep(5 * 1000);
if (target.equalsIgnoreCase("all")) {
2021-07-04 05:59:42 +00:00
Decompiler.KRAKATAU_DECOMPILER.getDecompiler().decompileToZip(tempZip.getAbsolutePath(), output.getAbsolutePath());
2018-01-31 15:41:24 +00:00
} else {
try {
2021-07-10 16:05:08 +00:00
ClassNode cn = BytecodeViewer.blindlySearchForClassNode(target);
2018-01-31 15:41:24 +00:00
final ClassWriter cw = accept(cn);
2021-07-04 05:59:42 +00:00
String contents = Decompiler.KRAKATAU_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray());
2018-01-31 15:41:24 +00:00
DiskWriter.replaceFile(output.getAbsolutePath(), contents, false);
} catch (Exception e) {
2021-07-07 02:51:10 +00:00
BytecodeViewer.handleException(e);
2018-01-31 15:41:24 +00:00
}
}
} else if (decompiler.equalsIgnoreCase("krakatau-bytecode")) {
System.out.println("Decompiling " + input.getAbsolutePath() + " with Krakatau-Bytecode");
BytecodeViewer.openFiles(new File[]{input}, false);
Thread.sleep(5 * 1000);
if (target.equalsIgnoreCase("all")) {
System.out.println("Coming soon.");
//Decompiler.krakatauDA.decompileToZip(output.getAbsolutePath());
} else {
try {
2021-07-10 16:05:08 +00:00
ClassNode cn = BytecodeViewer.blindlySearchForClassNode(target);
2018-01-31 15:41:24 +00:00
final ClassWriter cw = accept(cn);
2021-07-04 05:59:42 +00:00
String contents = Decompiler.KRAKATAU_DISASSEMBLER.getDecompiler().decompileClassNode(cn, cw.toByteArray());
2018-01-31 15:41:24 +00:00
DiskWriter.replaceFile(output.getAbsolutePath(), contents, false);
} catch (Exception e) {
2021-07-07 02:51:10 +00:00
BytecodeViewer.handleException(e);
2018-01-31 15:41:24 +00:00
}
}
} else if (decompiler.equalsIgnoreCase("jd-gui")) {
System.out.println("Decompiling " + input.getAbsolutePath() + " with JD-GUI");
BytecodeViewer.openFiles(new File[]{input}, false);
Thread.sleep(5 * 1000);
if (target.equalsIgnoreCase("all")) {
System.out.println("Coming soon.");
//Decompiler.jdgui.decompileToZip(output.getAbsolutePath());
} else {
try {
2021-07-10 16:05:08 +00:00
ClassNode cn = BytecodeViewer.blindlySearchForClassNode(target);
2018-01-31 15:41:24 +00:00
final ClassWriter cw = accept(cn);
2021-07-04 05:59:42 +00:00
String contents = Decompiler.JD_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray());
2018-01-31 15:41:24 +00:00
DiskWriter.replaceFile(output.getAbsolutePath(), contents, false);
} catch (Exception e) {
2021-07-07 02:51:10 +00:00
BytecodeViewer.handleException(e);
2018-01-31 15:41:24 +00:00
}
}
} else if (decompiler.equalsIgnoreCase("smali")) {
System.out.println("Decompiling " + input.getAbsolutePath() + " with Smali");
BytecodeViewer.openFiles(new File[]{input}, false);
Thread.sleep(5 * 1000);
if (target.equalsIgnoreCase("all")) {
System.out.println("Coming soon.");
//Decompiler.smali.decompileToZip(output.getAbsolutePath());
} else {
try {
2021-07-10 16:05:08 +00:00
ClassNode cn = BytecodeViewer.blindlySearchForClassNode(target);
2018-01-31 15:41:24 +00:00
final ClassWriter cw = accept(cn);
2021-07-04 05:59:42 +00:00
String contents = Decompiler.SMALI_DISASSEMBLER.getDecompiler().decompileClassNode(cn, cw.toByteArray());
DiskWriter.replaceFile(output.getAbsolutePath(), contents, false);
} catch (Exception e) {
2021-07-07 02:51:10 +00:00
BytecodeViewer.handleException(e);
}
}
} else if (decompiler.equalsIgnoreCase("jadx")) {
System.out.println("Decompiling " + input.getAbsolutePath() + " with JADX");
BytecodeViewer.openFiles(new File[]{input}, false);
Thread.sleep(5 * 1000);
if (target.equalsIgnoreCase("all")) {
System.out.println("Coming soon.");
//Decompiler.smali.decompileToZip(output.getAbsolutePath());
} else {
try {
2021-07-10 16:05:08 +00:00
ClassNode cn = BytecodeViewer.blindlySearchForClassNode(target);
final ClassWriter cw = accept(cn);
2021-07-04 05:59:42 +00:00
String contents = Decompiler.JADX_DECOMPILER.getDecompiler().decompileClassNode(cn, cw.toByteArray());
2018-01-31 15:41:24 +00:00
DiskWriter.replaceFile(output.getAbsolutePath(), contents, false);
} catch (Exception e) {
2021-07-07 02:51:10 +00:00
BytecodeViewer.handleException(e);
2018-01-31 15:41:24 +00:00
}
}
}
System.out.println("Finished.");
2021-07-08 09:24:12 +00:00
System.out.println("Bytecode Viewer " + VERSION + " [CLI] - Created by @Konloch - https://bytecodeviewer.com");
Configuration.canExit = true;
2018-01-31 15:41:24 +00:00
System.exit(0);
} catch (Exception e) {
2021-07-07 02:51:10 +00:00
BytecodeViewer.handleException(e);
2018-01-31 15:41:24 +00:00
}
}
2018-01-31 15:41:24 +00:00
public static ClassWriter accept(ClassNode cn) {
ClassWriter cw = new ClassWriter(0);
try {
cn.accept(cw);
} catch (Exception e) {
e.printStackTrace();
try {
Thread.sleep(200);
cn.accept(cw);
2021-04-12 22:31:22 +00:00
} catch (InterruptedException ignored) {
2018-01-31 15:41:24 +00:00
}
}
return cw;
}
}