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

286 lines
8.0 KiB
C#
Raw Permalink Normal View History

2012-02-07 09:05:42 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2012-02-07 09:05:42 +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.IO;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2012-02-07 09:05:42 +08:00
using de4dot.blocks;
2012-02-07 11:45:59 +08:00
namespace de4dot.code.deobfuscators.CodeVeil {
2012-05-29 17:13:39 +08:00
class ProxyCallFixer : ProxyCallFixer1 {
MainType mainType;
2012-02-07 09:05:42 +08:00
Info info = new Info();
2012-11-08 16:48:05 +08:00
IBinaryReader reader;
2012-02-07 09:05:42 +08:00
class Info {
public TypeDef proxyType;
public MethodDef initMethod;
public FieldDef dataField;
public TypeDef ilgeneratorType;
public TypeDef fieldInfoType;
public TypeDef methodInfoType;
2012-02-07 09:05:42 +08:00
}
class Context {
public int offset;
public Context(int offset) {
this.offset = offset;
}
}
public bool FoundProxyType {
get { return info.proxyType != null; }
}
public bool CanRemoveTypes {
get {
return info.proxyType != null &&
info.ilgeneratorType != null &&
info.fieldInfoType != null &&
info.methodInfoType != null;
}
}
public TypeDef IlGeneratorType {
get { return info.ilgeneratorType; }
}
public TypeDef FieldInfoType {
get { return info.fieldInfoType; }
}
public TypeDef MethodInfoType {
get { return info.methodInfoType; }
}
2012-11-08 16:48:05 +08:00
public ProxyCallFixer(ModuleDefMD module, MainType mainType)
2012-02-07 09:05:42 +08:00
: base(module) {
this.mainType = mainType;
2012-02-07 09:05:42 +08:00
}
2012-11-08 16:48:05 +08:00
public ProxyCallFixer(ModuleDefMD module, MainType mainType, ProxyCallFixer oldOne)
2012-02-07 11:45:59 +08:00
: base(module, oldOne) {
this.mainType = mainType;
2013-01-19 20:03:57 +08:00
info.proxyType = Lookup(oldOne.info.proxyType, "Could not find proxyType");
info.initMethod = Lookup(oldOne.info.initMethod, "Could not find initMethod");
info.dataField = Lookup(oldOne.info.dataField, "Could not find dataField");
info.ilgeneratorType = Lookup(oldOne.info.ilgeneratorType, "Could not find ilgeneratorType");
info.fieldInfoType = Lookup(oldOne.info.fieldInfoType, "Could not find fieldInfoType");
info.methodInfoType = Lookup(oldOne.info.methodInfoType, "Could not find methodInfoType");
2012-02-07 11:45:59 +08:00
}
2013-01-19 20:03:57 +08:00
protected override object CheckCctor(ref TypeDef type, MethodDef cctor) {
2012-02-07 09:05:42 +08:00
var instrs = cctor.Body.Instructions;
for (int i = 0; i < instrs.Count - 1; i++) {
var ldci4 = instrs[i];
2012-11-08 16:48:05 +08:00
if (!ldci4.IsLdcI4())
2012-02-07 09:05:42 +08:00
continue;
var call = instrs[i + 1];
if (call.OpCode.Code != Code.Call)
continue;
if (call.Operand != info.initMethod)
continue;
2012-11-08 16:48:05 +08:00
int offset = ldci4.GetLdcI4Value();
reader.Position = offset;
uint rid = reader.ReadCompressedUInt32();
if (rid != type.Rid)
2012-02-07 09:05:42 +08:00
throw new ApplicationException("Invalid RID");
return string.Empty; // It's non-null
}
return null;
}
2013-01-19 20:03:57 +08:00
protected override void GetCallInfo(object context, FieldDef field, out IMethod calledMethod, out OpCode callOpcode) {
2012-02-07 09:05:42 +08:00
byte flags = reader.ReadByte();
2012-11-08 16:48:05 +08:00
int methodToken = 0x06000000 + ((flags & 0x3F) << 24) + (int)reader.ReadCompressedUInt32();
int genericTypeToken = (flags & 0x40) == 0 ? -1 : 0x1B000000 + (int)reader.ReadCompressedUInt32();
2012-02-07 09:05:42 +08:00
callOpcode = (flags & 0x80) != 0 ? OpCodes.Callvirt : OpCodes.Call;
2012-11-08 16:48:05 +08:00
calledMethod = module.ResolveToken(methodToken) as IMethod;
2012-02-07 09:05:42 +08:00
if (calledMethod == null)
throw new ApplicationException("Could not find method");
if (genericTypeToken != -1 && calledMethod.DeclaringType.MDToken.ToInt32() != genericTypeToken)
2012-02-07 09:05:42 +08:00
throw new ApplicationException("Invalid declaring type token");
}
2013-01-19 20:03:57 +08:00
public void FindDelegateCreator() {
if (!mainType.Detected)
2012-02-07 09:05:42 +08:00
return;
var infoTmp = new Info();
2013-01-19 20:03:57 +08:00
if (!InitializeInfo(infoTmp, mainType.Type))
2012-02-07 09:05:42 +08:00
return;
info = infoTmp;
2013-01-19 20:03:57 +08:00
SetDelegateCreatorMethod(info.initMethod);
2012-02-07 09:05:42 +08:00
}
2013-01-19 20:03:57 +08:00
bool InitializeInfo(Info infoTmp, TypeDef type) {
2012-02-07 09:05:42 +08:00
foreach (var dtype in type.NestedTypes) {
2012-11-08 16:48:05 +08:00
var cctor = dtype.FindMethod(".cctor");
2012-02-07 09:05:42 +08:00
if (cctor == null)
continue;
2013-01-19 20:03:57 +08:00
if (!InitProxyType(infoTmp, cctor))
2012-02-07 09:05:42 +08:00
continue;
return true;
}
return false;
}
2013-01-19 20:03:57 +08:00
bool InitProxyType(Info infoTmp, MethodDef method) {
foreach (var calledMethod in DotNetUtils.GetCalledMethods(module, method)) {
2012-03-18 03:36:41 +08:00
if (!calledMethod.IsStatic)
2012-02-07 09:05:42 +08:00
continue;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(calledMethod, "System.Void", "(System.Int32)"))
2012-02-07 09:05:42 +08:00
continue;
2013-01-19 20:03:57 +08:00
if (!CheckProxyType(infoTmp, calledMethod.DeclaringType))
2012-02-07 09:05:42 +08:00
continue;
2012-03-18 03:36:41 +08:00
infoTmp.proxyType = calledMethod.DeclaringType;
infoTmp.initMethod = calledMethod;
2012-02-07 09:05:42 +08:00
return true;
}
return false;
}
static string[] requiredFields = new string[] {
"System.Byte[]",
"System.Int32",
"System.ModuleHandle",
"System.Reflection.Emit.OpCode",
"System.Reflection.Emit.OpCode[]",
};
2013-01-19 20:03:57 +08:00
bool CheckProxyType(Info infoTmp, TypeDef type) {
2012-02-07 09:05:42 +08:00
if (type.NestedTypes.Count != 1)
return false;
2013-01-19 20:03:57 +08:00
if (!new FieldTypes(type).All(requiredFields))
2012-02-07 09:05:42 +08:00
return false;
2013-01-19 20:03:57 +08:00
var fields = GetRvaFields(type);
2012-02-07 09:05:42 +08:00
if (fields.Count != 1)
return false;
var field = fields[0];
2013-01-19 20:03:57 +08:00
var fieldType = DotNetUtils.GetType(module, field.FieldSig.GetFieldType());
2012-02-07 09:05:42 +08:00
if (type.NestedTypes.IndexOf(fieldType) < 0)
return false;
if (field.InitialValue == null || field.InitialValue.Length == 0)
return false;
infoTmp.dataField = field;
return true;
}
2013-01-19 20:03:57 +08:00
static List<FieldDef> GetRvaFields(TypeDef type) {
var fields = new List<FieldDef>();
2012-02-07 09:05:42 +08:00
foreach (var field in type.Fields) {
if (field.RVA != 0)
fields.Add(field);
}
return fields;
}
2013-01-19 20:03:57 +08:00
protected override IEnumerable<TypeDef> GetDelegateTypes() {
if (!mainType.Detected)
return new List<TypeDef>();
return mainType.Type.NestedTypes;
2012-02-07 09:05:42 +08:00
}
2013-01-19 20:03:57 +08:00
public void Initialize() {
2012-02-07 09:05:42 +08:00
if (info.dataField == null)
return;
2013-01-19 20:03:57 +08:00
FindOtherTypes();
2013-01-19 20:03:57 +08:00
var decompressed = DeobUtils.Inflate(info.dataField.InitialValue, true);
2012-11-08 16:48:05 +08:00
reader = MemoryImageStream.Create(decompressed);
info.dataField.FieldSig.Type = module.CorLibTypes.Byte;
2012-02-09 17:14:59 +08:00
info.dataField.InitialValue = new byte[1];
info.dataField.RVA = 0;
2012-02-07 09:05:42 +08:00
}
2013-01-19 20:03:57 +08:00
void FindOtherTypes() {
if (info.proxyType == null)
return;
foreach (var method in info.proxyType.Methods) {
2012-11-08 16:48:05 +08:00
var sig = method.MethodSig;
if (sig == null || sig.Params.Count != 4)
continue;
2012-11-08 16:48:05 +08:00
if (sig.Params[2].GetFullName() != "System.Type[]")
continue;
2012-11-08 16:48:05 +08:00
var methodType = sig.Params[0].TryGetTypeDef();
var fieldType = sig.Params[1].TryGetTypeDef();
var ilgType = sig.Params[3].TryGetTypeDef();
2013-01-19 20:03:57 +08:00
if (!CheckMethodType(methodType))
continue;
2013-01-19 20:03:57 +08:00
if (!CheckFieldType(fieldType))
continue;
2013-01-19 20:03:57 +08:00
if (!CheckIlGeneratorType(ilgType))
continue;
info.ilgeneratorType = ilgType;
info.methodInfoType = methodType;
info.fieldInfoType = fieldType;
}
}
2013-01-19 20:03:57 +08:00
bool CheckMethodType(TypeDef type) {
2012-11-08 16:48:05 +08:00
if (type == null || type.BaseType == null || type.BaseType.FullName != "System.Object")
return false;
if (type.Fields.Count != 1)
return false;
2013-01-19 20:03:57 +08:00
if (DotNetUtils.GetField(type, "System.Reflection.MethodInfo") == null)
return false;
return true;
}
2013-01-19 20:03:57 +08:00
bool CheckFieldType(TypeDef type) {
2012-11-08 16:48:05 +08:00
if (type == null || type.BaseType == null || type.BaseType.FullName != "System.Object")
return false;
2013-01-19 20:03:57 +08:00
if (DotNetUtils.GetField(type, "System.Reflection.FieldInfo") == null)
return false;
return true;
}
2013-01-19 20:03:57 +08:00
bool CheckIlGeneratorType(TypeDef type) {
2012-11-08 16:48:05 +08:00
if (type == null || type.BaseType == null || type.BaseType.FullName != "System.Object")
return false;
if (type.Fields.Count != 1)
return false;
2013-01-19 20:03:57 +08:00
if (DotNetUtils.GetField(type, "System.Reflection.Emit.ILGenerator") == null)
return false;
return true;
}
2012-02-07 09:05:42 +08:00
}
}