Class/Method/Field Renaming

This commit is contained in:
im-frizzy 2015-02-28 21:08:28 -06:00
parent 1d32d00d4b
commit c8337e74fd
13 changed files with 3404 additions and 2947 deletions

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -1,56 +1,57 @@
package the.bytecode.club.bytecodeviewer.obfuscators; package the.bytecode.club.bytecodeviewer.obfuscators;
import java.util.ArrayList; import java.util.ArrayList;
import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.MiscUtils; import the.bytecode.club.bytecodeviewer.MiscUtils;
/** /**
* An unfinished obfuscator. * An unfinished obfuscator.
* *
* @author Konloch * @author Konloch
* *
*/ */
public abstract class JavaObfuscator extends Thread { public abstract class JavaObfuscator extends Thread {
@Override @Override
public void run() { public void run() {
System.out.println("mibbzz is gay"); System.out.println("mibbzz is gay");
BytecodeViewer.viewer.setIcon(true); BytecodeViewer.viewer.setIcon(true);
BytecodeViewer.runningObfuscation = true; BytecodeViewer.runningObfuscation = true;
obfuscate(); obfuscate();
BytecodeViewer.runningObfuscation = false; BytecodeViewer.refactorer.run();
BytecodeViewer.viewer.setIcon(false); BytecodeViewer.runningObfuscation = false;
} BytecodeViewer.viewer.setIcon(false);
}
public int getStringLength() {
if (BytecodeViewer.viewer.obfuscatorGroup public int getStringLength() {
.isSelected(BytecodeViewer.viewer.strongObf.getModel())) { if (BytecodeViewer.viewer.obfuscatorGroup
return MAX_STRING_LENGTH; .isSelected(BytecodeViewer.viewer.strongObf.getModel())) {
} else { // if(BytecodeViewer.viewer.obfuscatorGroup.isSelected(BytecodeViewer.viewer.lightObf.getModel())) return MAX_STRING_LENGTH;
// { } else { // if(BytecodeViewer.viewer.obfuscatorGroup.isSelected(BytecodeViewer.viewer.lightObf.getModel()))
return MIN_STRING_LENGTH; // {
} return MIN_STRING_LENGTH;
} }
}
public static int MAX_STRING_LENGTH = 250;
public static int MIN_STRING_LENGTH = 20; public static int MAX_STRING_LENGTH = 25;
private ArrayList<String> names = new ArrayList<String>(); public static int MIN_STRING_LENGTH = 5;
private ArrayList<String> names = new ArrayList<String>();
protected String generateUniqueName(int length) {
boolean found = false; protected String generateUniqueName(int length) {
String name = ""; boolean found = false;
while (!found) { String name = "";
String nameTry = MiscUtils.randomString(1) + MiscUtils.randomStringNum(length - 1); while (!found) {
if (!names.contains(nameTry)) { String nameTry = MiscUtils.randomString(1) + MiscUtils.randomStringNum(length - 1);
names.add(nameTry); if (!names.contains(nameTry)) {
name = nameTry; names.add(nameTry);
found = true; name = nameTry;
} found = true;
} }
return name; }
} return name;
}
public abstract void obfuscate();
} public abstract void obfuscate();
}

View file

@ -1,31 +0,0 @@
package the.bytecode.club.bytecodeviewer.obfuscators;
import org.objectweb.asm.tree.ClassNode;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.api.ASMUtil_OLD;
/**
* Rename classes.
*
* @author Konloch
*
*/
public class RenameClasses extends JavaObfuscator {
@Override
public void obfuscate() {
int stringLength = getStringLength();
System.out.println("Obfuscating class names...");
for (ClassNode c : BytecodeViewer.getLoadedClasses()) {
String newName = generateUniqueName(stringLength);
ASMUtil_OLD.renameClassNode(c.name, newName);
c.name = newName;
}
System.out.println("Obfuscated class names.");
}
}

View file

