2011-09-22 10:55:30 +08:00
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
2011-09-22 10:55:30 +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/>.
|
|
|
|
*/
|
|
|
|
|
2011-11-06 19:19:26 +08:00
|
|
|
using System.Collections.Generic;
|
2011-09-22 10:55:30 +08:00
|
|
|
using Mono.Cecil;
|
2011-11-06 19:19:26 +08:00
|
|
|
using Mono.MyStuff;
|
2011-09-22 10:55:30 +08:00
|
|
|
using de4dot.blocks;
|
2011-12-09 16:02:06 +08:00
|
|
|
using de4dot.code.PE;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
2011-12-09 16:02:06 +08:00
|
|
|
namespace de4dot.code.deobfuscators.CliSecure {
|
|
|
|
public class DeobfuscatorInfo : DeobfuscatorInfoBase {
|
2011-11-04 03:03:32 +08:00
|
|
|
public const string THE_NAME = "CliSecure";
|
2011-11-12 18:31:07 +08:00
|
|
|
public const string THE_TYPE = "cs";
|
2011-09-22 10:55:30 +08:00
|
|
|
const string DEFAULT_REGEX = @"[a-zA-Z_0-9>}$]$";
|
2011-11-06 19:19:26 +08:00
|
|
|
BoolOption decryptMethods;
|
2011-11-07 01:01:37 +08:00
|
|
|
BoolOption decryptResources;
|
2011-09-22 10:55:30 +08:00
|
|
|
BoolOption removeStackFrameHelper;
|
|
|
|
|
|
|
|
public DeobfuscatorInfo()
|
2011-11-04 03:03:32 +08:00
|
|
|
: base(DEFAULT_REGEX) {
|
2011-11-06 19:19:26 +08:00
|
|
|
decryptMethods = new BoolOption(null, makeArgName("methods"), "Decrypt methods", true);
|
2011-11-07 01:01:37 +08:00
|
|
|
decryptResources = new BoolOption(null, makeArgName("rsrc"), "Decrypt resources", true);
|
2011-09-22 10:55:30 +08:00
|
|
|
removeStackFrameHelper = new BoolOption(null, makeArgName("stack"), "Remove all StackFrameHelper code", true);
|
|
|
|
}
|
|
|
|
|
2011-11-04 03:03:32 +08:00
|
|
|
public override string Name {
|
|
|
|
get { return THE_NAME; }
|
2011-09-22 10:55:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override string Type {
|
2011-11-12 18:31:07 +08:00
|
|
|
get { return THE_TYPE; }
|
2011-09-22 10:55:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override IDeobfuscator createDeobfuscator() {
|
|
|
|
return new Deobfuscator(new Deobfuscator.Options {
|
|
|
|
ValidNameRegex = validNameRegex.get(),
|
2011-11-06 19:19:26 +08:00
|
|
|
DecryptMethods = decryptMethods.get(),
|
2011-11-07 01:01:37 +08:00
|
|
|
DecryptResources = decryptResources.get(),
|
2011-09-22 10:55:30 +08:00
|
|
|
RemoveStackFrameHelper = removeStackFrameHelper.get(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override IEnumerable<Option> getOptionsInternal() {
|
|
|
|
return new List<Option>() {
|
2011-11-06 19:19:26 +08:00
|
|
|
decryptMethods,
|
2011-11-07 01:01:37 +08:00
|
|
|
decryptResources,
|
2011-09-22 10:55:30 +08:00
|
|
|
removeStackFrameHelper,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Deobfuscator : DeobfuscatorBase {
|
|
|
|
Options options;
|
|
|
|
|
2011-11-06 19:19:26 +08:00
|
|
|
TypeDefinition cliSecureAttribute;
|
2011-09-22 10:55:30 +08:00
|
|
|
ProxyDelegateFinder proxyDelegateFinder;
|
2011-11-06 19:19:26 +08:00
|
|
|
CliSecureRtType cliSecureRtType;
|
|
|
|
StringDecrypter stringDecrypter;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
2011-11-06 19:19:26 +08:00
|
|
|
ResourceDecrypter resourceDecrypter;
|
|
|
|
StackFrameHelper stackFrameHelper;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
|
|
internal class Options : OptionsBase {
|
2011-11-06 19:19:26 +08:00
|
|
|
public bool DecryptMethods { get; set; }
|
2011-11-07 01:01:37 +08:00
|
|
|
public bool DecryptResources { get; set; }
|
2011-09-22 10:55:30 +08:00
|
|
|
public bool RemoveStackFrameHelper { 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; }
|
2011-09-22 10:55:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override string Name {
|
2011-11-12 18:31:07 +08:00
|
|
|
get { return TypeLong; }
|
2011-09-22 10:55:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public Deobfuscator(Options options)
|
|
|
|
: base(options) {
|
|
|
|
this.options = options;
|
|
|
|
}
|
|
|
|
|
2011-10-09 01:33:12 +08:00
|
|
|
public override void init(ModuleDefinition module) {
|
|
|
|
base.init(module);
|
2011-09-22 10:55:30 +08:00
|
|
|
}
|
|
|
|
|
2011-10-26 20:29:12 +08:00
|
|
|
protected override int detectInternal() {
|
2011-09-22 10:55:30 +08:00
|
|
|
int val = 0;
|
|
|
|
|
2011-11-06 19:19:26 +08:00
|
|
|
if (cliSecureRtType.Detected || cliSecureAttribute != null)
|
2011-09-22 10:55:30 +08:00
|
|
|
val += 100;
|
2011-11-06 19:19:26 +08:00
|
|
|
if (stringDecrypter.Detected)
|
2011-09-22 10:55:30 +08:00
|
|
|
val += 10;
|
2011-11-06 19:19:26 +08:00
|
|
|
if (proxyDelegateFinder.Detected)
|
2011-09-22 10:55:30 +08:00
|
|
|
val += 10;
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2011-10-26 20:29:12 +08:00
|
|
|
protected override void scanForObfuscator() {
|
2011-09-22 10:55:30 +08:00
|
|
|
findCliSecureAttribute();
|
2011-11-06 19:19:26 +08:00
|
|
|
cliSecureRtType = new CliSecureRtType(module);
|
|
|
|
cliSecureRtType.find();
|
|
|
|
stringDecrypter = new StringDecrypter(module, cliSecureRtType.StringDecrypterMethod);
|
|
|
|
stringDecrypter.find();
|
|
|
|
proxyDelegateFinder = new ProxyDelegateFinder(module);
|
|
|
|
proxyDelegateFinder.findDelegateCreator();
|
2011-09-22 10:55:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void findCliSecureAttribute() {
|
|
|
|
foreach (var type in module.Types) {
|
|
|
|
if (type.FullName == "SecureTeam.Attributes.ObfuscatedByCliSecureAttribute") {
|
2011-11-06 19:19:26 +08:00
|
|
|
cliSecureAttribute = type;
|
2011-09-22 10:55:30 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-06 19:19:26 +08:00
|
|
|
public override bool getDecryptedModule(ref byte[] newFileData, ref Dictionary<uint, DumpedMethod> dumpedMethods) {
|
|
|
|
if (!options.DecryptMethods)
|
|
|
|
return false;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
2011-11-06 20:46:50 +08:00
|
|
|
byte[] fileData = DeobUtils.readModule(module);
|
2011-12-01 01:27:01 +08:00
|
|
|
var peImage = new PeImage(fileData);
|
2011-09-22 10:55:30 +08:00
|
|
|
|
2011-11-07 23:16:18 +08:00
|
|
|
if (!new MethodsDecrypter().decrypt(peImage, ref dumpedMethods)) {
|
|
|
|
Log.v("Methods aren't encrypted or invalid signature");
|
2011-11-06 19:19:26 +08:00
|
|
|
return false;
|
2011-11-07 23:16:18 +08:00
|
|
|
}
|
2011-11-06 19:19:26 +08:00
|
|
|
|
|
|
|
newFileData = fileData;
|
|
|
|
return true;
|
2011-09-22 10:55:30 +08:00
|
|
|
}
|
|
|
|
|
2011-11-06 19:19:26 +08:00
|
|
|
public override IDeobfuscator moduleReloaded(ModuleDefinition module) {
|
|
|
|
var newOne = new Deobfuscator(options);
|
|
|
|
newOne.setModule(module);
|
|
|
|
newOne.cliSecureAttribute = DeobUtils.lookup(module, cliSecureAttribute, "Could not find CliSecure attribute");
|
|
|
|
newOne.cliSecureRtType = new CliSecureRtType(module, cliSecureRtType);
|
|
|
|
newOne.stringDecrypter = new StringDecrypter(module, stringDecrypter);
|
|
|
|
newOne.proxyDelegateFinder = new ProxyDelegateFinder(module, proxyDelegateFinder);
|
|
|
|
return newOne;
|
2011-09-22 10:55:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void deobfuscateBegin() {
|
|
|
|
base.deobfuscateBegin();
|
|
|
|
|
2011-11-06 19:19:26 +08:00
|
|
|
addAttributeToBeRemoved(cliSecureAttribute, "Obfuscator attribute");
|
|
|
|
|
|
|
|
resourceDecrypter = new ResourceDecrypter(module);
|
|
|
|
resourceDecrypter.find();
|
|
|
|
stackFrameHelper = new StackFrameHelper(module);
|
|
|
|
stackFrameHelper.find();
|
|
|
|
|
2011-09-22 10:55:30 +08:00
|
|
|
foreach (var type in module.Types) {
|
2011-11-04 07:39:48 +08:00
|
|
|
if (type.FullName == "InitializeDelegate" && DotNetUtils.derivesFromDelegate(type))
|
2011-09-22 10:55:30 +08:00
|
|
|
this.addTypeToBeRemoved(type, "Obfuscator type");
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyDelegateFinder.find();
|
|
|
|
|
2011-11-06 22:24:30 +08:00
|
|
|
staticStringDecrypter.add(stringDecrypter.Method, (method, args) => stringDecrypter.decrypt((string)args[0]));
|
2011-09-22 10:55:30 +08:00
|
|
|
|
2011-11-07 01:01:37 +08:00
|
|
|
if (options.DecryptMethods) {
|
|
|
|
addCctorInitCallToBeRemoved(cliSecureRtType.InitializeMethod);
|
|
|
|
addCctorInitCallToBeRemoved(cliSecureRtType.PostInitializeMethod);
|
|
|
|
findPossibleNamesToRemove(cliSecureRtType.LoadMethod);
|
|
|
|
}
|
|
|
|
if (options.DecryptResources)
|
2011-11-06 19:19:26 +08:00
|
|
|
addCctorInitCallToBeRemoved(resourceDecrypter.RsrcRrrMethod);
|
2011-09-22 10:55:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void decryptResources() {
|
2011-11-06 19:19:26 +08:00
|
|
|
var rsrc = resourceDecrypter.mergeResources();
|
|
|
|
if (rsrc == null)
|
2011-09-22 10:55:30 +08:00
|
|
|
return;
|
2011-11-10 21:48:33 +08:00
|
|
|
addResourceToBeRemoved(rsrc, "Encrypted resources");
|
|
|
|
addTypeToBeRemoved(resourceDecrypter.Type, "Resource decrypter type");
|
2011-09-22 10:55:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void deobfuscateMethodEnd(Blocks blocks) {
|
|
|
|
proxyDelegateFinder.deobfuscate(blocks);
|
|
|
|
removeStackFrameHelperCode(blocks);
|
|
|
|
base.deobfuscateMethodEnd(blocks);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void deobfuscateEnd() {
|
2011-11-07 01:01:37 +08:00
|
|
|
if (options.DecryptResources)
|
2011-09-22 10:55:30 +08:00
|
|
|
decryptResources();
|
|
|
|
removeProxyDelegates(proxyDelegateFinder);
|
2011-11-06 19:19:26 +08:00
|
|
|
if (options.RemoveStackFrameHelper) {
|
|
|
|
if (stackFrameHelper.ExceptionLoggerRemover.NumRemovedExceptionLoggers > 0)
|
|
|
|
addTypeToBeRemoved(stackFrameHelper.Type, "StackFrameHelper type");
|
|
|
|
}
|
2011-11-07 01:01:37 +08:00
|
|
|
if (Operations.DecryptStrings != OpDecryptString.None) {
|
2011-11-10 21:48:33 +08:00
|
|
|
addTypeToBeRemoved(stringDecrypter.Type, "String decrypter type");
|
2011-11-07 01:01:37 +08:00
|
|
|
if (options.DecryptMethods)
|
|
|
|
addTypeToBeRemoved(cliSecureRtType.Type, "Obfuscator type");
|
|
|
|
}
|
|
|
|
if (options.DecryptMethods) {
|
|
|
|
addResources("Obfuscator protection files");
|
|
|
|
addModuleReferences("Obfuscator protection files");
|
|
|
|
}
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
|
|
base.deobfuscateEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override IEnumerable<string> getStringDecrypterMethods() {
|
|
|
|
var list = new List<string>();
|
2011-11-06 22:24:30 +08:00
|
|
|
if (stringDecrypter.Method != null)
|
|
|
|
list.Add(stringDecrypter.Method.MetadataToken.ToInt32().ToString("X8"));
|
2011-09-22 10:55:30 +08:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeStackFrameHelperCode(Blocks blocks) {
|
2011-12-02 22:20:09 +08:00
|
|
|
if (!options.RemoveStackFrameHelper)
|
|
|
|
return;
|
2011-11-06 19:19:26 +08:00
|
|
|
if (stackFrameHelper.ExceptionLoggerRemover.remove(blocks))
|
2011-09-22 10:55:30 +08:00
|
|
|
Log.v("Removed StackFrameHelper code");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|