2012-08-21 21:11:14 +08:00
|
|
|
|
/*
|
2014-03-12 05:15:43 +08:00
|
|
|
|
Copyright (C) 2011-2014 de4dot@gmail.com
|
2012-08-21 21:11:14 +08:00
|
|
|
|
|
|
|
|
|
This file is part of de4dot.
|
|
|
|
|
|
|
|
|
|
de4dot is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
de4dot is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.IO;
|
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2012-08-21 21:11:14 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.CryptoObfuscator {
|
|
|
|
|
class MethodBodyReader : MethodBodyReaderBase {
|
2012-11-09 05:24:13 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-08-21 21:11:14 +08:00
|
|
|
|
ushort maxStackSize;
|
2014-05-10 15:00:43 +08:00
|
|
|
|
GenericParamContext gpContext;
|
2012-08-21 21:11:14 +08:00
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
public MethodBodyReader(ModuleDefMD module, IBinaryReader reader)
|
2012-08-21 21:11:14 +08:00
|
|
|
|
: base(reader) {
|
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Read(MethodDef method) {
|
2014-05-10 15:00:43 +08:00
|
|
|
|
this.gpContext = GenericParamContext.Create(method);
|
2012-11-09 05:24:13 +08:00
|
|
|
|
this.parameters = method.Parameters;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
SetLocals(GetLocals(method));
|
2012-08-21 21:11:14 +08:00
|
|
|
|
|
|
|
|
|
maxStackSize = (ushort)reader.ReadInt32();
|
2012-11-09 05:24:13 +08:00
|
|
|
|
ReadInstructionsNumBytes(reader.ReadUInt32());
|
2013-01-19 20:03:57 +08:00
|
|
|
|
ReadExceptionHandlers();
|
2012-08-21 21:11:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void ReadExceptionHandlers() {
|
2012-08-21 21:11:14 +08:00
|
|
|
|
int totalSize = reader.ReadInt32();
|
|
|
|
|
if (totalSize == 0)
|
|
|
|
|
return;
|
|
|
|
|
reader.ReadInt32();
|
2013-01-19 20:03:57 +08:00
|
|
|
|
ReadExceptionHandlers((totalSize - 4) / 24);
|
2012-08-21 21:11:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static IList<Local> GetLocals(MethodDef method) {
|
2012-11-09 05:24:13 +08:00
|
|
|
|
if (method.Body == null)
|
|
|
|
|
return new List<Local>();
|
2012-11-20 00:58:34 +08:00
|
|
|
|
return method.Body.Variables;
|
2012-08-21 21:11:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
protected override IField ReadInlineField(Instruction instr) {
|
2014-05-10 15:00:43 +08:00
|
|
|
|
return module.ResolveToken(reader.ReadUInt32(), gpContext) as IField;
|
2012-08-21 21:11:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
protected override IMethod ReadInlineMethod(Instruction instr) {
|
2014-05-10 15:00:43 +08:00
|
|
|
|
return module.ResolveToken(reader.ReadUInt32(), gpContext) as IMethod;
|
2012-08-21 21:11:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
protected override MethodSig ReadInlineSig(Instruction instr) {
|
2014-05-10 15:00:43 +08:00
|
|
|
|
var sas = module.ResolveStandAloneSig(MDToken.ToRID(reader.ReadUInt32()), gpContext);
|
2012-11-09 05:24:13 +08:00
|
|
|
|
return sas == null ? null : sas.MethodSig;
|
2012-08-21 21:11:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
protected override string ReadInlineString(Instruction instr) {
|
|
|
|
|
return module.ReadUserString(reader.ReadUInt32());
|
2012-08-21 21:11:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
protected override ITokenOperand ReadInlineTok(Instruction instr) {
|
2014-05-10 15:00:43 +08:00
|
|
|
|
return module.ResolveToken(reader.ReadUInt32(), gpContext) as ITokenOperand;
|
2012-08-21 21:11:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
protected override ITypeDefOrRef ReadInlineType(Instruction instr) {
|
2014-05-10 15:00:43 +08:00
|
|
|
|
return module.ResolveToken(reader.ReadUInt32(), gpContext) as ITypeDefOrRef;
|
2012-08-21 21:11:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void ReadExceptionHandlers(int numExceptionHandlers) {
|
2012-11-09 05:24:13 +08:00
|
|
|
|
exceptionHandlers = new ExceptionHandler[numExceptionHandlers];
|
|
|
|
|
for (int i = 0; i < exceptionHandlers.Count; i++)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
exceptionHandlers[i] = ReadExceptionHandler();
|
2012-08-21 21:11:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
ExceptionHandler ReadExceptionHandler() {
|
2012-11-09 05:24:13 +08:00
|
|
|
|
var eh = new ExceptionHandler((ExceptionHandlerType)reader.ReadUInt32());
|
2012-08-21 21:11:14 +08:00
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
uint tryOffset = reader.ReadUInt32();
|
|
|
|
|
eh.TryStart = GetInstructionThrow(tryOffset);
|
|
|
|
|
eh.TryEnd = GetInstruction(tryOffset + reader.ReadUInt32());
|
2012-08-21 21:11:14 +08:00
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
uint handlerOffset = reader.ReadUInt32();
|
|
|
|
|
eh.HandlerStart = GetInstructionThrow(handlerOffset);
|
|
|
|
|
eh.HandlerEnd = GetInstruction(handlerOffset + reader.ReadUInt32());
|
2012-08-21 21:11:14 +08:00
|
|
|
|
|
|
|
|
|
switch (eh.HandlerType) {
|
|
|
|
|
case ExceptionHandlerType.Catch:
|
2014-05-10 15:00:43 +08:00
|
|
|
|
eh.CatchType = module.ResolveToken(reader.ReadUInt32(), gpContext) as ITypeDefOrRef;
|
2012-08-21 21:11:14 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ExceptionHandlerType.Filter:
|
2012-11-09 05:24:13 +08:00
|
|
|
|
eh.FilterStart = GetInstructionThrow(reader.ReadUInt32());
|
2012-08-21 21:11:14 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ExceptionHandlerType.Finally:
|
|
|
|
|
case ExceptionHandlerType.Fault:
|
|
|
|
|
default:
|
2012-11-09 05:24:13 +08:00
|
|
|
|
reader.ReadUInt32();
|
2012-08-21 21:11:14 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return eh;
|
|
|
|
|
}
|
2012-08-22 02:17:35 +08:00
|
|
|
|
|
2012-11-18 15:13:51 +08:00
|
|
|
|
public override void RestoreMethod(MethodDef method) {
|
2012-11-09 05:24:13 +08:00
|
|
|
|
base.RestoreMethod(method);
|
|
|
|
|
method.Body.MaxStack = maxStackSize;
|
2012-08-22 02:17:35 +08:00
|
|
|
|
}
|
2012-08-21 21:11:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|