Untitled conversation
- Userjava
- Assistant
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.HashSet;
import java.util.Random;
import java.util.Set;
@TransformerHierarchy(priorityValue = 2)
public class DeadCodeTransformer implements ITransformer {
private static final Random random = new Random();
@Override
public void transform(ClassNode node) {
createRandomVariables(node);
insertDeadIfStatements(node);
}
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(10000);
} while (existingVariableNames.contains(varName));
existingVariableNames.add(varName);
FieldNode randomField = new FieldNode(
Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC,
varName,
typeDescriptor,
null,
null
);
node.fields.add(randomField);
for (MethodNode method : node.methods) {
if (method.instructions.size() > 0) {
int index = random.nextInt(method.instructions.size());
InsnList instructions = new InsnList();
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));
}
instructions.add(new FieldInsnNode(
Opcodes.PUTSTATIC,
node.name,
varName,
typeDescriptor
));
method.instructions.insert(method.instructions.get(index), instructions);
}
}
}
}
private void insertDeadIfStatements(ClassNode node) {
for (MethodNode method : node.methods) {
if (method.instructions.size() > 0) {
int index = random.nextInt(method.instructions.size());
InsnList instructions = new InsnList();
// Create a random condition that will always be false using integer variables
String var1 = "randomVar" + random.nextInt(10000); // Random variable name
String var2 = "randomVar" + random.nextInt(10000); // Another random variable name
// Load the first variable
instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, node.name, var1, "I"));
instructions.add(new LdcInsnNode(0)); // Load constant 0
instructions.add(new InsnNode(Opcodes.ISUB)); // Subtract to create a false condition
// Load the second variable
instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, node.name, var2, "I"));
instructions.add(new InsnNode(Opcodes.ISUB)); // Subtract to create a false condition
// Create a label for the jump
LabelNode label = new LabelNode();
instructions.add(new JumpInsnNode(Opcodes.IFNE, label)); // Jump if not equal (this will always be false)
// Add dead code here
instructions.add(new InsnNode(Opcodes.RETURN)); // This will never be executed
// Mark the label
instructions.add(label);
// Insert the instructions at a random position in the method
method.instructions.insert(method.instructions.get(index), instructions);
}
}
}
}
Enchance this code by adding more methods and upgrading existing ones, im using class writer both arguments so keep that in mind
Certainly! Enhancing your DeadCodeTransformer can involve adding more methods to inject various types of dead code, improving the randomness and ensuring better integration with the ClassWriter. Below is an enhanced version of your transformer with additional methods and upgrades to existing ones:
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();
@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);
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
));
// For demonstration, 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) {
if (method.instructions.size() > 0) {
int index = random.nextInt(method.instructions.size());
InsnList instructions = new InsnList();
// Choose random integer fields for condition
List<FieldNode> intFields = new ArrayList<>();
for (FieldNode field : node.fields) {
if (field.desc.equals("I")) {
intFields.add(field);
}
}
if (intFields.size() < 2) {
continue; // Need at least two int fields for comparison
}
// Select two distinct 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) { ... }
instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, node.name, field1.name, "I"));
instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, node.name, field2.name, "I"));
instructions.add(new InsnNode(Opcodes.ISUB)); // field1 - field2
LabelNode label = new LabelNode();
instructions.add(new JumpInsnNode(Opcodes.IFNE, label)); // Jump if not equal
// 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 or native methods
if ((method.access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE)) != 0) {
continue;
}
if (method.instructions.size() > 10) { // Ensure there's enough room
int index = random.nextInt(method.instructions.size() - 10) + 5;
InsnList instructions = new InsnList();
LabelNode start = new LabelNode();
LabelNode end = new LabelNode();
// Initialize loop counter
instructions.add(new InsnNode(Opcodes.ICONST_0));
instructions.add(new VarInsnNode(Opcodes.ISTORE, method.maxLocals));
// Start label
instructions.add(start);
// Load counter
instructions.add(new VarInsnNode(Opcodes.ILOAD, method.maxLocals));
instructions.add(new InsnNode(Opcodes.ICONST_5));
instructions.add(new JumpInsnNode(Opcodes.IF_ICMPGE, end)); // if counter >= 5, exit
// Redundant operation
instructions.add(new InsnNode(Opcodes.ICONST_1));
instructions.add(new InsnNode(Opcodes.IADD));
instructions.add(new VarInsnNode(Opcodes.ISTORE, method.maxLocals));
// Jump back to start
instructions.add(new JumpInsnNode(Opcodes.GOTO, start));
// End label
instructions.add(end);
// Update max locals to accommodate new variable
method.maxLocals += 1;
method.instructions.insert(method.instructions.get(index), instructions);
}
}
}
/**
* Inserts redundant arithmetic operations.
*/
private void insertRedundantOperations(ClassNode node) {
for (MethodNode method : node.methods) {
if (method.instructions.size() > 2) {
int index = random.nextInt(method.instructions.size() - 2);
InsnList instructions = new InsnList();
// Perform redundant arithmetic: (a + b) - b
List<FieldNode> intFields = new ArrayList<>();
for (FieldNode field : node.fields) {
if (field.desc.equals("I")) {
intFields.add(field);
}
}
if (intFields.size() < 2) {
continue;
}
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 = "deadMethod" + random.nextInt(10000);
String methodDesc = "()V";
// Ensure the method does not already exist
boolean exists = node.methods.stream().anyMatch(m -> m.name.equals(methodName) && m.desc.equals(methodDesc));
if (exists) continue;
MethodNode deadMethod = new MethodNode(
Opcodes.ACC_PRIVATE, // Private to reduce visibility
methodName,
methodDesc,
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);
}
}
/**
* 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(4);
switch (op) {
case 0:
deadInstructions.add(new InsnNode(Opcodes.NOP));
break;
case 1:
deadInstructions.add(new InsnNode(Opcodes.ICONST_0));
deadInstructions.add(new InsnNode(Opcodes.ICONST_1));
deadInstructions.add(new InsnNode(Opcodes.IADD));
deadInstructions.add(new InsnNode(Opcodes.POP));
break;
case 2:
deadInstructions.add(new InsnNode(Opcodes.ICONST_1));
deadInstructions.add(new InsnNode(Opcodes.IOR));
deadInstructions.add(new InsnNode(Opcodes.POP));
break;
case 3:
deadInstructions.add(new InsnNode(Opcodes.RETURN));
break;
default:
deadInstructions.add(new InsnNode(Opcodes.NOP));
}
}
return deadInstructions;
}
}
Additional Dead Code Injection Methods:
insertFakeLoops: Injects fake loops that perform redundant operations without affecting the program's logic.insertRedundantOperations: Adds unnecessary arithmetic operations to confuse decompilers or analysis tools.injectDeadMethods: Creates private methods that are never called, further obfuscating the class structure.Improved createRandomVariables Method:
Enhanced insertDeadIfStatements Method:
if statements behave as intended.Random Dead Code Generation:
randomDeadCode: Generates a series of dead instructions that include no-operations (NOP), stack manipulations, and other harmless instructions to make reverse engineering harder.General Improvements:
ClassWriterSince you're using ClassWriter alongside ClassNode, it's essential to ensure that the transformations don't interfere with the bytecode generation process. The enhanced transformer maintains compatibility by:
maxStack and maxLocals: When injecting loops or additional instructions that use local variables, maxStack and maxLocals are appropriately updated to accommodate new operations.private static to ensure they don't interfere with other parts of the class, especially if the original class wasn't using static fields.By implementing these enhancements, your DeadCodeTransformer will introduce a more diverse set of obfuscation techniques, making reverse engineering efforts more challenging.
Not yet included in a published release.