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

252 lines
7.4 KiB
C#
Raw Normal View History

2012-01-08 03:27:07 +08:00
/*
2014-03-12 05:15:43 +08:00
Copyright (C) 2011-2014 de4dot@gmail.com
2012-01-08 03:27:07 +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 System.IO;
using dnlib.IO;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2012-01-08 03:27:07 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.Babel_NET {
2012-04-05 03:06:10 +08:00
class MethodsDecrypter {
2012-11-08 14:06:46 +08:00
ModuleDefMD module;
2012-06-12 16:37:51 +08:00
ResourceDecrypter resourceDecrypter;
2012-04-05 03:06:10 +08:00
IDeobfuscatorContext deobfuscatorContext;
2012-01-08 03:27:07 +08:00
Dictionary<string, ImageReader> imageReaders = new Dictionary<string, ImageReader>(StringComparer.Ordinal);
TypeDef methodsDecrypterCreator;
TypeDef methodsDecrypter;
MethodDef decryptExecuteMethod;
2012-01-08 03:27:07 +08:00
EmbeddedResource encryptedResource;
public bool Detected {
get { return methodsDecrypterCreator != null; }
}
2012-11-08 14:06:46 +08:00
public MethodsDecrypter(ModuleDefMD module, ResourceDecrypter resourceDecrypter, IDeobfuscatorContext deobfuscatorContext) {
2012-01-08 03:27:07 +08:00
this.module = module;
2012-06-12 16:37:51 +08:00
this.resourceDecrypter = resourceDecrypter;
2012-04-05 03:06:10 +08:00
this.deobfuscatorContext = deobfuscatorContext;
2012-01-08 03:27:07 +08:00
}
2013-01-19 20:03:57 +08:00
public void Find() {
2012-01-08 03:27:07 +08:00
var requiredFields = new string[] {
"System.Threading.ReaderWriterLock",
"System.Collections.Hashtable",
};
foreach (var type in module.GetTypes()) {
var fieldTypes = new FieldTypes(type);
2013-01-19 20:03:57 +08:00
if (!fieldTypes.All(requiredFields))
2012-01-08 03:27:07 +08:00
continue;
2012-11-08 14:06:46 +08:00
if (type.FindMethod("Finalize") == null)
2012-01-08 03:27:07 +08:00
continue;
2013-01-19 20:03:57 +08:00
var executeMethod = DotNetUtils.GetMethod(type, "System.Object", "(System.String,System.Object[])");
2012-01-08 03:27:07 +08:00
if (executeMethod == null || !executeMethod.IsStatic || executeMethod.Body == null)
continue;
2013-01-19 20:03:57 +08:00
var decrypterType = FindMethodsDecrypterType(type);
2012-01-08 03:27:07 +08:00
if (decrypterType == null)
continue;
2013-01-19 20:03:57 +08:00
resourceDecrypter.DecryptMethod = FindDecryptMethod(decrypterType);
2012-06-12 16:37:51 +08:00
2012-01-08 03:27:07 +08:00
methodsDecrypterCreator = type;
methodsDecrypter = decrypterType;
decryptExecuteMethod = executeMethod;
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
var decryptMethod = ResourceDecrypter.FindDecrypterMethod(method);
2012-06-12 16:37:51 +08:00
if (decryptMethod != null)
return decryptMethod;
}
return null;
}
2013-01-19 20:03:57 +08:00
TypeDef FindMethodsDecrypterType(TypeDef type) {
2012-01-08 03:27:07 +08:00
foreach (var field in type.Fields) {
2013-01-19 20:03:57 +08:00
var fieldType = DotNetUtils.GetType(module, field.FieldSig.GetFieldType());
2012-01-08 03:27:07 +08:00
if (fieldType == null)
continue;
2012-11-08 14:06:46 +08:00
if (fieldType.FindMethod("Finalize") == null)
2012-01-08 03:27:07 +08:00
continue;
2013-01-19 20:03:57 +08:00
if (!new FieldTypes(fieldType).Exists("System.Collections.Hashtable"))
2012-01-08 03:27:07 +08:00
continue;
2013-01-19 20:03:57 +08:00
if (DotNetUtils.GetMethod(fieldType, "System.String", "()") == null)
2012-01-08 03:27:07 +08:00
continue;
return fieldType;
}
return null;
}
2013-01-19 20:03:57 +08:00
public void Initialize(ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob) {
2012-01-08 03:27:07 +08:00
if (methodsDecrypter == null)
return;
2013-01-19 20:03:57 +08:00
encryptedResource = BabelUtils.FindEmbeddedResource(module, methodsDecrypter, simpleDeobfuscator, deob);
2012-06-12 16:37:51 +08:00
if (encryptedResource != null)
2013-01-19 20:03:57 +08:00
AddImageReader("", resourceDecrypter.Decrypt(encryptedResource.Data.ReadAllBytes()));
2012-01-08 03:27:07 +08:00
}
2013-01-19 20:03:57 +08:00
ImageReader AddImageReader(string name, byte[] data) {
2012-04-05 03:06:10 +08:00
var imageReader = new ImageReader(deobfuscatorContext, module, data);
2013-01-19 20:03:57 +08:00
if (!imageReader.Initialize()) {
Logger.w("Could not read encrypted methods");
2012-06-12 16:37:51 +08:00
return null;
2012-01-08 03:27:07 +08:00
}
if (imageReaders.ContainsKey(name))
throw new ApplicationException(string.Format("ImageReader for name '{0}' already exists", name));
imageReaders[name] = imageReader;
2012-06-12 16:37:51 +08:00
return imageReader;
2012-01-08 03:27:07 +08:00
}
class EncryptInfo {
public string encryptedMethodName;
public string feature;
public MethodDef method;
2012-06-12 16:37:51 +08:00
public string FullName {
get {
if (string.IsNullOrEmpty(feature))
return encryptedMethodName;
return string.Format("{0}:{1}", feature, encryptedMethodName);
}
}
public EncryptInfo(string encryptedMethodName, string feature, MethodDef method) {
2012-01-08 03:27:07 +08:00
this.encryptedMethodName = encryptedMethodName;
this.feature = feature;
this.method = method;
}
public override string ToString() {
if (feature != "")
return string.Format("{0}:{1} {2:X8}", feature, encryptedMethodName, method.MDToken.ToInt32());
2012-01-08 03:27:07 +08:00
else
return string.Format("{0} {1:X8}", encryptedMethodName, method.MDToken.ToInt32());
2012-01-08 03:27:07 +08:00
}
}
public void decrypt() {
int numNonDecryptedMethods = 0;
int totalEncryptedMethods = 0;
2013-01-19 20:03:57 +08:00
foreach (var info in GetEncryptedMethods()) {
2012-01-08 03:27:07 +08:00
totalEncryptedMethods++;
2013-01-19 20:03:57 +08:00
var imageReader = GetImageReader(info.feature);
2012-06-12 16:37:51 +08:00
if (imageReader == null) {
2012-01-08 03:27:07 +08:00
numNonDecryptedMethods++;
continue;
}
Logger.v("Decrypting method {0:X8}", info.method.MDToken.ToInt32());
2013-01-19 20:03:57 +08:00
imageReader.Restore(info.FullName, info.method);
2012-01-08 03:27:07 +08:00
}
if (numNonDecryptedMethods > 0)
Logger.w("{0}/{1} methods not decrypted", numNonDecryptedMethods, totalEncryptedMethods);
2012-01-08 03:27:07 +08:00
}
2013-01-19 20:03:57 +08:00
ImageReader GetImageReader(string feature) {
2012-06-12 16:37:51 +08:00
ImageReader imageReader;
if (imageReaders.TryGetValue(feature, out imageReader))
return imageReader;
2013-01-19 20:03:57 +08:00
return CreateImageReader(feature);
2012-06-12 16:37:51 +08:00
}
2013-01-19 20:03:57 +08:00
ImageReader CreateImageReader(string feature) {
2012-06-12 16:37:51 +08:00
if (string.IsNullOrEmpty(feature))
return null;
try {
2013-01-19 20:03:57 +08:00
var encrypted = File.ReadAllBytes(GetFile(Path.GetDirectoryName(module.Location), feature));
var decrypted = resourceDecrypter.Decrypt(encrypted);
return AddImageReader(feature, decrypted);
2012-06-12 16:37:51 +08:00
}
catch {
return null;
}
}
2013-01-19 20:03:57 +08:00
static string GetFile(string dir, string name) {
2012-06-12 16:37:51 +08:00
try {
var di = new DirectoryInfo(dir);
foreach (var file in di.GetFiles()) {
if (Utils.StartsWith(file.Name, name, StringComparison.OrdinalIgnoreCase))
return file.FullName;
}
}
catch {
}
return null;
}
2013-01-19 20:03:57 +08:00
List<EncryptInfo> GetEncryptedMethods() {
2012-01-08 03:27:07 +08:00
var infos = new List<EncryptInfo>();
foreach (var type in module.GetTypes()) {
foreach (var method in type.Methods) {
EncryptInfo info;
2013-01-19 20:03:57 +08:00
if (CheckEncryptedMethod(method, out info))
2012-01-08 03:27:07 +08:00
infos.Add(info);
}
}
return infos;
}
2013-01-19 20:03:57 +08:00
bool CheckEncryptedMethod(MethodDef method, out EncryptInfo info) {
2012-01-08 03:27:07 +08:00
info = null;
if (method.Body == null)
return false;
2013-01-19 20:03:57 +08:00
if (!CallsExecuteMethod(method))
2012-01-08 03:27:07 +08:00
return false;
2013-01-19 20:03:57 +08:00
var strings = DotNetUtils.GetCodeStrings(method);
2012-01-08 03:27:07 +08:00
if (strings.Count != 1)
throw new ApplicationException(string.Format("Could not find name of encrypted method"));
string feature = "";
string name = strings[0];
int index = name.IndexOf(':');
if (index >= 0) {
feature = name.Substring(0, index);
name = name.Substring(index + 1);
}
info = new EncryptInfo(name, feature, method);
return true;
}
2013-01-19 20:03:57 +08:00
bool CallsExecuteMethod(MethodDef method) {
2012-01-08 03:27:07 +08:00
foreach (var instr in method.Body.Instructions) {
if (instr.OpCode.Code != Code.Call && instr.OpCode.Code != Code.Callvirt)
continue;
2012-11-08 14:06:46 +08:00
if (MethodEqualityComparer.CompareDeclaringTypes.Equals(decryptExecuteMethod, instr.Operand as IMethod))
2012-01-08 03:27:07 +08:00
return true;
}
return false;
}
}
}