2012-01-18 14:43:03 +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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-06-12 16:37:51 +08:00
|
|
|
|
using System;
|
2012-01-18 14:43:03 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
using Mono.Cecil;
|
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.Babel_NET {
|
|
|
|
|
class AssemblyResolver {
|
|
|
|
|
ModuleDefinition module;
|
2012-06-12 16:37:51 +08:00
|
|
|
|
ResourceDecrypter resourceDecrypter;
|
2012-01-18 14:43:03 +08:00
|
|
|
|
TypeDefinition resolverType;
|
|
|
|
|
MethodDefinition registerMethod;
|
|
|
|
|
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 TypeDefinition Type {
|
|
|
|
|
get { return resolverType; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MethodDefinition InitMethod {
|
|
|
|
|
get { return registerMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EmbeddedResource EncryptedResource {
|
|
|
|
|
get { return encryptedResource; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EmbeddedAssemblyInfo[] EmbeddedAssemblyInfos {
|
|
|
|
|
get { return embeddedAssemblyInfos; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-12 16:37:51 +08:00
|
|
|
|
public AssemblyResolver(ModuleDefinition 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void find() {
|
|
|
|
|
var requiredTypes = new string[] {
|
|
|
|
|
"System.Object",
|
|
|
|
|
"System.Int32",
|
|
|
|
|
"System.Collections.Hashtable",
|
|
|
|
|
};
|
|
|
|
|
foreach (var type in module.Types) {
|
|
|
|
|
if (type.HasEvents)
|
|
|
|
|
continue;
|
|
|
|
|
if (!new FieldTypes(type).exactly(requiredTypes))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
MethodDefinition regMethod, handler;
|
|
|
|
|
if (!BabelUtils.findRegisterMethod(type, out regMethod, out handler))
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-06-12 16:37:51 +08:00
|
|
|
|
var decryptMethod = findDecryptMethod(type);
|
|
|
|
|
if (decryptMethod == null)
|
|
|
|
|
throw new ApplicationException("Couldn't find resource type decrypt method");
|
|
|
|
|
resourceDecrypter.DecryptMethod = ResourceDecrypter.findDecrypterMethod(decryptMethod);
|
|
|
|
|
|
2012-01-18 14:43:03 +08:00
|
|
|
|
resolverType = type;
|
|
|
|
|
registerMethod = regMethod;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-12 16:37:51 +08:00
|
|
|
|
static MethodDefinition findDecryptMethod(TypeDefinition type) {
|
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (!DotNetUtils.isMethod(method, "System.Void", "(System.IO.Stream)"))
|
|
|
|
|
continue;
|
|
|
|
|
return method;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-18 14:43:03 +08:00
|
|
|
|
public void initialize(ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob) {
|
|
|
|
|
if (resolverType == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
encryptedResource = BabelUtils.findEmbeddedResource(module, resolverType, simpleDeobfuscator, deob);
|
|
|
|
|
if (encryptedResource == null) {
|
|
|
|
|
Log.w("Could not find embedded assemblies resource");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-12 16:37:51 +08:00
|
|
|
|
var decrypted = resourceDecrypter.decrypt(encryptedResource.GetResourceData());
|
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());
|
|
|
|
|
var mod = ModuleDefinition.ReadModule(new MemoryStream(data));
|
|
|
|
|
embeddedAssemblyInfos[i] = new EmbeddedAssemblyInfo(name, DeobUtils.getExtension(mod.Kind), data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|