Update MethodBodyReaderBase
- Change field types to IList<T> - Add restoreMethod() - Add readInstructionsNumBytes()
This commit is contained in:
parent
353173fcb3
commit
729780c235
|
@ -60,9 +60,9 @@ namespace de4dot.code.deobfuscators.Babel_NET {
|
||||||
|
|
||||||
public int Flags2 { get; set; }
|
public int Flags2 { get; set; }
|
||||||
public short MaxStack { get; set; }
|
public short MaxStack { get; set; }
|
||||||
public List<VariableDefinition> Locals { get; set; }
|
public IList<VariableDefinition> Locals { get; set; }
|
||||||
public Instruction[] Instructions { get; set; }
|
public IList<Instruction> Instructions { get; set; }
|
||||||
public ExceptionHandler[] ExceptionHandlers { get; set; }
|
public IList<ExceptionHandler> ExceptionHandlers { get; set; }
|
||||||
|
|
||||||
public bool IsStatic {
|
public bool IsStatic {
|
||||||
get { return (Flags2 & 0x10) != 0; }
|
get { return (Flags2 & 0x10) != 0; }
|
||||||
|
|
|
@ -241,28 +241,9 @@ namespace de4dot.code.deobfuscators.ILProtector {
|
||||||
}
|
}
|
||||||
|
|
||||||
static void restoreMethod(MethodDefinition method, MethodReader methodReader) {
|
static void restoreMethod(MethodDefinition method, MethodReader methodReader) {
|
||||||
var body = method.Body;
|
|
||||||
|
|
||||||
// body.MaxStackSize = <let Mono.Cecil calculate this>
|
// body.MaxStackSize = <let Mono.Cecil calculate this>
|
||||||
body.InitLocals = methodReader.InitLocals;
|
method.Body.InitLocals = methodReader.InitLocals;
|
||||||
|
methodReader.restoreMethod(method);
|
||||||
body.Variables.Clear();
|
|
||||||
if (methodReader.Locals != null) {
|
|
||||||
foreach (var local in methodReader.Locals)
|
|
||||||
body.Variables.Add(local);
|
|
||||||
}
|
|
||||||
|
|
||||||
body.Instructions.Clear();
|
|
||||||
if (methodReader.Instructions != null) {
|
|
||||||
foreach (var instr in methodReader.Instructions)
|
|
||||||
body.Instructions.Add(instr);
|
|
||||||
}
|
|
||||||
|
|
||||||
body.ExceptionHandlers.Clear();
|
|
||||||
if (methodReader.ExceptionHandlers != null) {
|
|
||||||
foreach (var eh in methodReader.ExceptionHandlers)
|
|
||||||
body.ExceptionHandlers.Add(eh);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int getMethodId(MethodDefinition method) {
|
int getMethodId(MethodDefinition method) {
|
||||||
|
|
|
@ -27,9 +27,9 @@ using de4dot.blocks;
|
||||||
namespace de4dot.code.deobfuscators {
|
namespace de4dot.code.deobfuscators {
|
||||||
abstract class MethodBodyReaderBase {
|
abstract class MethodBodyReaderBase {
|
||||||
protected BinaryReader reader;
|
protected BinaryReader reader;
|
||||||
public List<VariableDefinition> Locals { get; set; }
|
public IList<VariableDefinition> Locals { get; set; }
|
||||||
public Instruction[] Instructions { get; set; }
|
public IList<Instruction> Instructions { get; set; }
|
||||||
public ExceptionHandler[] ExceptionHandlers { get; set; }
|
public IList<ExceptionHandler> ExceptionHandlers { get; set; }
|
||||||
protected IList<ParameterDefinition> parameters;
|
protected IList<ParameterDefinition> parameters;
|
||||||
int currentOffset;
|
int currentOffset;
|
||||||
|
|
||||||
|
@ -46,17 +46,34 @@ namespace de4dot.code.deobfuscators {
|
||||||
protected void readInstructions(int numInstrs) {
|
protected void readInstructions(int numInstrs) {
|
||||||
Instructions = new Instruction[numInstrs];
|
Instructions = new Instruction[numInstrs];
|
||||||
currentOffset = 0;
|
currentOffset = 0;
|
||||||
for (int i = 0; i < Instructions.Length; i++) {
|
for (int i = 0; i < Instructions.Count; i++)
|
||||||
var instr = readInstruction();
|
Instructions[i] = readOneInstruction();
|
||||||
Instructions[i] = instr;
|
fixBranches();
|
||||||
if (instr.OpCode.Code == Code.Switch) {
|
}
|
||||||
int[] targets = (int[])instr.Operand;
|
|
||||||
currentOffset += instr.OpCode.Size + 4 + 4 * targets.Length;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
currentOffset += Instructions[i].GetSize();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
protected void readInstructionsNumBytes(uint codeSize) {
|
||||||
|
var instrs = new List<Instruction>();
|
||||||
|
long endOffs = reader.BaseStream.Position + codeSize;
|
||||||
|
while (reader.BaseStream.Position < endOffs)
|
||||||
|
instrs.Add(readOneInstruction());
|
||||||
|
if (reader.BaseStream.Position != endOffs)
|
||||||
|
throw new ApplicationException("Could not read all instructions");
|
||||||
|
Instructions = instrs;
|
||||||
|
fixBranches();
|
||||||
|
}
|
||||||
|
|
||||||
|
Instruction readOneInstruction() {
|
||||||
|
var instr = readInstruction();
|
||||||
|
if (instr.OpCode.Code == Code.Switch) {
|
||||||
|
int[] targets = (int[])instr.Operand;
|
||||||
|
currentOffset += instr.OpCode.Size + 4 + 4 * targets.Length;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
currentOffset += instr.GetSize();
|
||||||
|
return instr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void fixBranches() {
|
||||||
foreach (var instr in Instructions) {
|
foreach (var instr in Instructions) {
|
||||||
switch (instr.OpCode.OperandType) {
|
switch (instr.OpCode.OperandType) {
|
||||||
case OperandType.InlineBrTarget:
|
case OperandType.InlineBrTarget:
|
||||||
|
@ -231,10 +248,32 @@ namespace de4dot.code.deobfuscators {
|
||||||
|
|
||||||
protected void readExceptionHandlers(int numExceptionHandlers) {
|
protected void readExceptionHandlers(int numExceptionHandlers) {
|
||||||
ExceptionHandlers = new ExceptionHandler[numExceptionHandlers];
|
ExceptionHandlers = new ExceptionHandler[numExceptionHandlers];
|
||||||
for (int i = 0; i < ExceptionHandlers.Length; i++)
|
for (int i = 0; i < ExceptionHandlers.Count; i++)
|
||||||
ExceptionHandlers[i] = readExceptionHandler();
|
ExceptionHandlers[i] = readExceptionHandler();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract ExceptionHandler readExceptionHandler();
|
protected abstract ExceptionHandler readExceptionHandler();
|
||||||
|
|
||||||
|
public void restoreMethod(MethodDefinition method) {
|
||||||
|
var body = method.Body;
|
||||||
|
|
||||||
|
body.Variables.Clear();
|
||||||
|
if (Locals != null) {
|
||||||
|
foreach (var local in Locals)
|
||||||
|
body.Variables.Add(local);
|
||||||
|
}
|
||||||
|
|
||||||
|
body.Instructions.Clear();
|
||||||
|
if (Instructions != null) {
|
||||||
|
foreach (var instr in Instructions)
|
||||||
|
body.Instructions.Add(instr);
|
||||||
|
}
|
||||||
|
|
||||||
|
body.ExceptionHandlers.Clear();
|
||||||
|
if (ExceptionHandlers != null) {
|
||||||
|
foreach (var eh in ExceptionHandlers)
|
||||||
|
body.ExceptionHandlers.Add(eh);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user