2012-02-02 13:56:14 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2012-02-02 13:56:14 +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-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
2012-02-03 11:26:55 +08:00
|
|
|
|
using de4dot.blocks;
|
2012-02-02 13:56:14 +08:00
|
|
|
|
using de4dot.blocks.cflow;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.Spices_Net {
|
|
|
|
|
public class DeobfuscatorInfo : DeobfuscatorInfoBase {
|
|
|
|
|
public const string THE_NAME = "Spices.Net";
|
|
|
|
|
public const string THE_TYPE = "sn";
|
2012-02-03 17:44:58 +08:00
|
|
|
|
const string DEFAULT_REGEX = @"!^[a-zA-Z0-9]{1,2}$&" + DeobfuscatorBase.DEFAULT_VALID_NAME_REGEX;
|
2012-02-02 17:55:30 +08:00
|
|
|
|
BoolOption inlineMethods;
|
|
|
|
|
BoolOption removeInlinedMethods;
|
2012-02-03 17:07:44 +08:00
|
|
|
|
BoolOption removeNamespaces;
|
2012-02-03 19:43:24 +08:00
|
|
|
|
BoolOption restoreResourceNames;
|
2012-02-02 13:56:14 +08:00
|
|
|
|
|
|
|
|
|
public DeobfuscatorInfo()
|
2012-02-03 17:44:58 +08:00
|
|
|
|
: 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);
|
|
|
|
|
removeNamespaces = new BoolOption(null, MakeArgName("ns1"), "Clear namespace if there's only one class in it", true);
|
|
|
|
|
restoreResourceNames = new BoolOption(null, MakeArgName("rsrc"), "Restore resource names", true);
|
2012-02-02 13:56:14 +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-02-02 13:56:14 +08:00
|
|
|
|
return new Deobfuscator(new Deobfuscator.Options {
|
|
|
|
|
ValidNameRegex = validNameRegex.get(),
|
2012-02-02 17:55:30 +08:00
|
|
|
|
InlineMethods = inlineMethods.get(),
|
|
|
|
|
RemoveInlinedMethods = removeInlinedMethods.get(),
|
2012-02-03 17:07:44 +08:00
|
|
|
|
RemoveNamespaces = removeNamespaces.get(),
|
2012-02-03 19:43:24 +08:00
|
|
|
|
RestoreResourceNames = restoreResourceNames.get(),
|
2012-02-02 13:56:14 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override IEnumerable<Option> GetOptionsInternal() {
|
2012-02-02 13:56:14 +08:00
|
|
|
|
return new List<Option>() {
|
2012-02-02 17:55:30 +08:00
|
|
|
|
inlineMethods,
|
|
|
|
|
removeInlinedMethods,
|
2012-02-03 17:07:44 +08:00
|
|
|
|
removeNamespaces,
|
2012-02-03 19:43:24 +08:00
|
|
|
|
restoreResourceNames,
|
2012-02-02 13:56:14 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Deobfuscator : DeobfuscatorBase {
|
|
|
|
|
Options options;
|
|
|
|
|
bool foundSpicesAttribute = false;
|
|
|
|
|
bool startedDeobfuscating = false;
|
|
|
|
|
|
|
|
|
|
StringDecrypter stringDecrypter;
|
2012-02-03 10:03:19 +08:00
|
|
|
|
SpicesMethodCallInliner methodCallInliner;
|
2012-02-03 19:43:24 +08:00
|
|
|
|
ResourceNamesRestorer resourceNamesRestorer;
|
2012-02-02 13:56:14 +08:00
|
|
|
|
|
|
|
|
|
internal class Options : OptionsBase {
|
2012-02-02 17:55:30 +08:00
|
|
|
|
public bool InlineMethods { get; set; }
|
|
|
|
|
public bool RemoveInlinedMethods { get; set; }
|
2012-02-03 17:07:44 +08:00
|
|
|
|
public bool RemoveNamespaces { get; set; }
|
2012-02-03 19:43:24 +08:00
|
|
|
|
public bool RestoreResourceNames { get; set; }
|
2012-02-02 13:56:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Type {
|
|
|
|
|
get { return DeobfuscatorInfo.THE_TYPE; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string TypeLong {
|
|
|
|
|
get { return DeobfuscatorInfo.THE_NAME; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Name {
|
|
|
|
|
get { return DeobfuscatorInfo.THE_NAME; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-02 17:55:30 +08:00
|
|
|
|
protected override bool CanInlineMethods {
|
|
|
|
|
get { return startedDeobfuscating ? options.InlineMethods : true; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-30 04:22:43 +08:00
|
|
|
|
public override IEnumerable<IBlocksDeobfuscator> BlocksDeobfuscators {
|
2012-02-02 17:55:30 +08:00
|
|
|
|
get {
|
2012-04-30 04:22:43 +08:00
|
|
|
|
var list = new List<IBlocksDeobfuscator>();
|
2012-02-02 17:55:30 +08:00
|
|
|
|
if (CanInlineMethods)
|
2012-04-30 04:22:43 +08:00
|
|
|
|
list.Add(methodCallInliner);
|
|
|
|
|
return list;
|
2012-02-02 17:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-02 13:56:14 +08:00
|
|
|
|
public Deobfuscator(Options options)
|
|
|
|
|
: base(options) {
|
|
|
|
|
this.options = options;
|
2012-02-03 17:07:44 +08:00
|
|
|
|
|
|
|
|
|
if (options.RemoveNamespaces)
|
|
|
|
|
this.RenamingOptions |= RenamingOptions.RemoveNamespaceIfOneType;
|
|
|
|
|
else
|
|
|
|
|
this.RenamingOptions &= ~RenamingOptions.RemoveNamespaceIfOneType;
|
2012-02-02 13:56:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override int DetectInternal() {
|
2012-02-02 13:56:14 +08:00
|
|
|
|
int val = 0;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
int sum = ToInt32(stringDecrypter.Detected);
|
2012-02-02 13:56:14 +08:00
|
|
|
|
if (sum > 0)
|
|
|
|
|
val += 100 + 10 * (sum - 1);
|
2012-02-29 07:13:57 +08:00
|
|
|
|
if (foundSpicesAttribute)
|
|
|
|
|
val += 10;
|
2012-02-02 13:56:14 +08:00
|
|
|
|
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override void ScanForObfuscator() {
|
2012-02-03 10:03:19 +08:00
|
|
|
|
methodCallInliner = new SpicesMethodCallInliner(module);
|
2012-02-02 13:56:14 +08:00
|
|
|
|
stringDecrypter = new StringDecrypter(module);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
stringDecrypter.Find();
|
|
|
|
|
FindSpicesAttributes();
|
2012-02-02 13:56:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void FindSpicesAttributes() {
|
2012-02-02 13:56:14 +08:00
|
|
|
|
foreach (var type in module.Types) {
|
2012-02-03 17:45:31 +08:00
|
|
|
|
switch (type.FullName) {
|
|
|
|
|
case "NineRays.Decompiler.NotDecompile":
|
|
|
|
|
case "NineRays.Obfuscator.Evaluation":
|
2012-11-15 04:45:12 +08:00
|
|
|
|
case "NineRays.Obfuscator.SoftwareWatermarkAttribute":
|
2013-01-19 20:03:57 +08:00
|
|
|
|
AddAttributeToBeRemoved(type, "Obfuscator attribute");
|
2012-02-02 13:56:14 +08:00
|
|
|
|
foundSpicesAttribute = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override void DeobfuscateBegin() {
|
|
|
|
|
base.DeobfuscateBegin();
|
2012-02-02 13:56:14 +08:00
|
|
|
|
|
2013-09-29 01:51:25 +08:00
|
|
|
|
methodCallInliner.Initialize(DeobfuscatedFile);
|
2012-02-03 10:03:19 +08:00
|
|
|
|
|
2012-02-03 19:43:24 +08:00
|
|
|
|
if (options.RestoreResourceNames) {
|
|
|
|
|
resourceNamesRestorer = new ResourceNamesRestorer(module);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
resourceNamesRestorer.Find();
|
|
|
|
|
resourceNamesRestorer.RenameResources();
|
2012-02-03 19:43:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
stringDecrypter.Initialize();
|
2012-02-02 13:56:14 +08:00
|
|
|
|
foreach (var info in stringDecrypter.DecrypterInfos) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
staticStringInliner.Add(info.method, (method2, gim, args) => {
|
|
|
|
|
return stringDecrypter.Decrypt(method2);
|
2012-02-02 13:56:14 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
2013-01-19 20:03:57 +08:00
|
|
|
|
DeobfuscatedFile.StringDecryptersAdded();
|
2012-02-02 13:56:14 +08:00
|
|
|
|
|
|
|
|
|
startedDeobfuscating = true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override void DeobfuscateMethodEnd(Blocks blocks) {
|
|
|
|
|
methodCallInliner.Deobfuscate(blocks);
|
2012-02-03 19:43:24 +08:00
|
|
|
|
if (options.RestoreResourceNames)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
resourceNamesRestorer.Deobfuscate(blocks);
|
|
|
|
|
base.DeobfuscateMethodEnd(blocks);
|
2012-02-03 11:26:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override void DeobfuscateEnd() {
|
|
|
|
|
RemoveInlinedMethods();
|
2012-02-02 17:55:30 +08:00
|
|
|
|
|
2012-02-03 19:43:24 +08:00
|
|
|
|
if (options.RestoreResourceNames) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
AddTypeToBeRemoved(resourceNamesRestorer.ResourceManagerType, "Obfuscator ResourceManager type");
|
|
|
|
|
AddTypeToBeRemoved(resourceNamesRestorer.ComponentResourceManagerType, "Obfuscator ComponentResourceManager type");
|
2012-02-03 19:43:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-02 13:56:14 +08:00
|
|
|
|
if (Operations.DecryptStrings != OpDecryptString.None) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
AddTypeToBeRemoved(stringDecrypter.Type, "String decrypter type");
|
|
|
|
|
AddTypeToBeRemoved(stringDecrypter.EncryptedStringsType, "Encrypted strings field type");
|
|
|
|
|
stringDecrypter.CleanUp();
|
2012-02-02 13:56:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
base.DeobfuscateEnd();
|
2012-02-02 13:56:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void RemoveInlinedMethods() {
|
2012-02-02 17:55:30 +08:00
|
|
|
|
if (!options.InlineMethods || !options.RemoveInlinedMethods)
|
|
|
|
|
return;
|
2012-02-03 13:33:54 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var unusedMethods = new UnusedMethodsFinder(module, methodCallInliner.GetInlinedMethods(), GetRemovedMethods()).Find();
|
|
|
|
|
var removedTypes = methodCallInliner.GetInlinedTypes(unusedMethods);
|
2012-02-03 13:33:54 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
AddTypesToBeRemoved(removedTypes.GetKeys(), "Obfuscator methods type");
|
2012-02-03 13:33:54 +08:00
|
|
|
|
foreach (var method in unusedMethods) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!removedTypes.Find(method.DeclaringType))
|
|
|
|
|
AddMethodToBeRemoved(method, "Inlined method");
|
2012-02-03 13:33:54 +08:00
|
|
|
|
}
|
2012-02-02 17:55:30 +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-02-02 13:56:14 +08:00
|
|
|
|
foreach (var info in stringDecrypter.DecrypterInfos)
|
2012-11-02 22:57:11 +08:00
|
|
|
|
list.Add(info.method.MDToken.ToInt32());
|
2012-02-02 13:56:14 +08:00
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|