de4dot-cex/de4dot.code/deobfuscators/MethodCallRestorerBase.cs

125 lines
4.4 KiB
C#
Raw Normal View History

2012-04-29 06:01: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/>.
*/
using System.Collections.Generic;
2012-11-01 23:42:02 +08:00
using dot10.DotNet;
using dot10.DotNet.Emit;
2012-04-29 06:01:50 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators {
2012-04-29 06:56:17 +08:00
class MethodCallRestorerBase {
2012-11-22 16:14:51 +08:00
protected MemberRefBuilder builder;
2012-11-01 23:42:02 +08:00
protected ModuleDefMD module;
2012-11-22 16:14:51 +08:00
MethodDefAndDeclaringTypeDict<NewMethodInfo> oldToNewMethod = new MethodDefAndDeclaringTypeDict<NewMethodInfo>();
2012-04-29 06:01:50 +08:00
2012-04-29 06:51:09 +08:00
class NewMethodInfo {
public OpCode opCode;
2012-11-01 23:42:02 +08:00
public IMethod method;
2012-04-29 06:51:09 +08:00
2012-11-01 23:42:02 +08:00
public NewMethodInfo(OpCode opCode, IMethod method) {
2012-04-29 06:51:09 +08:00
this.opCode = opCode;
this.method = method;
}
}
2012-11-01 23:42:02 +08:00
public MethodCallRestorerBase(ModuleDefMD module) {
2012-04-29 06:01:50 +08:00
this.module = module;
2012-11-22 16:14:51 +08:00
this.builder = new MemberRefBuilder(module);
2012-04-29 06:01:50 +08:00
}
2012-11-01 23:42:02 +08:00
public void createGetManifestResourceStream1(MethodDef oldMethod) {
2012-04-29 06:51:09 +08:00
if (oldMethod == null)
return;
2012-04-29 06:01:50 +08:00
var assemblyType = builder.type("System.Reflection", "Assembly", builder.CorLib);
var streamType = builder.type("System.IO", "Stream", builder.CorLib);
2012-11-02 04:09:09 +08:00
var newMethod = builder.instanceMethod("GetManifestResourceStream", assemblyType.TypeDefOrRef, streamType, builder.String);
2012-04-29 06:51:09 +08:00
add(oldMethod, newMethod, OpCodes.Callvirt);
2012-04-29 06:01:50 +08:00
}
2012-11-01 23:42:02 +08:00
public void createGetManifestResourceStream2(MethodDef oldMethod) {
2012-04-29 06:51:09 +08:00
if (oldMethod == null)
return;
2012-04-29 06:01:50 +08:00
var assemblyType = builder.type("System.Reflection", "Assembly", builder.CorLib);
var typeType = builder.type("System", "Type", builder.CorLib);
var streamType = builder.type("System.IO", "Stream", builder.CorLib);
2012-11-02 04:09:09 +08:00
var newMethod = builder.instanceMethod("GetManifestResourceStream", assemblyType.TypeDefOrRef, streamType, typeType, builder.String);
2012-04-29 06:51:09 +08:00
add(oldMethod, newMethod, OpCodes.Callvirt);
2012-04-29 06:01:50 +08:00
}
2012-11-01 23:42:02 +08:00
public void createGetManifestResourceNames(MethodDef oldMethod) {
2012-04-29 06:51:09 +08:00
if (oldMethod == null)
return;
2012-04-29 06:01:50 +08:00
var assemblyType = builder.type("System.Reflection", "Assembly", builder.CorLib);
var stringArrayType = builder.array(builder.String);
2012-11-02 04:09:09 +08:00
var newMethod = builder.instanceMethod("GetManifestResourceNames", assemblyType.TypeDefOrRef, stringArrayType);
2012-04-29 06:51:09 +08:00
add(oldMethod, newMethod, OpCodes.Callvirt);
}
2012-11-01 23:42:02 +08:00
public void createBitmapCtor(MethodDef oldMethod) {
2012-04-29 06:51:09 +08:00
if (oldMethod == null)
return;
var bitmapType = builder.type("System.Drawing", "Bitmap", "System.Drawing");
var typeType = builder.type("System", "Type", builder.CorLib);
2012-11-02 04:09:09 +08:00
var newMethod = builder.instanceMethod(".ctor", bitmapType.TypeDefOrRef, builder.Void, typeType, builder.String);
2012-04-29 06:51:09 +08:00
add(oldMethod, newMethod, OpCodes.Newobj);
}
2012-11-01 23:42:02 +08:00
public void createIconCtor(MethodDef oldMethod) {
2012-04-29 06:51:09 +08:00
if (oldMethod == null)
return;
var iconType = builder.type("System.Drawing", "Icon", "System.Drawing");
var typeType = builder.type("System", "Type", builder.CorLib);
2012-11-02 04:09:09 +08:00
var newMethod = builder.instanceMethod(".ctor", iconType.TypeDefOrRef, builder.Void, typeType, builder.String);
2012-04-29 06:51:09 +08:00
add(oldMethod, newMethod, OpCodes.Newobj);
2012-04-29 06:01:50 +08:00
}
2012-11-01 23:42:02 +08:00
protected void add(MethodDef oldMethod, IMethod newMethod) {
add(oldMethod, newMethod, OpCodes.Callvirt);
}
2012-11-01 23:42:02 +08:00
protected void add(MethodDef oldMethod, IMethod newMethod, OpCode opCode) {
2012-04-29 06:01:50 +08:00
if (oldMethod == null)
return;
2012-04-29 06:51:09 +08:00
oldToNewMethod.add(oldMethod, new NewMethodInfo(opCode, newMethod));
2012-04-29 06:01:50 +08:00
}
public void deobfuscate(Blocks blocks) {
if (oldToNewMethod.Count == 0)
return;
2012-04-29 06:01:50 +08:00
foreach (var block in blocks.MethodBlocks.getAllBlocks()) {
var instrs = block.Instructions;
for (int i = 0; i < instrs.Count; i++) {
var call = instrs[i];
if (call.OpCode.Code != Code.Call)
continue;
2012-11-01 23:42:02 +08:00
var calledMethod = call.Operand as MethodDef;
2012-04-29 06:01:50 +08:00
if (calledMethod == null)
continue;
2012-04-29 06:51:09 +08:00
var newMethodInfo = oldToNewMethod.find(calledMethod);
if (newMethodInfo == null)
2012-04-29 06:01:50 +08:00
continue;
2012-04-29 06:51:09 +08:00
instrs[i] = new Instr(Instruction.Create(newMethodInfo.opCode, newMethodInfo.method));
2012-04-29 06:01:50 +08:00
}
}
}
}
}