@ -0,0 +1,46 @@
package the.bytecode.club.bytecodeviewer.obfuscators.mapping;
import java.util.ArrayList;
import java.util.List;
import the.bytecode.club.bytecodeviewer.obfuscators.mapping.data.FieldMappingData;
import the.bytecode.club.bytecodeviewer.obfuscators.mapping.data.MappingData;
import the.bytecode.club.bytecodeviewer.obfuscators.mapping.data.MethodMappingData;
public class HookMap {
protected List<MappingData> classes;
protected List<FieldMappingData> fields;
protected List<MethodMappingData> methods;
public HookMap() {
classes = new ArrayList<MappingData>();
fields = new ArrayList<FieldMappingData>();
methods = new ArrayList<MethodMappingData>();
}
public void addClass(MappingData clazz) {
classes.add(clazz);
}
public void addField(FieldMappingData field) {
fields.add(field);
}
public void addMethod(MethodMappingData method) {
methods.add(method);
}
public List<MappingData> getClasses() {
return classes;
}
public List<FieldMappingData> getFields() {
return fields;
}
public List<MethodMappingData> getMethods() {
return methods;
}
}

View file

@ -0,0 +1,69 @@
package the.bytecode.club.bytecodeviewer.obfuscators.mapping;
import java.util.HashMap;
import java.util.Map;
import org.objectweb.asm.commons.Remapper;
import the.bytecode.club.bytecodeviewer.obfuscators.mapping.data.FieldMappingData;
import the.bytecode.club.bytecodeviewer.obfuscators.mapping.data.MappingData;
import the.bytecode.club.bytecodeviewer.obfuscators.mapping.data.MethodMappingData;
/**
* @author sc4re
*/
public class RefactorMapper extends Remapper {
protected final Map<String, MappingData> sortedClasses;
protected final Map<String, MethodMappingData> sortedMethods;
protected final Map<String, FieldMappingData> sortedFields;
public RefactorMapper(HookMap hookMap) {
sortedClasses = new HashMap<>();
sortedMethods = new HashMap<>();
sortedFields = new HashMap<>();
for(MappingData hook : hookMap.getClasses()){
if(hook.getObfuscatedName().contains("$"))
continue;
String obfuscatedName = hook.getObfuscatedName();
String refactoredName = hook.getRefactoredName();
sortedClasses.put(obfuscatedName, hook);
sortedClasses.put(refactoredName, hook);
}
for (MethodMappingData hook : hookMap.getMethods()) {
String obfuscatedName = hook.getMethodName().getObfuscatedName();
String obfuscatedDesc = hook.getMethodDesc();
String obfuscatedCname = hook.getMethodOwner();
sortedMethods.put(obfuscatedCname + "$$$$" + obfuscatedName + "$$$$" + obfuscatedDesc, hook);
}
for (FieldMappingData hook : hookMap.getFields()) {
String obfuscatedName = hook.getName().getObfuscatedName();
String obfuscatedDesc = hook.getDesc();
String obfuscatedCname = hook.getFieldOwner();
sortedFields.put(obfuscatedCname + "$$$$" + obfuscatedName + "$$$$" + obfuscatedDesc, hook);
}
}
@Override
public String map(String type) {
if (sortedClasses.containsKey(type))
return sortedClasses.get(type).getRefactoredName();
return type;
}
@Override
public String mapFieldName(String owner, String name, String desc) {
String obfKey = owner + "$$$$" + name + "$$$$" + desc;
if (sortedFields.containsKey(obfKey))
name = sortedFields.get(obfKey).getName().getRefactoredName();
return name;
}
@Override
public String mapMethodName(String owner, String name, String desc) {
String obfKey = owner + "$$$$" + name + "$$$$" + desc;
if (sortedMethods.containsKey(obfKey))
name = sortedMethods.get(obfKey).getMethodName().getRefactoredName();
return name;
}
}

View file

