de4dot-cex/de4dot.code/deobfuscators/dotNET_Reactor/v4/ResourceResolver.cs

172 lines
5.3 KiB
C#
Raw Normal View History

2011-11-09 02:32:10 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 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;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2011-11-09 02:32:10 +08:00
using de4dot.blocks;
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;
MethodDef initMethod;
2011-11-09 02:32:10 +08:00
public bool Detected {
get { return encryptedResource.Method != null; }
}
public TypeDef Type {
get { return encryptedResource.Type; }
2011-11-09 02:32:10 +08:00
}
public MethodDef InitMethod {
2011-11-09 02:32:10 +08:00
get { return initMethod; }
}
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);
}
2013-01-19 20:03:57 +08:00
public void Find(ISimpleDeobfuscator simpleDeobfuscator) {
2011-11-09 02:32:10 +08:00
var additionalTypes = new string[] {
"System.String",
};
foreach (var type in module.Types) {
if (type.BaseType == null || type.BaseType.FullName != "System.Object")
continue;
2013-01-19 20:03:57 +08:00
if (!CheckFields(type.Fields))
2011-11-09 02:32:10 +08:00
continue;
foreach (var method in type.Methods) {
if (!method.IsStatic || !method.HasBody)
continue;
2013-01-19 20:03:57 +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;
2014-04-19 01:02:42 +08:00
var initMethod = GetResourceDecrypterInitMethod(method, additionalTypes, false);
if (initMethod == null)
2011-11-09 02:32:10 +08:00
continue;
2014-04-19 01:02:42 +08:00
encryptedResource.Method = initMethod;
2011-11-09 02:32:10 +08:00
return;
}
}
}
2014-04-19 01:02:42 +08:00
MethodDef GetResourceDecrypterInitMethod(MethodDef method, string[] additionalTypes, bool checkResource) {
if (encryptedResource.CouldBeResourceDecrypter(method, additionalTypes, checkResource))
return method;
foreach (var calledMethod in DotNetUtils.GetCalledMethods(module, method)) {
if (!DotNetUtils.IsMethod(calledMethod, "System.Void", "()"))
continue;
if (encryptedResource.CouldBeResourceDecrypter(calledMethod, additionalTypes, checkResource))
return calledMethod;
}
return null;
}
2013-01-19 20:03:57 +08:00
bool CheckFields(IList<FieldDef> fields) {
2014-04-19 01:02:42 +08:00
if (fields.Count != 3 && fields.Count != 4)
2011-11-09 02:32:10 +08:00
return false;
2014-04-19 01:02:42 +08:00
int numBools = fields.Count == 3 ? 1 : 2;
2011-11-09 02:32:10 +08:00
var fieldTypes = new FieldTypes(fields);
2014-04-19 01:02:42 +08:00
if (fieldTypes.Count("System.Boolean") != numBools)
2011-11-09 02:32:10 +08:00
return false;
2013-01-19 20:03:57 +08:00
if (fieldTypes.Count("System.Object") == 2)
2011-11-09 02:32:10 +08:00
return true;
2013-01-19 20:03:57 +08:00
return fieldTypes.Count("System.Reflection.Assembly") == 1 &&
fieldTypes.Count("System.String[]") == 1;
2011-11-09 02:32:10 +08:00
}
2013-01-19 20:03:57 +08:00
public void Initialize(ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob) {
2011-11-09 02:32:10 +08:00
if (encryptedResource.Method == null)
return;
2013-01-19 20:03:57 +08:00
initMethod = FindInitMethod(simpleDeobfuscator);
2011-11-09 02:32:10 +08:00
if (initMethod == null)
throw new ApplicationException("Could not find resource resolver init method");
2013-01-19 20:03:57 +08:00
simpleDeobfuscator.Deobfuscate(encryptedResource.Method);
simpleDeobfuscator.DecryptStrings(encryptedResource.Method, deob);
encryptedResource.Initialize(simpleDeobfuscator);
2011-11-09 02:32:10 +08:00
}
2013-01-19 20:03:57 +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;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(method, "System.Void", "()"))
2011-11-09 02:32:10 +08:00
continue;
if (method.Body.Variables.Count > 1)
2011-11-09 02:32:10 +08:00
continue;
2013-01-19 20:03:57 +08:00
simpleDeobfuscator.Deobfuscate(method);
2011-11-09 02:32:10 +08:00
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;
}
2013-01-19 20:03:57 +08:00
public EmbeddedResource MergeResources() {
2011-11-09 02:32:10 +08:00
if (encryptedResource.Resource == null)
return null;
2013-01-19 20:03:57 +08:00
DeobUtils.DecryptAndAddResources(module, encryptedResource.Resource.Name.String, () => {
return QuickLZ.Decompress(encryptedResource.Decrypt());
2011-11-09 02:32:10 +08:00
});
return encryptedResource.Resource;
}
}
}