2012-07-07 06:58:50 +08:00
|
|
|
|
/*
|
|
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
|
|
|
|
|
|
|
|
|
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-11-02 22:57:11 +08:00
|
|
|
|
using dot10.DotNet;
|
|
|
|
|
using dot10.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;
|
|
|
|
|
|
|
|
|
|
public ProxyCallFixer(ModuleDefinition module, ISimpleDeobfuscator simpleDeobfuscator)
|
|
|
|
|
: base(module) {
|
|
|
|
|
this.simpleDeobfuscator = simpleDeobfuscator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void findDelegateCreator() {
|
|
|
|
|
foreach (var type in module.Types) {
|
|
|
|
|
var creatorMethod = checkType(type);
|
|
|
|
|
if (creatorMethod == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
setDelegateCreatorMethod(creatorMethod);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static readonly string[] requiredFields = new string[] {
|
|
|
|
|
"System.Reflection.Module",
|
|
|
|
|
};
|
2012-11-02 22:57:11 +08:00
|
|
|
|
static MethodDef checkType(TypeDef type) {
|
2012-07-07 06:58:50 +08:00
|
|
|
|
if (!new FieldTypes(type).exactly(requiredFields))
|
|
|
|
|
return null;
|
|
|
|
|
if (DotNetUtils.getMethod(type, ".cctor") == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return checkMethods(type);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
static MethodDef checkMethods(TypeDef type) {
|
|
|
|
|
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;
|
|
|
|
|
if (!DotNetUtils.isMethod(method, "System.Void", "(System.Int32)"))
|
|
|
|
|
return null;
|
|
|
|
|
if (!DeobUtils.hasInteger(method, 0x02000000))
|
|
|
|
|
return null;
|
|
|
|
|
if (!DeobUtils.hasInteger(method, 0x06000000))
|
|
|
|
|
return null;
|
|
|
|
|
creatorMethod = method;
|
|
|
|
|
}
|
|
|
|
|
return creatorMethod;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
protected override object checkCctor(ref TypeDef type, MethodDef cctor) {
|
2012-07-07 06:58:50 +08:00
|
|
|
|
simpleDeobfuscator.deobfuscate(cctor);
|
|
|
|
|
var realType = getDelegateType(cctor);
|
|
|
|
|
if (realType == null)
|
|
|
|
|
return null;
|
|
|
|
|
type = realType;
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +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];
|
|
|
|
|
if (!DotNetUtils.isLdcI4(ldci4))
|
|
|
|
|
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;
|
2012-07-07 06:58:50 +08:00
|
|
|
|
if (calledMethod == null || !isDelegateCreatorMethod(calledMethod))
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
return module.LookupToken(0x02000000 + DotNetUtils.getLdcI4Value(ldci4)) as TypeDef;
|
2012-07-07 06:58:50 +08:00
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
protected override void getCallInfo(object context, FieldDef field, out MethodReference calledMethod, out OpCode callOpcode) {
|
|
|
|
|
calledMethod = module.LookupToken(0x06000000 + field.MDToken.ToInt32()) as MethodReference;
|
2012-07-07 06:58:50 +08:00
|
|
|
|
callOpcode = OpCodes.Call;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|