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;
|
|
|
|
|
using Mono.Cecil;
|
|
|
|
|
using Mono.Cecil.Cil;
|
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators {
|
|
|
|
|
class ResourceMethodsRestorerBase {
|
|
|
|
|
protected MemberReferenceBuilder builder;
|
|
|
|
|
protected ModuleDefinition module;
|
|
|
|
|
|
2012-04-29 06:51:09 +08:00
|
|
|
|
class NewMethodInfo {
|
|
|
|
|
public OpCode opCode;
|
|
|
|
|
public MethodReference method;
|
|
|
|
|
|
|
|
|
|
public NewMethodInfo(OpCode opCode, MethodReference method) {
|
|
|
|
|
this.opCode = opCode;
|
|
|
|
|
this.method = method;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MethodDefinitionAndDeclaringTypeDict<NewMethodInfo> oldToNewMethod = new MethodDefinitionAndDeclaringTypeDict<NewMethodInfo>();
|
2012-04-29 06:01:50 +08:00
|
|
|
|
|
|
|
|
|
public ResourceMethodsRestorerBase(ModuleDefinition module) {
|
|
|
|
|
this.module = module;
|
|
|
|
|
this.builder = new MemberReferenceBuilder(module);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void createGetManifestResourceStream1(MethodDefinition 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);
|
|
|
|
|
var newMethod = builder.instanceMethod("GetManifestResourceStream", assemblyType, streamType, builder.String);
|
2012-04-29 06:51:09 +08:00
|
|
|
|
add(oldMethod, newMethod, OpCodes.Callvirt);
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void createGetManifestResourceStream2(MethodDefinition 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);
|
|
|
|
|
var newMethod = builder.instanceMethod("GetManifestResourceStream", assemblyType, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void createGetManifestResourceNames(MethodDefinition 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);
|
|
|
|
|
var newMethod = builder.instanceMethod("GetManifestResourceNames", assemblyType, stringArrayType);
|
2012-04-29 06:51:09 +08:00
|
|
|
|
add(oldMethod, newMethod, OpCodes.Callvirt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void createBitmapCtor(MethodDefinition oldMethod) {
|
|
|
|
|
if (oldMethod == null)
|
|
|
|
|
return;
|
|
|
|
|
var bitmapType = builder.type("System.Drawing", "Bitmap", "System.Drawing");
|
|
|
|
|
var typeType = builder.type("System", "Type", builder.CorLib);
|
|
|
|
|
var newMethod = builder.instanceMethod(".ctor", bitmapType, builder.Void, typeType, builder.String);
|
|
|
|
|
add(oldMethod, newMethod, OpCodes.Newobj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void createIconCtor(MethodDefinition oldMethod) {
|
|
|
|
|
if (oldMethod == null)
|
|
|
|
|
return;
|
|
|
|
|
var iconType = builder.type("System.Drawing", "Icon", "System.Drawing");
|
|
|
|
|
var typeType = builder.type("System", "Type", builder.CorLib);
|
|
|
|
|
var newMethod = builder.instanceMethod(".ctor", iconType, builder.Void, typeType, builder.String);
|
|
|
|
|
add(oldMethod, newMethod, OpCodes.Newobj);
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-29 06:51:09 +08:00
|
|
|
|
void add(MethodDefinition oldMethod, MethodReference 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) {
|
|
|
|
|
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;
|
|
|
|
|
var calledMethod = call.Operand as MethodDefinition;
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|