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

282 lines
8.8 KiB
C#
Raw Normal View History

2011-12-29 15:26:36 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2011-12-29 15:26:36 +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;
2011-12-29 15:26:36 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.Goliath_NET {
public class DeobfuscatorInfo : DeobfuscatorInfoBase {
public const string THE_NAME = "Goliath.NET";
public const string THE_TYPE = "go";
const string DEFAULT_REGEX = @"!^[A-Za-z]{1,2}(?:`\d+)?$&" + DeobfuscatorBase.DEFAULT_ASIAN_VALID_NAME_REGEX;
2011-12-29 15:26:36 +08:00
BoolOption inlineMethods;
BoolOption removeInlinedMethods;
BoolOption restoreLocals;
BoolOption decryptIntegers;
BoolOption decryptArrays;
BoolOption removeAntiStrongName;
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);
restoreLocals = new BoolOption(null, MakeArgName("locals"), "Restore locals", true);
decryptIntegers = new BoolOption(null, MakeArgName("ints"), "Decrypt integers", true);
decryptArrays = new BoolOption(null, MakeArgName("arrays"), "Decrypt arrays", true);
removeAntiStrongName = new BoolOption(null, MakeArgName("sn"), "Remove anti strong name code", true);
2011-12-29 15:26:36 +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() {
2011-12-29 15:26:36 +08:00
return new Deobfuscator(new Deobfuscator.Options {
RenameResourcesInCode = false,
2013-11-18 23:28:44 +08:00
ValidNameRegex = validNameRegex.Get(),
InlineMethods = inlineMethods.Get(),
RemoveInlinedMethods = removeInlinedMethods.Get(),
RestoreLocals = restoreLocals.Get(),
DecryptIntegers = decryptIntegers.Get(),
DecryptArrays = decryptArrays.Get(),
RemoveAntiStrongName = removeAntiStrongName.Get(),
2011-12-29 15:26:36 +08:00
});
}
2013-01-19 20:03:57 +08:00
protected override IEnumerable<Option> GetOptionsInternal() {
2011-12-29 15:26:36 +08:00
return new List<Option>() {
inlineMethods,
removeInlinedMethods,
restoreLocals,
decryptIntegers,
decryptArrays,
removeAntiStrongName,
};
}
}
class Deobfuscator : DeobfuscatorBase {
Options options;
string obfuscatorName = DeobfuscatorInfo.THE_NAME;
2012-05-29 17:13:39 +08:00
ProxyCallFixer proxyCallFixer;
2011-12-29 15:26:36 +08:00
LocalsRestorer localsRestorer;
LogicalExpressionFixer logicalExpressionFixer;
StringDecrypter stringDecrypter;
IntegerDecrypter integerDecrypter;
2012-01-14 19:04:59 +08:00
Int32ValueInliner int32ValueInliner;
2011-12-29 15:26:36 +08:00
ArrayDecrypter arrayDecrypter;
ArrayValueInliner arrayValueInliner;
StrongNameChecker strongNameChecker;
bool foundGoliathAttribute = false;
bool startedDeobfuscating = false;
internal class Options : OptionsBase {
public bool InlineMethods { get; set; }
public bool RemoveInlinedMethods { get; set; }
public bool RestoreLocals { get; set; }
public bool DecryptIntegers { get; set; }
public bool DecryptArrays { get; set; }
public bool RemoveAntiStrongName { get; set; }
}
public override string Type {
get { return DeobfuscatorInfo.THE_TYPE; }
}
public override string TypeLong {
get { return DeobfuscatorInfo.THE_NAME; }
}
public override string Name {
get { return obfuscatorName; }
}
2012-01-14 04:30:29 +08:00
protected override bool CanInlineMethods {
2011-12-29 15:26:36 +08:00
get { return startedDeobfuscating ? options.InlineMethods : true; }
}
internal Deobfuscator(Options options)
: base(options) {
this.options = options;
}
2013-01-19 20:03:57 +08:00
protected override int DetectInternal() {
2011-12-29 15:26:36 +08:00
int val = 0;
2013-01-19 20:03:57 +08:00
int sum = ToInt32(stringDecrypter.Detected) +
ToInt32(integerDecrypter.Detected) +
ToInt32(arrayDecrypter.Detected) +
ToInt32(strongNameChecker.Detected) +
ToInt32(HasMetadataStream("#GOLIATH"));
2011-12-29 15:26:36 +08:00
if (sum > 0)
val += 100 + 10 * (sum - 1);
2012-02-29 07:13:57 +08:00
if (foundGoliathAttribute)
val += 10;
2011-12-29 15:26:36 +08:00
return val;
}
2013-01-19 20:03:57 +08:00
protected override void ScanForObfuscator() {
FindGoliathAttribute();
2011-12-29 15:26:36 +08:00
stringDecrypter = new StringDecrypter(module);
2013-01-19 20:03:57 +08:00
stringDecrypter.Find();
2011-12-29 15:26:36 +08:00
integerDecrypter = new IntegerDecrypter(module);
2013-01-19 20:03:57 +08:00
integerDecrypter.Find();
2011-12-29 15:26:36 +08:00
arrayDecrypter = new ArrayDecrypter(module);
2013-01-19 20:03:57 +08:00
arrayDecrypter.Find();
2011-12-29 15:26:36 +08:00
strongNameChecker = new StrongNameChecker(module);
2013-01-19 20:03:57 +08:00
strongNameChecker.Find();
2011-12-29 15:26:36 +08:00
}
2013-01-19 20:03:57 +08:00
void FindGoliathAttribute() {
2011-12-29 15:26:36 +08:00
foreach (var type in module.Types) {
if (type.FullName.Contains("ObfuscatedByGoliath")) {
foundGoliathAttribute = true;
2013-01-19 20:03:57 +08:00
AddAttributeToBeRemoved(type, "Obfuscator attribute");
InitializeVersion(type);
2011-12-29 15:26:36 +08:00
break;
}
}
}
2013-01-19 20:03:57 +08:00
void InitializeVersion(TypeDef attr) {
var s = DotNetUtils.GetCustomArgAsString(GetAssemblyAttribute(attr), 0);
2011-12-29 15:26:36 +08:00
if (s == null)
return;
var val = System.Text.RegularExpressions.Regex.Match(s, @"^Goliath \.NET Obfuscator rel\. (\d+\.\d+\.\d+)$");
if (val.Groups.Count < 2)
return;
obfuscatorName = DeobfuscatorInfo.THE_NAME + " " + val.Groups[1].ToString();
return;
}
2013-01-19 20:03:57 +08:00
public override void DeobfuscateBegin() {
base.DeobfuscateBegin();
2011-12-29 15:26:36 +08:00
2012-05-29 17:13:39 +08:00
proxyCallFixer = new ProxyCallFixer(module);
2013-01-19 20:03:57 +08:00
proxyCallFixer.Find();
2011-12-29 15:26:36 +08:00
localsRestorer = new LocalsRestorer(module);
if (options.RestoreLocals)
2013-01-19 20:03:57 +08:00
localsRestorer.Find();
2011-12-29 15:26:36 +08:00
logicalExpressionFixer = new LogicalExpressionFixer();
2013-01-19 20:03:57 +08:00
stringDecrypter.Initialize();
integerDecrypter.Initialize();
arrayDecrypter.Initialize();
2011-12-29 15:26:36 +08:00
if (options.DecryptIntegers) {
2012-01-14 19:04:59 +08:00
int32ValueInliner = new Int32ValueInliner();
2013-01-19 20:03:57 +08:00
foreach (var method in integerDecrypter.GetMethods()) {
int32ValueInliner.Add(method, (method2, gim, args) => {
return integerDecrypter.Decrypt(method2);
2011-12-29 15:26:36 +08:00
});
}
}
if (options.DecryptArrays) {
arrayValueInliner = new ArrayValueInliner(module, initializedDataCreator);
2013-01-19 20:03:57 +08:00
foreach (var method in arrayDecrypter.GetMethods()) {
arrayValueInliner.Add(method, (method2, gim, args) => {
return arrayDecrypter.Decrypt(method2);
2011-12-29 15:26:36 +08:00
});
}
}
2013-01-19 20:03:57 +08:00
foreach (var method in stringDecrypter.GetMethods()) {
staticStringInliner.Add(method, (method2, gim, args) => {
return stringDecrypter.Decrypt(method2);
2011-12-29 15:26:36 +08:00
});
2013-01-19 20:03:57 +08:00
DeobfuscatedFile.StringDecryptersAdded();
2011-12-29 15:26:36 +08:00
}
if (options.RemoveAntiStrongName)
2013-01-19 20:03:57 +08:00
AddTypeToBeRemoved(strongNameChecker.Type, "Strong name checker type");
2011-12-29 15:26:36 +08:00
startedDeobfuscating = true;
}
2013-01-19 20:03:57 +08:00
public override void DeobfuscateMethodBegin(Blocks blocks) {
proxyCallFixer.Deobfuscate(blocks);
base.DeobfuscateMethodBegin(blocks);
2011-12-29 15:26:36 +08:00
}
2013-01-19 20:03:57 +08:00
public override void DeobfuscateMethodEnd(Blocks blocks) {
stringDecrypter.Deobfuscate(blocks);
int32ValueInliner.Decrypt(blocks);
arrayValueInliner.Decrypt(blocks);
2011-12-29 15:26:36 +08:00
if (options.RestoreLocals)
2013-01-19 20:03:57 +08:00
localsRestorer.Deobfuscate(blocks);
2011-12-29 15:26:36 +08:00
if (options.RemoveAntiStrongName) {
2013-01-19 20:03:57 +08:00
if (strongNameChecker.Deobfuscate(blocks))
Logger.v("Removed strong name checker code");
2011-12-29 15:26:36 +08:00
}
2013-01-19 20:03:57 +08:00
logicalExpressionFixer.Deobfuscate(blocks);
base.DeobfuscateMethodEnd(blocks);
2011-12-29 15:26:36 +08:00
}
2013-01-19 20:03:57 +08:00
public override void DeobfuscateEnd() {
RemoveProxyDelegates(proxyCallFixer);
RemoveInlinedMethods();
AddTypesToBeRemoved(localsRestorer.Types, "Method locals obfuscation type");
2011-12-29 15:26:36 +08:00
if (CanRemoveStringDecrypterType) {
2013-01-19 20:03:57 +08:00
RemoveDecrypterStuff(stringDecrypter, "String", "strings");
AddTypeToBeRemoved(stringDecrypter.StringStruct, "String struct");
2012-01-02 14:02:43 +08:00
}
2011-12-29 15:26:36 +08:00
if (options.DecryptIntegers)
2013-01-19 20:03:57 +08:00
RemoveDecrypterStuff(integerDecrypter, "Integer", "integers");
2011-12-29 15:26:36 +08:00
if (options.DecryptArrays)
2013-01-19 20:03:57 +08:00
RemoveDecrypterStuff(arrayDecrypter, "Array", "arrays");
2011-12-29 15:26:36 +08:00
2013-01-19 20:03:57 +08:00
base.DeobfuscateEnd();
2011-12-29 15:26:36 +08:00
}
2013-01-19 20:03:57 +08:00
void RemoveDecrypterStuff(DecrypterBase decrypter, string name1, string name2) {
AddResourceToBeRemoved(decrypter.EncryptedResource, "Encrypted " + name2);
AddTypesToBeRemoved(decrypter.DecrypterTypes, name1 + " decrypter type");
AddTypeToBeRemoved(decrypter.Type, name1 + " resource decrypter type");
2011-12-29 15:26:36 +08:00
if (decrypter.DelegateInitType != null) {
2013-01-19 20:03:57 +08:00
AddTypeToBeRemoved(decrypter.DelegateType, name1 + " resource decrypter delegate type");
AddTypeToBeRemoved(decrypter.DelegateInitType, name1 + " delegate initializer type");
2011-12-29 15:26:36 +08:00
}
}
2013-01-19 20:03:57 +08:00
void RemoveInlinedMethods() {
2011-12-29 15:26:36 +08:00
if (!options.InlineMethods || !options.RemoveInlinedMethods)
return;
2013-01-19 20:03:57 +08:00
FindAndRemoveInlinedMethods();
2011-12-29 15:26:36 +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>();
2013-01-19 20:03:57 +08:00
foreach (var method in stringDecrypter.GetMethods())
list.Add(method.MDToken.ToInt32());
2011-12-29 15:26:36 +08:00
return list;
}
}
}