de4dot-cex/de4dot.code/deobfuscators/Agile_NET/vm/v1/CsvmToCilMethodConverter.cs

436 lines
14 KiB
C#
Raw Normal View History

2012-04-06 00:06:56 +08:00
/*
2013-01-02 00:03:16 +08:00
Copyright (C) 2011-2013 de4dot@gmail.com
2012-04-06 00:06:56 +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;
using System.IO;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2012-04-06 00:06:56 +08:00
using de4dot.blocks;
2013-10-30 01:11:31 +08:00
namespace de4dot.code.deobfuscators.Agile_NET.vm.v1 {
2012-04-06 00:06:56 +08:00
class CsvmToCilMethodConverter {
IDeobfuscatorContext deobfuscatorContext;
2012-11-07 12:17:45 +08:00
ModuleDefMD module;
2012-04-06 00:06:56 +08:00
VmOpCodeHandlerDetector opCodeDetector;
CilOperandInstructionRestorer operandRestorer = new CilOperandInstructionRestorer();
2012-11-07 12:17:45 +08:00
public CsvmToCilMethodConverter(IDeobfuscatorContext deobfuscatorContext, ModuleDefMD module, VmOpCodeHandlerDetector opCodeDetector) {
2012-04-06 00:06:56 +08:00
this.deobfuscatorContext = deobfuscatorContext;
this.module = module;
this.opCodeDetector = opCodeDetector;
}
2013-01-19 20:03:57 +08:00
public void Convert(MethodDef cilMethod, CsvmMethodData csvmMethod) {
var newInstructions = ReadInstructions(cilMethod, csvmMethod);
var newLocals = ReadLocals(cilMethod, csvmMethod);
var newExceptions = ReadExceptions(cilMethod, csvmMethod, newInstructions);
2012-04-06 00:06:56 +08:00
2013-01-19 20:03:57 +08:00
FixInstructionOperands(newInstructions);
FixLocals(newInstructions, cilMethod.Body.Variables);
FixArgs(newInstructions, cilMethod);
2012-04-06 00:06:56 +08:00
2013-01-19 20:03:57 +08:00
DotNetUtils.RestoreBody(cilMethod, newInstructions, newExceptions);
2012-04-06 00:06:56 +08:00
2013-01-19 20:03:57 +08:00
if (!operandRestorer.Restore(cilMethod))
Logger.w("Failed to restore one or more instruction operands in CSVM method {0:X8}", cilMethod.MDToken.ToInt32());
2013-01-19 20:03:57 +08:00
RestoreConstrainedPrefix(cilMethod);
2012-04-06 00:06:56 +08:00
}
2013-01-19 20:03:57 +08:00
void FixLocals(IList<Instruction> instrs, IList<Local> locals) {
2012-04-06 00:06:56 +08:00
foreach (var instr in instrs) {
var op = instr.Operand as LocalOperand;
if (op == null)
continue;
2013-01-19 20:03:57 +08:00
UpdateLocalInstruction(instr, locals[op.local], op.local);
2012-04-06 00:06:56 +08:00
}
}
2013-01-19 20:03:57 +08:00
static void UpdateLocalInstruction(Instruction instr, Local local, int index) {
2012-04-06 00:06:56 +08:00
object operand = null;
OpCode opcode;
switch (instr.OpCode.Code) {
case Code.Ldloc_S:
case Code.Ldloc:
if (index == 0)
opcode = OpCodes.Ldloc_0;
else if (index == 1)
opcode = OpCodes.Ldloc_1;
else if (index == 2)
opcode = OpCodes.Ldloc_2;
else if (index == 3)
opcode = OpCodes.Ldloc_3;
else if (byte.MinValue <= index && index <= byte.MaxValue) {
opcode = OpCodes.Ldloc_S;
operand = local;
}
else {
opcode = OpCodes.Ldloc;
operand = local;
}
break;
case Code.Stloc:
case Code.Stloc_S:
if (index == 0)
opcode = OpCodes.Stloc_0;
else if (index == 1)
opcode = OpCodes.Stloc_1;
else if (index == 2)
opcode = OpCodes.Stloc_2;
else if (index == 3)
opcode = OpCodes.Stloc_3;
else if (byte.MinValue <= index && index <= byte.MaxValue) {
opcode = OpCodes.Stloc_S;
operand = local;
}
else {
opcode = OpCodes.Stloc;
operand = local;
}
break;
case Code.Ldloca:
case Code.Ldloca_S:
if (byte.MinValue <= index && index <= byte.MaxValue) {
opcode = OpCodes.Ldloca_S;
operand = local;
}
else {
opcode = OpCodes.Ldloca;
operand = local;
}
break;
default:
throw new ApplicationException("Invalid opcode");
}
instr.OpCode = opcode;
instr.Operand = operand;
}
2013-01-19 20:03:57 +08:00
void FixArgs(IList<Instruction> instrs, MethodDef method) {
2012-04-06 00:06:56 +08:00
foreach (var instr in instrs) {
var op = instr.Operand as ArgOperand;
if (op == null)
continue;
2013-01-19 20:03:57 +08:00
UpdateArgInstruction(instr, method.Parameters[op.arg], op.arg);
2012-04-06 00:06:56 +08:00
}
}
2013-01-19 20:03:57 +08:00
static void UpdateArgInstruction(Instruction instr, Parameter arg, int index) {
2012-04-06 00:06:56 +08:00
switch (instr.OpCode.Code) {
case Code.Ldarg:
case Code.Ldarg_S:
if (index == 0) {
instr.OpCode = OpCodes.Ldarg_0;
instr.Operand = null;
}
else if (index == 1) {
instr.OpCode = OpCodes.Ldarg_1;
instr.Operand = null;
}
else if (index == 2) {
instr.OpCode = OpCodes.Ldarg_2;
instr.Operand = null;
}
else if (index == 3) {
instr.OpCode = OpCodes.Ldarg_3;
instr.Operand = null;
}
else if (byte.MinValue <= index && index <= byte.MaxValue) {
instr.OpCode = OpCodes.Ldarg_S;
instr.Operand = arg;
}
else {
instr.OpCode = OpCodes.Ldarg;
instr.Operand = arg;
}
break;
case Code.Starg:
case Code.Starg_S:
if (byte.MinValue <= index && index <= byte.MaxValue) {
instr.OpCode = OpCodes.Starg_S;
instr.Operand = arg;
}
else {
instr.OpCode = OpCodes.Starg;
instr.Operand = arg;
}
break;
case Code.Ldarga:
case Code.Ldarga_S:
if (byte.MinValue <= index && index <= byte.MaxValue) {
instr.OpCode = OpCodes.Ldarga_S;
instr.Operand = arg;
}
else {
instr.OpCode = OpCodes.Ldarga;
instr.Operand = arg;
}
break;
default:
throw new ApplicationException("Invalid opcode");
}
}
2013-01-19 20:03:57 +08:00
List<Instruction> ReadInstructions(MethodDef cilMethod, CsvmMethodData csvmMethod) {
2012-04-06 00:06:56 +08:00
var reader = new BinaryReader(new MemoryStream(csvmMethod.Instructions));
var instrs = new List<Instruction>();
2012-11-07 12:17:45 +08:00
uint offset = 0;
2012-04-06 00:06:56 +08:00
while (reader.BaseStream.Position < reader.BaseStream.Length) {
int vmOpCode = reader.ReadUInt16();
var instr = opCodeDetector.Handlers[vmOpCode].Read(reader);
2012-04-06 00:06:56 +08:00
instr.Offset = offset;
2013-01-19 20:03:57 +08:00
offset += (uint)GetInstructionSize(instr);
2012-04-06 00:06:56 +08:00
instrs.Add(instr);
}
return instrs;
}
2013-01-19 20:03:57 +08:00
static int GetInstructionSize(Instruction instr) {
2012-04-06 00:06:56 +08:00
var opcode = instr.OpCode;
if (opcode == null)
return 5; // Load store/field
var op = instr.Operand as SwitchTargetDisplOperand;
if (op == null)
return instr.GetSize();
return instr.OpCode.Size + (op.targetDisplacements.Length + 1) * 4;
}
2013-01-19 20:03:57 +08:00
List<Local> ReadLocals(MethodDef cilMethod, CsvmMethodData csvmMethod) {
2012-11-07 12:17:45 +08:00
var locals = new List<Local>();
2012-04-06 00:06:56 +08:00
var reader = new BinaryReader(new MemoryStream(csvmMethod.Locals));
if (csvmMethod.Locals.Length == 0)
return locals;
2012-05-16 01:05:44 +08:00
// v6.0.0.5 sometimes duplicates the last two locals so only check for a negative value.
2012-04-06 00:06:56 +08:00
int numLocals = reader.ReadInt32();
2012-05-16 01:05:44 +08:00
if (numLocals < 0)
2012-04-06 00:06:56 +08:00
throw new ApplicationException("Invalid number of locals");
for (int i = 0; i < numLocals; i++)
2013-01-19 20:03:57 +08:00
locals.Add(new Local(ReadTypeRef(reader)));
2012-04-06 00:06:56 +08:00
return locals;
}
2013-01-19 20:03:57 +08:00
TypeSig ReadTypeRef(BinaryReader reader) {
2012-04-06 00:06:56 +08:00
var etype = (ElementType)reader.ReadInt32();
switch (etype) {
2012-11-07 12:17:45 +08:00
case ElementType.Void: return module.CorLibTypes.Void;
case ElementType.Boolean: return module.CorLibTypes.Boolean;
case ElementType.Char: return module.CorLibTypes.Char;
case ElementType.I1: return module.CorLibTypes.SByte;
case ElementType.U1: return module.CorLibTypes.Byte;
case ElementType.I2: return module.CorLibTypes.Int16;
case ElementType.U2: return module.CorLibTypes.UInt16;
case ElementType.I4: return module.CorLibTypes.Int32;
case ElementType.U4: return module.CorLibTypes.UInt32;
case ElementType.I8: return module.CorLibTypes.Int64;
case ElementType.U8: return module.CorLibTypes.UInt64;
case ElementType.R4: return module.CorLibTypes.Single;
case ElementType.R8: return module.CorLibTypes.Double;
case ElementType.String: return module.CorLibTypes.String;
case ElementType.TypedByRef: return module.CorLibTypes.TypedReference;
case ElementType.I: return module.CorLibTypes.IntPtr;
case ElementType.U: return module.CorLibTypes.UIntPtr;
case ElementType.Object: return module.CorLibTypes.Object;
2012-04-06 00:06:56 +08:00
2012-04-06 18:25:15 +08:00
case ElementType.ValueType:
case ElementType.Var:
case ElementType.MVar:
2012-11-07 12:17:45 +08:00
return (module.ResolveToken(reader.ReadUInt32()) as ITypeDefOrRef).ToTypeSig();
2012-04-06 18:25:15 +08:00
case ElementType.GenericInst:
etype = (ElementType)reader.ReadInt32();
if (etype == ElementType.ValueType)
2012-11-07 12:17:45 +08:00
return (module.ResolveToken(reader.ReadUInt32()) as ITypeDefOrRef).ToTypeSig();
2012-04-06 18:25:15 +08:00
// ElementType.Class
2012-11-07 12:17:45 +08:00
return module.CorLibTypes.Object;
2012-04-06 18:25:15 +08:00
2012-04-06 00:06:56 +08:00
case ElementType.Ptr:
case ElementType.Class:
case ElementType.Array:
case ElementType.FnPtr:
2012-11-07 12:17:45 +08:00
case ElementType.SZArray:
2012-04-06 00:06:56 +08:00
case ElementType.ByRef:
2012-11-07 12:17:45 +08:00
case ElementType.CModReqd:
2012-04-06 00:06:56 +08:00
case ElementType.CModOpt:
case ElementType.Internal:
case ElementType.Sentinel:
case ElementType.Pinned:
default:
2012-11-07 12:17:45 +08:00
return module.CorLibTypes.Object;
2012-04-06 00:06:56 +08:00
}
}
2013-01-19 20:03:57 +08:00
List<ExceptionHandler> ReadExceptions(MethodDef cilMethod, CsvmMethodData csvmMethod, List<Instruction> cilInstructions) {
2012-04-06 00:06:56 +08:00
var reader = new BinaryReader(new MemoryStream(csvmMethod.Exceptions));
var ehs = new List<ExceptionHandler>();
if (reader.BaseStream.Length == 0)
return ehs;
int numExceptions = reader.ReadInt32();
if (numExceptions < 0)
throw new ApplicationException("Invalid number of exception handlers");
for (int i = 0; i < numExceptions; i++) {
var eh = new ExceptionHandler((ExceptionHandlerType)reader.ReadInt32());
2013-01-19 20:03:57 +08:00
eh.TryStart = GetInstruction(cilInstructions, reader.ReadInt32());
eh.TryEnd = GetInstructionEnd(cilInstructions, reader.ReadInt32());
eh.HandlerStart = GetInstruction(cilInstructions, reader.ReadInt32());
eh.HandlerEnd = GetInstructionEnd(cilInstructions, reader.ReadInt32());
2012-04-06 00:06:56 +08:00
if (eh.HandlerType == ExceptionHandlerType.Catch)
2012-11-07 12:17:45 +08:00
eh.CatchType = module.ResolveToken(reader.ReadUInt32()) as ITypeDefOrRef;
2012-04-06 00:06:56 +08:00
else if (eh.HandlerType == ExceptionHandlerType.Filter)
2013-01-19 20:03:57 +08:00
eh.FilterStart = GetInstruction(cilInstructions, reader.ReadInt32());
2012-04-06 00:06:56 +08:00
ehs.Add(eh);
}
return ehs;
}
2013-01-19 20:03:57 +08:00
static Instruction GetInstruction(IList<Instruction> instrs, int index) {
2012-04-06 00:06:56 +08:00
return instrs[index];
}
2013-01-19 20:03:57 +08:00
static Instruction GetInstructionEnd(IList<Instruction> instrs, int index) {
2012-04-06 00:06:56 +08:00
index++;
if (index == instrs.Count)
return null;
return instrs[index];
}
2013-01-19 20:03:57 +08:00
static Instruction GetInstruction(IList<Instruction> instrs, Instruction source, int displ) {
2012-04-06 00:06:56 +08:00
int sourceIndex = instrs.IndexOf(source);
if (sourceIndex < 0)
throw new ApplicationException("Could not find source instruction");
return instrs[sourceIndex + displ];
}
2013-01-19 20:03:57 +08:00
void FixInstructionOperands(IList<Instruction> instrs) {
2012-04-06 00:06:56 +08:00
foreach (var instr in instrs) {
var op = instr.Operand as IVmOperand;
if (op != null)
2013-01-19 20:03:57 +08:00
instr.Operand = FixOperand(instrs, instr, op);
2012-04-06 00:06:56 +08:00
}
}
2013-01-19 20:03:57 +08:00
object FixOperand(IList<Instruction> instrs, Instruction instr, IVmOperand vmOperand) {
2012-04-06 00:06:56 +08:00
if (vmOperand is TokenOperand)
2013-01-19 20:03:57 +08:00
return GetMemberRef(((TokenOperand)vmOperand).token);
2012-04-06 00:06:56 +08:00
if (vmOperand is TargetDisplOperand)
2013-01-19 20:03:57 +08:00
return GetInstruction(instrs, instr, ((TargetDisplOperand)vmOperand).displacement);
2012-04-06 00:06:56 +08:00
if (vmOperand is SwitchTargetDisplOperand) {
var targetDispls = ((SwitchTargetDisplOperand)vmOperand).targetDisplacements;
Instruction[] targets = new Instruction[targetDispls.Length];
for (int i = 0; i < targets.Length; i++)
2013-01-19 20:03:57 +08:00
targets[i] = GetInstruction(instrs, instr, targetDispls[i]);
2012-04-06 00:06:56 +08:00
return targets;
}
if (vmOperand is ArgOperand || vmOperand is LocalOperand)
return vmOperand;
if (vmOperand is LoadFieldOperand)
2013-01-19 20:03:57 +08:00
return FixLoadStoreFieldInstruction(instr, ((LoadFieldOperand)vmOperand).token, OpCodes.Ldsfld, OpCodes.Ldfld);
2012-04-06 00:06:56 +08:00
if (vmOperand is LoadFieldAddressOperand)
2013-01-19 20:03:57 +08:00
return FixLoadStoreFieldInstruction(instr, ((LoadFieldAddressOperand)vmOperand).token, OpCodes.Ldsflda, OpCodes.Ldflda);
2012-04-06 00:06:56 +08:00
if (vmOperand is StoreFieldOperand)
2013-01-19 20:03:57 +08:00
return FixLoadStoreFieldInstruction(instr, ((StoreFieldOperand)vmOperand).token, OpCodes.Stsfld, OpCodes.Stfld);
2012-04-06 00:06:56 +08:00
throw new ApplicationException(string.Format("Unknown operand type: {0}", vmOperand.GetType()));
}
2013-01-19 20:03:57 +08:00
IField FixLoadStoreFieldInstruction(Instruction instr, int token, OpCode staticInstr, OpCode instanceInstr) {
2012-11-07 12:17:45 +08:00
var fieldRef = module.ResolveToken(token) as IField;
2013-01-19 20:03:57 +08:00
var field = deobfuscatorContext.ResolveField(fieldRef);
2012-04-06 00:06:56 +08:00
bool isStatic;
if (field == null) {
Logger.w("Could not resolve field {0:X8}. Assuming it's not static.", token);
2012-04-06 00:06:56 +08:00
isStatic = false;
}
else
isStatic = field.IsStatic;
instr.OpCode = isStatic ? staticInstr : instanceInstr;
return fieldRef;
}
2013-01-19 20:03:57 +08:00
ITokenOperand GetMemberRef(int token) {
2012-11-07 12:17:45 +08:00
var memberRef = module.ResolveToken(token) as ITokenOperand;
2012-04-06 00:06:56 +08:00
if (memberRef == null)
throw new ApplicationException(string.Format("Could not find member ref: {0:X8}", token));
return memberRef;
}
2012-04-06 22:08:35 +08:00
2013-01-19 20:03:57 +08:00
static void RestoreConstrainedPrefix(MethodDef method) {
2012-04-06 22:08:35 +08:00
if (method == null || method.Body == null)
return;
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count; i++) {
var instr = instrs[i];
if (instr.OpCode.Code != Code.Callvirt)
continue;
2012-11-07 12:17:45 +08:00
var calledMethod = instr.Operand as IMethod;
if (calledMethod == null)
2012-04-06 22:08:35 +08:00
continue;
2012-11-07 12:17:45 +08:00
var sig = calledMethod.MethodSig;
if (sig == null || !sig.HasThis)
continue;
2013-01-19 20:03:57 +08:00
var thisType = MethodStack.GetLoadedType(method, instrs, i, sig.Params.Count) as ByRefSig;
2012-04-06 22:08:35 +08:00
if (thisType == null)
continue;
2013-01-19 20:03:57 +08:00
if (HasPrefix(instrs, i, Code.Constrained))
2012-04-06 22:08:35 +08:00
continue;
2012-12-16 07:03:56 +08:00
instrs.Insert(i, OpCodes.Constrained.ToInstruction(thisType.Next.ToTypeDefOrRef()));
2012-04-06 22:08:35 +08:00
i++;
}
}
2013-01-19 20:03:57 +08:00
static bool HasPrefix(IList<Instruction> instrs, int index, Code prefix) {
2012-04-06 22:08:35 +08:00
index--;
for (; index >= 0; index--) {
var instr = instrs[index];
if (instr.OpCode.OpCodeType != OpCodeType.Prefix)
break;
if (instr.OpCode.Code == prefix)
return true;
}
return false;
}
2012-04-06 00:06:56 +08:00
}
}