@ -0,0 +1,56 @@
package the.bytecode.club.bytecodeviewer.obfuscators.mapping;
import java.util.HashMap;
import java.util.Map;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.commons.RemappingClassAdapter;
import org.objectweb.asm.tree.ClassNode;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
/**
* @author sc4re
*/
public class Refactorer {
protected HookMap hooks;
public Refactorer() {
hooks = new HookMap();
}
public HookMap getHooks() {
return hooks;
}
public void run() {
if (getHooks() == null)
return;
RefactorMapper mapper = new RefactorMapper(getHooks());
Map<String, ClassNode> refactored = new HashMap<>();
for (ClassNode cn : BytecodeViewer.getLoadedClasses()) {
String oldName = cn.name;
ClassReader cr = new ClassReader(getClassNodeBytes(cn));
ClassWriter cw = new ClassWriter(cr, 0);
RemappingClassAdapter rca = new RemappingClassAdapter(cw, mapper);
cr.accept(rca, ClassReader.EXPAND_FRAMES);
cr = new ClassReader(cw.toByteArray());
cn = new ClassNode();
cr.accept(cn, 0);
refactored.put(oldName, cn);
}
for (Map.Entry<String, ClassNode> factor : refactored.entrySet()) {
BytecodeViewer.relocate(factor.getKey(), factor.getValue());
}
}
private byte[] getClassNodeBytes(ClassNode cn) {
ClassWriter cw = new ClassWriter(0);
cn.accept(cw);
byte[] b = cw.toByteArray();
return b;
}
}

View file

