Skip to content

Commit

Permalink
API and CLI update (java-deobfuscator#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
samczsun authored Oct 7, 2017
1 parent 97b661c commit f4c7421
Show file tree
Hide file tree
Showing 46 changed files with 930 additions and 817 deletions.
43 changes: 30 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,49 @@ Things like method names, class names, etc cannot be deobfuscated because there
```java
public class SomeRandomDeobfuscator {
public static void main(String[] args) throws Throwable {
new Deobfuscator()
.withInput(new File("input.jar"))
.withOutput(new File("output.jar"))
.withClasspath(new File("path/to/rt.jar"))
.withTransformer(Transformers.General.SYNTHETIC_BRIDGE)
.start();
Configuration config = new Configuration();
config.setInput(new File("input.jar"));
config.setOutput(new File("output.jar"));
config.setPath(Arrays.asList(
new File("C:\\Program Files\\Java\\jdk_8\\jre\\lib\\rt.jar"),
new File("C:\\Program Files\\Java\\jdk_8\\jre\\lib\\jce.jar"),
new File("C:\\Program Files\\Java\\jdk_8\\jre\\lib\\ext\\jfxrt.jar"),
new File("C:\\Program Files\\Java\\jdk_8\\lib\\tools.jar")
));
config.setTransformers(Arrays.asList(
TransformerConfig.configFor(PeepholeOptimizer.class)
));
new Deobfuscator(config).start();
}
}
```

### CLI

If you don't want to import the project, you can always use the command line interface. There are four arguments that are taken.
If you don't want to import the project, you can always use the command line interface.

| Argument | Description |
| --- | --- |
| -input | The JAR to deobfuscate |
| -output | The file to write to |
| -transformer | A canonical name of the transformer class|
| -path | A dependency of the JAR being deobfuscated |
| --config | The configuration file |

You may specify multiple transformers, and they will be applied in the order given. Order does matter as sometimes one transformation depends on another not being present.

If you wish to use one of the default transformers, then you may remove the `com.javadeobfuscator.deobfuscator.transformers` prefix. For example, the command below will do the same as the example above.
If you wish to use one of the default transformers, then you may remove the `com.javadeobfuscator.deobfuscator.transformers` prefix.

`java -jar deobfuscator.jar -input input.jar -output output.jar -transformer general.SyntheticBridgeTransformer -path path/to/rt.jar`
Here is a sample `config.yaml`:

```yaml
input: input.jar
output: output.jar
transformers:
- normalizer.MethodNormalizer:
mapping-file: normalizer.txt
- stringer.StringEncryptionTransformer
- normalizer.ClassNormalizer: {}
normalizer.FieldNormalizer: {}
```
For more details, please take a look at the wiki.
## Transformers
Expand Down
30 changes: 27 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
Expand All @@ -40,7 +40,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
Expand All @@ -53,7 +53,6 @@
<include>*:*</include>
</includes>
</artifactSet>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
Expand Down Expand Up @@ -94,6 +93,31 @@
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.8.0-alpha2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.8.0-alpha2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
103 changes: 53 additions & 50 deletions src/main/java/com/javadeobfuscator/deobfuscator/Deobfuscator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.javadeobfuscator.deobfuscator;

import com.javadeobfuscator.deobfuscator.config.Configuration;
import com.javadeobfuscator.deobfuscator.config.TransformerConfig;
import com.javadeobfuscator.deobfuscator.exceptions.NoClassInPathException;
import com.javadeobfuscator.deobfuscator.org.objectweb.asm.ClassReader;
import com.javadeobfuscator.deobfuscator.org.objectweb.asm.ClassWriter;
Expand All @@ -30,9 +32,11 @@
import com.javadeobfuscator.deobfuscator.utils.ClassTree;
import com.javadeobfuscator.deobfuscator.utils.Utils;
import com.javadeobfuscator.deobfuscator.utils.WrappedClassNode;
import sun.security.krb5.Config;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Modifier;
import java.util.AbstractMap.SimpleEntry;
Expand All @@ -44,14 +48,15 @@
import java.util.zip.ZipOutputStream;

public class Deobfuscator {

private List<Class<? extends Transformer>> transformers = new ArrayList<>();
private List<File> classpathFiles = new ArrayList<>();
private Map<String, WrappedClassNode> classpath = new HashMap<>();
private Map<String, WrappedClassNode> classes = new HashMap<>();
private Map<String, ClassTree> hierachy = new HashMap<>();
private File input;
private File output;

private final Configuration configuration;

public Deobfuscator(Configuration configuration) {
this.configuration = configuration;
}

private static final boolean DEBUG = false;
/**
Expand All @@ -60,55 +65,48 @@ public class Deobfuscator {
*/
private static final boolean DELETE_USELESS_CLASSES = false;

public Deobfuscator withTransformer(Class<? extends Transformer> transformer) {
this.transformers.add(transformer);
return this;
}
private Map<String, WrappedClassNode> loadClasspathFile(File file) throws IOException {
Map<String, WrappedClassNode> map = new HashMap<>();

public Deobfuscator withClasspath(File file) {
if (file.isDirectory()) {
File[] files = file.listFiles((dir, name) -> name.endsWith(".jar"));
if (files != null) {
classpathFiles.addAll(Arrays.asList(files));
ZipFile zipIn = new ZipFile(file);
Enumeration<? extends ZipEntry> entries = zipIn.entries();
while (entries.hasMoreElements()) {
ZipEntry ent = entries.nextElement();
if (ent.getName().endsWith(".class")) {
if (ent.getName().equals("module-info.class")) { // *rolls eyes*
continue;
}
ClassReader reader = new ClassReader(zipIn.getInputStream(ent));
ClassNode node = new ClassNode();
node.isLibrary = true;
reader.accept(node, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
WrappedClassNode wrappedClassNode = new WrappedClassNode(node, reader.getItemCount());
map.put(node.name, wrappedClassNode);
}
} else {
this.classpathFiles.add(file);
}
return this;
}

public Deobfuscator withInput(File input) {
this.input = input;
return this;
}
zipIn.close();

public Deobfuscator withOutput(File output) {
this.output = output;
return this;
return map;
}

public void start() throws Throwable {
for (File file : classpathFiles) {
ZipFile zipIn = new ZipFile(file);
Enumeration<? extends ZipEntry> entries = zipIn.entries();
while (entries.hasMoreElements()) {
ZipEntry ent = entries.nextElement();
if (ent.getName().endsWith(".class")) {
if (ent.getName().equals("module-info.class")) { // *rolls eyes*
continue;
if (configuration.getPath() != null) {
for (File file : configuration.getPath()) {
if (file.isFile()) {
classpath.putAll(loadClasspathFile(file));
} else {
File[] files = file.listFiles(child -> child.getName().endsWith(".jar"));
if (files != null) {
for (File child : files) {
classpath.putAll(loadClasspathFile(child));
}
}
ClassReader reader = new ClassReader(zipIn.getInputStream(ent));
ClassNode node = new ClassNode();
node.isLibrary = true;
reader.accept(node, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
WrappedClassNode wrappedClassNode = new WrappedClassNode(node, reader.getItemCount());
classpath.put(node.name, wrappedClassNode);
}
}
zipIn.close();
}
ZipFile zipIn = new ZipFile(input);
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(output));

ZipFile zipIn = new ZipFile(configuration.getInput());
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(configuration.getOutput()));
Enumeration<? extends ZipEntry> e = zipIn.entries();
while (e.hasMoreElements()) {
ZipEntry next = e.nextElement();
Expand Down Expand Up @@ -176,11 +174,10 @@ public void start() throws Throwable {
System.out.println("Transforming");
System.out.println();


for (Class<? extends Transformer> transformerClass : transformers) {
Transformer transformer = transformerClass.getConstructor(Map.class, Map.class).newInstance(classes, classpath);
transformer.setDeobfuscator(this);
transformer.transform();
if (configuration.getTransformers() != null) {
for (TransformerConfig config : configuration.getTransformers()) {
runFromConfig(config);
}
}

System.out.println();
Expand Down Expand Up @@ -208,6 +205,12 @@ public void start() throws Throwable {
zipIn.close();
}

public boolean runFromConfig(TransformerConfig config) throws Throwable {
Transformer transformer = config.getImplementation().newInstance();
transformer.init(this, config, classes, classpath);
return transformer.transform();
}

public ClassNode assureLoaded(String ref) {
WrappedClassNode clazz = classpath.get(ref);
if (clazz == null) {
Expand Down Expand Up @@ -387,8 +390,8 @@ public byte[] toByteArray(ClassNode node) {
return null;
}

public File getFile() {
return input;
public Configuration getConfig() {
return this.configuration;
}

public class CustomClassWriter extends ClassWriter {
Expand Down
Loading

0 comments on commit f4c7421

Please sign in to comment.