2011-11-09 02:32:10 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
2011-11-09 02:32:10 +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;
|
|
|
|
|
using System.Collections.Generic;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
using dot10.DotNet;
|
|
|
|
|
using dot10.DotNet.Emit;
|
2011-11-09 02:32:10 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
2011-12-22 01:55:36 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 {
|
2011-11-09 02:32:10 +08:00
|
|
|
|
class ResourceResolver {
|
2012-11-18 01:57:36 +08:00
|
|
|
|
ModuleDefMD module;
|
2011-11-09 02:32:10 +08:00
|
|
|
|
EncryptedResource encryptedResource;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
MethodDef initMethod;
|
2011-11-09 02:32:10 +08:00
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return encryptedResource.Method != null; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef Type {
|
2011-11-09 04:27:03 +08:00
|
|
|
|
get { return encryptedResource.Type; }
|
2011-11-09 02:32:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef InitMethod {
|
2011-11-09 02:32:10 +08:00
|
|
|
|
get { return initMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-09 04:27:03 +08:00
|
|
|
|
public bool FoundResource {
|
|
|
|
|
get { return encryptedResource.FoundResource; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-18 01:57:36 +08:00
|
|
|
|
public ResourceResolver(ModuleDefMD module) {
|
2011-11-09 02:32:10 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
this.encryptedResource = new EncryptedResource(module);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-18 01:57:36 +08:00
|
|
|
|
public ResourceResolver(ModuleDefMD module, ResourceResolver oldOne) {
|
2011-11-09 02:32:10 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
this.encryptedResource = new EncryptedResource(module, oldOne.encryptedResource);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void find(ISimpleDeobfuscator simpleDeobfuscator) {
|
|
|
|
|
var additionalTypes = new string[] {
|
|
|
|
|
"System.String",
|
|
|
|
|
};
|
|
|
|
|
foreach (var type in module.Types) {
|
|
|
|
|
if (type.BaseType == null || type.BaseType.FullName != "System.Object")
|
|
|
|
|
continue;
|
|
|
|
|
if (!checkFields(type.Fields))
|
|
|
|
|
continue;
|
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (!method.IsStatic || !method.HasBody)
|
|
|
|
|
continue;
|
2011-11-09 04:27:03 +08:00
|
|
|
|
if (!DotNetUtils.isMethod(method, "System.Reflection.Assembly", "(System.Object,System.ResolveEventArgs)") &&
|
|
|
|
|
!DotNetUtils.isMethod(method, "System.Reflection.Assembly", "(System.Object,System.Object)"))
|
2011-11-09 02:32:10 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (!encryptedResource.couldBeResourceDecrypter(method, additionalTypes, false))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
encryptedResource.Method = method;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
bool checkFields(IList<FieldDef> fields) {
|
2011-11-09 02:32:10 +08:00
|
|
|
|
if (fields.Count != 3)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var fieldTypes = new FieldTypes(fields);
|
|
|
|
|
if (fieldTypes.count("System.Boolean") != 1)
|
|
|
|
|
return false;
|
|
|
|
|
if (fieldTypes.count("System.Object") == 2)
|
|
|
|
|
return true;
|
|
|
|
|
return fieldTypes.count("System.Reflection.Assembly") == 1 &&
|
|
|
|
|
fieldTypes.count("System.String[]") == 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void init(ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob) {
|
|
|
|
|
if (encryptedResource.Method == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
initMethod = findInitMethod(simpleDeobfuscator);
|
|
|
|
|
if (initMethod == null)
|
|
|
|
|
throw new ApplicationException("Could not find resource resolver init method");
|
|
|
|
|
|
|
|
|
|
simpleDeobfuscator.deobfuscate(encryptedResource.Method);
|
|
|
|
|
simpleDeobfuscator.decryptStrings(encryptedResource.Method, deob);
|
|
|
|
|
encryptedResource.init(simpleDeobfuscator);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
MethodDef findInitMethod(ISimpleDeobfuscator simpleDeobfuscator) {
|
2012-11-18 01:57:36 +08:00
|
|
|
|
var ctor = Type.FindMethod(".ctor");
|
2011-11-09 02:32:10 +08:00
|
|
|
|
foreach (var method in Type.Methods) {
|
|
|
|
|
if (!method.IsStatic || method.Body == null)
|
|
|
|
|
continue;
|
|
|
|
|
if (!DotNetUtils.isMethod(method, "System.Void", "()"))
|
|
|
|
|
continue;
|
2012-11-20 00:58:34 +08:00
|
|
|
|
if (method.Body.Variables.Count > 1)
|
2011-11-09 02:32:10 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
simpleDeobfuscator.deobfuscate(method);
|
|
|
|
|
bool stsfldUsed = false, newobjUsed = false;
|
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
|
|
|
|
if (instr.OpCode.Code == Code.Stsfld) {
|
2012-11-18 01:57:36 +08:00
|
|
|
|
var field = instr.Operand as IField;
|
|
|
|
|
if (field == null || field.FieldSig.GetFieldType().GetElementType() != ElementType.Boolean)
|
2011-11-09 02:32:10 +08:00
|
|
|
|
continue;
|
2012-11-18 01:57:36 +08:00
|
|
|
|
if (!new SigComparer().Equals(Type, field.DeclaringType))
|
2011-11-09 02:32:10 +08:00
|
|
|
|
continue;
|
|
|
|
|
stsfldUsed = true;
|
|
|
|
|
}
|
|
|
|
|
else if (instr.OpCode.Code == Code.Newobj) {
|
2012-11-18 01:57:36 +08:00
|
|
|
|
var calledCtor = instr.Operand as IMethod;
|
2011-11-09 02:32:10 +08:00
|
|
|
|
if (calledCtor == null)
|
|
|
|
|
continue;
|
2012-11-18 01:57:36 +08:00
|
|
|
|
if (!MethodEqualityComparer.CompareDeclaringTypes.Equals(calledCtor, ctor))
|
2011-11-09 02:32:10 +08:00
|
|
|
|
continue;
|
|
|
|
|
newobjUsed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!stsfldUsed || !newobjUsed)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return method;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EmbeddedResource mergeResources() {
|
|
|
|
|
if (encryptedResource.Resource == null)
|
|
|
|
|
return null;
|
2012-11-18 01:57:36 +08:00
|
|
|
|
DeobUtils.decryptAndAddResources(module, encryptedResource.Resource.Name.String, () => {
|
2011-11-09 02:32:10 +08:00
|
|
|
|
return QuickLZ.decompress(encryptedResource.decrypt());
|
|
|
|
|
});
|
|
|
|
|
return encryptedResource.Resource;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|