@ -0,0 +1,83 @@
package the.bytecode.club.bytecodeviewer.obfuscators.mapping.data;
public class FieldMappingData {
protected String fieldOwner;
protected MappingData name;
protected String desc;
public FieldMappingData(MappingData name, String desc) {
this("", name, desc);
}
public FieldMappingData(String fieldOwner, MappingData name, String desc) {
this.fieldOwner = fieldOwner;
this.name = name;
this.desc = desc;
}
public String getFieldOwner() {
return fieldOwner;
}
public FieldMappingData setFieldOwner(String fieldOwner) {
this.fieldOwner = fieldOwner;
return this;
}
public MappingData getName() {
return name;
}
public FieldMappingData setName(MappingData name) {
this.name = name;
return this;
}
public String getDesc() {
return desc;
}
public FieldMappingData setDesc(String desc) {
this.desc = desc;
return this;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = (prime * result) + ((desc == null) ? 0 : desc.hashCode());
result = (prime * result) + ((fieldOwner == null) ? 0 : fieldOwner.hashCode());
result = (prime * result) + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
FieldMappingData other = (FieldMappingData) obj;
if (desc == null) {
if (other.desc != null)
return false;
} else if (!desc.equals(other.desc))
return false;
if (fieldOwner == null) {
if (other.fieldOwner != null)
return false;
} else if (!fieldOwner.equals(other.fieldOwner))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}

View file

@ -0,0 +1,65 @@
package the.bytecode.club.bytecodeviewer.obfuscators.mapping.data;
public class MappingData {
protected String obfuscatedName;
protected String refactoredName;
public MappingData(String refactoredName) {
this("", refactoredName);
}
public MappingData(String obfuscatedName, String refactoredName) {
this.obfuscatedName = obfuscatedName;
this.refactoredName = refactoredName;
}
public String getObfuscatedName() {
return obfuscatedName;
}
public MappingData setObfuscatedName(String obfuscatedName) {
this.obfuscatedName = obfuscatedName;
return this;
}
public String getRefactoredName() {
return refactoredName;
}
public MappingData setRefactoredName(String refactoredName) {
this.refactoredName = refactoredName;
return this;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = (prime * result) + ((obfuscatedName == null) ? 0 : obfuscatedName.hashCode());
result = (prime * result) + ((refactoredName == null) ? 0 : refactoredName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MappingData other = (MappingData) obj;
if (obfuscatedName == null) {
if (other.obfuscatedName != null)
return false;
} else if (!obfuscatedName.equals(other.obfuscatedName))
return false;
if (refactoredName == null) {
if (other.refactoredName != null)
return false;
} else if (!refactoredName.equals(other.refactoredName))
return false;
return true;
}
}

View file

@ -0,0 +1,83 @@
package the.bytecode.club.bytecodeviewer.obfuscators.mapping.data;
public class MethodMappingData {
protected String methodOwner;
protected MappingData methodName;
protected String methodDesc;
public MethodMappingData(MappingData methodName, String methodDesc) {
this("", methodName, methodDesc);
}
public MethodMappingData(String methodOwner, MappingData methodName, String methodDesc) {
this.methodOwner = methodOwner;
this.methodName = methodName;
this.methodDesc = methodDesc;
}
public String getMethodOwner() {
return methodOwner;
}
public MethodMappingData setMethodOwner(String methodOwner) {
this.methodOwner = methodOwner;
return this;
}
public MappingData getMethodName() {
return methodName;
}
public MethodMappingData setMethodName(MappingData methodName) {
this.methodName = methodName;
return this;
}
public String getMethodDesc() {
return methodDesc;
}
public MethodMappingData setMethodDesc(String methodDesc) {
this.methodDesc = methodDesc;
return this;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = (prime * result) + ((methodDesc == null) ? 0 : methodDesc.hashCode());
result = (prime * result) + ((methodName == null) ? 0 : methodName.hashCode());
result = (prime * result) + ((methodOwner == null) ? 0 : methodOwner.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MethodMappingData other = (MethodMappingData) obj;
if (methodDesc == null) {
if (other.methodDesc != null)
return false;
} else if (!methodDesc.equals(other.methodDesc))
return false;
if (methodName == null) {
if (other.methodName != null)
return false;
} else if (!methodName.equals(other.methodName))
return false;
if (methodOwner == null) {
if (other.methodOwner != null)
return false;
} else if (!methodOwner.equals(other.methodOwner))
return false;
return true;
}
}

View file

@ -0,0 +1,49 @@
package the.bytecode.club.bytecodeviewer.obfuscators.rename;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.MethodNode;
import com.sun.xml.internal.ws.org.objectweb.asm.Opcodes;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.api.ASMUtil_OLD;
import the.bytecode.club.bytecodeviewer.obfuscators.JavaObfuscator;
import the.bytecode.club.bytecodeviewer.obfuscators.mapping.data.MappingData;
/**
* Rename classes.
*
* @author Konloch
*
*/
public class RenameClasses extends JavaObfuscator {
@Override
public void obfuscate() {
int stringLength = getStringLength();
System.out.println("Obfuscating class names...");
classLoop: for (ClassNode c : BytecodeViewer.getLoadedClasses()) {
/** As we dont want to rename classes that contain native dll methods */
for (Object o : c.methods) {
MethodNode m = (MethodNode) o;
/* As we dont want to rename native dll methods */
if ((m.access & Opcodes.ACC_NATIVE) != 0)
continue classLoop;
}
String newName = generateUniqueName(stringLength);
BytecodeViewer.refactorer.getHooks().addClass(new MappingData(c.name, newName));
ASMUtil_OLD.renameClassNode(c.name, newName);
c.name = newName;
}
System.out.println("Obfuscated class names.");
}
}

View file

@ -1,36 +1,44 @@
package the.bytecode.club.bytecodeviewer.obfuscators; package the.bytecode.club.bytecodeviewer.obfuscators.rename;
import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode; import org.objectweb.asm.tree.FieldNode;
import the.bytecode.club.bytecodeviewer.BytecodeViewer; import com.sun.xml.internal.ws.org.objectweb.asm.Opcodes;
import the.bytecode.club.bytecodeviewer.api.ASMUtil_OLD;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
/** import the.bytecode.club.bytecodeviewer.api.ASMUtil_OLD;
* Rename fields. import the.bytecode.club.bytecodeviewer.obfuscators.JavaObfuscator;
* import the.bytecode.club.bytecodeviewer.obfuscators.mapping.data.FieldMappingData;
* @author Konloch import the.bytecode.club.bytecodeviewer.obfuscators.mapping.data.MappingData;
*
*/ /**
* Rename fields.
public class RenameFields extends JavaObfuscator { *
* @author Konloch
@Override *
public void obfuscate() { */
int stringLength = getStringLength();
public class RenameFields extends JavaObfuscator {
System.out.println("Obfuscating fields names...");
for (ClassNode c : BytecodeViewer.getLoadedClasses()) { @Override
for (Object o : c.fields.toArray()) { public void obfuscate() {
FieldNode f = (FieldNode) o; int stringLength = getStringLength();
String newName = generateUniqueName(stringLength);
ASMUtil_OLD.renameFieldNode(c.name, f.name, f.desc, null, System.out.println("Obfuscating fields names...");
newName, null); for (ClassNode c : BytecodeViewer.getLoadedClasses()) {
f.name = newName; for (Object o : c.fields.toArray()) {
} FieldNode f = (FieldNode) o;
}
String newName = generateUniqueName(stringLength);
System.out.println("Obfuscated field names.");
} BytecodeViewer.refactorer.getHooks().addField(new FieldMappingData(c.name, new MappingData(f.name, newName), f.desc));
} /*ASMUtil_OLD.renameFieldNode(c.name, f.name, f.desc, null, newName, null);
f.name = newName;*/
}
}
System.out.println("Obfuscated field names.");
}
}

View file

@ -1,55 +1,66 @@
package the.bytecode.club.bytecodeviewer.obfuscators; package the.bytecode.club.bytecodeviewer.obfuscators.rename;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode; import org.objectweb.asm.tree.MethodNode;
import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.api.ASMUtil_OLD; import the.bytecode.club.bytecodeviewer.api.ASMUtil_OLD;
import the.bytecode.club.bytecodeviewer.obfuscators.JavaObfuscator;
/** import the.bytecode.club.bytecodeviewer.obfuscators.mapping.data.MappingData;
* Rename methods. import the.bytecode.club.bytecodeviewer.obfuscators.mapping.data.MethodMappingData;
*
* @author Konloch /**
* * Rename methods.
*/ *
* @author Konloch
public class RenameMethods extends JavaObfuscator { *
*/
@Override
public void obfuscate() { public class RenameMethods extends JavaObfuscator {
int stringLength = getStringLength();
@Override
System.out.println("Obfuscating method names..."); public void obfuscate() {
for (ClassNode c : BytecodeViewer.getLoadedClasses()) { int stringLength = getStringLength();
for (Object o : c.methods.toArray()) {
MethodNode m = (MethodNode) o; System.out.println("Obfuscating method names...");
if (m.access != Opcodes.ACC_ABSTRACT for (ClassNode c : BytecodeViewer.getLoadedClasses()) {
&& m.access != Opcodes.ACC_ABSTRACT methodLoop: for (Object o : c.methods.toArray()) {
+ Opcodes.ACC_STATIC MethodNode m = (MethodNode) o;
&& m.access != Opcodes.ACC_ABSTRACT
+ Opcodes.ACC_STATIC + Opcodes.ACC_PUBLIC /* As we dont want to rename native dll methods */
&& m.access != Opcodes.ACC_ABSTRACT if ((m.access & Opcodes.ACC_NATIVE) != 0)
+ Opcodes.ACC_STATIC + Opcodes.ACC_PRIVATE continue methodLoop;
&& m.access != Opcodes.ACC_ABSTRACT
+ Opcodes.ACC_STATIC + Opcodes.ACC_PROTECTED if (m.access != Opcodes.ACC_ABSTRACT
&& m.access != Opcodes.ACC_ABSTRACT && m.access != Opcodes.ACC_ABSTRACT
+ Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC
&& m.access != Opcodes.ACC_ABSTRACT && m.access != Opcodes.ACC_ABSTRACT
+ Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_PUBLIC
&& m.access != Opcodes.ACC_ABSTRACT && m.access != Opcodes.ACC_ABSTRACT
+ Opcodes.ACC_PROTECTED) { + Opcodes.ACC_STATIC + Opcodes.ACC_PRIVATE
if (!m.name.equals("main") && !m.name.equals("<init>") && m.access != Opcodes.ACC_ABSTRACT
&& !m.name.equals("<clinit>")) { + Opcodes.ACC_STATIC + Opcodes.ACC_PROTECTED
String newName = generateUniqueName(stringLength); && m.access != Opcodes.ACC_ABSTRACT
ASMUtil_OLD.renameMethodNode(c.name, m.name, m.desc, + Opcodes.ACC_PUBLIC
null, newName, null); && m.access != Opcodes.ACC_ABSTRACT
} + Opcodes.ACC_PRIVATE
} && m.access != Opcodes.ACC_ABSTRACT
} + Opcodes.ACC_PROTECTED) {
} if (!m.name.equals("main") && !m.name.equals("<init>")
&& !m.name.equals("<clinit>")) {
System.out.println("Obfuscated method names."); String newName = generateUniqueName(stringLength);
}
BytecodeViewer.refactorer.getHooks().addMethod(new MethodMappingData(c.name, new MappingData(m.name, newName), m.desc));
}
/*ASMUtil_OLD.renameMethodNode(c.name, m.name, m.desc,
null, newName, null);*/
}
}
}
}
System.out.println("Obfuscated method names.");
}
}