2012-02-05 23:17:47 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2012-02-05 23:17:47 +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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-02-07 22:05:27 +08:00
|
|
|
|
using System;
|
2012-02-05 23:17:47 +08:00
|
|
|
|
using System.Collections.Generic;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
2012-02-12 06:12:42 +08:00
|
|
|
|
using de4dot.blocks;
|
2012-02-05 23:17:47 +08:00
|
|
|
|
|
2012-02-07 11:45:59 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.CodeVeil {
|
2012-02-05 23:17:47 +08:00
|
|
|
|
public class DeobfuscatorInfo : DeobfuscatorInfoBase {
|
|
|
|
|
public const string THE_NAME = "CodeVeil";
|
2012-02-07 11:45:59 +08:00
|
|
|
|
public const string THE_TYPE = "cv";
|
2013-11-18 22:43:08 +08:00
|
|
|
|
const string DEFAULT_REGEX = @"!^[A-Za-z]{1,2}$&" + DeobfuscatorBase.DEFAULT_ASIAN_VALID_NAME_REGEX;
|
2012-02-05 23:17:47 +08:00
|
|
|
|
|
|
|
|
|
public DeobfuscatorInfo()
|
2012-02-06 15:20:04 +08:00
|
|
|
|
: base(DEFAULT_REGEX) {
|
2012-02-05 23:17:47 +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-05 23:17:47 +08:00
|
|
|
|
return new Deobfuscator(new Deobfuscator.Options {
|
2013-11-18 23:28:44 +08:00
|
|
|
|
ValidNameRegex = validNameRegex.Get(),
|
2012-02-05 23:17:47 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override IEnumerable<Option> GetOptionsInternal() {
|
2012-02-05 23:17:47 +08:00
|
|
|
|
return new List<Option>() {
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Deobfuscator : DeobfuscatorBase {
|
|
|
|
|
Options options;
|
2012-02-07 11:45:59 +08:00
|
|
|
|
string obfuscatorName = DeobfuscatorInfo.THE_NAME;
|
2012-02-05 23:17:47 +08:00
|
|
|
|
|
2012-02-08 22:40:11 +08:00
|
|
|
|
MainType mainType;
|
2012-02-05 23:17:47 +08:00
|
|
|
|
MethodsDecrypter methodsDecrypter;
|
2012-05-29 17:13:39 +08:00
|
|
|
|
ProxyCallFixer proxyCallFixer;
|
2012-02-06 01:47:31 +08:00
|
|
|
|
StringDecrypter stringDecrypter;
|
2012-02-08 19:06:14 +08:00
|
|
|
|
AssemblyResolver assemblyResolver;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
TypeDef killType;
|
2012-02-12 06:12:42 +08:00
|
|
|
|
ResourceDecrypter resourceDecrypter;
|
2012-02-05 23:17:47 +08:00
|
|
|
|
|
|
|
|
|
internal class Options : OptionsBase {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Deobfuscator(Options options)
|
|
|
|
|
: base(options) {
|
|
|
|
|
this.options = options;
|
2012-02-09 01:40:24 +08:00
|
|
|
|
StringFeatures = StringFeatures.AllowStaticDecryption | StringFeatures.AllowDynamicDecryption;
|
2012-02-05 23:17:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override int DetectInternal() {
|
2012-02-05 23:17:47 +08:00
|
|
|
|
int val = 0;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
int sum = ToInt32(mainType.Detected) +
|
|
|
|
|
ToInt32(methodsDecrypter.Detected) +
|
|
|
|
|
ToInt32(stringDecrypter.Detected) +
|
|
|
|
|
ToInt32(proxyCallFixer.Detected);
|
2012-02-05 23:17:47 +08:00
|
|
|
|
if (sum > 0)
|
|
|
|
|
val += 100 + 10 * (sum - 1);
|
|
|
|
|
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override void ScanForObfuscator() {
|
|
|
|
|
FindKillType();
|
2012-02-08 22:40:11 +08:00
|
|
|
|
mainType = new MainType(module);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
mainType.Find();
|
2012-05-29 17:13:39 +08:00
|
|
|
|
proxyCallFixer = new ProxyCallFixer(module, mainType);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
proxyCallFixer.FindDelegateCreator();
|
2012-02-08 22:40:11 +08:00
|
|
|
|
methodsDecrypter = new MethodsDecrypter(mainType);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
methodsDecrypter.Find();
|
2012-02-08 22:40:11 +08:00
|
|
|
|
stringDecrypter = new StringDecrypter(module, mainType);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
stringDecrypter.Find();
|
|
|
|
|
var version = DetectVersion();
|
2012-02-07 22:05:27 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(version))
|
|
|
|
|
obfuscatorName = obfuscatorName + " " + version;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
string DetectVersion() {
|
2012-02-08 22:40:11 +08:00
|
|
|
|
if (mainType.Detected) {
|
|
|
|
|
switch (mainType.Version) {
|
2012-02-08 15:55:45 +08:00
|
|
|
|
case ObfuscatorVersion.Unknown:
|
2012-02-08 15:50:36 +08:00
|
|
|
|
return null;
|
2012-02-07 22:05:27 +08:00
|
|
|
|
|
2012-02-08 15:55:45 +08:00
|
|
|
|
case ObfuscatorVersion.V3:
|
2012-02-08 15:50:36 +08:00
|
|
|
|
return "3.x";
|
2012-02-07 22:05:27 +08:00
|
|
|
|
|
2012-02-08 15:55:45 +08:00
|
|
|
|
case ObfuscatorVersion.V4_0:
|
2012-02-08 15:50:36 +08:00
|
|
|
|
return "4.0";
|
2012-02-07 22:05:27 +08:00
|
|
|
|
|
2012-02-08 15:55:45 +08:00
|
|
|
|
case ObfuscatorVersion.V4_1:
|
2012-02-08 15:50:36 +08:00
|
|
|
|
return "4.1";
|
2012-02-07 22:05:27 +08:00
|
|
|
|
|
2012-02-08 15:55:45 +08:00
|
|
|
|
case ObfuscatorVersion.V5_0:
|
2012-02-08 15:50:36 +08:00
|
|
|
|
return "5.0";
|
2012-02-07 22:05:27 +08:00
|
|
|
|
|
2012-02-08 15:50:36 +08:00
|
|
|
|
default:
|
|
|
|
|
throw new ApplicationException("Unknown version");
|
|
|
|
|
}
|
2012-02-07 22:05:27 +08:00
|
|
|
|
}
|
2012-02-08 15:50:36 +08:00
|
|
|
|
|
|
|
|
|
return null;
|
2012-02-05 23:17:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void FindKillType() {
|
2012-02-05 23:17:47 +08:00
|
|
|
|
foreach (var type in module.Types) {
|
|
|
|
|
if (type.FullName == "____KILL") {
|
2012-02-09 02:54:05 +08:00
|
|
|
|
killType = type;
|
2012-02-05 23:17:47 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override bool GetDecryptedModule(int count, ref byte[] newFileData, ref DumpedMethods dumpedMethods) {
|
2012-07-24 23:02:27 +08:00
|
|
|
|
if (count != 0 || !methodsDecrypter.Detected)
|
2012-02-05 23:17:47 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var fileData = DeobUtils.ReadModule(module);
|
|
|
|
|
if (!methodsDecrypter.Decrypt(fileData, ref dumpedMethods))
|
2012-02-05 23:17:47 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
newFileData = fileData;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override IDeobfuscator ModuleReloaded(ModuleDefMD module) {
|
2012-02-05 23:17:47 +08:00
|
|
|
|
var newOne = new Deobfuscator(options);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
newOne.SetModule(module);
|
2012-02-08 22:40:11 +08:00
|
|
|
|
newOne.mainType = new MainType(module, mainType);
|
|
|
|
|
newOne.methodsDecrypter = new MethodsDecrypter(mainType, methodsDecrypter);
|
|
|
|
|
newOne.stringDecrypter = new StringDecrypter(module, newOne.mainType, stringDecrypter);
|
2012-05-29 17:13:39 +08:00
|
|
|
|
newOne.proxyCallFixer = new ProxyCallFixer(module, newOne.mainType, proxyCallFixer);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
newOne.killType = DeobUtils.Lookup(module, killType, "Could not find KILL type");
|
2012-02-05 23:17:47 +08:00
|
|
|
|
return newOne;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override void DeobfuscateBegin() {
|
|
|
|
|
base.DeobfuscateBegin();
|
2012-02-05 23:17:47 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
AddTypeToBeRemoved(killType, "KILL type");
|
2012-02-09 02:54:05 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
mainType.Initialize();
|
2012-02-09 02:40:17 +08:00
|
|
|
|
foreach (var initMethod in mainType.OtherInitMethods) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
AddCctorInitCallToBeRemoved(initMethod);
|
|
|
|
|
AddCtorInitCallToBeRemoved(initMethod);
|
2012-02-09 02:40:17 +08:00
|
|
|
|
}
|
2012-02-09 01:40:24 +08:00
|
|
|
|
|
2012-02-06 01:47:31 +08:00
|
|
|
|
if (Operations.DecryptStrings != OpDecryptString.None) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
stringDecrypter.Initialize();
|
|
|
|
|
staticStringInliner.Add(stringDecrypter.DecryptMethod, (method, gim, args) => {
|
|
|
|
|
return stringDecrypter.Decrypt((int)args[0]);
|
2012-02-06 01:47:31 +08:00
|
|
|
|
});
|
2013-01-19 20:03:57 +08:00
|
|
|
|
DeobfuscatedFile.StringDecryptersAdded();
|
|
|
|
|
AddModuleCctorInitCallToBeRemoved(stringDecrypter.InitMethod);
|
|
|
|
|
AddCallToBeRemoved(mainType.GetInitStringDecrypterMethod(stringDecrypter.InitMethod), stringDecrypter.InitMethod);
|
2012-02-06 01:47:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-08 19:06:14 +08:00
|
|
|
|
assemblyResolver = new AssemblyResolver(module);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
assemblyResolver.Initialize();
|
|
|
|
|
DumpEmbeddedAssemblies();
|
2012-02-08 19:06:14 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
RemoveTamperDetection();
|
2012-02-09 01:40:24 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
proxyCallFixer.Initialize();
|
|
|
|
|
proxyCallFixer.Find();
|
2012-02-11 23:46:39 +08:00
|
|
|
|
|
2012-02-12 06:12:42 +08:00
|
|
|
|
resourceDecrypter = new ResourceDecrypter(module);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
resourceDecrypter.Initialize();
|
|
|
|
|
resourceDecrypter.Decrypt();
|
2012-02-12 06:12:42 +08:00
|
|
|
|
if (resourceDecrypter.CanRemoveTypes) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
AddTypeToBeRemoved(resourceDecrypter.ResourceFlagsType, "Obfuscator ResourceFlags type");
|
|
|
|
|
AddTypeToBeRemoved(resourceDecrypter.ResType, "Obfuscator Res type");
|
|
|
|
|
AddTypeToBeRemoved(resourceDecrypter.ResourceEnumeratorType, "Obfuscator ResourceEnumerator type");
|
|
|
|
|
AddTypeToBeRemoved(resourceDecrypter.EncryptedResourceReaderType, "Obfuscator EncryptedResourceReader type");
|
|
|
|
|
AddTypeToBeRemoved(resourceDecrypter.EncryptedResourceSetType, "Obfuscator EncryptedResourceSet type");
|
|
|
|
|
AddTypeToBeRemoved(resourceDecrypter.EncryptedResourceStreamType, "Obfuscator EncryptedResourceStream type");
|
2012-02-12 06:12:42 +08:00
|
|
|
|
}
|
2012-02-05 23:17:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void RemoveTamperDetection() {
|
2012-02-09 01:40:24 +08:00
|
|
|
|
var tamperDetection = new TamperDetection(module, mainType);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
tamperDetection.Initialize();
|
2012-02-09 01:40:24 +08:00
|
|
|
|
foreach (var tamperDetectionMethod in tamperDetection.Methods)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
AddCctorInitCallToBeRemoved(tamperDetectionMethod);
|
|
|
|
|
AddTypeToBeRemoved(tamperDetection.Type, "Tamper detection type");
|
2012-02-09 01:40:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void DumpEmbeddedAssemblies() {
|
2012-02-08 19:06:14 +08:00
|
|
|
|
foreach (var info in assemblyResolver.AssemblyInfos)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
DeobfuscatedFile.CreateAssemblyFile(info.data, info.simpleName, info.extension);
|
|
|
|
|
AddResourceToBeRemoved(assemblyResolver.BundleDataResource, "Embedded assemblies resource");
|
|
|
|
|
AddResourceToBeRemoved(assemblyResolver.BundleXmlFileResource, "Embedded assemblies XML file resource");
|
|
|
|
|
AddTypesToBeRemoved(assemblyResolver.BundleTypes, "Obfuscator assembly bundle types");
|
2012-02-08 19:06:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override void DeobfuscateMethodBegin(Blocks blocks) {
|
|
|
|
|
proxyCallFixer.Deobfuscate(blocks);
|
|
|
|
|
base.DeobfuscateMethodBegin(blocks);
|
2012-02-07 11:45:59 +08:00
|
|
|
|
}
|
2012-02-05 23:17:47 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override void DeobfuscateMethodEnd(Blocks blocks) {
|
|
|
|
|
mainType.RemoveInitCall(blocks);
|
|
|
|
|
resourceDecrypter.Deobfuscate(blocks);
|
|
|
|
|
base.DeobfuscateMethodEnd(blocks);
|
2012-02-09 02:20:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override void DeobfuscateEnd() {
|
2012-05-29 17:13:39 +08:00
|
|
|
|
bool canRemoveProxyTypes = proxyCallFixer.CanRemoveTypes;
|
2012-02-12 20:00:38 +08:00
|
|
|
|
|
2012-02-21 23:56:29 +08:00
|
|
|
|
if (CanRemoveStringDecrypterType)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
AddTypeToBeRemoved(stringDecrypter.Type, "String decrypter type");
|
2012-02-21 23:56:29 +08:00
|
|
|
|
|
2012-02-12 20:00:38 +08:00
|
|
|
|
if (!mainType.Detected) {
|
|
|
|
|
}
|
|
|
|
|
else if (mainType.Version >= ObfuscatorVersion.V5_0) {
|
2012-05-29 17:13:39 +08:00
|
|
|
|
if (!proxyCallFixer.FoundProxyType || canRemoveProxyTypes)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
AddTypeToBeRemoved(mainType.Type, "Main CV type");
|
2012-02-12 20:00:38 +08:00
|
|
|
|
}
|
|
|
|
|
else {
|
2012-02-12 21:35:40 +08:00
|
|
|
|
var type = mainType.Type;
|
|
|
|
|
if (!type.HasNestedTypes && !type.HasProperties && !type.HasEvents && !type.HasFields)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
AddTypeToBeRemoved(type, "Main CV type");
|
2012-02-12 21:35:40 +08:00
|
|
|
|
else {
|
|
|
|
|
foreach (var method in type.Methods)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
AddMethodToBeRemoved(method, "CV main type method");
|
2012-02-12 21:35:40 +08:00
|
|
|
|
}
|
2012-02-12 20:00:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
RemoveProxyDelegates(proxyCallFixer, canRemoveProxyTypes);
|
2012-02-12 20:00:38 +08:00
|
|
|
|
if (canRemoveProxyTypes) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
AddTypeToBeRemoved(proxyCallFixer.IlGeneratorType, "Obfuscator proxy method ILGenerator type");
|
|
|
|
|
AddTypeToBeRemoved(proxyCallFixer.FieldInfoType, "Obfuscator proxy method FieldInfo type");
|
|
|
|
|
AddTypeToBeRemoved(proxyCallFixer.MethodInfoType, "Obfuscator proxy method MethodInfo type");
|
2012-02-12 20:00:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
AddMethodsToBeRemoved(InvalidMethodsFinder.FindAll(module), "Anti-reflection method");
|
2012-02-12 23:29:29 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
base.DeobfuscateEnd();
|
2012-02-05 23:17:47 +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-06 01:59:29 +08:00
|
|
|
|
if (stringDecrypter.DecryptMethod != null)
|
2012-11-02 22:57:11 +08:00
|
|
|
|
list.Add(stringDecrypter.DecryptMethod.MDToken.ToInt32());
|
2012-02-05 23:17:47 +08:00
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|