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.Type; 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, redundant operations, * and other complex obfuscations. */ @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 of various types 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(6)) { 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; case 3: typeDescriptor = "F"; // float initialValue = random.nextFloat(); break; case 4: typeDescriptor = "D"; // double initialValue = random.nextDouble(); break; case 5: typeDescriptor = "[I"; // int array initialValue = null; // We'll initialize the array later 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); // Store fields of certain types 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)); } else if (typeDescriptor.equals("F")) { instructions.add(new LdcInsnNode(initialValue)); } else if (typeDescriptor.equals("D")) { instructions.add(new LdcInsnNode(initialValue)); } else if (typeDescriptor.equals("[I")) { // Initialize array instructions.add(new InsnNode(Opcodes.ICONST_5)); // Array length 5 instructions.add(new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_INT)); } // 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 MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I", false)); instructions.add(new InsnNode(Opcodes.POP)); } else if (typeDescriptor.equals("F")) { instructions.add(new LdcInsnNode(1.0f)); instructions.add(new InsnNode(Opcodes.FADD)); instructions.add(new InsnNode(Opcodes.POP)); } else if (typeDescriptor.equals("D")) { instructions.add(new LdcInsnNode(1.0)); instructions.add(new InsnNode(Opcodes.DADD)); instructions.add(new InsnNode(Opcodes.POP2)); } else if (typeDescriptor.equals("[I")) { // Load array, push index and value, store into array instructions.add(new InsnNode(Opcodes.ICONST_0)); // index 0 instructions.add(new InsnNode(Opcodes.ICONST_1)); // value 1 instructions.add(new InsnNode(Opcodes.IASTORE)); } } method.instructions.insert(method.instructions.get(index), instructions); } } } } /** * Inserts dead if-statements with complex conditions 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 (method.instructions.size() > 0) { int index = random.nextInt(method.instructions.size()); InsnList instructions = new InsnList(); // Build a complex condition that is always false LabelNode labelFalse = new LabelNode(); LabelNode labelEnd = new LabelNode(); // For example, construct: if ((1 & 0) | false) { dead code } instructions.add(new InsnNode(Opcodes.ICONST_1)); // Push 1 instructions.add(new InsnNode(Opcodes.ICONST_0)); // Push 0 instructions.add(new InsnNode(Opcodes.IAND)); // 1 & 0 => 0 (false) instructions.add(new InsnNode(Opcodes.ICONST_0)); // Push 0 (false) instructions.add(new InsnNode(Opcodes.IOR)); // 0 | 0 => 0 (false) instructions.add(new JumpInsnNode(Opcodes.IFNE, labelFalse)); // if result != 0 (false), jump (never jumps) // Dead code: will never execute instructions.add(randomComplexDeadCode(node)); // Optionally, add some else-dead-code instructions.add(new JumpInsnNode(Opcodes.GOTO, labelEnd)); // labelFalse: instructions.add(labelFalse); if (random.nextBoolean()) { instructions.add(randomComplexDeadCode(node)); } // labelEnd: instructions.add(labelEnd); method.instructions.insert(method.instructions.get(index), instructions); } } } /** * Inserts fake nested 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 (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 new local variables for loop counters int loopCounter1 = method.maxLocals; method.maxLocals += 1; int loopCounter2 = method.maxLocals; method.maxLocals += 1; LabelNode outerStart = new LabelNode(); LabelNode outerEnd = new LabelNode(); LabelNode innerStart = new LabelNode(); LabelNode innerEnd = new LabelNode(); // Initialize outer loop counter to 0 instructions.add(new InsnNode(Opcodes.ICONST_0)); instructions.add(new VarInsnNode(Opcodes.ISTORE, loopCounter1)); // Outer loop start label instructions.add(outerStart); // Load outer loop counter instructions.add(new VarInsnNode(Opcodes.ILOAD, loopCounter1)); instructions.add(new InsnNode(Opcodes.ICONST_3)); instructions.add(new JumpInsnNode(Opcodes.IF_ICMPGE, outerEnd)); // if counter >= 3, exit outer loop // Initialize inner loop counter to 0 instructions.add(new InsnNode(Opcodes.ICONST_0)); instructions.add(new VarInsnNode(Opcodes.ISTORE, loopCounter2)); // Inner loop start label instructions.add(innerStart); // Load inner loop counter instructions.add(new VarInsnNode(Opcodes.ILOAD, loopCounter2)); instructions.add(new InsnNode(Opcodes.ICONST_2)); instructions.add(new JumpInsnNode(Opcodes.IF_ICMPGE, innerEnd)); // if counter >= 2, exit inner loop // Redundant operation inside inner loop instructions.add(randomComplexDeadCode(node)); // Increment inner loop counter instructions.add(new VarInsnNode(Opcodes.ILOAD, loopCounter2)); instructions.add(new InsnNode(Opcodes.ICONST_1)); instructions.add(new InsnNode(Opcodes.IADD)); instructions.add(new VarInsnNode(Opcodes.ISTORE, loopCounter2)); // Jump back to inner loop start instructions.add(new JumpInsnNode(Opcodes.GOTO, innerStart)); // Inner loop end label instructions.add(innerEnd); // Increment outer loop counter instructions.add(new VarInsnNode(Opcodes.ILOAD, loopCounter1)); instructions.add(new InsnNode(Opcodes.ICONST_1)); instructions.add(new InsnNode(Opcodes.IADD)); instructions.add(new VarInsnNode(Opcodes.ISTORE, loopCounter1)); // Jump back to outer loop start instructions.add(new JumpInsnNode(Opcodes.GOTO, outerStart)); // Outer loop end label instructions.add(outerEnd); // Insert the loop instructions at the random position method.instructions.insert(method.instructions.get(index), instructions); } } } /** * Inserts redundant arithmetic and logical operations of various types. */ 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 (method.instructions.size() > 2) { int index = random.nextInt(method.instructions.size() - 2); InsnList instructions = new InsnList(); int opType = random.nextInt(3); switch (opType) { case 0: // Integer arithmetic instructions.add(new LdcInsnNode(random.nextInt())); instructions.add(new LdcInsnNode(random.nextInt())); instructions.add(new InsnNode(Opcodes.IXOR)); instructions.add(new LdcInsnNode(random.nextInt())); instructions.add(new InsnNode(Opcodes.IAND)); instructions.add(new InsnNode(Opcodes.POP)); break; case 1: // Floating point arithmetic instructions.add(new LdcInsnNode(random.nextDouble())); instructions.add(new LdcInsnNode(random.nextDouble())); instructions.add(new InsnNode(Opcodes.DMUL)); instructions.add(new LdcInsnNode(random.nextDouble())); instructions.add(new InsnNode(Opcodes.DDIV)); instructions.add(new InsnNode(Opcodes.POP2)); break; case 2: // Bitwise operations on integers instructions.add(new LdcInsnNode(random.nextInt())); instructions.add(new LdcInsnNode(random.nextInt())); instructions.add(new InsnNode(Opcodes.IOR)); instructions.add(new LdcInsnNode(random.nextInt())); instructions.add(new InsnNode(Opcodes.IREM)); instructions.add(new InsnNode(Opcodes.POP)); break; default: instructions.add(new InsnNode(Opcodes.NOP)); } method.instructions.insert(method.instructions.get(index), instructions); } } } /** * Injects dead methods that may be called within dead code blocks but do nothing significant. */ 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 + Opcodes.ACC_STATIC, // Make static so it can be called easily methodName, "()V", null, null ); InsnList instructions = new InsnList(); instructions.add(randomComplexDeadCode(node)); instructions.add(new InsnNode(Opcodes.RETURN)); deadMethod.instructions = instructions; deadMethod.maxStack = 5; // Adjust stack size as needed deadMethod.maxLocals = 0; node.methods.add(deadMethod); } // Add a dead helper method that returns int if it doesn't exist if (!methodExists(node, "deadHelperMethod", "()I")) { MethodNode helperMethod = new MethodNode( Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC, "deadHelperMethod", "()I", null, null ); InsnList instructions = new InsnList(); instructions.add(new LdcInsnNode(42)); // Push a constant instructions.add(new InsnNode(Opcodes.IRETURN)); helperMethod.instructions = instructions; helperMethod.maxStack = 1; helperMethod.maxLocals = 0; node.methods.add(helperMethod); } } /** * 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 more complex dead instructions that do not affect the program. */ /** * Generates a set of more complex dead instructions that do not affect the program. */ private InsnList randomComplexDeadCode(ClassNode node) { InsnList deadInstructions = new InsnList(); int deadOpsCount = random.nextInt(5) + 3; // Add between 3 and 7 dead operations for (int i = 0; i < deadOpsCount; i++) { int op = random.nextInt(6); switch (op) { case 0: // Add some math operations deadInstructions.add(new LdcInsnNode(random.nextInt(100))); deadInstructions.add(new LdcInsnNode(random.nextInt(100))); deadInstructions.add(new InsnNode(Opcodes.IADD)); deadInstructions.add(new InsnNode(Opcodes.POP)); break; case 1: // Call a dead helper method deadInstructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, node.name, "deadHelperMethod", "()I", false)); deadInstructions.add(new InsnNode(Opcodes.POP)); break; case 2: // Create an object and discard it deadInstructions.add(new TypeInsnNode(Opcodes.NEW, "java/lang/Object")); deadInstructions.add(new InsnNode(Opcodes.DUP)); deadInstructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false)); deadInstructions.add(new InsnNode(Opcodes.POP)); break; case 3: // Push a float and pop it deadInstructions.add(new LdcInsnNode(random.nextFloat())); deadInstructions.add(new InsnNode(Opcodes.POP)); break; case 4: // Push a double and pop it deadInstructions.add(new LdcInsnNode(random.nextDouble())); deadInstructions.add(new InsnNode(Opcodes.POP2)); break; case 5: // Push a string and invoke length deadInstructions.add(new LdcInsnNode("deadString" + random.nextInt(1000))); deadInstructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I", false)); deadInstructions.add(new InsnNode(Opcodes.POP)); break; default: deadInstructions.add(new InsnNode(Opcodes.NOP)); } } return deadInstructions; } }``` Can we highly enchance this code? add veryyy big amount of dead code by that i mean veryyyyyyyyyy big - Assistant
- User
give me whole changed code
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.