de4dot-cex/de4dot.code/deobfuscators/CryptoObfuscator/ProxyCallFixer.cs

165 lines
5.1 KiB
C#
Raw Normal View History

2011-10-23 19:43:32 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2011-10-23 19:43:32 +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 dnlib.DotNet;
using dnlib.DotNet.Emit;
2011-10-23 19:43:32 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.CryptoObfuscator {
2012-05-29 17:13:39 +08:00
class ProxyCallFixer : ProxyCallFixer2 {
Dictionary<MethodDef, ProxyCreatorType> methodToType = new Dictionary<MethodDef, ProxyCreatorType>();
2011-10-23 19:43:32 +08:00
2012-11-09 05:24:13 +08:00
public ProxyCallFixer(ModuleDefMD module)
2011-10-23 19:43:32 +08:00
: base(module) {
}
enum ProxyCreatorType {
None,
CallOrCallvirt,
CallCtor,
Newobj,
}
class Context {
2012-11-09 05:24:13 +08:00
public uint typeToken;
public uint methodToken;
public uint declaringTypeToken;
2011-10-23 19:43:32 +08:00
public ProxyCreatorType proxyCreatorType;
2012-11-09 05:24:13 +08:00
public Context(uint typeToken, uint methodToken, uint declaringTypeToken, ProxyCreatorType proxyCreatorType) {
2011-10-23 19:43:32 +08:00
this.typeToken = typeToken;
this.methodToken = methodToken;
this.declaringTypeToken = declaringTypeToken;
this.proxyCreatorType = proxyCreatorType;
}
}
2013-01-19 20:03:57 +08:00
protected override object CheckCctor(TypeDef type, MethodDef cctor) {
2011-10-23 19:43:32 +08:00
var instructions = cctor.Body.Instructions;
for (int i = 0; i < instructions.Count; i++) {
2013-01-19 20:03:57 +08:00
var instrs = DotNetUtils.GetInstructions(instructions, i, OpCodes.Ldc_I4, OpCodes.Ldc_I4, OpCodes.Ldc_I4, OpCodes.Call);
2011-10-23 19:43:32 +08:00
if (instrs == null)
continue;
2012-11-09 05:24:13 +08:00
uint typeToken = (uint)(int)instrs[0].Operand;
uint methodToken = (uint)(int)instrs[1].Operand;
uint declaringTypeToken = (uint)(int)instrs[2].Operand;
var createMethod = instrs[3].Operand as MethodDef;
2011-10-23 19:43:32 +08:00
ProxyCreatorType proxyCreatorType;
if (!methodToType.TryGetValue(createMethod, out proxyCreatorType))
continue;
return new Context(typeToken, methodToken, declaringTypeToken, proxyCreatorType);
}
return null;
}
2013-01-19 20:03:57 +08:00
protected override void GetCallInfo(object context, FieldDef field, out IMethod calledMethod, out OpCode callOpcode) {
2011-10-23 19:43:32 +08:00
var ctx = (Context)context;
switch (ctx.proxyCreatorType) {
case ProxyCreatorType.CallOrCallvirt:
2012-11-17 06:50:52 +08:00
callOpcode = field.IsFamilyOrAssembly ? OpCodes.Callvirt : OpCodes.Call;
2011-10-23 19:43:32 +08:00
break;
case ProxyCreatorType.CallCtor:
callOpcode = OpCodes.Call;
break;
case ProxyCreatorType.Newobj:
callOpcode = OpCodes.Newobj;
break;
default:
throw new ApplicationException(string.Format("Invalid proxy creator type: {0}", ctx.proxyCreatorType));
}
2012-11-09 05:24:13 +08:00
calledMethod = module.ResolveToken(ctx.methodToken) as IMethod;
2011-10-23 19:43:32 +08:00
}
2013-01-19 20:03:57 +08:00
public void FindDelegateCreator() {
2011-10-23 19:43:32 +08:00
foreach (var type in module.Types) {
2013-01-19 20:03:57 +08:00
var createMethod = GetProxyCreateMethod(type);
2011-10-23 19:43:32 +08:00
if (createMethod == null)
continue;
2013-01-19 20:03:57 +08:00
var proxyCreatorType = GetProxyCreatorType(type, createMethod);
2011-10-23 19:43:32 +08:00
if (proxyCreatorType == ProxyCreatorType.None)
continue;
methodToType[createMethod] = proxyCreatorType;
2013-01-19 20:03:57 +08:00
SetDelegateCreatorMethod(createMethod);
2011-10-23 19:43:32 +08:00
}
}
2013-01-19 20:03:57 +08:00
MethodDef GetProxyCreateMethod(TypeDef type) {
if (DotNetUtils.FindFieldType(type, "System.ModuleHandle", true) == null)
2011-10-23 19:43:32 +08:00
return null;
2014-05-09 21:59:50 +08:00
if (type.Fields.Count < 1 || type.Fields.Count > 20)
2012-04-16 05:42:11 +08:00
return null;
2011-10-23 19:43:32 +08:00
MethodDef createMethod = null;
2011-10-23 19:43:32 +08:00
foreach (var m in type.Methods) {
if (m.Name == ".ctor" || m.Name == ".cctor")
continue;
2013-01-19 20:03:57 +08:00
if (createMethod == null && DotNetUtils.IsMethod(m, "System.Void", "(System.Int32,System.Int32,System.Int32)")) {
2011-10-23 19:43:32 +08:00
createMethod = m;
continue;
}
2011-12-15 23:16:21 +08:00
continue;
2011-10-23 19:43:32 +08:00
}
2011-10-23 23:23:33 +08:00
if (createMethod == null || !createMethod.HasBody)
2011-10-23 19:43:32 +08:00
return null;
2013-01-19 20:03:57 +08:00
if (!DeobUtils.HasInteger(createMethod, 0xFFFFFF))
2011-10-23 19:43:32 +08:00
return null;
return createMethod;
}
2013-01-19 20:03:57 +08:00
ProxyCreatorType GetProxyCreatorType(TypeDef type, MethodDef createMethod) {
2011-10-23 19:43:32 +08:00
int numCalls = 0, numCallvirts = 0, numNewobjs = 0;
foreach (var instr in createMethod.Body.Instructions) {
if (instr.OpCode.Code != Code.Ldsfld)
continue;
2012-11-09 05:24:13 +08:00
var field = instr.Operand as IField;
2011-10-23 19:43:32 +08:00
if (field == null)
continue;
switch (field.FullName) {
case "System.Reflection.Emit.OpCode System.Reflection.Emit.OpCodes::Call":
numCalls++;
break;
case "System.Reflection.Emit.OpCode System.Reflection.Emit.OpCodes::Callvirt":
numCallvirts++;
break;
case "System.Reflection.Emit.OpCode System.Reflection.Emit.OpCodes::Newobj":
numNewobjs++;
break;
}
}
if (numCalls == 1 && numCallvirts == 1 && numNewobjs == 0)
return ProxyCreatorType.CallOrCallvirt;
if (numCalls == 1 && numCallvirts == 0 && numNewobjs == 0)
return ProxyCreatorType.CallCtor;
if (numCalls == 0 && numCallvirts == 0 && numNewobjs == 1)
return ProxyCreatorType.Newobj;
return ProxyCreatorType.None;
}
}
}