From 26b3aa46a5c1a7ea1ffc64f79b0c01b360e4245a Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 20 Nov 2015 19:30:15 -0500 Subject: [PATCH] Store obfuscated getter in annotations --- .../runelite/deob/attributes/Attributes.java | 23 ++++++++++++ .../attributes/annotation/Annotation.java | 2 +- .../deob/attributes/annotation/Element.java | 32 ++++++++++++----- .../runelite/deob/deobfuscators/Renamer.java | 35 ++++--------------- .../deobfuscators/arithmetic/ModArith.java | 28 ++++++++++++--- 5 files changed, 77 insertions(+), 43 deletions(-) diff --git a/src/main/java/net/runelite/deob/attributes/Attributes.java b/src/main/java/net/runelite/deob/attributes/Attributes.java index b76cf0d24c..7adc2b1f29 100644 --- a/src/main/java/net/runelite/deob/attributes/Attributes.java +++ b/src/main/java/net/runelite/deob/attributes/Attributes.java @@ -9,6 +9,10 @@ import java.io.IOException; import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.List; +import net.runelite.deob.attributes.annotation.Annotation; +import net.runelite.deob.attributes.annotation.Element; +import net.runelite.deob.pool.PoolEntry; +import net.runelite.deob.signature.Type; public class Attributes { @@ -123,4 +127,23 @@ public class Attributes assert a.getAttributes() == this; attributes.add(a); } + + public void addAnnotation(Type type, PoolEntry value) + { + Annotations an = (Annotations) findType(AttributeType.RUNTIMEVISIBLEANNOTATIONS); + if (an == null) + { + an = new Annotations(this); + this.addAttribute(an); + } + + Annotation annotation = new Annotation(an); + annotation.setType(type); + an.addAnnotation(annotation); + + Element element = new Element(annotation); + element.setType(new Type("value")); + element.setValue(value); + annotation.addElement(element); + } } diff --git a/src/main/java/net/runelite/deob/attributes/annotation/Annotation.java b/src/main/java/net/runelite/deob/attributes/annotation/Annotation.java index dbd6ccb949..bf44f27d01 100644 --- a/src/main/java/net/runelite/deob/attributes/annotation/Annotation.java +++ b/src/main/java/net/runelite/deob/attributes/annotation/Annotation.java @@ -13,7 +13,7 @@ public class Annotation { private final Annotations annotations; private Type type; - private List elements = new ArrayList<>(); + private final List elements = new ArrayList<>(); public Annotation(Annotations annotations) { diff --git a/src/main/java/net/runelite/deob/attributes/annotation/Element.java b/src/main/java/net/runelite/deob/attributes/annotation/Element.java index a05bf34f69..94da9891a2 100644 --- a/src/main/java/net/runelite/deob/attributes/annotation/Element.java +++ b/src/main/java/net/runelite/deob/attributes/annotation/Element.java @@ -4,13 +4,14 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import net.runelite.deob.ConstantPool; +import net.runelite.deob.pool.PoolEntry; import net.runelite.deob.signature.Type; public class Element { private final Annotation annotation; private Type type; - private String value; + private PoolEntry value; public Element(Annotation annotation) { @@ -32,12 +33,12 @@ public class Element this.type = type; } - public String getValue() + public PoolEntry getValue() { return value; } - public void setValue(String value) + public void setValue(PoolEntry value) { this.value = value; } @@ -51,12 +52,12 @@ public class Element byte type = is.readByte(); - if (type != 's') - throw new RuntimeException("can't parse non string annotation element"); + if (type != 's' && type != 'I' && type != 'J') + throw new RuntimeException("can't parse annotation element type " + type); int index = is.readShort(); // pool index to String - value = pool.getUTF8(index); + value = pool.getEntry(index); } public void write(DataOutputStream out) throws IOException @@ -64,7 +65,22 @@ public class Element ConstantPool pool = annotation.getAnnotations().getAttributes().getClassFile().getPool(); out.writeShort(pool.makeUTF8(type.toString())); - out.write('s'); - out.writeShort(pool.makeUTF8(value)); + byte type; + switch (value.getType()) + { + case UTF8: + type = 's'; + break; + case INTEGER: + type = 'I'; + break; + case LONG: + type = 'J'; + break; + default: + throw new RuntimeException("can't write annotation type " + value); + } + out.write(type); + out.writeShort(pool.make(value)); } } diff --git a/src/main/java/net/runelite/deob/deobfuscators/Renamer.java b/src/main/java/net/runelite/deob/deobfuscators/Renamer.java index 14c60ec514..bd2bc197eb 100644 --- a/src/main/java/net/runelite/deob/deobfuscators/Renamer.java +++ b/src/main/java/net/runelite/deob/deobfuscators/Renamer.java @@ -10,20 +10,18 @@ import net.runelite.deob.Deobfuscator; import net.runelite.deob.Field; import net.runelite.deob.Interfaces; import net.runelite.deob.Method; -import net.runelite.deob.attributes.Annotations; -import net.runelite.deob.attributes.AttributeType; -import net.runelite.deob.attributes.Attributes; import net.runelite.deob.attributes.Code; -import net.runelite.deob.attributes.annotation.Annotation; -import net.runelite.deob.attributes.annotation.Element; import net.runelite.deob.attributes.code.Exceptions; import net.runelite.deob.pool.NameAndType; +import net.runelite.deob.pool.UTF8; import net.runelite.deob.signature.Signature; import net.runelite.deob.signature.Type; import net.runelite.deob.util.NameMappings; public class Renamer implements Deobfuscator { + private static final Type OBFUSCATED_NAME_TYPE = new Type("Lnet/runelite/mapping/ObfuscatedName;"); + private final NameMappings mappings; public Renamer(NameMappings mappings) @@ -90,7 +88,7 @@ public class Renamer implements Deobfuscator field.setType(new Type("L" + name + ";", field.getType().getArrayDims())); } - createOriginalNameAnnotation(cf.getAttributes(), cf.getName()); + cf.getAttributes().addAnnotation(OBFUSCATED_NAME_TYPE, new UTF8(cf.getName())); cf.setName(name); } @@ -166,27 +164,6 @@ public class Renamer implements Deobfuscator c.getInstructions().regeneratePool(); } } - - private static final Type OBFUSCATED_NAME_TYPE = new Type("Lnet/runelite/mapping/ObfuscatedName"); - - private void createOriginalNameAnnotation(Attributes attr, String name) - { - Annotations an = (Annotations) attr.findType(AttributeType.RUNTIMEVISIBLEANNOTATIONS); - if (an == null) - { - an = new Annotations(attr); - attr.addAttribute(an); - } - - Annotation annotation = new Annotation(an); - annotation.setType(OBFUSCATED_NAME_TYPE); - an.addAnnotation(annotation); - - Element element = new Element(annotation); - element.setType(new Type("value")); - element.setValue(name); - annotation.addElement(element); - } @Override public void run(ClassGroup group) @@ -204,7 +181,7 @@ public class Renamer implements Deobfuscator if (newName == null) continue; - createOriginalNameAnnotation(field.getAttributes(), field.getName()); + field.getAttributes().addAnnotation(OBFUSCATED_NAME_TYPE, new UTF8(field.getName())); field.setName(newName); ++fields; } @@ -222,7 +199,7 @@ public class Renamer implements Deobfuscator for (Method m : virtualMethods) { - createOriginalNameAnnotation(m.getAttributes(), m.getName()); + m.getAttributes().addAnnotation(OBFUSCATED_NAME_TYPE, new UTF8(m.getName())); m.setName(newName); } methods += virtualMethods.size(); diff --git a/src/main/java/net/runelite/deob/deobfuscators/arithmetic/ModArith.java b/src/main/java/net/runelite/deob/deobfuscators/arithmetic/ModArith.java index 8d15a44d62..ec0ad76bfc 100644 --- a/src/main/java/net/runelite/deob/deobfuscators/arithmetic/ModArith.java +++ b/src/main/java/net/runelite/deob/deobfuscators/arithmetic/ModArith.java @@ -32,6 +32,8 @@ import net.runelite.deob.execution.Execution; import net.runelite.deob.execution.Frame; import net.runelite.deob.execution.InstructionContext; import net.runelite.deob.execution.StackContext; +import net.runelite.deob.pool.PoolEntry; +import net.runelite.deob.signature.Type; import org.apache.commons.collections4.map.MultiValueMap; public class ModArith implements Deobfuscator @@ -41,7 +43,6 @@ public class ModArith implements Deobfuscator private MultiValueMap constantGetters = new MultiValueMap<>(), constantSetters = new MultiValueMap<>(); private List pairs = new ArrayList<>(); - private Encryption encryption = new Encryption(); private List getInsInExpr(InstructionContext ctx, Set set) { @@ -650,6 +651,8 @@ public class ModArith implements Deobfuscator execution.populateInitialMethods(); execution.run(); + Encryption encryption = new Encryption(); + findUses(); findUses2(); reduce2(); @@ -668,7 +671,7 @@ public class ModArith implements Deobfuscator System.out.println("Changed " + ++i); } - System.out.println(pairs); + annotateEncryption(encryption); try { @@ -681,9 +684,24 @@ public class ModArith implements Deobfuscator return i; } - - public Encryption getEncryption() + + private static final Type OBFUSCATED_GETTER = new Type("Lnet/runelite/mapping/ObfuscatedGetter;"); + + private void annotateEncryption(Encryption encryption) { - return encryption; + for (ClassFile cf : group.getClasses()) + { + for (Field f : cf.getFields().getFields()) + { + Pair pair = encryption.getField(f.getPoolField()); + if (pair == null) + continue; + + PoolEntry value = pair.getType() == Long.class ? + new net.runelite.deob.pool.Long((long) pair.getter) : + new net.runelite.deob.pool.Integer((int) pair.getter); + f.getAttributes().addAnnotation(OBFUSCATED_GETTER, value); + } + } } }