From 902b1b519f8ae89910f6f0d9f077738711349324 Mon Sep 17 00:00:00 2001 From: Cuchaz Date: Mon, 23 Mar 2015 16:26:44 -0400 Subject: add protectifier! Muwahahaha!!! --- src/cuchaz/enigma/CommandMain.java | 13 ++++++- src/cuchaz/enigma/Deobfuscator.java | 32 ++++++++++++++++++ src/cuchaz/enigma/bytecode/ClassProtectifier.java | 41 +++++++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/cuchaz/enigma/bytecode/ClassProtectifier.java (limited to 'src') diff --git a/src/cuchaz/enigma/CommandMain.java b/src/cuchaz/enigma/CommandMain.java index 0253a92..74c0191 100644 --- a/src/cuchaz/enigma/CommandMain.java +++ b/src/cuchaz/enigma/CommandMain.java @@ -54,8 +54,10 @@ public class CommandMain { String command = getArg(args, 0, "command", true); if (command.equalsIgnoreCase("deobfuscate")) { deobfuscate(args); - } else if(command.equalsIgnoreCase("decompile")) { + } else if (command.equalsIgnoreCase("decompile")) { decompile(args); + } else if (command.equalsIgnoreCase("protectify")) { + protectify(args); } else { throw new IllegalArgumentException("Command not recognized: " + command); } @@ -72,6 +74,7 @@ public class CommandMain { System.out.println("\twhere is one of:"); System.out.println("\t\tdeobfuscate []"); System.out.println("\t\tdecompile []"); + System.out.println("\t\tprotectify "); } private static void decompile(String[] args) @@ -92,6 +95,14 @@ public class CommandMain { deobfuscator.writeJar(fileJarOut, new ConsoleProgressListener()); } + private static void protectify(String[] args) + throws Exception { + File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true)); + File fileJarOut = getWritableFile(getArg(args, 2, "out jar", true)); + Deobfuscator deobfuscator = getDeobfuscator(null, new JarFile(fileJarIn)); + deobfuscator.protectifyJar(fileJarOut, new ConsoleProgressListener()); + } + private static Deobfuscator getDeobfuscator(File fileMappings, JarFile jar) throws Exception { System.out.println("Reading jar..."); diff --git a/src/cuchaz/enigma/Deobfuscator.java b/src/cuchaz/enigma/Deobfuscator.java index b63f163..82ce6a2 100644 --- a/src/cuchaz/enigma/Deobfuscator.java +++ b/src/cuchaz/enigma/Deobfuscator.java @@ -45,6 +45,7 @@ import cuchaz.enigma.analysis.JarIndex; import cuchaz.enigma.analysis.SourceIndex; import cuchaz.enigma.analysis.SourceIndexVisitor; import cuchaz.enigma.analysis.Token; +import cuchaz.enigma.bytecode.ClassProtectifier; import cuchaz.enigma.mapping.ArgumentEntry; import cuchaz.enigma.mapping.BehaviorEntry; import cuchaz.enigma.mapping.ClassEntry; @@ -357,6 +358,37 @@ public class Deobfuscator { } } + public void protectifyJar(File out, ProgressListener progress) { + try (JarOutputStream outJar = new JarOutputStream(new FileOutputStream(out))) { + if (progress != null) { + progress.init(JarClassIterator.getClassEntries(m_jar).size(), "Protectifying classes..."); + } + + int i = 0; + for (CtClass c : JarClassIterator.classes(m_jar)) { + if (progress != null) { + progress.onProgress(i++, c.getName()); + } + + try { + c = ClassProtectifier.protectify(c); + outJar.putNextEntry(new JarEntry(c.getName().replace('.', '/') + ".class")); + outJar.write(c.toBytecode()); + outJar.closeEntry(); + } catch (Throwable t) { + throw new Error("Unable to protectify class " + c.getName(), t); + } + } + if (progress != null) { + progress.onProgress(i, "Done!"); + } + + outJar.close(); + } catch (IOException ex) { + throw new Error("Unable to write to Jar file!"); + } + } + public T obfuscateEntry(T deobfEntry) { if (deobfEntry == null) { return null; diff --git a/src/cuchaz/enigma/bytecode/ClassProtectifier.java b/src/cuchaz/enigma/bytecode/ClassProtectifier.java new file mode 100644 index 0000000..49a6269 --- /dev/null +++ b/src/cuchaz/enigma/bytecode/ClassProtectifier.java @@ -0,0 +1,41 @@ +package cuchaz.enigma.bytecode; + +import javassist.CtBehavior; +import javassist.CtClass; +import javassist.CtField; +import javassist.bytecode.AccessFlag; +import javassist.bytecode.InnerClassesAttribute; + + +public class ClassProtectifier { + + public static CtClass protectify(CtClass c) { + + // protectify all the fields + for (CtField field : c.getDeclaredFields()) { + field.setModifiers(protectify(field.getModifiers())); + } + + // protectify all the methods and constructors + for (CtBehavior behavior : c.getDeclaredBehaviors()) { + behavior.setModifiers(protectify(behavior.getModifiers())); + } + + // protectify all the inner classes + InnerClassesAttribute attr = (InnerClassesAttribute)c.getClassFile().getAttribute(InnerClassesAttribute.tag); + if (attr != null) { + for (int i=0; i