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

301 lines
9.8 KiB
C#
Raw Normal View History

2012-01-23 02:58:31 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2012-01-23 02:58:31 +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;
using dnlib.DotNet;
2012-02-12 06:11:54 +08:00
using de4dot.blocks;
2012-01-23 02:58:31 +08:00
using de4dot.blocks.cflow;
namespace de4dot.code.deobfuscators.DeepSea {
public class DeobfuscatorInfo : DeobfuscatorInfoBase {
public const string THE_NAME = "DeepSea";
public const string THE_TYPE = "ds";
const string DEFAULT_REGEX = DeobfuscatorBase.DEFAULT_ASIAN_VALID_NAME_REGEX;
2012-01-23 02:58:31 +08:00
BoolOption inlineMethods;
BoolOption removeInlinedMethods;
BoolOption decryptResources;
BoolOption dumpEmbeddedAssemblies;
2012-01-25 00:10:11 +08:00
BoolOption restoreFields;
BoolOption renameResourceKeys;
BoolOption castDeobfuscation;
2012-01-23 02:58:31 +08:00
public DeobfuscatorInfo()
: base(DEFAULT_REGEX) {
2013-01-19 20:03:57 +08:00
inlineMethods = new BoolOption(null, MakeArgName("inline"), "Inline short methods", true);
removeInlinedMethods = new BoolOption(null, MakeArgName("remove-inlined"), "Remove inlined methods", true);
decryptResources = new BoolOption(null, MakeArgName("rsrc"), "Decrypt resources", true);
dumpEmbeddedAssemblies = new BoolOption(null, MakeArgName("embedded"), "Dump embedded assemblies", true);
restoreFields = new BoolOption(null, MakeArgName("fields"), "Restore fields", true);
renameResourceKeys = new BoolOption(null, MakeArgName("keys"), "Rename resource keys", true);
castDeobfuscation = new BoolOption(null, MakeArgName("casts"), "Deobfuscate casts", true);
2012-01-23 02:58:31 +08:00
}
public override string Name {
get { return THE_NAME; }
}
public override string Type {
get { return THE_TYPE; }
}
2013-01-19 20:03:57 +08:00
public override IDeobfuscator CreateDeobfuscator() {
2012-01-23 02:58:31 +08:00
return new Deobfuscator(new Deobfuscator.Options {
2013-11-18 23:28:44 +08:00
ValidNameRegex = validNameRegex.Get(),
InlineMethods = inlineMethods.Get(),
RemoveInlinedMethods = removeInlinedMethods.Get(),
DecryptResources = decryptResources.Get(),
DumpEmbeddedAssemblies = dumpEmbeddedAssemblies.Get(),
RestoreFields = restoreFields.Get(),
RenameResourceKeys = renameResourceKeys.Get(),
CastDeobfuscation = castDeobfuscation.Get(),
2012-01-23 02:58:31 +08:00
});
}
2013-01-19 20:03:57 +08:00
protected override IEnumerable<Option> GetOptionsInternal() {
2012-01-23 02:58:31 +08:00
return new List<Option>() {
inlineMethods,
removeInlinedMethods,
decryptResources,
dumpEmbeddedAssemblies,
2012-01-25 00:10:11 +08:00
restoreFields,
renameResourceKeys,
castDeobfuscation,
2012-01-23 02:58:31 +08:00
};
}
}
class Deobfuscator : DeobfuscatorBase {
Options options;
2012-01-24 07:41:09 +08:00
string obfuscatorName = DeobfuscatorInfo.THE_NAME;
2012-01-23 02:58:31 +08:00
bool startedDeobfuscating = false;
StringDecrypter stringDecrypter;
ResourceResolver resourceResolver;
AssemblyResolver assemblyResolver;
2012-01-25 00:10:11 +08:00
FieldsRestorer fieldsRestorer;
2012-07-17 00:04:20 +08:00
ArrayBlockState arrayBlockState;
2012-01-23 02:58:31 +08:00
internal class Options : OptionsBase {
public bool InlineMethods { get; set; }
public bool RemoveInlinedMethods { get; set; }
public bool DecryptResources { get; set; }
public bool DumpEmbeddedAssemblies { get; set; }
2012-01-25 00:10:11 +08:00
public bool RestoreFields { get; set; }
public bool RenameResourceKeys { get; set; }
public bool CastDeobfuscation { get; set; }
2012-01-23 02:58:31 +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-24 07:41:09 +08:00
get { return obfuscatorName; }
2012-01-23 02:58:31 +08:00
}
protected override bool CanInlineMethods {
get { return startedDeobfuscating ? options.InlineMethods : true; }
}
public override IEnumerable<IBlocksDeobfuscator> BlocksDeobfuscators {
2012-01-23 02:58:31 +08:00
get {
2013-01-19 20:03:57 +08:00
var list = new List<IBlocksDeobfuscator>(GetBlocksDeobfuscators());
2012-05-03 14:01:35 +08:00
if (CanInlineMethods)
2013-01-19 20:03:57 +08:00
list.Add(new DsMethodCallInliner(new CachedCflowDeobfuscator(GetBlocksDeobfuscators())));
return list;
2012-01-23 02:58:31 +08:00
}
}
2013-01-19 20:03:57 +08:00
List<IBlocksDeobfuscator> GetBlocksDeobfuscators() {
2012-05-03 14:01:35 +08:00
var list = new List<IBlocksDeobfuscator>();
2012-07-17 00:04:20 +08:00
if (arrayBlockState != null && arrayBlockState.Detected)
list.Add(new ArrayBlockDeobfuscator(arrayBlockState));
if (!startedDeobfuscating || options.CastDeobfuscation)
2012-05-03 14:01:35 +08:00
list.Add(new CastDeobfuscator());
return list;
}
2012-01-23 02:58:31 +08:00
public Deobfuscator(Options options)
: base(options) {
this.options = options;
if (options.RenameResourceKeys)
this.RenamingOptions |= RenamingOptions.RenameResourceKeys;
else
this.RenamingOptions &= ~RenamingOptions.RenameResourceKeys;
2012-01-23 02:58:31 +08:00
}
2013-01-19 20:03:57 +08:00
protected override int DetectInternal() {
2012-01-23 02:58:31 +08:00
int val = 0;
2013-01-19 20:03:57 +08:00
int sum = ToInt32(stringDecrypter.Detected) +
ToInt32(resourceResolver.Detected) +
ToInt32(assemblyResolver.Detected);
2012-01-23 02:58:31 +08:00
if (sum > 0)
val += 100 + 10 * (sum - 1);
return val;
}
2013-01-19 20:03:57 +08:00
protected override void ScanForObfuscator() {
2012-04-30 18:18:47 +08:00
staticStringInliner.UseUnknownArgs = true;
2012-07-17 00:04:20 +08:00
arrayBlockState = new ArrayBlockState(module);
2013-01-19 20:03:57 +08:00
arrayBlockState.Initialize(DeobfuscatedFile);
2012-01-23 02:58:31 +08:00
stringDecrypter = new StringDecrypter(module);
2013-01-19 20:03:57 +08:00
stringDecrypter.Find(DeobfuscatedFile);
2012-01-24 10:22:59 +08:00
resourceResolver = new ResourceResolver(module, DeobfuscatedFile, this);
2013-01-19 20:03:57 +08:00
resourceResolver.Find();
2012-01-24 10:22:59 +08:00
assemblyResolver = new AssemblyResolver(module, DeobfuscatedFile, this);
2013-01-19 20:03:57 +08:00
assemblyResolver.Find();
obfuscatorName = DetectVersion();
2012-01-24 07:41:09 +08:00
}
2013-01-19 20:03:57 +08:00
string DetectVersion() {
2012-01-24 07:41:09 +08:00
switch (stringDecrypter.Version) {
case StringDecrypter.DecrypterVersion.V1_3:
2013-01-19 20:03:57 +08:00
if (DetectMethodProxyObfuscation())
2012-01-24 08:01:30 +08:00
return DeobfuscatorInfo.THE_NAME + " 3.5";
2012-01-24 07:41:09 +08:00
return DeobfuscatorInfo.THE_NAME + " 1.x-3.x";
2012-04-30 14:33:01 +08:00
case StringDecrypter.DecrypterVersion.V4_0:
return DeobfuscatorInfo.THE_NAME + " 4.0";
case StringDecrypter.DecrypterVersion.V4_1:
return DeobfuscatorInfo.THE_NAME + " 4.1";
2012-01-24 07:41:09 +08:00
}
return DeobfuscatorInfo.THE_NAME;
2012-01-23 02:58:31 +08:00
}
2013-01-19 20:03:57 +08:00
bool DetectMethodProxyObfuscation() {
2012-01-24 21:54:23 +08:00
const int MIN_FOUND_PROXIES = 10;
2012-01-24 08:01:30 +08:00
int foundProxies = 0, checkedMethods = 0;
foreach (var type in module.GetTypes()) {
foreach (var method in type.Methods) {
if (foundProxies >= MIN_FOUND_PROXIES)
goto done;
if (!method.IsStatic || method.Body == null)
continue;
if (checkedMethods++ >= 1000)
goto done;
2013-01-19 20:03:57 +08:00
if (!DsMethodCallInliner.CanInline(method))
2012-01-24 08:01:30 +08:00
continue;
foundProxies++;
}
}
done:
return foundProxies >= MIN_FOUND_PROXIES;
}
2013-01-19 20:03:57 +08:00
public override void DeobfuscateBegin() {
base.DeobfuscateBegin();
2012-01-23 02:58:31 +08:00
2012-01-25 00:10:11 +08:00
if (options.RestoreFields) {
fieldsRestorer = new FieldsRestorer(module);
2013-01-19 20:03:57 +08:00
fieldsRestorer.Initialize();
2012-01-25 00:10:11 +08:00
}
2012-01-23 02:58:31 +08:00
foreach (var method in stringDecrypter.DecrypterMethods) {
2013-01-19 20:03:57 +08:00
staticStringInliner.Add(method, (method2, gim, args) => {
return stringDecrypter.Decrypt(method2, args);
2012-01-23 02:58:31 +08:00
});
}
2013-01-19 20:03:57 +08:00
DeobfuscatedFile.StringDecryptersAdded();
2012-01-23 02:58:31 +08:00
2013-01-19 20:03:57 +08:00
resourceResolver.Initialize();
DecryptResources();
2012-01-23 02:58:31 +08:00
2013-01-19 20:03:57 +08:00
DumpEmbeddedAssemblies();
2012-01-23 02:58:31 +08:00
startedDeobfuscating = true;
}
2013-01-19 20:03:57 +08:00
void DecryptResources() {
2012-01-23 02:58:31 +08:00
if (!options.DecryptResources)
return;
2012-01-24 10:22:59 +08:00
EmbeddedResource rsrc;
2013-01-19 20:03:57 +08:00
if (!resourceResolver.MergeResources(out rsrc))
2012-01-23 02:58:31 +08:00
return;
2013-01-19 20:03:57 +08:00
AddResourceToBeRemoved(rsrc, "Encrypted resources");
AddCctorInitCallToBeRemoved(resourceResolver.InitMethod);
AddCallToBeRemoved(module.EntryPoint, resourceResolver.InitMethod);
AddMethodToBeRemoved(resourceResolver.InitMethod, "Resource resolver init method");
AddMethodToBeRemoved(resourceResolver.InitMethod2, "Resource resolver init method #2");
AddMethodToBeRemoved(resourceResolver.HandlerMethod, "Resource resolver handler method");
AddMethodToBeRemoved(resourceResolver.GetDataMethod, "Resource resolver 'get resource data' method");
2012-01-23 02:58:31 +08:00
}
2013-01-19 20:03:57 +08:00
void DumpEmbeddedAssemblies() {
2012-01-23 02:58:31 +08:00
if (!options.DumpEmbeddedAssemblies)
return;
2013-01-19 20:03:57 +08:00
foreach (var info in assemblyResolver.GetAssemblyInfos()) {
2012-01-24 12:08:24 +08:00
if (info.resource != null && info.resource == resourceResolver.Resource)
continue;
2013-01-19 20:03:57 +08:00
DeobfuscatedFile.CreateAssemblyFile(info.data, info.simpleName, info.extension);
AddResourceToBeRemoved(info.resource, string.Format("Embedded assembly: {0}", info.fullName));
2012-01-23 02:58:31 +08:00
}
2013-01-19 20:03:57 +08:00
AddCctorInitCallToBeRemoved(assemblyResolver.InitMethod);
AddCallToBeRemoved(module.EntryPoint, assemblyResolver.InitMethod);
AddMethodToBeRemoved(assemblyResolver.InitMethod, "Assembly resolver init method");
AddMethodToBeRemoved(assemblyResolver.HandlerMethod, "Assembly resolver handler method");
AddMethodToBeRemoved(assemblyResolver.DecryptMethod, "Assembly resolver decrypt method");
2012-01-23 02:58:31 +08:00
}
2013-01-19 20:03:57 +08:00
public override void DeobfuscateMethodEnd(Blocks blocks) {
2012-01-25 00:10:11 +08:00
if (options.RestoreFields)
2013-01-19 20:03:57 +08:00
fieldsRestorer.Deobfuscate(blocks);
base.DeobfuscateMethodEnd(blocks);
2012-01-25 00:10:11 +08:00
}
2013-01-19 20:03:57 +08:00
public override void DeobfuscateEnd() {
if (options.RestoreFields && CanRemoveTypes)
2013-01-19 20:03:57 +08:00
fieldsRestorer.CleanUp();
RemoveInlinedMethods();
2012-01-23 02:58:31 +08:00
2012-01-25 00:10:11 +08:00
if (options.RestoreFields)
2013-01-19 20:03:57 +08:00
AddTypesToBeRemoved(fieldsRestorer.FieldStructs, "Type with moved fields");
2012-01-25 00:10:11 +08:00
if (CanRemoveStringDecrypterType) {
2013-01-19 20:03:57 +08:00
AddMethodsToBeRemoved(stringDecrypter.DecrypterMethods, "String decrypter method");
stringDecrypter.CleanUp();
2012-01-24 06:13:04 +08:00
}
2012-01-23 02:58:31 +08:00
2013-01-19 20:03:57 +08:00
AddFieldsToBeRemoved(arrayBlockState.CleanUp(), "Control flow obfuscation array");
2012-05-11 00:38:27 +08:00
2013-01-19 20:03:57 +08:00
base.DeobfuscateEnd();
2012-01-23 02:58:31 +08:00
}
2013-01-19 20:03:57 +08:00
void RemoveInlinedMethods() {
2012-01-23 02:58:31 +08:00
if (!options.InlineMethods || !options.RemoveInlinedMethods)
return;
2013-01-19 20:03:57 +08:00
RemoveInlinedMethods(DsInlinedMethodsFinder.Find(module, staticStringInliner.Methods));
2012-01-23 02:58:31 +08:00
}
2013-01-19 20:03:57 +08:00
public override IEnumerable<int> GetStringDecrypterMethods() {
2012-02-25 13:25:40 +08:00
var list = new List<int>();
2012-01-23 02:58:31 +08:00
foreach (var method in stringDecrypter.DecrypterMethods)
list.Add(method.MDToken.ToInt32());
2012-01-23 02:58:31 +08:00
return list;
}
}
}