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

126 lines
3.6 KiB
C#
Raw Normal View History

2012-05-30 01:14:41 +08:00
/*
2014-03-12 05:15:43 +08:00
Copyright (C) 2011-2014 de4dot@gmail.com
2012-05-30 01:14:41 +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;
2012-05-30 01:14:41 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.CodeFort {
class ProxyCallFixer : ProxyCallFixer3 {
2012-11-22 16:14:51 +08:00
MethodDefAndDeclaringTypeDict<bool> proxyTargetMethods = new MethodDefAndDeclaringTypeDict<bool>();
TypeDef proxyMethodsType;
public TypeDef ProxyMethodsType {
get { return proxyMethodsType; }
}
2012-05-30 01:14:41 +08:00
2012-11-08 14:43:57 +08:00
public ProxyCallFixer(ModuleDefMD module)
2012-05-30 01:14:41 +08:00
: base(module) {
}
2013-01-19 20:03:57 +08:00
public bool IsProxyTargetMethod(IMethod method) {
return proxyTargetMethods.Find(method);
}
2013-01-19 20:03:57 +08:00
public void FindDelegateCreator() {
2012-05-30 01:14:41 +08:00
foreach (var type in module.Types) {
2013-01-19 20:03:57 +08:00
var creatorMethod = CheckType(type);
2012-05-30 01:14:41 +08:00
if (creatorMethod == null)
continue;
2013-01-19 20:03:57 +08:00
SetDelegateCreatorMethod(creatorMethod);
2012-05-30 01:14:41 +08:00
return;
}
}
2013-01-19 20:03:57 +08:00
static MethodDef CheckType(TypeDef type) {
2012-05-30 01:14:41 +08:00
if (type.Fields.Count != 1)
return null;
2012-11-08 14:43:57 +08:00
if (type.Fields[0].FieldSig.GetFieldType().GetFullName() != "System.Reflection.Module")
2012-05-30 01:14:41 +08:00
return null;
2013-01-19 20:03:57 +08:00
return CheckMethods(type);
2012-05-30 01:14:41 +08:00
}
2013-01-19 20:03:57 +08:00
static MethodDef CheckMethods(TypeDef type) {
2012-05-30 01:14:41 +08:00
if (type.Methods.Count != 3)
return null;
MethodDef creatorMethod = null;
2012-05-30 01:14:41 +08:00
foreach (var method in type.Methods) {
if (method.Name == ".cctor")
continue;
2013-01-19 20:03:57 +08:00
if (DotNetUtils.IsMethod(method, "System.Void", "(System.Int32)")) {
2012-05-30 01:14:41 +08:00
creatorMethod = method;
continue;
}
2013-01-19 20:03:57 +08:00
if (DotNetUtils.IsMethod(method, "System.MulticastDelegate", "(System.Type,System.Reflection.MethodInfo,System.Int32)"))
2012-05-30 01:14:41 +08:00
continue;
return null;
}
return creatorMethod;
}
2013-01-19 20:03:57 +08:00
protected override object CheckCctor(ref TypeDef type, MethodDef cctor) {
2012-05-30 01:14:41 +08:00
var instrs = cctor.Body.Instructions;
if (instrs.Count != 3)
return null;
var ldci4 = instrs[0];
2012-11-08 14:43:57 +08:00
if (!ldci4.IsLdcI4())
2012-05-30 01:14:41 +08:00
return null;
var call = instrs[1];
if (call.OpCode.Code != Code.Call)
return null;
2013-01-19 20:03:57 +08:00
if (!IsDelegateCreatorMethod(call.Operand as MethodDef))
2012-05-30 01:14:41 +08:00
return null;
2012-11-08 14:43:57 +08:00
int rid = ldci4.GetLdcI4Value();
if (cctor.DeclaringType.Rid != rid)
2012-05-30 01:14:41 +08:00
throw new ApplicationException("Invalid rid");
return rid;
}
2013-01-19 20:03:57 +08:00
protected override void GetCallInfo(object context, FieldDef field, out IMethod calledMethod, out OpCode callOpcode) {
2012-11-08 14:43:57 +08:00
uint rid = 0;
foreach (var c in field.Name.String)
2013-01-19 20:03:57 +08:00
rid = (rid << 4) + (uint)HexToInt((char)((byte)c + 0x2F));
2012-05-30 01:14:41 +08:00
rid &= 0x00FFFFFF;
2012-11-08 14:43:57 +08:00
calledMethod = module.ResolveMemberRef(rid);
2013-01-19 20:03:57 +08:00
var calledMethodDef = DotNetUtils.GetMethod2(module, calledMethod);
if (calledMethodDef != null) {
proxyMethodsType = calledMethodDef.DeclaringType;
2013-01-19 20:03:57 +08:00
proxyTargetMethods.Add(calledMethodDef, true);
2012-05-30 01:14:41 +08:00
calledMethod = calledMethodDef;
}
2012-05-30 01:14:41 +08:00
callOpcode = OpCodes.Call;
}
2013-01-19 20:03:57 +08:00
static int HexToInt(char c) {
2012-05-30 01:14:41 +08:00
if ('0' <= c && c <= '9')
return c - '0';
if ('a' <= c && c <= 'f')
return c - 'a' + 10;
if ('A' <= c && c <= 'F')
return c - 'A' + 10;
throw new ApplicationException("Invalid hex digit");
}
}
}