o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Untitled conversation
- User
package me.altair.obf.transformers.impl; import me.altair.obf.transformers.ITransformer; import me.altair.obf.transformers.TransformerHierarchy; import org.objectweb.asm.Opcodes; import org.objectweb.asm.tree.*; import java.util.*; /** * Enhanced DeadCodeTransformer that injects various types of dead code, * including random variables, dead if-statements, fake loops, and redundant operations. */ @TransformerHierarchy(priorityValue = 2) public class DeadCodeTransformer implements ITransformer { private static final Random random = new Random(); // Keep track of all randomly added integer fields to use in dead code private final List<FieldNode> intFields = new ArrayList<>(); @Override public void transform(ClassNode node) { createRandomVariables(node); insertDeadIfStatements(node); insertFakeLoops(node); insertRedundantOperations(node); injectDeadMethods(node); } /** * Creates random fields and initializes them within methods. */ private void createRandomVariables(ClassNode node) { int numberOfVariables = random.nextInt(20) + 10; // Add between 10 and 30 random variables Set<String> existingVariableNames = new HashSet<>(); for (FieldNode field : node.fields) { existingVariableNames.add(field.name); } for (int i = 0; i < numberOfVariables; i++) { String typeDescriptor; Object initialValue; switch (random.nextInt(3)) { case 0: typeDescriptor = "I"; // int initialValue = random.nextInt(); break; case 1: typeDescriptor = "Z"; // boolean initialValue = random.nextBoolean(); break; case 2: typeDescriptor = "Ljava/lang/String;"; // String initialValue = "str" + random.nextInt(10000); break; default: continue; } // Ensure unique variable names String varName; do { varName = "randomVar" + random.nextInt(100000); } while (existingVariableNames.contains(varName)); existingVariableNames.add(varName); FieldNode randomField = new FieldNode( Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC, varName, typeDescriptor, null, initialValue ); node.fields.add(randomField); // If the field is of type int, add to intFields list for later use if (typeDescriptor.equals("I")) { intFields.add(randomField); } for (MethodNode method : node.methods) { if (method.instructions.size() > 0) { int index = random.nextInt(method.instructions.size()); InsnList instructions = new InsnList(); // Load initial value if (typeDescriptor.equals("I")) { instructions.add(new LdcInsnNode(initialValue)); } else if (typeDescriptor.equals("Z")) { instructions.add(new InsnNode((boolean) initialValue ? Opcodes.ICONST_1 : Opcodes.ICONST_0)); } else if (typeDescriptor.equals("Ljava/lang/String;")) { instructions.add(new LdcInsnNode(initialValue)); } // Put static field instructions.add(new FieldInsnNode( Opcodes.PUTSTATIC, node.name, varName, typeDescriptor )); // Optionally, get the field to make it appear used if (random.nextBoolean()) { instructions.add(new FieldInsnNode( Opcodes.GETSTATIC, node.name, varName, typeDescriptor )); // Perform a simple operation based on type if (typeDescriptor.equals("I")) { instructions.add(new InsnNode(Opcodes.ICONST_1)); instructions.add(new InsnNode(Opcodes.IADD)); instructions.add(new InsnNode(Opcodes.POP)); } else if (typeDescriptor.equals("Z")) { instructions.add(new InsnNode(Opcodes.ICONST_1)); instructions.add(new InsnNode(Opcodes.IOR)); instructions.add(new InsnNode(Opcodes.POP)); } else if (typeDescriptor.equals("Ljava/lang/String;")) { instructions.add(new InsnNode(Opcodes.POP)); } } method.instructions.insert(method.instructions.get(index), instructions); } } } } /** * Inserts dead if-statements that never execute their bodies. */ private void insertDeadIfStatements(ClassNode node) { for (MethodNode method : node.methods) { // Avoid injecting into abstract, native, or constructor methods if ((method.access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE)) != 0 || method.name.equals("<init>") || method.name.equals("<clinit>")) { continue; } if (intFields.size() < 2) { continue; // Need at least two int fields for comparison } if (method.instructions.size() > 0) { int index = random.nextInt(method.instructions.size()); InsnList instructions = new InsnList(); // Select two distinct int fields FieldNode field1 = intFields.get(random.nextInt(intFields.size())); FieldNode field2; do { field2 = intFields.get(random.nextInt(intFields.size())); } while (field2 == field1); // Construct condition: if (field1 - field2 != 0) { ... } // To ensure the condition is always false, initialize fields to the same value // However, since fields are randomly initialized, it's safer to use a constant condition // Alternative approach: Use a condition that is always false, e.g., if (false) // Using ICONST_0 followed by IFNE which will never jump instructions.add(new InsnNode(Opcodes.ICONST_0)); // Push 0 (false) onto the stack LabelNode label = new LabelNode(); instructions.add(new JumpInsnNode(Opcodes.IFNE, label)); // if (0 != 0) jump to label (never jumps) // Dead code: will never execute instructions.add(randomDeadCode()); // Label instructions.add(label); method.instructions.insert(method.instructions.get(index), instructions); } } } /** * Inserts fake loops that perform redundant operations. */ private void insertFakeLoops(ClassNode node) { for (MethodNode method : node.methods) { // Avoid injecting into abstract, native, or constructor methods if ((method.access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE)) != 0 || method.name.equals("<init>") || method.name.equals("<clinit>")) { continue; } if (intFields.isEmpty()) { continue; // Need at least one int field as a loop counter } if (method.instructions.size() > 10) { // Ensure there's enough room int index = random.nextInt(method.instructions.size() - 10) + 5; InsnList instructions = new InsnList(); // Increment maxLocals to use a new local variable for the loop counter int newLocal = method.maxLocals; method.maxLocals += 1; LabelNode start = new LabelNode(); LabelNode end = new LabelNode(); // Initialize loop counter to 0 instructions.add(new InsnNode(Opcodes.ICONST_0)); instructions.add(new VarInsnNode(Opcodes.ISTORE, newLocal)); // Start label instructions.add(start); // Load counter instructions.add(new VarInsnNode(Opcodes.ILOAD, newLocal)); instructions.add(new InsnNode(Opcodes.ICONST_5)); instructions.add(new JumpInsnNode(Opcodes.IF_ICMPGE, end)); // if counter >= 5, exit loop // Redundant operation (e.g., increment counter) instructions.add(new VarInsnNode(Opcodes.ILOAD, newLocal)); instructions.add(new InsnNode(Opcodes.ICONST_1)); instructions.add(new InsnNode(Opcodes.IADD)); instructions.add(new VarInsnNode(Opcodes.ISTORE, newLocal)); // Jump back to start instructions.add(new JumpInsnNode(Opcodes.GOTO, start)); // End label instructions.add(end); // Insert the loop instructions at the random position method.instructions.insert(method.instructions.get(index), instructions); } } } /** * Inserts redundant arithmetic operations. */ private void insertRedundantOperations(ClassNode node) { for (MethodNode method : node.methods) { // Avoid injecting into abstract, native, or constructor methods if ((method.access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE)) != 0 || method.name.equals("<init>") || method.name.equals("<clinit>")) { continue; } if (intFields.size() < 2) { continue; // Need at least two int fields for arithmetic operations } if (method.instructions.size() > 2) { int index = random.nextInt(method.instructions.size() - 2); InsnList instructions = new InsnList(); // Select two distinct int fields FieldNode a = intFields.get(random.nextInt(intFields.size())); FieldNode b = intFields.get(random.nextInt(intFields.size())); while (b == a) { b = intFields.get(random.nextInt(intFields.size())); } instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, node.name, a.name, "I")); instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, node.name, b.name, "I")); instructions.add(new InsnNode(Opcodes.IADD)); // a + b instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, node.name, b.name, "I")); instructions.add(new InsnNode(Opcodes.ISUB)); // (a + b) - b instructions.add(new InsnNode(Opcodes.POP)); // Discard the result method.instructions.insert(method.instructions.get(index), instructions); } } } /** * Injects dead methods that are never called. */ private void injectDeadMethods(ClassNode node) { int numberOfDeadMethods = random.nextInt(5) + 1; // Inject between 1 and 5 dead methods for (int i = 0; i < numberOfDeadMethods; i++) { String methodName; do { methodName = "deadMethod" + random.nextInt(100000); } while (methodExists(node, methodName, "()V")); MethodNode deadMethod = new MethodNode( Opcodes.ACC_PRIVATE, // Private to reduce visibility methodName, "()V", null, null ); InsnList instructions = new InsnList(); instructions.add(new InsnNode(Opcodes.RETURN)); deadMethod.instructions = instructions; deadMethod.maxStack = 0; deadMethod.maxLocals = 0; node.methods.add(deadMethod); } } /** * Checks if a method with the given name and descriptor already exists in the class. */ private boolean methodExists(ClassNode node, String name, String desc) { for (MethodNode method : node.methods) { if (method.name.equals(name) && method.desc.equals(desc)) { return true; } } return false; } /** * Generates a set of dead instructions that do not affect the program. */ private InsnList randomDeadCode() { InsnList deadInstructions = new InsnList(); int deadOpsCount = random.nextInt(5) + 1; // Add between 1 and 5 dead operations for (int i = 0; i < deadOpsCount; i++) { int op = random.nextInt(3); // Reduced to 3 for safer injects switch (op) { case 0: deadInstructions.add(new InsnNode(Opcodes.NOP)); break; case 1: // Push a constant and pop it immediately deadInstructions.add(new InsnNode(Opcodes.ICONST_0)); deadInstructions.add(new InsnNode(Opcodes.POP)); break; case 2: // Arbitrary stack operations that do not affect the program state deadInstructions.add(new InsnNode(Opcodes.ICONST_1)); deadInstructions.add(new InsnNode(Opcodes.ICONST_1)); deadInstructions.add(new InsnNode(Opcodes.IADD)); deadInstructions.add(new InsnNode(Opcodes.POP)); break; default: deadInstructions.add(new InsnNode(Opcodes.NOP)); } } return deadInstructions; } }``` Enchance this code by addind even more dead code, more logic, more complexity, and make it more optimized - Assistant