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

270 lines
8.9 KiB
C#
Raw Normal View History

2012-01-08 03:27:07 +08:00
/*
2012-01-10 06:04:52 +08:00
Copyright (C) 2011-2012 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.Collections.Generic;
2012-01-11 09:35:02 +08:00
using System.Text.RegularExpressions;
2012-01-08 03:27:07 +08:00
using Mono.Cecil;
using de4dot.blocks;
namespace de4dot.code.deobfuscators.Babel_NET {
public class DeobfuscatorInfo : DeobfuscatorInfoBase {
public const string THE_NAME = "Babel .NET";
public const string THE_TYPE = "bl";
BoolOption decryptMethods;
BoolOption decryptResources;
BoolOption decryptConstants;
2012-01-18 14:43:03 +08:00
BoolOption dumpEmbeddedAssemblies;
2012-01-08 03:27:07 +08:00
public DeobfuscatorInfo()
: base() {
decryptMethods = new BoolOption(null, makeArgName("methods"), "Decrypt methods", true);
decryptResources = new BoolOption(null, makeArgName("rsrc"), "Decrypt resources", true);
decryptConstants = new BoolOption(null, makeArgName("consts"), "Decrypt constants and arrays", true);
2012-01-18 14:43:03 +08:00
dumpEmbeddedAssemblies = new BoolOption(null, makeArgName("embedded"), "Dump embedded assemblies", true);
2012-01-08 03:27:07 +08:00
}
public override string Name {
get { return THE_NAME; }
}
public override string Type {
get { return THE_TYPE; }
}
public override IDeobfuscator createDeobfuscator() {
return new Deobfuscator(new Deobfuscator.Options {
ValidNameRegex = validNameRegex.get(),
DecryptMethods = decryptMethods.get(),
DecryptResources = decryptResources.get(),
DecryptConstants = decryptConstants.get(),
2012-01-18 14:43:03 +08:00
DumpEmbeddedAssemblies = dumpEmbeddedAssemblies.get(),
2012-01-08 03:27:07 +08:00
});
}
protected override IEnumerable<Option> getOptionsInternal() {
return new List<Option>() {
decryptMethods,
decryptResources,
decryptConstants,
2012-01-18 14:43:03 +08:00
dumpEmbeddedAssemblies,
2012-01-08 03:27:07 +08:00
};
}
}
class Deobfuscator : DeobfuscatorBase {
Options options;
bool foundBabelAttribute = false;
2012-01-11 09:35:02 +08:00
string obfuscatorName = DeobfuscatorInfo.THE_NAME;
2012-01-08 03:27:07 +08:00
ResourceResolver resourceResolver;
2012-01-18 14:43:03 +08:00
AssemblyResolver assemblyResolver;
2012-01-08 03:27:07 +08:00
StringDecrypter stringDecrypter;
ConstantsDecrypter constantsDecrypter;
Int32ValueInliner int32ValueInliner;
Int64ValueInliner int64ValueInliner;
SingleValueInliner singleValueInliner;
DoubleValueInliner doubleValueInliner;
2012-05-29 17:13:39 +08:00
ProxyCallFixer proxyCallFixer;
2012-01-08 03:27:07 +08:00
MethodsDecrypter methodsDecrypter;
internal class Options : OptionsBase {
public bool DecryptMethods { get; set; }
public bool DecryptResources { get; set; }
public bool DecryptConstants { get; set; }
2012-01-18 14:43:03 +08:00
public bool DumpEmbeddedAssemblies { get; set; }
2012-01-08 03:27:07 +08:00
}
public override string Type {
get { return DeobfuscatorInfo.THE_TYPE; }
}
public override string TypeLong {
get { return DeobfuscatorInfo.THE_NAME; }
}
public override string Name {
2012-01-11 09:35:02 +08:00
get { return obfuscatorName; }
2012-01-08 03:27:07 +08:00
}
public Deobfuscator(Options options)
: base(options) {
this.options = options;
}
public override void init(ModuleDefinition module) {
base.init(module);
}
protected override int detectInternal() {
int val = 0;
2012-02-29 07:13:57 +08:00
int sum = toInt32(resourceResolver.Detected) +
2012-01-18 14:43:03 +08:00
toInt32(assemblyResolver.Detected) +
2012-01-08 03:27:07 +08:00
toInt32(stringDecrypter.Detected) +
toInt32(constantsDecrypter.Detected) +
2012-05-29 17:13:39 +08:00
toInt32(proxyCallFixer.Detected) +
2012-01-08 03:27:07 +08:00
toInt32(methodsDecrypter.Detected) +
toInt32(hasMetadataStream("Babel"));
if (sum > 0)
val += 100 + 10 * (sum - 1);
2012-02-29 07:13:57 +08:00
if (foundBabelAttribute)
val += 10;
2012-01-08 03:27:07 +08:00
return val;
}
protected override void scanForObfuscator() {
findBabelAttribute();
resourceResolver = new ResourceResolver(module);
resourceResolver.find();
2012-01-18 14:43:03 +08:00
assemblyResolver = new AssemblyResolver(module);
assemblyResolver.find();
2012-01-08 03:27:07 +08:00
stringDecrypter = new StringDecrypter(module);
2012-06-07 03:16:18 +08:00
stringDecrypter.find(DeobfuscatedFile);
2012-01-08 03:27:07 +08:00
constantsDecrypter = new ConstantsDecrypter(module, initializedDataCreator);
constantsDecrypter.find();
2012-05-29 17:13:39 +08:00
proxyCallFixer = new ProxyCallFixer(module);
proxyCallFixer.findDelegateCreator();
2012-04-05 03:06:10 +08:00
methodsDecrypter = new MethodsDecrypter(module, DeobfuscatedFile.DeobfuscatorContext);
2012-01-08 03:27:07 +08:00
methodsDecrypter.find();
}
void findBabelAttribute() {
foreach (var type in module.Types) {
if (type.FullName == "BabelAttribute" || type.FullName == "BabelObfuscatorAttribute") {
foundBabelAttribute = true;
2012-01-11 09:35:02 +08:00
checkVersion(type);
2012-01-08 03:27:07 +08:00
addAttributeToBeRemoved(type, "Obfuscator attribute");
return;
}
}
}
2012-01-11 09:35:02 +08:00
void checkVersion(TypeDefinition attr) {
var versionField = DotNetUtils.getFieldByName(attr, "Version");
2012-01-11 09:35:02 +08:00
if (versionField != null && versionField.IsLiteral && versionField.Constant != null && versionField.Constant is string) {
var val = Regex.Match((string)versionField.Constant, @"^(\d+\.\d+\.\d+\.\d+)$");
if (val.Groups.Count < 2)
return;
obfuscatorName = string.Format("{0} {1}", DeobfuscatorInfo.THE_NAME, val.Groups[1].ToString());
return;
}
}
2012-01-08 03:27:07 +08:00
public override void deobfuscateBegin() {
base.deobfuscateBegin();
if (options.DecryptResources) {
addCctorInitCallToBeRemoved(resourceResolver.InitMethod);
addTypeToBeRemoved(resourceResolver.Type, "Resource resolver type");
}
decryptResources();
stringDecrypter.initialize();
if (Operations.DecryptStrings != OpDecryptString.None) {
2012-01-19 12:38:58 +08:00
if (stringDecrypter.Resource != null)
2012-01-08 03:27:07 +08:00
Log.v("Adding string decrypter. Resource: {0}", Utils.toCsharpString(stringDecrypter.Resource.Name));
2012-01-20 02:19:08 +08:00
staticStringInliner.add(stringDecrypter.DecryptMethod, (method, args) => {
2012-01-19 12:38:58 +08:00
return stringDecrypter.decrypt(args);
});
DeobfuscatedFile.stringDecryptersAdded();
2012-01-08 03:27:07 +08:00
}
if (options.DumpEmbeddedAssemblies) {
2012-01-18 14:43:03 +08:00
assemblyResolver.initialize(DeobfuscatedFile, this);
// Need to dump the assemblies before decrypting methods in case there's a reference
// in the encrypted code to one of these assemblies.
dumpEmbeddedAssemblies();
}
2012-01-08 03:27:07 +08:00
if (options.DecryptMethods) {
methodsDecrypter.initialize(DeobfuscatedFile, this);
methodsDecrypter.decrypt();
}
if (options.DecryptConstants) {
constantsDecrypter.initialize(DeobfuscatedFile, this);
addTypeToBeRemoved(constantsDecrypter.Type, "Constants decrypter type");
addResourceToBeRemoved(constantsDecrypter.Resource, "Encrypted constants");
int32ValueInliner = new Int32ValueInliner();
int32ValueInliner.add(constantsDecrypter.Int32Decrypter, (method, args) => constantsDecrypter.decryptInt32((int)args[0]));
int64ValueInliner = new Int64ValueInliner();
int64ValueInliner.add(constantsDecrypter.Int64Decrypter, (method, args) => constantsDecrypter.decryptInt64((int)args[0]));
singleValueInliner = new SingleValueInliner();
singleValueInliner.add(constantsDecrypter.SingleDecrypter, (method, args) => constantsDecrypter.decryptSingle((int)args[0]));
doubleValueInliner = new DoubleValueInliner();
doubleValueInliner.add(constantsDecrypter.DoubleDecrypter, (method, args) => constantsDecrypter.decryptDouble((int)args[0]));
}
2012-05-29 17:13:39 +08:00
proxyCallFixer.find();
2012-01-08 03:27:07 +08:00
}
2012-01-18 14:43:03 +08:00
void dumpEmbeddedAssemblies() {
if (!options.DumpEmbeddedAssemblies)
return;
foreach (var info in assemblyResolver.EmbeddedAssemblyInfos)
DeobfuscatedFile.createAssemblyFile(info.data, Utils.getAssemblySimpleName(info.fullname), info.extension);
addTypeToBeRemoved(assemblyResolver.Type, "Assembly resolver type");
addCctorInitCallToBeRemoved(assemblyResolver.InitMethod);
addResourceToBeRemoved(assemblyResolver.EncryptedResource, "Embedded encrypted assemblies");
}
2012-01-08 03:27:07 +08:00
void decryptResources() {
if (!options.DecryptResources)
return;
var rsrc = resourceResolver.mergeResources();
if (rsrc == null)
return;
addResourceToBeRemoved(rsrc, "Encrypted resources");
}
public override void deobfuscateMethodEnd(Blocks blocks) {
2012-05-29 17:13:39 +08:00
proxyCallFixer.deobfuscate(blocks);
2012-01-08 03:27:07 +08:00
if (options.DecryptConstants) {
2012-01-14 19:37:20 +08:00
int32ValueInliner.decrypt(blocks);
int64ValueInliner.decrypt(blocks);
singleValueInliner.decrypt(blocks);
doubleValueInliner.decrypt(blocks);
2012-01-08 03:27:07 +08:00
constantsDecrypter.deobfuscate(blocks);
}
base.deobfuscateMethodEnd(blocks);
}
public override void deobfuscateEnd() {
if (CanRemoveStringDecrypterType) {
addResourceToBeRemoved(stringDecrypter.Resource, "Encrypted strings");
addTypeToBeRemoved(stringDecrypter.Type, "String decrypter type");
}
2012-05-29 17:13:39 +08:00
removeProxyDelegates(proxyCallFixer);
2012-01-08 03:27:07 +08:00
base.deobfuscateEnd();
}
2012-02-25 13:25:40 +08:00
public override IEnumerable<int> getStringDecrypterMethods() {
var list = new List<int>();
2012-01-08 03:27:07 +08:00
if (stringDecrypter.DecryptMethod != null)
2012-02-25 13:25:40 +08:00
list.Add(stringDecrypter.DecryptMethod.MetadataToken.ToInt32());
2012-01-08 03:27:07 +08:00
return list;
}
}
}