de4dot-cex/de4dot.code/deobfuscators/SmartAssembly/Deobfuscator.cs

549 lines
17 KiB
C#
Raw Normal View History

2011-09-22 10:55:30 +08:00
/*
2012-01-10 06:02:47 +08:00
Copyright (C) 2011-2012 de4dot@gmail.com
2011-09-22 10:55:30 +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 System.Text;
using dot10.DotNet;
using dot10.DotNet.Emit;
2011-09-22 10:55:30 +08:00
using de4dot.blocks;
// SmartAssembly can add so much junk that it's very difficult to find and remove all of it.
// I remove some safe types that are almost guaranteed not to have any references in the code.
namespace de4dot.code.deobfuscators.SmartAssembly {
public class DeobfuscatorInfo : DeobfuscatorInfoBase {
2011-11-04 03:03:32 +08:00
public const string THE_NAME = "SmartAssembly";
2011-11-12 18:31:07 +08:00
public const string THE_TYPE = "sa";
2011-09-22 10:55:30 +08:00
BoolOption removeAutomatedErrorReporting;
BoolOption removeTamperProtection;
BoolOption removeMemoryManager;
public DeobfuscatorInfo()
2011-11-04 03:03:32 +08:00
: base() {
2011-09-22 10:55:30 +08:00
removeAutomatedErrorReporting = new BoolOption(null, makeArgName("error"), "Remove automated error reporting code", true);
removeTamperProtection = new BoolOption(null, makeArgName("tamper"), "Remove tamper protection code", true);
removeMemoryManager = new BoolOption(null, makeArgName("memory"), "Remove memory manager code", true);
}
2011-11-04 03:03:32 +08:00
public override string Name {
get { return THE_NAME; }
2011-09-22 10:55:30 +08:00
}
public override string Type {
2011-11-12 18:31:07 +08:00
get { return THE_TYPE; }
2011-09-22 10:55:30 +08:00
}
public override IDeobfuscator createDeobfuscator() {
return new Deobfuscator(new Deobfuscator.Options {
ValidNameRegex = validNameRegex.get(),
RemoveAutomatedErrorReporting = removeAutomatedErrorReporting.get(),
RemoveTamperProtection = removeTamperProtection.get(),
RemoveMemoryManager = removeMemoryManager.get(),
});
}
protected override IEnumerable<Option> getOptionsInternal() {
return new List<Option>() {
removeAutomatedErrorReporting,
removeTamperProtection,
removeMemoryManager,
};
}
}
class Deobfuscator : DeobfuscatorBase {
Options options;
2011-10-13 18:22:17 +08:00
bool foundVersion = false;
2012-01-17 09:54:48 +08:00
Version approxVersion = new Version(0, 0, 0, 0);
bool canRemoveTypes;
2011-10-13 18:22:17 +08:00
string poweredByAttributeString = null;
2011-11-12 18:31:07 +08:00
string obfuscatorName = DeobfuscatorInfo.THE_NAME;
2011-09-22 10:55:30 +08:00
bool foundSmartAssemblyAttribute = false;
IList<StringDecrypterInfo> stringDecrypterInfos = new List<StringDecrypterInfo>();
IList<StringDecrypter> stringDecrypters = new List<StringDecrypter>();
ResourceDecrypterInfo resourceDecrypterInfo;
ResourceDecrypter resourceDecrypter;
AssemblyResolverInfo assemblyResolverInfo;
AssemblyResolver assemblyResolver;
ResourceResolverInfo resourceResolverInfo;
ResourceResolver resourceResolver;
MemoryManagerInfo memoryManagerInfo;
2012-05-29 17:13:39 +08:00
ProxyCallFixer proxyCallFixer;
2011-09-22 10:55:30 +08:00
AutomatedErrorReportingFinder automatedErrorReportingFinder;
TamperProtectionRemover tamperProtectionRemover;
internal class Options : OptionsBase {
public bool RemoveAutomatedErrorReporting { get; set; }
public bool RemoveTamperProtection { get; set; }
public bool RemoveMemoryManager { get; set; }
}
public override string Type {
2011-11-12 18:31:07 +08:00
get { return DeobfuscatorInfo.THE_TYPE; }
}
public override string TypeLong {
2011-11-04 03:03:32 +08:00
get { return DeobfuscatorInfo.THE_NAME; }
2011-09-22 10:55:30 +08:00
}
public override string Name {
get { return obfuscatorName; }
}
2011-10-13 18:22:17 +08:00
string ObfuscatorName {
set {
obfuscatorName = value;
foundVersion = true;
}
}
2011-09-22 10:55:30 +08:00
public Deobfuscator(Options options)
: base(options) {
this.options = options;
StringFeatures = StringFeatures.AllowStaticDecryption;
}
public override void init(ModuleDefinition module) {
base.init(module);
2011-09-22 10:55:30 +08:00
}
protected override int detectInternal() {
2011-09-22 10:55:30 +08:00
int val = 0;
if (memoryManagerInfo.Detected)
2012-02-29 07:13:57 +08:00
val += 100;
if (foundSmartAssemblyAttribute)
2011-09-22 10:55:30 +08:00
val += 10;
return val;
}
protected override void scanForObfuscator() {
2011-09-22 10:55:30 +08:00
findSmartAssemblyAttributes();
memoryManagerInfo = new MemoryManagerInfo(module);
2012-01-17 09:54:48 +08:00
memoryManagerInfo.find();
2012-05-29 17:13:39 +08:00
proxyCallFixer = new ProxyCallFixer(module, DeobfuscatedFile);
proxyCallFixer.findDelegateCreator(module);
2011-10-13 18:22:17 +08:00
if (!foundVersion)
guessVersion();
2011-09-22 10:55:30 +08:00
}
void findSmartAssemblyAttributes() {
foreach (var type in module.Types) {
if (Utils.StartsWith(type.FullName, "SmartAssembly.Attributes.PoweredByAttribute", StringComparison.Ordinal)) {
2011-09-22 10:55:30 +08:00
foundSmartAssemblyAttribute = true;
addAttributeToBeRemoved(type, "Obfuscator attribute");
2011-10-13 18:22:17 +08:00
initializeVersion(type);
2011-09-22 10:55:30 +08:00
}
}
}
void initializeVersion(TypeDef attr) {
2011-09-22 10:55:30 +08:00
var s = DotNetUtils.getCustomArgAsString(getAssemblyAttribute(attr), 0);
if (s == null)
2011-10-13 18:22:17 +08:00
return;
poweredByAttributeString = s;
2011-09-22 10:55:30 +08:00
2012-01-17 09:54:48 +08:00
var val = System.Text.RegularExpressions.Regex.Match(s, @"^Powered by (SmartAssembly (\d+)\.(\d+)\.(\d+)\.(\d+))$");
if (val.Groups.Count < 6)
2011-10-13 18:22:17 +08:00
return;
ObfuscatorName = val.Groups[1].ToString();
2012-01-17 09:54:48 +08:00
approxVersion = new Version(int.Parse(val.Groups[2].ToString()),
int.Parse(val.Groups[3].ToString()),
int.Parse(val.Groups[4].ToString()),
int.Parse(val.Groups[5].ToString()));
2011-10-13 18:22:17 +08:00
return;
2011-10-13 05:16:03 +08:00
}
void guessVersion() {
2011-10-13 18:22:17 +08:00
if (poweredByAttributeString == "Powered by SmartAssembly") {
ObfuscatorName = "SmartAssembly 5.0/5.1";
2012-01-17 09:54:48 +08:00
approxVersion = new Version(5, 0, 0, 0);
2011-10-13 18:22:17 +08:00
return;
}
if (poweredByAttributeString == "Powered by {smartassembly}") {
// It's SA 1.x - 4.x
2012-05-29 17:13:39 +08:00
if (proxyCallFixer.Detected || hasEmptyClassesInEveryNamespace()) {
2011-10-13 18:22:17 +08:00
ObfuscatorName = "SmartAssembly 4.x";
2012-01-17 09:54:48 +08:00
approxVersion = new Version(4, 0, 0, 0);
2011-10-13 18:22:17 +08:00
return;
}
int ver = checkTypeIdAttribute();
if (ver == 2) {
ObfuscatorName = "SmartAssembly 2.x";
2012-01-17 09:54:48 +08:00
approxVersion = new Version(2, 0, 0, 0);
2011-10-13 18:22:17 +08:00
return;
}
if (ver == 1) {
ObfuscatorName = "SmartAssembly 1.x-2.x";
2012-01-17 09:54:48 +08:00
approxVersion = new Version(1, 0, 0, 0);
2011-10-13 18:22:17 +08:00
return;
}
if (hasModuleCctor()) {
ObfuscatorName = "SmartAssembly 3.x";
2012-01-17 09:54:48 +08:00
approxVersion = new Version(3, 0, 0, 0);
2011-10-13 18:22:17 +08:00
return;
}
ObfuscatorName = "SmartAssembly 1.x-4.x";
2012-01-17 09:54:48 +08:00
approxVersion = new Version(1, 0, 0, 0);
2011-10-13 18:22:17 +08:00
return;
}
}
int checkTypeIdAttribute() {
var type = getTypeIdAttribute();
if (type == null)
return -1;
var fields = type.Fields;
if (fields.Count == 1)
return 1; // 1.x: int ID
if (fields.Count == 2)
return 2; // 2.x: int ID, static int AssemblyID
return -1;
}
TypeDef getTypeIdAttribute() {
Dictionary<TypeDef, bool> attrs = null;
2011-10-13 18:22:17 +08:00
int counter = 0;
foreach (var type in module.GetTypes()) {
counter++;
var cattrs = type.CustomAttributes;
if (cattrs.Count == 0)
return null;
var attrs2 = new Dictionary<TypeDef, bool>();
2011-10-13 18:22:17 +08:00
foreach (var cattr in cattrs) {
if (!DotNetUtils.isMethod(cattr.Constructor, "System.Void", "(System.Int32)"))
continue;
var attrType = cattr.AttributeType as TypeDef;
2011-10-13 18:22:17 +08:00
if (attrType == null)
continue;
if (attrs != null && !attrs.ContainsKey(attrType))
continue;
attrs2[attrType] = true;
}
attrs = attrs2;
if (attrs.Count == 0)
return null;
if (attrs.Count == 1 && counter >= 30)
break;
}
if (attrs == null)
return null;
foreach (var type in attrs.Keys)
return type;
return null;
2011-10-13 05:16:03 +08:00
}
2011-10-13 18:22:17 +08:00
bool hasModuleCctor() {
2012-03-15 16:35:44 +08:00
return DotNetUtils.getModuleTypeCctor(module) != null;
2011-10-13 18:22:17 +08:00
}
bool hasEmptyClassesInEveryNamespace() {
2011-10-13 05:16:03 +08:00
var namespaces = new Dictionary<string, int>(StringComparer.Ordinal);
2012-01-09 01:46:23 +08:00
var moduleType = DotNetUtils.getModuleType(module);
2011-10-13 05:16:03 +08:00
foreach (var type in module.Types) {
2012-01-09 01:46:23 +08:00
if (type == moduleType)
2011-10-13 18:22:17 +08:00
continue;
2011-10-13 05:16:03 +08:00
var ns = type.Namespace;
if (!namespaces.ContainsKey(ns))
namespaces[ns] = 0;
2011-10-13 05:30:57 +08:00
if (type.Name != "" || type.IsPublic || type.HasFields || type.HasMethods || type.HasProperties || type.HasEvents)
2011-10-13 05:16:03 +08:00
continue;
namespaces[ns]++;
}
foreach (int count in namespaces.Values) {
if (count < 1)
return false;
}
return true;
2011-09-22 10:55:30 +08:00
}
public override void deobfuscateBegin() {
base.deobfuscateBegin();
2012-01-17 09:54:48 +08:00
tamperProtectionRemover = new TamperProtectionRemover(module);
automatedErrorReportingFinder = new AutomatedErrorReportingFinder(module);
automatedErrorReportingFinder.find();
2011-11-05 17:10:36 +08:00
if (options.RemoveMemoryManager) {
2011-10-23 14:41:33 +08:00
addModuleCctorInitCallToBeRemoved(memoryManagerInfo.CctorInitMethod);
2011-11-05 17:10:36 +08:00
addCallToBeRemoved(module.EntryPoint, memoryManagerInfo.CctorInitMethod);
}
2012-01-17 09:54:48 +08:00
2011-09-22 10:55:30 +08:00
initDecrypters();
2012-05-29 17:13:39 +08:00
proxyCallFixer.find();
2011-09-22 10:55:30 +08:00
}
void initDecrypters() {
assemblyResolverInfo = new AssemblyResolverInfo(module, DeobfuscatedFile, this);
2011-11-13 04:04:24 +08:00
assemblyResolverInfo.findTypes();
2012-01-17 09:54:48 +08:00
resourceDecrypterInfo = new ResourceDecrypterInfo(module, assemblyResolverInfo.SimpleZipTypeMethod, DeobfuscatedFile);
2011-11-13 04:04:24 +08:00
resourceResolverInfo = new ResourceResolverInfo(module, DeobfuscatedFile, this, assemblyResolverInfo);
resourceResolverInfo.findTypes();
2011-09-22 10:55:30 +08:00
resourceDecrypter = new ResourceDecrypter(resourceDecrypterInfo);
assemblyResolver = new AssemblyResolver(resourceDecrypter, assemblyResolverInfo);
resourceResolver = new ResourceResolver(module, assemblyResolver, resourceResolverInfo);
initStringDecrypterInfos();
assemblyResolverInfo.findTypes();
resourceResolverInfo.findTypes();
2011-10-23 14:41:33 +08:00
addModuleCctorInitCallToBeRemoved(assemblyResolverInfo.CallResolverMethod);
2011-12-22 12:41:28 +08:00
addCallToBeRemoved(module.EntryPoint, assemblyResolverInfo.CallResolverMethod);
2011-10-23 14:41:33 +08:00
addModuleCctorInitCallToBeRemoved(resourceResolverInfo.CallResolverMethod);
2011-12-22 12:41:28 +08:00
addCallToBeRemoved(module.EntryPoint, resourceResolverInfo.CallResolverMethod);
2011-09-22 10:55:30 +08:00
2012-01-17 09:54:48 +08:00
resourceDecrypterInfo.setSimpleZipType(getGlobalSimpleZipTypeMethod(), DeobfuscatedFile);
2011-09-22 10:55:30 +08:00
if (!decryptResources())
throw new ApplicationException("Could not decrypt resources");
dumpEmbeddedAssemblies();
}
void dumpEmbeddedAssemblies() {
assemblyResolver.resolveResources();
foreach (var tuple in assemblyResolver.getDecryptedResources()) {
2012-07-07 13:11:32 +08:00
DeobfuscatedFile.createAssemblyFile(tuple.Item2, tuple.Item1.simpleName, null);
2011-09-22 10:55:30 +08:00
addResourceToBeRemoved(tuple.Item1.resource, string.Format("Embedded assembly: {0}", tuple.Item1.assemblyName));
}
}
bool decryptResources() {
if (!resourceResolver.canDecryptResource())
return false;
2011-11-13 04:04:24 +08:00
var info = resourceResolver.mergeResources();
if (info == null)
2011-09-22 10:55:30 +08:00
return true;
2011-11-13 04:04:24 +08:00
addResourceToBeRemoved(info.resource, "Encrypted resources");
2011-09-22 10:55:30 +08:00
assemblyResolver.resolveResources();
return true;
}
MethodDef getGlobalSimpleZipTypeMethod() {
2012-01-17 09:54:48 +08:00
if (assemblyResolverInfo.SimpleZipTypeMethod != null)
return assemblyResolverInfo.SimpleZipTypeMethod;
2011-09-22 10:55:30 +08:00
foreach (var info in stringDecrypterInfos) {
2012-01-17 09:54:48 +08:00
if (info.SimpleZipTypeMethod != null)
return info.SimpleZipTypeMethod;
2011-09-22 10:55:30 +08:00
}
return null;
}
void initStringDecrypterInfos() {
var stringEncoderClassFinder = new StringEncoderClassFinder(module, DeobfuscatedFile);
stringEncoderClassFinder.find();
foreach (var info in stringEncoderClassFinder.StringsEncoderInfos) {
var sinfo = new StringDecrypterInfo(module, info.StringDecrypterClass) {
GetStringDelegate = info.GetStringDelegate,
StringsType = info.StringsType,
CreateStringDelegateMethod = info.CreateStringDelegateMethod,
};
stringDecrypterInfos.Add(sinfo);
}
// There may be more than one string decrypter. The strings in the first one's
// methods may be decrypted by the other string decrypter.
var initd = new Dictionary<StringDecrypterInfo, bool>(stringDecrypterInfos.Count);
while (initd.Count != stringDecrypterInfos.Count) {
StringDecrypterInfo initdInfo = null;
for (int i = 0; i < 2; i++) {
foreach (var info in stringDecrypterInfos) {
if (initd.ContainsKey(info))
continue;
if (info.init(this, DeobfuscatedFile)) {
2012-01-17 09:54:48 +08:00
resourceDecrypterInfo.setSimpleZipType(info.SimpleZipTypeMethod, DeobfuscatedFile);
2011-09-22 10:55:30 +08:00
initdInfo = info;
break;
}
}
if (initdInfo != null)
break;
2011-10-07 23:32:03 +08:00
assemblyResolverInfo.findTypes();
resourceResolverInfo.findTypes();
2011-09-22 10:55:30 +08:00
decryptResources();
}
if (initdInfo == null)
break;
2011-09-22 10:55:30 +08:00
initd[initdInfo] = true;
initStringDecrypter(initdInfo);
}
// Sometimes there could be a string decrypter present that isn't called by anyone.
foreach (var info in stringDecrypterInfos) {
if (initd.ContainsKey(info))
continue;
Logger.v("String decrypter not initialized. Token {0:X8}", info.StringsEncodingClass.MDToken.ToInt32());
}
2011-09-22 10:55:30 +08:00
}
void initStringDecrypter(StringDecrypterInfo info) {
Logger.v("Adding string decrypter. Resource: {0}", Utils.toCsharpString(info.StringsResource.Name));
2011-09-22 10:55:30 +08:00
var decrypter = new StringDecrypter(info);
if (decrypter.CanDecrypt) {
2012-07-28 10:22:17 +08:00
staticStringInliner.add(DotNetUtils.getMethod(info.GetStringDelegate, "Invoke"), (method, gim, args) => {
2011-09-22 10:55:30 +08:00
var fieldDefinition = DotNetUtils.getField(module, (FieldReference)args[0]);
return decrypter.decrypt(fieldDefinition.MDToken.ToInt32(), (int)args[1]);
2011-09-22 10:55:30 +08:00
});
2012-07-28 10:22:17 +08:00
staticStringInliner.add(info.StringDecrypterMethod, (method, gim, args) => {
2011-09-22 10:55:30 +08:00
return decrypter.decrypt(0, (int)args[0]);
});
}
stringDecrypters.Add(decrypter);
DeobfuscatedFile.stringDecryptersAdded();
}
public override void deobfuscateMethodEnd(Blocks blocks) {
2012-05-29 17:13:39 +08:00
proxyCallFixer.deobfuscate(blocks);
2011-09-22 10:55:30 +08:00
removeAutomatedErrorReportingCode(blocks);
removeTamperProtection(blocks);
removeStringsInitCode(blocks);
base.deobfuscateMethodEnd(blocks);
}
public override void deobfuscateEnd() {
2012-01-17 09:54:48 +08:00
canRemoveTypes = findBigType() == null;
2012-05-29 17:13:39 +08:00
removeProxyDelegates(proxyCallFixer, canRemoveTypes);
2011-09-22 10:55:30 +08:00
removeMemoryManagerStuff();
removeTamperProtectionStuff();
removeStringDecryptionStuff();
removeResolverInfoTypes(assemblyResolverInfo, "Assembly");
removeResolverInfoTypes(resourceResolverInfo, "Resource");
base.deobfuscateEnd();
}
TypeDef findBigType() {
2012-01-17 09:54:48 +08:00
if (approxVersion <= new Version(6, 5, 3, 53))
return null;
TypeDef bigType = null;
2012-01-17 09:54:48 +08:00
foreach (var type in module.Types) {
if (isBigType(type)) {
if (bigType == null || type.Methods.Count > bigType.Methods.Count)
bigType = type;
}
}
return bigType;
}
bool isBigType(TypeDef type) {
2012-01-17 09:54:48 +08:00
if (type.Methods.Count < 50)
return false;
if (type.HasProperties || type.HasEvents)
return false;
if (type.Fields.Count > 3)
return false;
foreach (var method in type.Methods) {
if (!method.IsStatic)
return false;
}
return true;
}
2011-09-22 10:55:30 +08:00
void removeResolverInfoTypes(ResolverInfoBase info, string typeName) {
2012-01-17 09:54:48 +08:00
if (!canRemoveTypes)
return;
2012-01-30 16:12:26 +08:00
if (info.CallResolverType == null || info.Type == null)
return;
2011-09-22 10:55:30 +08:00
addTypeToBeRemoved(info.CallResolverType, string.Format("{0} resolver type #1", typeName));
2011-11-06 22:24:30 +08:00
addTypeToBeRemoved(info.Type, string.Format("{0} resolver type #2", typeName));
2011-09-22 10:55:30 +08:00
}
void removeAutomatedErrorReportingCode(Blocks blocks) {
if (!options.RemoveAutomatedErrorReporting)
return;
2012-01-17 09:54:48 +08:00
if (automatedErrorReportingFinder.remove(blocks))
Logger.v("Removed Automated Error Reporting code");
2011-09-22 10:55:30 +08:00
}
void removeTamperProtection(Blocks blocks) {
if (!options.RemoveTamperProtection)
return;
if (tamperProtectionRemover.remove(blocks))
Logger.v("Removed Tamper Protection code");
2011-09-22 10:55:30 +08:00
}
void removeMemoryManagerStuff() {
2012-01-17 09:54:48 +08:00
if (!canRemoveTypes || !options.RemoveMemoryManager)
2011-09-22 10:55:30 +08:00
return;
2011-11-06 22:24:30 +08:00
addTypeToBeRemoved(memoryManagerInfo.Type, "Memory manager type");
2011-09-22 10:55:30 +08:00
}
void removeTamperProtectionStuff() {
if (!options.RemoveTamperProtection)
return;
addMethodsToBeRemoved(tamperProtectionRemover.PinvokeMethods, "Tamper protection PInvoke method");
}
void removeStringDecryptionStuff() {
if (!CanRemoveStringDecrypterType)
2011-09-22 10:55:30 +08:00
return;
foreach (var decrypter in stringDecrypters) {
var info = decrypter.StringDecrypterInfo;
addResourceToBeRemoved(info.StringsResource, "Encrypted strings");
addFieldsToBeRemoved(info.getAllStringDelegateFields(), "String decrypter delegate field");
2012-01-17 09:54:48 +08:00
if (canRemoveTypes) {
addTypeToBeRemoved(info.StringsEncodingClass, "String decrypter type");
addTypeToBeRemoved(info.StringsType, "Creates the string decrypter delegates");
addTypeToBeRemoved(info.GetStringDelegate, "String decrypter delegate type");
}
2011-09-22 10:55:30 +08:00
}
}
void removeStringsInitCode(Blocks blocks) {
if (!CanRemoveStringDecrypterType)
2011-09-22 10:55:30 +08:00
return;
if (blocks.Method.Name == ".cctor") {
foreach (var decrypter in stringDecrypters)
decrypter.StringDecrypterInfo.removeInitCode(blocks);
}
}
2012-02-25 13:25:40 +08:00
public override IEnumerable<int> getStringDecrypterMethods() {
var list = new List<int>();
2012-01-20 02:16:44 +08:00
foreach (var method in staticStringInliner.Methods)
list.Add(method.MDToken.ToInt32());
2011-09-22 10:55:30 +08:00
return list;
}
}
}