Merge pull request #23 from im-frizzy/master

Obfuscation - Renaming
This commit is contained in:
Kalen (Konloch) Kinloch 2015-02-28 20:30:15 -08:00
commit 1799890f7a

AI 샘플 코드 생성 중입니다

Loading...
13 changed files with 3449 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,59 @@
package the.bytecode.club.bytecodeviewer.obfuscators;
import java.util.ArrayList;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.MiscUtils;
/**
* An unfinished obfuscator.
*
* @author Konloch
*
*/
public abstract class JavaObfuscator extends Thread {
@Override
public void run() {
System.out.println("mibbzz is gay");
BytecodeViewer.viewer.setIcon(true);
BytecodeViewer.runningObfuscation = true;
obfuscate();
BytecodeViewer.runningObfuscation = false;
BytecodeViewer.viewer.setIcon(false);
}
public int getStringLength() {
if (BytecodeViewer.viewer.obfuscatorGroup
.isSelected(BytecodeViewer.viewer.strongObf.getModel())) {
return MAX_STRING_LENGTH;
} else { // if(BytecodeViewer.viewer.obfuscatorGroup.isSelected(BytecodeViewer.viewer.lightObf.getModel()))
// {
return MIN_STRING_LENGTH;
}
}
public static int MAX_STRING_LENGTH = 250;
public static int MIN_STRING_LENGTH = 20;
private ArrayList<String> names = new ArrayList<String>();
protected String generateUniqueName(int length) {
boolean found = false;
String name = "";
while (!found) {
String nameTry = MiscUtils.randomString(1) + MiscUtils.randomStringNum(length - 1);
if (!names.contains(nameTry)) {
names.add(nameTry);
name = nameTry;
found = true;
}
}
return name;
}
public abstract void obfuscate();
}
package the.bytecode.club.bytecodeviewer.obfuscators;
import java.util.ArrayList;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.MiscUtils;
/**
* An unfinished obfuscator.
*
* @author Konloch
*
*/
public abstract class JavaObfuscator extends Thread {
@Override
public void run() {
BytecodeViewer.viewer.setIcon(true);
BytecodeViewer.runningObfuscation = true;
obfuscate();
BytecodeViewer.refactorer.run();
BytecodeViewer.runningObfuscation = false;
BytecodeViewer.viewer.setIcon(false);
}
public int getStringLength() {
if (BytecodeViewer.viewer.obfuscatorGroup
.isSelected(BytecodeViewer.viewer.strongObf.getModel())) {
return MAX_STRING_LENGTH;
} else { // if(BytecodeViewer.viewer.obfuscatorGroup.isSelected(BytecodeViewer.viewer.lightObf.getModel()))
// {
return MIN_STRING_LENGTH;
}
}
public static int MAX_STRING_LENGTH = 25;
public static int MIN_STRING_LENGTH = 5;
private ArrayList<String> names = new ArrayList<String>();
protected String generateUniqueName(int length) {
boolean found = false;
String name = "";
while (!found) {
String nameTry = MiscUtils.randomString(1) + MiscUtils.randomStringNum(length - 1);
if (!Character.isJavaIdentifierStart(nameTry.toCharArray()[0]))
continue;
if (!names.contains(nameTry)) {
names.add(nameTry);
name = nameTry;
found = true;
}
}
return name;
}
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,96 @@
package the.bytecode.club.bytecodeviewer.obfuscators.mapping;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
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;
protected final List<String> mappingList;
private StringBuilder builder;
public RefactorMapper(HookMap hookMap) {
sortedClasses = new HashMap<>();
sortedMethods = new HashMap<>();
sortedFields = new HashMap<>();
mappingList = new ArrayList<>();
builder = new StringBuilder();
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)) {
String map = new String(type + " --> " + sortedClasses.get(type).getRefactoredName() + "\n");
if (!mappingList.contains(map))
mappingList.add(map);
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)) {
String map = new String(owner + "." + name + " --> " + owner + sortedFields.get(obfKey).getName().getRefactoredName() + "\n");
if (!mappingList.contains(map))
mappingList.add(map);
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)) {
String map = new String(owner + "." + name + " --> " + owner + sortedMethods.get(obfKey).getMethodName().getRefactoredName() + "\n");
if (!mappingList.contains(map))
mappingList.add(map);
name = sortedMethods.get(obfKey).getMethodName().getRefactoredName();
}
return name;
}
public void printMap() {
for (String map : mappingList) {
builder.append(map);
}
System.out.println(builder.toString());
}
}

View File

