Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added two more deobfuscator options, refactored options and more #81

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified assets/run-deobfuscator.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,26 @@ public class Context {
private final Map<String, ClassWrapper> originalClasses = new ConcurrentHashMap<>();
private final Map<String, byte[]> files = new ConcurrentHashMap<>();

private LibraryClassLoader loader;
private SandBox sandBox;
private final DeobfuscatorOptions options;
private final LibraryClassLoader loader;
private final SandBox sandBox;

public LibraryClassLoader getLoader() {
return loader;
public Context(DeobfuscatorOptions options, LibraryClassLoader loader, SandBox sandBox) {
this.options = options;
this.loader = loader;
this.sandBox = sandBox;
}

public void setLoader(LibraryClassLoader loader) {
this.loader = loader;
public DeobfuscatorOptions getOptions() {
return options;
}

public SandBox getSandBox() {
return sandBox;
public LibraryClassLoader getLoader() {
return loader;
}

public void setSandBox(SandBox sandBox) {
this.sandBox = sandBox;
public SandBox getSandBox() {
return sandBox;
}

public Collection<ClassWrapper> classes() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
package uwu.narumi.deobfuscator.api.context;

import dev.xdark.ssvm.VirtualMachine;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import uwu.narumi.deobfuscator.api.transformer.Transformer;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

/**
* Immutable options for deobfuscator
*/
public record DeobfuscatorOptions(
@Nullable Path inputJar,
List<ExternalClass> classes,
Set<Path> libraries,

@Nullable Path outputJar,
@Nullable Path outputDir,

List<Supplier<Transformer>> transformers,

int classReaderFlags,
int classWriterFlags,

boolean consoleDebug,
boolean suppressErrors,
boolean verifyBytecode,

VirtualMachine virtualMachine
) {
public static DeobfuscatorOptions.Builder builder() {
return new DeobfuscatorOptions.Builder();
}

public record ExternalClass(Path path, String relativePath) {
}

/**
* Builder for {@link DeobfuscatorOptions}
*/
public static class Builder {
// Inputs
@Nullable
private Path inputJar = null;
private final List<DeobfuscatorOptions.ExternalClass> classes = new ArrayList<>();
private final Set<Path> libraries = new HashSet<>();

// Outputs
@Nullable
private Path outputJar = null;
@Nullable
private Path outputDir = null;

// Transformers
private final List<Supplier<Transformer>> transformers = new ArrayList<>();

// Other config
private int classReaderFlags = ClassReader.SKIP_FRAMES;
private int classWriterFlags = ClassWriter.COMPUTE_FRAMES;

private boolean consoleDebug = false;
private boolean suppressErrors = false;
private boolean verifyBytecode = false;

private VirtualMachine virtualMachine = null;

private Builder() {
}

/**
* Your input jar file
*/
@Contract("_ -> this")
public DeobfuscatorOptions.Builder inputJar(@Nullable Path inputJar) {
this.inputJar = inputJar;
if (this.inputJar != null) {
String fullName = inputJar.getFileName().toString();
int dot = fullName.lastIndexOf('.');

// Auto fill output jar
this.outputJar = inputJar.getParent()
.resolve(dot == -1 ? fullName + "-out" : fullName.substring(0, dot) + "-out" + fullName.substring(dot));
this.libraries.add(inputJar);
}
return this;
}

/**
* Output jar for deobfuscated classes. Automatically filled when input jar is set
*/
@Contract("_ -> this")
public DeobfuscatorOptions.Builder outputJar(@Nullable Path outputJar) {
this.outputJar = outputJar;
return this;
}

/**
* Set output dir it if you want to output raw compiled classes instead of jar file
*/
@Contract("_ -> this")
public DeobfuscatorOptions.Builder outputDir(@Nullable Path outputDir) {
this.outputDir = outputDir;
return this;
}

@Contract("_ -> this")
public DeobfuscatorOptions.Builder libraries(Path... paths) {
this.libraries.addAll(List.of(paths));
return this;
}

/**
* Add external class to deobfuscate
*
* @param path Path to external class
* @param relativePath Relative path for saving purposes
*/
@Contract("_,_ -> this")
public DeobfuscatorOptions.Builder clazz(Path path, String relativePath) {
this.classes.add(new DeobfuscatorOptions.ExternalClass(path, relativePath));
return this;
}

/**
* Transformers to run. You need to specify them in lambda form:
* <pre>
* {@code
* () -> new MyTransformer(true, false),
* () -> new AnotherTransformer(),
* () -> new SuperTransformer()
* }
* </pre>
*
* We can push it further, and we can replace lambdas with no arguments with method references:
* <pre>
* {@code
* () -> new MyTransformer(true, false),
* AnotherTransformer::new,
* SuperTransformer::new
* }
* </pre>
*/
@SafeVarargs
@Contract("_ -> this")
public final DeobfuscatorOptions.Builder transformers(Supplier<Transformer>... transformers) {
this.transformers.addAll(List.of(transformers));
return this;
}

/**
* Flags for {@link ClassReader}
*/
@Contract("_ -> this")
public DeobfuscatorOptions.Builder classReaderFlags(int classReaderFlags) {
this.classReaderFlags = classReaderFlags;
return this;
}

/**
* Flags for {@link ClassWriter}
*/
@Contract("_ -> this")
public DeobfuscatorOptions.Builder classWriterFlags(int classWriterFlags) {
this.classWriterFlags = classWriterFlags;
return this;
}

/**
* Enables stacktraces logging
*/
@Contract(" -> this")
public DeobfuscatorOptions.Builder consoleDebug() {
this.consoleDebug = true;
return this;
}

/**
* Continue deobfuscation even if errors occur
*/
@Contract(" -> this")
public DeobfuscatorOptions.Builder suppressErrors() {
this.suppressErrors = true;
return this;
}

/**
* Verify bytecode after each transformer run. Useful when debugging which
* transformer is causing issues (aka broke bytecode)
*/
@Contract(" -> this")
public DeobfuscatorOptions.Builder verifyBytecode() {
this.verifyBytecode = true;
return this;
}

@Contract("_ -> this")
public DeobfuscatorOptions.Builder virtualMachine(VirtualMachine virtualMachine) {
this.virtualMachine = virtualMachine;
return this;
}

/**
* Build immutable {@link DeobfuscatorOptions} with options verification
*/
public DeobfuscatorOptions build() {
// Verify some options
if (this.inputJar == null && this.classes.isEmpty()) {
throw new IllegalStateException("No input files provided");
}
if (this.outputJar == null && this.outputDir == null) {
throw new IllegalStateException("No output file or directory provided");
}
if (this.outputJar != null && this.outputDir != null) {
throw new IllegalStateException("Output jar and output dir cannot be set at the same time");
}

return new DeobfuscatorOptions(
// Input
inputJar,
classes,
libraries,
// Output
outputJar,
outputDir,
// Transformers
transformers,
// Flags
classReaderFlags,
classWriterFlags,
// Other config
consoleDebug,
suppressErrors,
verifyBytecode,

virtualMachine
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,21 @@ private static boolean transform(
LOGGER.info("Ended {} transformer in {} ms", transformer.name(), (System.currentTimeMillis() - start));

// Bytecode verification
/*if (oldInstance == null && changed) {
// Verify if code is valid
if (context.getOptions().verifyBytecode() && oldInstance == null && changed) {
// Verify if bytecode is valid
try {
verifyBytecode(scope, context);
} catch (RuntimeException e) {
LOGGER.error("Transformer {} produced invalid bytecode", transformer.name(), e);
}
}*/
}
} catch (TransformerException e) {
LOGGER.error("! {}: {}", transformer.name(), e.getMessage());
} catch (Exception e) {
LOGGER.error("Error occurred when transforming {}", transformer.name(), e);
if (!context.getOptions().suppressErrors()) {
throw new RuntimeException(e);
}
}
LOGGER.info("-------------------------------------\n");

Expand Down
Loading