summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/cuchaz/enigma/CommandMain.java13
-rw-r--r--src/cuchaz/enigma/Deobfuscator.java32
-rw-r--r--src/cuchaz/enigma/bytecode/ClassProtectifier.java41
3 files changed, 85 insertions, 1 deletions
diff --git a/src/cuchaz/enigma/CommandMain.java b/src/cuchaz/enigma/CommandMain.java
index 0253a92f..74c01913 100644
--- a/src/cuchaz/enigma/CommandMain.java
+++ b/src/cuchaz/enigma/CommandMain.java
@@ -54,8 +54,10 @@ public class CommandMain {
54 String command = getArg(args, 0, "command", true); 54 String command = getArg(args, 0, "command", true);
55 if (command.equalsIgnoreCase("deobfuscate")) { 55 if (command.equalsIgnoreCase("deobfuscate")) {
56 deobfuscate(args); 56 deobfuscate(args);
57 } else if(command.equalsIgnoreCase("decompile")) { 57 } else if (command.equalsIgnoreCase("decompile")) {
58 decompile(args); 58 decompile(args);
59 } else if (command.equalsIgnoreCase("protectify")) {
60 protectify(args);
59 } else { 61 } else {
60 throw new IllegalArgumentException("Command not recognized: " + command); 62 throw new IllegalArgumentException("Command not recognized: " + command);
61 } 63 }
@@ -72,6 +74,7 @@ public class CommandMain {
72 System.out.println("\twhere <command> is one of:"); 74 System.out.println("\twhere <command> is one of:");
73 System.out.println("\t\tdeobfuscate <in jar> <out jar> [<mappings file>]"); 75 System.out.println("\t\tdeobfuscate <in jar> <out jar> [<mappings file>]");
74 System.out.println("\t\tdecompile <in jar> <out folder> [<mappings file>]"); 76 System.out.println("\t\tdecompile <in jar> <out folder> [<mappings file>]");
77 System.out.println("\t\tprotectify <in jar> <out jar>");
75 } 78 }
76 79
77 private static void decompile(String[] args) 80 private static void decompile(String[] args)
@@ -92,6 +95,14 @@ public class CommandMain {
92 deobfuscator.writeJar(fileJarOut, new ConsoleProgressListener()); 95 deobfuscator.writeJar(fileJarOut, new ConsoleProgressListener());
93 } 96 }
94 97
98 private static void protectify(String[] args)
99 throws Exception {
100 File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true));
101 File fileJarOut = getWritableFile(getArg(args, 2, "out jar", true));
102 Deobfuscator deobfuscator = getDeobfuscator(null, new JarFile(fileJarIn));
103 deobfuscator.protectifyJar(fileJarOut, new ConsoleProgressListener());
104 }
105
95 private static Deobfuscator getDeobfuscator(File fileMappings, JarFile jar) 106 private static Deobfuscator getDeobfuscator(File fileMappings, JarFile jar)
96 throws Exception { 107 throws Exception {
97 System.out.println("Reading jar..."); 108 System.out.println("Reading jar...");
diff --git a/src/cuchaz/enigma/Deobfuscator.java b/src/cuchaz/enigma/Deobfuscator.java
index b63f1639..82ce6a2b 100644
--- a/src/cuchaz/enigma/Deobfuscator.java
+++ b/src/cuchaz/enigma/Deobfuscator.java
@@ -45,6 +45,7 @@ import cuchaz.enigma.analysis.JarIndex;
45import cuchaz.enigma.analysis.SourceIndex; 45import cuchaz.enigma.analysis.SourceIndex;
46import cuchaz.enigma.analysis.SourceIndexVisitor; 46import cuchaz.enigma.analysis.SourceIndexVisitor;
47import cuchaz.enigma.analysis.Token; 47import cuchaz.enigma.analysis.Token;
48import cuchaz.enigma.bytecode.ClassProtectifier;
48import cuchaz.enigma.mapping.ArgumentEntry; 49import cuchaz.enigma.mapping.ArgumentEntry;
49import cuchaz.enigma.mapping.BehaviorEntry; 50import cuchaz.enigma.mapping.BehaviorEntry;
50import cuchaz.enigma.mapping.ClassEntry; 51import cuchaz.enigma.mapping.ClassEntry;
@@ -357,6 +358,37 @@ public class Deobfuscator {
357 } 358 }
358 } 359 }
359 360
361 public void protectifyJar(File out, ProgressListener progress) {
362 try (JarOutputStream outJar = new JarOutputStream(new FileOutputStream(out))) {
363 if (progress != null) {
364 progress.init(JarClassIterator.getClassEntries(m_jar).size(), "Protectifying classes...");
365 }
366
367 int i = 0;
368 for (CtClass c : JarClassIterator.classes(m_jar)) {
369 if (progress != null) {
370 progress.onProgress(i++, c.getName());
371 }
372
373 try {
374 c = ClassProtectifier.protectify(c);
375 outJar.putNextEntry(new JarEntry(c.getName().replace('.', '/') + ".class"));
376 outJar.write(c.toBytecode());
377 outJar.closeEntry();
378 } catch (Throwable t) {
379 throw new Error("Unable to protectify class " + c.getName(), t);
380 }
381 }
382 if (progress != null) {
383 progress.onProgress(i, "Done!");
384 }
385
386 outJar.close();
387 } catch (IOException ex) {
388 throw new Error("Unable to write to Jar file!");
389 }
390 }
391
360 public <T extends Entry> T obfuscateEntry(T deobfEntry) { 392 public <T extends Entry> T obfuscateEntry(T deobfEntry) {
361 if (deobfEntry == null) { 393 if (deobfEntry == null) {
362 return null; 394 return null;
diff --git a/src/cuchaz/enigma/bytecode/ClassProtectifier.java b/src/cuchaz/enigma/bytecode/ClassProtectifier.java
new file mode 100644
index 00000000..49a62690
--- /dev/null
+++ b/src/cuchaz/enigma/bytecode/ClassProtectifier.java
@@ -0,0 +1,41 @@
1package cuchaz.enigma.bytecode;
2
3import javassist.CtBehavior;
4import javassist.CtClass;
5import javassist.CtField;
6import javassist.bytecode.AccessFlag;
7import javassist.bytecode.InnerClassesAttribute;
8
9
10public class ClassProtectifier {
11
12 public static CtClass protectify(CtClass c) {
13
14 // protectify all the fields
15 for (CtField field : c.getDeclaredFields()) {
16 field.setModifiers(protectify(field.getModifiers()));
17 }
18
19 // protectify all the methods and constructors
20 for (CtBehavior behavior : c.getDeclaredBehaviors()) {
21 behavior.setModifiers(protectify(behavior.getModifiers()));
22 }
23
24 // protectify all the inner classes
25 InnerClassesAttribute attr = (InnerClassesAttribute)c.getClassFile().getAttribute(InnerClassesAttribute.tag);
26 if (attr != null) {
27 for (int i=0; i<attr.tableLength(); i++) {
28 attr.setAccessFlags(i, protectify(attr.accessFlags(i)));
29 }
30 }
31
32 return c;
33 }
34
35 private static int protectify(int flags) {
36 if (AccessFlag.isPrivate(flags)) {
37 flags = AccessFlag.setProtected(flags);
38 }
39 return flags;
40 }
41}