Move deobfuscation methods to their own files
This commit is contained in:
107
src/main/java/info/sigterm/deob/deobfuscators/Jumps.java
Normal file
107
src/main/java/info/sigterm/deob/deobfuscators/Jumps.java
Normal file
@@ -0,0 +1,107 @@
|
||||
package info.sigterm.deob.deobfuscators;
|
||||
|
||||
import info.sigterm.deob.ClassFile;
|
||||
import info.sigterm.deob.ClassGroup;
|
||||
import info.sigterm.deob.Method;
|
||||
import info.sigterm.deob.attributes.code.Block;
|
||||
import info.sigterm.deob.attributes.code.Instruction;
|
||||
import info.sigterm.deob.attributes.code.Instructions;
|
||||
import info.sigterm.deob.attributes.code.instructions.Goto;
|
||||
import info.sigterm.deob.attributes.code.instructions.GotoW;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Jumps
|
||||
{
|
||||
private int checkBlockGraphOnce(ClassGroup group)
|
||||
{
|
||||
int count = 0;
|
||||
for (ClassFile cf : group.getClasses())
|
||||
{
|
||||
for (Method m : new ArrayList<>(cf.getMethods().getMethods()))
|
||||
{
|
||||
if (m.getCode() == null)
|
||||
continue;
|
||||
|
||||
Instructions ins = m.getCode().getInstructions();
|
||||
ins.buildBlocks();
|
||||
ins.buildJumpGraph();
|
||||
List<Block> blocks = ins.getBlocks();
|
||||
for (int i = 0; i < blocks.size(); ++i)
|
||||
{
|
||||
Block block = blocks.get(i);
|
||||
Block prev = i > 0 ? blocks.get(i - 1) : null;
|
||||
|
||||
// only one thing jumps here
|
||||
if (block.begin.from.size() == 1 && prev != null && prev.end.isTerminal())
|
||||
{
|
||||
Instruction from = block.begin.from.get(0); // this instruction jumps to block
|
||||
|
||||
if (from.block == block)
|
||||
continue;
|
||||
|
||||
if (from instanceof Goto || from instanceof GotoW)
|
||||
{
|
||||
++count;
|
||||
|
||||
List<Instruction> ilist = ins.getInstructions();
|
||||
|
||||
// remove instructions
|
||||
for (Instruction in : block.instructions)
|
||||
ilist.remove(in);
|
||||
|
||||
int index = ilist.indexOf(from);
|
||||
|
||||
assert from.block != block;
|
||||
from.block = null;
|
||||
|
||||
// move instructions which jump here to jump to block.begin
|
||||
for (Instruction in : from.from)
|
||||
{
|
||||
assert in.jump.contains(from);
|
||||
assert !in.jump.contains(block.begin);
|
||||
|
||||
in.jump.remove(from);
|
||||
|
||||
in.jump.add(block.begin);
|
||||
block.begin.from.add(in);
|
||||
}
|
||||
from.from.clear();
|
||||
|
||||
// .replace ins
|
||||
for (Instruction in : ilist)
|
||||
in.replace(from, block.begin);
|
||||
|
||||
for (info.sigterm.deob.attributes.code.Exception e : m.getCode().getExceptions().getExceptions())
|
||||
e.replace(from, block.begin);
|
||||
|
||||
ins.remove(from); // remove jump
|
||||
|
||||
// insert instructions from block where jump was
|
||||
for (Instruction in : block.instructions)
|
||||
ilist.add(index++, in);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public void run(ClassGroup g)
|
||||
{
|
||||
int count = 0;
|
||||
int passes = 0;
|
||||
int i;
|
||||
do
|
||||
{
|
||||
i = checkBlockGraphOnce(g);
|
||||
count += i;
|
||||
++passes;
|
||||
}
|
||||
while (i > 0);
|
||||
|
||||
System.out.println("Inlined " + count + " jumps in " + passes + " passes");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package info.sigterm.deob.deobfuscators;
|
||||
|
||||
import info.sigterm.deob.ClassFile;
|
||||
import info.sigterm.deob.ClassGroup;
|
||||
import info.sigterm.deob.Method;
|
||||
import info.sigterm.deob.attributes.Code;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class RuntimeExceptions
|
||||
{
|
||||
public void run(ClassGroup group)
|
||||
{
|
||||
int i = 0;
|
||||
for (ClassFile cf : group.getClasses())
|
||||
{
|
||||
for (Method m : new ArrayList<>(cf.getMethods().getMethods()))
|
||||
{
|
||||
Code c = m.getCode();
|
||||
if (c == null)
|
||||
continue;
|
||||
|
||||
for (info.sigterm.deob.attributes.code.Exception e : new ArrayList<>(c.getExceptions().getExceptions()))
|
||||
{
|
||||
if (e.getCatchType() != null && e.getCatchType().getName().equals("java/lang/RuntimeException"))
|
||||
{
|
||||
c.getExceptions().remove(e);
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("Removed " + i + " exception handlers");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package info.sigterm.deob.deobfuscators;
|
||||
|
||||
import info.sigterm.deob.ClassFile;
|
||||
import info.sigterm.deob.ClassGroup;
|
||||
import info.sigterm.deob.Method;
|
||||
import info.sigterm.deob.attributes.code.Block;
|
||||
import info.sigterm.deob.attributes.code.Instructions;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class UnusedBlocks
|
||||
{
|
||||
public void run(ClassGroup group)
|
||||
{
|
||||
int i = 0;
|
||||
for (ClassFile cf : group.getClasses())
|
||||
{
|
||||
for (Method m : new ArrayList<>(cf.getMethods().getMethods()))
|
||||
{
|
||||
if (m.getCode() == null)
|
||||
continue;
|
||||
|
||||
Instructions ins = m.getCode().getInstructions();
|
||||
ins.buildBlocks();
|
||||
|
||||
int count = 0;
|
||||
for (Block b : new ArrayList<>(ins.getBlocks()))
|
||||
{
|
||||
// first block is the entrypoint, so its always used
|
||||
if (count++ == 0)
|
||||
continue;
|
||||
|
||||
if (b.begin.from.isEmpty() && b.begin.exce.isEmpty())
|
||||
{
|
||||
ins.remove(b);
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("Removed " + i + " unused blocks");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package info.sigterm.deob.deobfuscators;
|
||||
|
||||
import info.sigterm.deob.ClassFile;
|
||||
import info.sigterm.deob.ClassGroup;
|
||||
import info.sigterm.deob.Method;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class UnusedMethods
|
||||
{
|
||||
public void run(ClassGroup group)
|
||||
{
|
||||
group.buildCallGraph();
|
||||
|
||||
int i = 0;
|
||||
for (ClassFile cf : group.getClasses())
|
||||
{
|
||||
for (Method m : new ArrayList<>(cf.getMethods().getMethods()))
|
||||
{
|
||||
/* assume obfuscated names are <= 2 chars */
|
||||
if (m.getName().length() > 2)
|
||||
continue;
|
||||
|
||||
if (!m.isUsed())
|
||||
{
|
||||
cf.getMethods().removeMethod(m);
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Removed " + i + " methods");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package info.sigterm.deob.deobfuscators;
|
||||
|
||||
import info.sigterm.deob.ClassFile;
|
||||
import info.sigterm.deob.ClassGroup;
|
||||
import info.sigterm.deob.Method;
|
||||
import info.sigterm.deob.attributes.code.Instruction;
|
||||
import info.sigterm.deob.execution.Execution;
|
||||
import info.sigterm.deob.pool.NameAndType;
|
||||
import info.sigterm.deob.signature.Signature;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class UnusedParameters
|
||||
{
|
||||
private int[] checkParametersOnce(Execution execution, ClassGroup group)
|
||||
{
|
||||
// removing parameters shifts the others around which is annoying.
|
||||
// if more than one is unused, we'll just remove the one
|
||||
// and do the others on another pass
|
||||
|
||||
int count = 0;
|
||||
int collide = 0;
|
||||
int overrides = 0;
|
||||
for (ClassFile cf : group.getClasses())
|
||||
{
|
||||
for (Method m : cf.getMethods().getMethods())
|
||||
{
|
||||
int offset = m.isStatic() ? 0 : 1;
|
||||
NameAndType nat = m.getNameAndType();
|
||||
Signature signature = nat.getDescriptor();
|
||||
|
||||
for (int variableIndex = 0, lvtIndex = offset;
|
||||
variableIndex < signature.size();
|
||||
lvtIndex += signature.getTypeOfArg(variableIndex++).getSlots())
|
||||
{
|
||||
List<? extends Instruction> lv = m.findLVTInstructionsForVariable(lvtIndex);
|
||||
|
||||
if (lv == null)
|
||||
continue;
|
||||
|
||||
// XXX instead of checking if the lvt index is never accessed,
|
||||
// check execution frames and see if it is never read prior to being
|
||||
// written to, and if so, then remove the parameter, but don't re index
|
||||
// the lvt table.
|
||||
if (!lv.isEmpty())
|
||||
continue;
|
||||
|
||||
if (!m.getOverriddenMethods().isEmpty())
|
||||
{
|
||||
++overrides;
|
||||
continue;
|
||||
}
|
||||
|
||||
Signature newSig = new Signature(m.getDescriptor());
|
||||
newSig.remove(variableIndex);
|
||||
|
||||
Method otherMethod = cf.getMethods().findMethod(new NameAndType(m.getName(), newSig));
|
||||
if (otherMethod != null)
|
||||
{
|
||||
// sometimes removing an unused parameter will cause a signature collision with another function,
|
||||
// just ignore it atm (there seems to be very few)
|
||||
++collide;
|
||||
continue;
|
||||
}
|
||||
|
||||
m.removeParameter(execution, variableIndex, lvtIndex);
|
||||
++count;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return new int[] { count, collide, overrides };
|
||||
}
|
||||
|
||||
public void run(ClassGroup group)
|
||||
{
|
||||
Execution execution = new Execution(group);
|
||||
execution.run();
|
||||
|
||||
int count = 0;
|
||||
int collide = 0;
|
||||
int override = 0;
|
||||
int[] i;
|
||||
do
|
||||
{
|
||||
i = checkParametersOnce(execution, group);
|
||||
|
||||
count += i[0];
|
||||
collide = i[1]; // the next pass may be able to reduce the collisions
|
||||
override = i[2];
|
||||
}
|
||||
while (i[0] > 0);
|
||||
|
||||
System.out.println("Removed " + count + " unused parameters, unable to remove " + collide + " because of signature collisions and " + override + " due to overriding");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user