Skip to content

Commit

Permalink
Rewrite InlineStaticFieldTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
EpicPlayerA10 committed Sep 1, 2024
1 parent f4c9c87 commit 710bcef
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 195 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ A deobfuscator for java
> - Testing InstructionMatcher
> - Implementing/Improving transformers
> - Writing tests
> - Safety checks on putstatic in FieldInlineTransformers (overriding values)
> - Feedback on how the new api presents itself (mainly InstructionMatcher)
> <br>
>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package uwu.narumi.deobfuscator.api.asm;

import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.FieldNode;

/**
* @param owner Class that owns this field
* @param name Field's name
* @param desc Field's descriptor
*/
public record FieldRef(String owner, String name, String desc) {
public static FieldRef of(ClassNode classNode, FieldNode fieldNode) {
return new FieldRef(classNode.name, fieldNode.name, fieldNode.desc);
}

public static FieldRef of(FieldInsnNode fieldInsn) {
return new FieldRef(fieldInsn.owner, fieldInsn.name, fieldInsn.desc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ public DeobfuscatorOptions.Builder classReaderFlags(int classReaderFlags) {
}

/**
* Flags for {@link ClassWriter}
* Flags for {@link ClassWriter}. When you set it to {@code 0} you will disable checking the validity
* of the bytecode. Although this is not recommended.
*/
@Contract("_ -> this")
public DeobfuscatorOptions.Builder classWriterFlags(int classWriterFlags) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.function.Predicate;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.*;
import org.objectweb.asm.tree.analysis.AnalyzerException;
import org.objectweb.asm.tree.analysis.Frame;
Expand Down Expand Up @@ -139,6 +140,13 @@ public static List<AbstractInsnNode> getInstructionsBetween(
return instructions;
}

/**
* Analyzes the stack frames of the method
*
* @param classNode The owner class
* @param methodNode Method
* @return A map which corresponds to: instruction -> its own stack frame
*/
public static Map<AbstractInsnNode, Frame<OriginalSourceValue>> analyzeSource(
ClassNode classNode, MethodNode methodNode
) {
Expand Down Expand Up @@ -171,6 +179,27 @@ public static void removeValuesFromStack(MethodNode methodNode, Frame<OriginalSo
}
}

/**
* Convert constant value to instruction that represents this constant
*
* @param value A constant value
* @return An instruction that represents this constant
*/
public static AbstractInsnNode toConstantInsn(Object value) {
if (value == null)
return new InsnNode(ACONST_NULL);
if (value instanceof String || value instanceof Type)
return new LdcInsnNode(value);
if (value instanceof Number number)
return getNumber(number);
if (value instanceof Boolean bool)
return getNumber(bool ? 1 : 0);
if (value instanceof Character character)
return getNumber(character);

throw new IllegalArgumentException("Not a constant");
}

public static InsnList from(AbstractInsnNode... nodes) {
InsnList insnList = new InsnList();
for (AbstractInsnNode node : nodes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,40 @@ public abstract class FramedInstructionsTransformer extends Transformer {
/**
* Returns classes stream on which the transformer will be iterating
*/
protected Stream<ClassWrapper> getClassesStream(Stream<ClassWrapper> stream) {
protected Stream<ClassWrapper> buildClassesStream(Stream<ClassWrapper> stream) {
// Override this method to filter classes
return stream;
}

/**
* Returns methods stream on which the transformer will be iterating
*/
protected Stream<MethodNode> getMethodsStream(Stream<MethodNode> stream) {
protected Stream<MethodNode> buildMethodsStream(Stream<MethodNode> stream) {
// Override this method to filter methods
return stream;
}

/**
* Returns instructions stream on which the transformer will be iterating
*/
protected Stream<AbstractInsnNode> getInstructionsStream(Stream<AbstractInsnNode> stream) {
protected Stream<AbstractInsnNode> buildInstructionsStream(Stream<AbstractInsnNode> stream) {
// Override this method to filter instructions
return stream;
}

@Override
protected boolean transform(ClassWrapper scope, Context context) throws Exception {
getClassesStream(context.classes(scope).stream()).forEach(classWrapper -> getMethodsStream(classWrapper.methods().stream())
buildClassesStream(context.classes(scope).stream()).forEach(classWrapper -> buildMethodsStream(classWrapper.methods().stream())
.forEach(methodNode -> {
// Skip if no instructions
if (getInstructionsStream(Arrays.stream(methodNode.instructions.toArray())).findAny().isEmpty()) return;
if (buildInstructionsStream(Arrays.stream(methodNode.instructions.toArray())).findAny().isEmpty()) return;

// Get frames of the method
Map<AbstractInsnNode, Frame<OriginalSourceValue>> frames = analyzeSource(classWrapper.getClassNode(), methodNode);
if (frames == null) return;

// Iterate over instructions
getInstructionsStream(Arrays.stream(methodNode.instructions.toArray())).forEach(insn -> {
buildInstructionsStream(Arrays.stream(methodNode.instructions.toArray())).forEach(insn -> {
// Get current frame
Frame<OriginalSourceValue> frame = frames.get(insn);
if (frame == null) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ protected void registerAll() {
), "TestInlineLocalVariables");
register("Simple flow obfuscation", InputType.JAVA_CODE, List.of(ComposedGeneralFlowTransformer::new), "TestSimpleFlowObfuscation");
register("Universal Number Transformer", InputType.JAVA_CODE, List.of(UniversalNumberTransformer::new), "TestUniversalNumberTransformer");
// TODO: Uninitialized static fields should replace with 0?
register("Inline static fields", InputType.JAVA_CODE, List.of(InlineStaticFieldTransformer::new), "TestInlineStaticFields");
// TODO: Account for static field modification
register("Inline static fields with modification", InputType.JAVA_CODE, List.of(InlineStaticFieldTransformer::new), "TestInlineStaticFieldsWithModification");

// Samples
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import uwu.narumi.deobfuscator.api.transformer.ComposedTransformer;
import uwu.narumi.deobfuscator.core.other.impl.clean.InvalidMethodCleanTransformer;
import uwu.narumi.deobfuscator.core.other.impl.clean.PeepholeCleanTransformer;
import uwu.narumi.deobfuscator.core.other.impl.clean.peephole.DeadCodeCleanTransformer;
import uwu.narumi.deobfuscator.core.other.impl.pool.InlineLocalVariablesTransformer;
import uwu.narumi.deobfuscator.core.other.impl.pool.InlineStaticFieldTransformer;
import uwu.narumi.deobfuscator.core.other.impl.universal.UniversalNumberTransformer;
import uwu.narumi.deobfuscator.core.other.impl.universal.UniversalFlowTransformer;

public class ComposedGeneralFlowTransformer extends ComposedTransformer {
Expand All @@ -15,7 +13,7 @@ public ComposedGeneralFlowTransformer() {
super(
// Preparation
InvalidMethodCleanTransformer::new,
() -> new InlineStaticFieldTransformer(true, true),
InlineStaticFieldTransformer::new,
InlineLocalVariablesTransformer::new,

// Main transformer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class UselessPopCleanTransformer extends FramedInstructionsTransformer {

@Override
protected Stream<AbstractInsnNode> getInstructionsStream(Stream<AbstractInsnNode> stream) {
protected Stream<AbstractInsnNode> buildInstructionsStream(Stream<AbstractInsnNode> stream) {
return stream
.filter(insn -> insn.getOpcode() == POP || insn.getOpcode() == POP2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public InlineLocalVariablesTransformer() {
}

@Override
protected Stream<AbstractInsnNode> getInstructionsStream(Stream<AbstractInsnNode> stream) {
protected Stream<AbstractInsnNode> buildInstructionsStream(Stream<AbstractInsnNode> stream) {
return stream
.filter(AbstractInsnNode::isVarLoad);
}
Expand Down
Loading

0 comments on commit 710bcef

Please sign in to comment.