de4dot-cex/de4dot.code/deobfuscators/ILProtector/MainType.cs

137 lines
3.6 KiB
C#
Raw Normal View History

2012-06-04 10:20:43 +08:00
/*
2014-03-12 05:15:43 +08:00
Copyright (C) 2011-2014 de4dot@gmail.com
2012-06-04 10:20:43 +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.Collections.Generic;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2012-06-04 10:20:43 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.ILProtector {
class MainType {
2012-11-18 15:13:51 +08:00
ModuleDefMD module;
2013-11-17 03:04:23 +08:00
List<RuntimeFileInfo> runtimeFileInfos;
TypeDef invokerDelegate;
FieldDef invokerInstanceField;
2012-06-04 10:20:43 +08:00
2013-11-17 03:04:23 +08:00
public List<RuntimeFileInfo> RuntimeFileInfos {
get { return runtimeFileInfos; }
2012-06-04 10:20:43 +08:00
}
public TypeDef InvokerDelegate {
2012-06-04 10:20:43 +08:00
get { return invokerDelegate; }
}
public FieldDef InvokerInstanceField {
2012-06-04 10:20:43 +08:00
get { return invokerInstanceField; }
}
public bool Detected {
2013-11-17 03:04:23 +08:00
get { return runtimeFileInfos != null; }
2012-06-04 10:20:43 +08:00
}
2012-11-18 15:13:51 +08:00
public MainType(ModuleDefMD module) {
2012-06-04 10:20:43 +08:00
this.module = module;
}
2013-01-19 20:03:57 +08:00
public void Find() {
CheckMethod(DotNetUtils.GetModuleTypeCctor(module));
2012-06-04 10:20:43 +08:00
}
2013-10-26 23:25:58 +08:00
static string[] ilpLocalsV1x = new string[] {
2012-06-04 10:20:43 +08:00
"System.Boolean",
"System.IntPtr",
"System.Object[]",
};
2013-10-26 23:25:58 +08:00
static string[] ilpLocalsV2x = new string[] {
"System.IntPtr",
};
2013-01-19 20:03:57 +08:00
bool CheckMethod(MethodDef cctor) {
2012-06-04 10:20:43 +08:00
if (cctor == null || cctor.Body == null)
return false;
2013-10-26 23:25:58 +08:00
var localTypes = new LocalTypes(cctor);
if (!localTypes.Exactly(ilpLocalsV1x) &&
!localTypes.Exactly(ilpLocalsV2x))
2012-06-04 10:20:43 +08:00
return false;
var type = cctor.DeclaringType;
2013-01-19 20:03:57 +08:00
var methods = GetPinvokeMethods(type, "Protect");
2012-06-04 10:20:43 +08:00
if (methods.Count == 0)
2013-01-19 20:03:57 +08:00
methods = GetPinvokeMethods(type, "P0");
2012-06-04 10:20:43 +08:00
if (methods.Count != 2)
return false;
2013-10-26 23:25:58 +08:00
if (type.Fields.Count < 1 || type.Fields.Count > 2)
2012-06-04 10:20:43 +08:00
return false;
2013-10-26 23:25:58 +08:00
if (!GetDelegate(type, out invokerInstanceField, out invokerDelegate))
2012-06-04 10:20:43 +08:00
return false;
2013-11-17 03:04:23 +08:00
runtimeFileInfos = new List<RuntimeFileInfo>(methods.Count);
foreach (var method in methods)
runtimeFileInfos.Add(new RuntimeFileInfo(method));
2012-06-04 10:20:43 +08:00
return true;
}
2013-10-26 23:25:58 +08:00
bool GetDelegate(TypeDef type, out FieldDef field, out TypeDef del) {
foreach (var fld in type.Fields) {
var theDelegate = fld.FieldType.TryGetTypeDef();
if (theDelegate != null && DotNetUtils.DerivesFromDelegate(theDelegate)) {
field = fld;
del = theDelegate;
return true;
}
}
field = null;
del = null;
return false;
}
2013-01-19 20:03:57 +08:00
static List<MethodDef> GetPinvokeMethods(TypeDef type, string name) {
var list = new List<MethodDef>();
2012-06-04 10:20:43 +08:00
foreach (var method in type.Methods) {
2012-11-18 15:13:51 +08:00
if (method.ImplMap != null && method.ImplMap.Name == name)
2012-06-04 10:20:43 +08:00
list.Add(method);
}
return list;
}
2013-11-17 03:04:23 +08:00
public string GetRuntimeVersionString() {
if (runtimeFileInfos == null)
return null;
foreach (var info in runtimeFileInfos) {
var version = info.GetVersion();
if (version != null)
return version.ToString();
}
return null;
}
2013-01-19 20:03:57 +08:00
public void CleanUp() {
var cctor = DotNetUtils.GetModuleTypeCctor(module);
2012-06-04 10:20:43 +08:00
if (cctor != null) {
cctor.Body.InitLocals = false;
cctor.Body.Variables.Clear();
2012-06-04 10:20:43 +08:00
cctor.Body.Instructions.Clear();
2012-12-16 07:03:56 +08:00
cctor.Body.Instructions.Add(OpCodes.Ret.ToInstruction());
2012-06-04 10:20:43 +08:00
cctor.Body.ExceptionHandlers.Clear();
}
}
}
}