2012-07-07 06:58:50 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2012-07-07 06:58:50 +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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2012-07-07 06:58:50 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 {
|
|
|
|
|
class ProxyCallFixer : ProxyCallFixer3 {
|
|
|
|
|
ISimpleDeobfuscator simpleDeobfuscator;
|
|
|
|
|
|
2012-11-18 01:57:36 +08:00
|
|
|
|
public ProxyCallFixer(ModuleDefMD module, ISimpleDeobfuscator simpleDeobfuscator)
|
2012-07-07 06:58:50 +08:00
|
|
|
|
: base(module) {
|
|
|
|
|
this.simpleDeobfuscator = simpleDeobfuscator;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void FindDelegateCreator() {
|
2012-07-07 06:58:50 +08:00
|
|
|
|
foreach (var type in module.Types) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var creatorMethod = CheckType(type);
|
2012-07-07 06:58:50 +08:00
|
|
|
|
if (creatorMethod == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
SetDelegateCreatorMethod(creatorMethod);
|
2012-07-07 06:58:50 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static readonly string[] requiredFields = new string[] {
|
|
|
|
|
"System.Reflection.Module",
|
|
|
|
|
};
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static MethodDef CheckType(TypeDef type) {
|
|
|
|
|
if (!new FieldTypes(type).Exactly(requiredFields))
|
2012-07-07 06:58:50 +08:00
|
|
|
|
return null;
|
2012-11-18 01:57:36 +08:00
|
|
|
|
if (type.FindStaticConstructor() == null)
|
2012-07-07 06:58:50 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
return CheckMethods(type);
|
2012-07-07 06:58:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static MethodDef CheckMethods(TypeDef type) {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
MethodDef creatorMethod = null;
|
2012-07-07 06:58:50 +08:00
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (method.Body == null)
|
|
|
|
|
return null;
|
|
|
|
|
if (method.Name == ".cctor" || method.Name == ".ctor")
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(method, "System.Void", "(System.Int32)"))
|
2012-07-07 06:58:50 +08:00
|
|
|
|
return null;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DeobUtils.HasInteger(method, 0x02000000))
|
2012-07-07 06:58:50 +08:00
|
|
|
|
return null;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DeobUtils.HasInteger(method, 0x06000000))
|
2012-07-07 06:58:50 +08:00
|
|
|
|
return null;
|
|
|
|
|
creatorMethod = method;
|
|
|
|
|
}
|
|
|
|
|
return creatorMethod;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override object CheckCctor(ref TypeDef type, MethodDef cctor) {
|
|
|
|
|
simpleDeobfuscator.Deobfuscate(cctor);
|
|
|
|
|
var realType = GetDelegateType(cctor);
|
2012-07-07 06:58:50 +08:00
|
|
|
|
if (realType == null)
|
|
|
|
|
return null;
|
|
|
|
|
type = realType;
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
TypeDef GetDelegateType(MethodDef method) {
|
2012-07-07 06:58:50 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 1; i++) {
|
|
|
|
|
var ldci4 = instrs[i];
|
2012-11-18 01:57:36 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-07-07 06:58:50 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var call = instrs[i + 1];
|
|
|
|
|
if (call.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
var calledMethod = call.Operand as MethodDef;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (calledMethod == null || !IsDelegateCreatorMethod(calledMethod))
|
2012-07-07 06:58:50 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-18 01:57:36 +08:00
|
|
|
|
return module.ResolveToken(0x02000000 + ldci4.GetLdcI4Value()) as TypeDef;
|
2012-07-07 06:58:50 +08:00
|
|
|
|
}
|
|
|
|
|
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-11-18 01:57:36 +08:00
|
|
|
|
calledMethod = module.ResolveToken(0x06000000 + field.MDToken.ToInt32()) as IMethod;
|
2012-07-07 06:58:50 +08:00
|
|
|
|
callOpcode = OpCodes.Call;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|