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

290 lines
10 KiB
C#
Raw Normal View History

/*
2012-01-10 06:02:47 +08:00
Copyright (C) 2011-2012 de4dot@gmail.com
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/>.
*/
2012-03-14 03:27:41 +08:00
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using dot10.DotNet;
using de4dot.blocks;
namespace de4dot.code.deobfuscators.CryptoObfuscator {
public class DeobfuscatorInfo : DeobfuscatorInfoBase {
2011-11-04 03:03:32 +08:00
public const string THE_NAME = "Crypto Obfuscator";
2011-11-12 18:31:07 +08:00
public const string THE_TYPE = "co";
2011-11-23 18:32:36 +08:00
const string DEFAULT_REGEX = @"!^(get_|set_|add_|remove_)?[A-Z]{1,3}(?:`\d+)?$&!^(get_|set_|add_|remove_)?c[0-9a-f]{32}(?:`\d+)?$&" + DeobfuscatorBase.DEFAULT_VALID_NAME_REGEX;
BoolOption removeTamperProtection;
2012-07-11 08:18:01 +08:00
BoolOption decryptConstants;
public DeobfuscatorInfo()
2011-11-04 03:03:32 +08:00
: base(DEFAULT_REGEX) {
removeTamperProtection = new BoolOption(null, makeArgName("tamper"), "Remove tamper protection code", true);
2012-07-11 08:18:01 +08:00
decryptConstants = new BoolOption(null, makeArgName("consts"), "Decrypt constants", true);
}
2011-11-04 03:03:32 +08:00
public override string Name {
get { return THE_NAME; }
}
public override string Type {
2011-11-12 18:31:07 +08:00
get { return THE_TYPE; }
}
public override IDeobfuscator createDeobfuscator() {
return new Deobfuscator(new Deobfuscator.Options {
ValidNameRegex = validNameRegex.get(),
RemoveTamperProtection = removeTamperProtection.get(),
2012-07-11 08:18:01 +08:00
DecryptConstants = decryptConstants.get(),
});
}
protected override IEnumerable<Option> getOptionsInternal() {
return new List<Option>() {
removeTamperProtection,
2012-07-11 08:18:01 +08:00
decryptConstants,
};
}
}
class Deobfuscator : DeobfuscatorBase {
Options options;
2012-04-08 17:36:24 +08:00
string obfuscatorName = DeobfuscatorInfo.THE_NAME;
bool foundCryptoObfuscatorAttribute = false;
bool foundObfuscatedSymbols = false;
2012-03-14 03:27:41 +08:00
bool foundObfuscatorUserString = false;
2012-08-21 21:11:14 +08:00
MethodsDecrypter methodsDecrypter;
2012-05-29 17:13:39 +08:00
ProxyCallFixer proxyCallFixer;
ResourceDecrypter resourceDecrypter;
ResourceResolver resourceResolver;
AssemblyResolver assemblyResolver;
2011-10-22 23:13:28 +08:00
StringDecrypter stringDecrypter;
TamperDetection tamperDetection;
AntiDebugger antiDebugger;
2012-07-11 08:18:01 +08:00
ConstantsDecrypter constantsDecrypter;
Int32ValueInliner int32ValueInliner;
Int64ValueInliner int64ValueInliner;
SingleValueInliner singleValueInliner;
DoubleValueInliner doubleValueInliner;
internal class Options : OptionsBase {
public bool RemoveTamperProtection { get; set; }
2012-07-11 08:18:01 +08:00
public bool DecryptConstants { 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; }
}
public override string Name {
get { return obfuscatorName; }
}
public Deobfuscator(Options options)
: base(options) {
this.options = options;
StringFeatures = StringFeatures.AllowStaticDecryption | StringFeatures.AllowDynamicDecryption;
}
2012-11-09 05:24:13 +08:00
public override void init(ModuleDefMD module) {
base.init(module);
}
protected override int detectInternal() {
int val = 0;
2012-08-21 21:11:14 +08:00
int sum = toInt32(methodsDecrypter.Detected) +
toInt32(stringDecrypter.Detected) +
2012-02-29 07:13:57 +08:00
toInt32(tamperDetection.Detected) +
toInt32(proxyCallFixer.Detected) +
2012-07-11 08:18:01 +08:00
toInt32(constantsDecrypter.Detected);
2012-02-29 07:13:57 +08:00
if (sum > 0)
val += 100 + 10 * (sum - 1);
2012-03-14 03:27:41 +08:00
if (foundCryptoObfuscatorAttribute || foundObfuscatedSymbols || foundObfuscatorUserString)
2011-10-23 19:43:32 +08:00
val += 10;
return val;
}
protected override void scanForObfuscator() {
foreach (var type in module.Types) {
if (type.FullName == "CryptoObfuscator.ProtectedWithCryptoObfuscatorAttribute") {
foundCryptoObfuscatorAttribute = true;
addAttributeToBeRemoved(type, "Obfuscator attribute");
initializeVersion(type);
}
}
if (checkCryptoObfuscator())
foundObfuscatedSymbols = true;
2011-10-22 23:13:28 +08:00
2012-08-21 21:11:14 +08:00
methodsDecrypter = new MethodsDecrypter(module);
methodsDecrypter.find();
2012-05-29 17:13:39 +08:00
proxyCallFixer = new ProxyCallFixer(module);
proxyCallFixer.findDelegateCreator();
2011-10-22 23:13:28 +08:00
stringDecrypter = new StringDecrypter(module);
stringDecrypter.find();
tamperDetection = new TamperDetection(module);
tamperDetection.find();
2012-07-11 08:18:01 +08:00
constantsDecrypter = new ConstantsDecrypter(module);
constantsDecrypter.find();
2012-11-09 05:24:13 +08:00
foundObfuscatorUserString = Utils.StartsWith(module.ReadUserString(0x70000001), "\u0011\"3D9B94A98B-76A8-4810-B1A0-4BE7C4F9C98D", StringComparison.Ordinal);
}
void initializeVersion(TypeDef attr) {
var s = DotNetUtils.getCustomArgAsString(getAssemblyAttribute(attr), 0);
if (s == null)
return;
var val = Regex.Match(s, @"^Protected with (Crypto Obfuscator.*)$");
if (val.Groups.Count < 2)
return;
obfuscatorName = val.Groups[1].ToString();
return;
}
bool checkCryptoObfuscator() {
int matched = 0;
foreach (var type in module.Types) {
if (type.Namespace != "A")
continue;
2012-11-09 05:24:13 +08:00
if (Regex.IsMatch(type.Name.String, "^c[0-9a-f]{32}$"))
return true;
2012-11-09 05:24:13 +08:00
else if (Regex.IsMatch(type.Name.String, "^A[A-Z]*$")) {
if (++matched >= 10)
return true;
}
}
return false;
}
public override void deobfuscateBegin() {
base.deobfuscateBegin();
2011-12-21 13:39:56 +08:00
resourceDecrypter = new ResourceDecrypter(module, DeobfuscatedFile);
resourceResolver = new ResourceResolver(module, resourceDecrypter);
assemblyResolver = new AssemblyResolver(module);
resourceResolver.find();
assemblyResolver.find();
2011-10-22 23:13:28 +08:00
decryptResources();
stringDecrypter.init(resourceDecrypter);
2011-11-06 22:24:30 +08:00
if (stringDecrypter.Method != null) {
2012-07-28 10:22:17 +08:00
staticStringInliner.add(stringDecrypter.Method, (method, gim, args) => {
2011-10-22 23:13:28 +08:00
return stringDecrypter.decrypt((int)args[0]);
});
DeobfuscatedFile.stringDecryptersAdded();
2011-10-22 23:13:28 +08:00
}
2012-08-21 21:11:14 +08:00
methodsDecrypter.decrypt(resourceDecrypter);
if (methodsDecrypter.Detected) {
if (!assemblyResolver.Detected)
assemblyResolver.find();
if (!tamperDetection.Detected)
tamperDetection.find();
}
antiDebugger = new AntiDebugger(module, DeobfuscatedFile, this);
antiDebugger.find();
2012-07-11 08:18:01 +08:00
if (options.DecryptConstants) {
constantsDecrypter.init(resourceDecrypter);
int32ValueInliner = new Int32ValueInliner();
2012-07-28 10:22:17 +08:00
int32ValueInliner.add(constantsDecrypter.Int32Decrypter, (method, gim, args) => constantsDecrypter.decryptInt32((int)args[0]));
2012-07-11 08:18:01 +08:00
int64ValueInliner = new Int64ValueInliner();
2012-07-28 10:22:17 +08:00
int64ValueInliner.add(constantsDecrypter.Int64Decrypter, (method, gim, args) => constantsDecrypter.decryptInt64((int)args[0]));
2012-07-11 08:18:01 +08:00
singleValueInliner = new SingleValueInliner();
2012-07-28 10:22:17 +08:00
singleValueInliner.add(constantsDecrypter.SingleDecrypter, (method, gim, args) => constantsDecrypter.decryptSingle((int)args[0]));
2012-07-11 08:18:01 +08:00
doubleValueInliner = new DoubleValueInliner();
2012-07-28 10:22:17 +08:00
doubleValueInliner.add(constantsDecrypter.DoubleDecrypter, (method, gim, args) => constantsDecrypter.decryptDouble((int)args[0]));
2012-07-11 08:18:01 +08:00
addTypeToBeRemoved(constantsDecrypter.Type, "Constants decrypter type");
addResourceToBeRemoved(constantsDecrypter.Resource, "Encrypted constants");
}
2011-11-06 22:24:30 +08:00
addModuleCctorInitCallToBeRemoved(resourceResolver.Method);
addModuleCctorInitCallToBeRemoved(assemblyResolver.Method);
addCallToBeRemoved(module.EntryPoint, tamperDetection.Method);
2011-12-15 23:16:21 +08:00
addModuleCctorInitCallToBeRemoved(tamperDetection.Method);
2011-11-06 22:24:30 +08:00
addCallToBeRemoved(module.EntryPoint, antiDebugger.Method);
2011-12-15 23:16:21 +08:00
addModuleCctorInitCallToBeRemoved(antiDebugger.Method);
2011-11-06 22:24:30 +08:00
addTypeToBeRemoved(resourceResolver.Type, "Resource resolver type");
addTypeToBeRemoved(assemblyResolver.Type, "Assembly resolver type");
addTypeToBeRemoved(tamperDetection.Type, "Tamper detection type");
addTypeToBeRemoved(antiDebugger.Type, "Anti-debugger type");
2012-08-21 21:11:14 +08:00
addTypeToBeRemoved(methodsDecrypter.Type, "Methods decrypter type");
addTypesToBeRemoved(methodsDecrypter.DelegateTypes, "Methods decrypter delegate type");
addResourceToBeRemoved(methodsDecrypter.Resource, "Encrypted methods");
2011-10-23 15:03:00 +08:00
2012-05-29 17:13:39 +08:00
proxyCallFixer.find();
2011-10-23 19:43:32 +08:00
dumpEmbeddedAssemblies();
}
2011-10-23 19:43:32 +08:00
public override void deobfuscateMethodEnd(Blocks blocks) {
2012-05-29 17:13:39 +08:00
proxyCallFixer.deobfuscate(blocks);
2012-07-11 08:18:01 +08:00
if (options.DecryptConstants) {
int32ValueInliner.decrypt(blocks);
int64ValueInliner.decrypt(blocks);
singleValueInliner.decrypt(blocks);
doubleValueInliner.decrypt(blocks);
}
2011-10-23 19:43:32 +08:00
base.deobfuscateMethodEnd(blocks);
}
public override void deobfuscateEnd() {
2012-05-29 17:13:39 +08:00
removeProxyDelegates(proxyCallFixer);
if (CanRemoveStringDecrypterType) {
addResourceToBeRemoved(stringDecrypter.Resource, "Encrypted strings");
addTypeToBeRemoved(stringDecrypter.Type, "String decrypter type");
}
2011-10-23 19:43:32 +08:00
base.deobfuscateEnd();
}
2011-10-22 23:13:28 +08:00
void decryptResources() {
var rsrc = resourceResolver.mergeResources();
if (rsrc == null)
return;
addResourceToBeRemoved(rsrc, "Encrypted resources");
}
void dumpEmbeddedAssemblies() {
foreach (var info in assemblyResolver.AssemblyInfos) {
2011-10-23 23:23:33 +08:00
dumpEmbeddedFile(info.resource, info.assemblyName, ".dll", string.Format("Embedded assembly: {0}", info.assemblyName));
if (info.symbolsResource != null)
2011-10-23 23:23:33 +08:00
dumpEmbeddedFile(info.symbolsResource, info.assemblyName, ".pdb", string.Format("Embedded pdb: {0}", info.assemblyName));
}
}
2011-10-23 23:23:33 +08:00
void dumpEmbeddedFile(EmbeddedResource resource, string assemblyName, string extension, string reason) {
DeobfuscatedFile.createAssemblyFile(resourceDecrypter.decrypt(resource.GetResourceStream()), Utils.getAssemblySimpleName(assemblyName), extension);
2011-10-22 23:13:28 +08:00
addResourceToBeRemoved(resource, reason);
}
2012-02-25 13:25:40 +08:00
public override IEnumerable<int> getStringDecrypterMethods() {
var list = new List<int>();
2011-11-06 22:24:30 +08:00
if (stringDecrypter.Method != null)
list.Add(stringDecrypter.Method.MDToken.ToInt32());
2011-10-22 23:13:28 +08:00
return list;
}
}
}