de4dot-cex/de4dot.code/deobfuscators/Babel_NET/AssemblyResolver.cs

131 lines
3.8 KiB
C#
Raw Normal View History

2012-01-18 14:43:03 +08:00
/*
2014-03-12 05:15:43 +08:00
Copyright (C) 2011-2014 de4dot@gmail.com
2012-01-18 14:43:03 +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/>.
*/
2012-06-12 16:37:51 +08:00
using System;
2012-01-18 14:43:03 +08:00
using System.IO;
using dnlib.IO;
using dnlib.DotNet;
2012-01-18 14:43:03 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.Babel_NET {
class AssemblyResolver {
2012-11-08 14:06:46 +08:00
ModuleDefMD module;
2012-06-12 16:37:51 +08:00
ResourceDecrypter resourceDecrypter;
TypeDef resolverType;
MethodDef registerMethod;
2012-01-18 14:43:03 +08:00
EmbeddedResource encryptedResource;
EmbeddedAssemblyInfo[] embeddedAssemblyInfos = new EmbeddedAssemblyInfo[0];
public class EmbeddedAssemblyInfo {
public string fullname;
public string extension;
public byte[] data;
public EmbeddedAssemblyInfo(string fullName, string extension, byte[] data) {
this.fullname = fullName;
this.extension = extension;
this.data = data;
}
}
public bool Detected {
get { return resolverType != null; }
}
public TypeDef Type {
2012-01-18 14:43:03 +08:00
get { return resolverType; }
}
public MethodDef InitMethod {
2012-01-18 14:43:03 +08:00
get { return registerMethod; }
}
public EmbeddedResource EncryptedResource {
get { return encryptedResource; }
}
public EmbeddedAssemblyInfo[] EmbeddedAssemblyInfos {
get { return embeddedAssemblyInfos; }
}
2012-11-08 14:06:46 +08:00
public AssemblyResolver(ModuleDefMD module, ResourceDecrypter resourceDecrypter) {
2012-01-18 14:43:03 +08:00
this.module = module;
2012-06-12 16:37:51 +08:00
this.resourceDecrypter = resourceDecrypter;
2012-01-18 14:43:03 +08:00
}
2013-01-19 20:03:57 +08:00
public void Find() {
2012-01-18 14:43:03 +08:00
var requiredTypes = new string[] {
"System.Object",
"System.Int32",
"System.Collections.Hashtable",
};
foreach (var type in module.Types) {
if (type.HasEvents)
continue;
2013-01-19 20:03:57 +08:00
if (!new FieldTypes(type).Exactly(requiredTypes))
2012-01-18 14:43:03 +08:00
continue;
MethodDef regMethod, handler;
2013-01-19 20:03:57 +08:00
if (!BabelUtils.FindRegisterMethod(type, out regMethod, out handler))
2012-01-18 14:43:03 +08:00
continue;
2013-01-19 20:03:57 +08:00
var decryptMethod = FindDecryptMethod(type);
2012-06-12 16:37:51 +08:00
if (decryptMethod == null)
throw new ApplicationException("Couldn't find resource type decrypt method");
2013-01-19 20:03:57 +08:00
resourceDecrypter.DecryptMethod = ResourceDecrypter.FindDecrypterMethod(decryptMethod);
2012-06-12 16:37:51 +08:00
2012-01-18 14:43:03 +08:00
resolverType = type;
registerMethod = regMethod;
return;
}
}
2013-01-19 20:03:57 +08:00
static MethodDef FindDecryptMethod(TypeDef type) {
2012-06-12 16:37:51 +08:00
foreach (var method in type.Methods) {
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(method, "System.Void", "(System.IO.Stream)"))
2012-06-12 16:37:51 +08:00
continue;
return method;
}
return null;
}
2013-01-19 20:03:57 +08:00
public void Initialize(ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob) {
2012-01-18 14:43:03 +08:00
if (resolverType == null)
return;
2013-01-19 20:03:57 +08:00
encryptedResource = BabelUtils.FindEmbeddedResource(module, resolverType, simpleDeobfuscator, deob);
2012-01-18 14:43:03 +08:00
if (encryptedResource == null) {
Logger.w("Could not find embedded assemblies resource");
2012-01-18 14:43:03 +08:00
return;
}
2013-01-19 20:03:57 +08:00
var decrypted = resourceDecrypter.Decrypt(encryptedResource.Data.ReadAllBytes());
2012-01-18 14:43:03 +08:00
var reader = new BinaryReader(new MemoryStream(decrypted));
int numAssemblies = reader.ReadInt32();
embeddedAssemblyInfos = new EmbeddedAssemblyInfo[numAssemblies];
for (int i = 0; i < numAssemblies; i++) {
string name = reader.ReadString();
var data = reader.ReadBytes(reader.ReadInt32());
2012-11-08 14:06:46 +08:00
var mod = ModuleDefMD.Load(data);
2013-01-19 20:03:57 +08:00
embeddedAssemblyInfos[i] = new EmbeddedAssemblyInfo(name, DeobUtils.GetExtension(mod.Kind), data);
2012-01-18 14:43:03 +08:00
}
}
}
}