@ -0,0 +1,57 @@
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());
}
mapper.printMap();
}
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,55 @@
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 = 5;//getStringLength();
System.out.println("Obfuscating class names...");
classLoop: for (ClassNode c : BytecodeViewer.getLoadedClasses()) {
/** As we dont want to rename classes the user specified as main-classes */
for (String name : BytecodeViewer.main_classes) {
if (name.equals(c.name))
continue classLoop;
}
/** 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;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.api.ASMUtil_OLD;
/**
* Rename fields.
*
* @author Konloch
*
*/
public class RenameFields extends JavaObfuscator {
@Override
public void obfuscate() {
int stringLength = getStringLength();
System.out.println("Obfuscating fields names...");
for (ClassNode c : BytecodeViewer.getLoadedClasses()) {
for (Object o : c.fields.toArray()) {
FieldNode f = (FieldNode) o;
String newName = generateUniqueName(stringLength);
ASMUtil_OLD.renameFieldNode(c.name, f.name, f.desc, null,
newName, null);
f.name = newName;
}
}
System.out.println("Obfuscated field names.");
}
}
package the.bytecode.club.bytecodeviewer.obfuscators.rename;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
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.FieldMappingData;
import the.bytecode.club.bytecodeviewer.obfuscators.mapping.data.MappingData;
/**
* Rename fields.
*
* @author Konloch
*
*/
public class RenameFields extends JavaObfuscator {
@Override
public void obfuscate() {
int stringLength = getStringLength();
System.out.println("Obfuscating fields names...");
for (ClassNode c : BytecodeViewer.getLoadedClasses()) {
for (Object o : c.fields.toArray()) {
FieldNode f = (FieldNode) o;
String newName = generateUniqueName(stringLength);
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;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.api.ASMUtil_OLD;
/**
* Rename methods.
*
* @author Konloch
*
*/
public class RenameMethods extends JavaObfuscator {
@Override
public void obfuscate() {
int stringLength = getStringLength();
System.out.println("Obfuscating method names...");
for (ClassNode c : BytecodeViewer.getLoadedClasses()) {
for (Object o : c.methods.toArray()) {
MethodNode m = (MethodNode) o;
if (m.access != Opcodes.ACC_ABSTRACT
&& m.access != Opcodes.ACC_ABSTRACT
+ Opcodes.ACC_STATIC
&& m.access != Opcodes.ACC_ABSTRACT
+ Opcodes.ACC_STATIC + Opcodes.ACC_PUBLIC
&& m.access != Opcodes.ACC_ABSTRACT
+ Opcodes.ACC_STATIC + Opcodes.ACC_PRIVATE
&& m.access != Opcodes.ACC_ABSTRACT
+ Opcodes.ACC_STATIC + Opcodes.ACC_PROTECTED
&& m.access != Opcodes.ACC_ABSTRACT
+ Opcodes.ACC_PUBLIC
&& 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>")) {
String newName = generateUniqueName(stringLength);
ASMUtil_OLD.renameMethodNode(c.name, m.name, m.desc,
null, newName, null);
}
}
}
}
System.out.println("Obfuscated method names.");
}
}
package the.bytecode.club.bytecodeviewer.obfuscators.rename;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
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;
import the.bytecode.club.bytecodeviewer.obfuscators.mapping.data.MethodMappingData;
/**
* Rename methods.
*
* @author Konloch
*
*/
public class RenameMethods extends JavaObfuscator {
@Override
public void obfuscate() {
int stringLength = getStringLength();
System.out.println("Obfuscating method names...");
for (ClassNode c : BytecodeViewer.getLoadedClasses()) {
methodLoop: for (Object o : c.methods.toArray()) {
MethodNode m = (MethodNode) o;
/* As we dont want to rename native dll methods */
if ((m.access & Opcodes.ACC_NATIVE) != 0)
continue methodLoop;
if (m.access != Opcodes.ACC_ABSTRACT
&& m.access != Opcodes.ACC_ABSTRACT
+ Opcodes.ACC_STATIC
&& m.access != Opcodes.ACC_ABSTRACT
+ Opcodes.ACC_STATIC + Opcodes.ACC_PUBLIC
&& m.access != Opcodes.ACC_ABSTRACT
+ Opcodes.ACC_STATIC + Opcodes.ACC_PRIVATE
&& m.access != Opcodes.ACC_ABSTRACT
+ Opcodes.ACC_STATIC + Opcodes.ACC_PROTECTED
&& m.access != Opcodes.ACC_ABSTRACT
+ Opcodes.ACC_PUBLIC
&& 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>")) {
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.");
}
}