2011-12-21 04:47:45 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
2011-12-21 04:47:45 +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;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
using dot10.DotNet;
|
|
|
|
|
using dot10.DotNet.Emit;
|
2011-12-21 04:47:45 +08:00
|
|
|
|
using Mono.MyStuff;
|
|
|
|
|
using de4dot.blocks;
|
2012-04-10 22:32:15 +08:00
|
|
|
|
using de4dot.PE;
|
2011-12-21 04:47:45 +08:00
|
|
|
|
|
2011-12-22 01:55:36 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 {
|
2011-12-21 04:47:45 +08:00
|
|
|
|
public class DeobfuscatorInfo : DeobfuscatorInfoBase {
|
|
|
|
|
public const string THE_NAME = ".NET Reactor";
|
|
|
|
|
public const string THE_TYPE = "dr3";
|
|
|
|
|
const string DEFAULT_REGEX = DeobfuscatorBase.DEFAULT_VALID_NAME_REGEX;
|
|
|
|
|
BoolOption restoreTypes;
|
|
|
|
|
BoolOption inlineMethods;
|
|
|
|
|
BoolOption removeInlinedMethods;
|
|
|
|
|
BoolOption removeNamespaces;
|
|
|
|
|
BoolOption removeAntiStrongName;
|
|
|
|
|
|
|
|
|
|
public DeobfuscatorInfo()
|
|
|
|
|
: base(DEFAULT_REGEX) {
|
|
|
|
|
restoreTypes = new BoolOption(null, makeArgName("types"), "Restore types (object -> real type)", true);
|
|
|
|
|
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);
|
|
|
|
|
removeAntiStrongName = new BoolOption(null, makeArgName("sn"), "Remove anti strong name code", true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Name {
|
|
|
|
|
get { return THE_NAME; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Type {
|
|
|
|
|
get { return THE_TYPE; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override IDeobfuscator createDeobfuscator() {
|
|
|
|
|
return new Deobfuscator(new Deobfuscator.Options {
|
|
|
|
|
ValidNameRegex = validNameRegex.get(),
|
|
|
|
|
RestoreTypes = restoreTypes.get(),
|
|
|
|
|
InlineMethods = inlineMethods.get(),
|
|
|
|
|
RemoveInlinedMethods = removeInlinedMethods.get(),
|
|
|
|
|
RemoveNamespaces = removeNamespaces.get(),
|
|
|
|
|
RemoveAntiStrongName = removeAntiStrongName.get(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override IEnumerable<Option> getOptionsInternal() {
|
|
|
|
|
return new List<Option>() {
|
|
|
|
|
restoreTypes,
|
|
|
|
|
inlineMethods,
|
|
|
|
|
removeInlinedMethods,
|
|
|
|
|
removeNamespaces,
|
|
|
|
|
removeAntiStrongName,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Deobfuscator : DeobfuscatorBase {
|
|
|
|
|
Options options;
|
|
|
|
|
string obfuscatorName = DeobfuscatorInfo.THE_NAME;
|
|
|
|
|
|
2011-12-21 07:31:27 +08:00
|
|
|
|
DecrypterType decrypterType;
|
|
|
|
|
NativeLibSaver nativeLibSaver;
|
2011-12-22 01:04:49 +08:00
|
|
|
|
AntiStrongName antiStrongName;
|
2011-12-27 03:32:42 +08:00
|
|
|
|
LibAssemblyResolver libAssemblyResolver;
|
2011-12-21 04:47:45 +08:00
|
|
|
|
List<UnpackedFile> unpackedFiles = new List<UnpackedFile>();
|
|
|
|
|
|
|
|
|
|
bool unpackedNativeFile = false;
|
2011-12-21 07:31:27 +08:00
|
|
|
|
bool canRemoveDecrypterType = true;
|
2011-12-21 04:47:45 +08:00
|
|
|
|
bool startedDeobfuscating = false;
|
|
|
|
|
|
|
|
|
|
internal class Options : OptionsBase {
|
|
|
|
|
public bool RestoreTypes { get; set; }
|
|
|
|
|
public bool InlineMethods { get; set; }
|
|
|
|
|
public bool RemoveInlinedMethods { get; set; }
|
|
|
|
|
public bool RemoveNamespaces { 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 + " 3.x"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Name {
|
|
|
|
|
get { return obfuscatorName; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-11 11:38:02 +08:00
|
|
|
|
protected override bool CanInlineMethods {
|
2011-12-21 04:47:45 +08:00
|
|
|
|
get { return startedDeobfuscating ? options.InlineMethods : true; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Deobfuscator(Options options)
|
|
|
|
|
: base(options) {
|
|
|
|
|
this.options = options;
|
|
|
|
|
|
|
|
|
|
if (options.RemoveNamespaces)
|
|
|
|
|
this.RenamingOptions |= RenamingOptions.RemoveNamespaceIfOneType;
|
|
|
|
|
else
|
|
|
|
|
this.RenamingOptions &= ~RenamingOptions.RemoveNamespaceIfOneType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override byte[] unpackNativeFile(PeImage peImage) {
|
2011-12-23 06:50:33 +08:00
|
|
|
|
var unpacker = new ApplicationModeUnpacker(peImage);
|
|
|
|
|
var data = unpacker.unpack();
|
2011-12-21 04:47:45 +08:00
|
|
|
|
if (data == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2011-12-23 06:50:33 +08:00
|
|
|
|
unpackedFiles.AddRange(unpacker.EmbeddedAssemblies);
|
2011-12-21 04:47:45 +08:00
|
|
|
|
unpackedNativeFile = true;
|
|
|
|
|
ModuleBytes = data;
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-23 01:49:22 +08:00
|
|
|
|
bool needsPatching() {
|
|
|
|
|
return decrypterType.LinkedResource != null || nativeLibSaver.Resource != null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-24 23:02:27 +08:00
|
|
|
|
public override bool getDecryptedModule(int count, ref byte[] newFileData, ref DumpedMethods dumpedMethods) {
|
|
|
|
|
if (count != 0 || !needsPatching())
|
2011-12-21 07:31:27 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var fileData = ModuleBytes ?? DeobUtils.readModule(module);
|
|
|
|
|
var peImage = new PeImage(fileData);
|
2011-12-21 23:56:12 +08:00
|
|
|
|
if (!decrypterType.patch(peImage))
|
2011-12-21 07:31:27 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
newFileData = fileData;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override IDeobfuscator moduleReloaded(ModuleDefinition module) {
|
|
|
|
|
var newOne = new Deobfuscator(options);
|
|
|
|
|
newOne.setModule(module);
|
|
|
|
|
newOne.decrypterType = new DecrypterType(module, decrypterType);
|
|
|
|
|
newOne.nativeLibSaver = new NativeLibSaver(module, nativeLibSaver);
|
|
|
|
|
return newOne;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-21 04:47:45 +08:00
|
|
|
|
public override void init(ModuleDefinition module) {
|
|
|
|
|
base.init(module);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Regex isRandomName = new Regex(@"^[A-Z]{30,40}$");
|
|
|
|
|
static Regex isRandomNameMembers = new Regex(@"^[a-zA-Z0-9]{9,11}$"); // methods, fields, props, events
|
|
|
|
|
static Regex isRandomNameTypes = new Regex(@"^[a-zA-Z0-9]{18,19}(?:`\d+)?$"); // types, namespaces
|
|
|
|
|
|
|
|
|
|
bool checkValidName(string name, Regex regex) {
|
|
|
|
|
if (isRandomName.IsMatch(name))
|
|
|
|
|
return false;
|
|
|
|
|
if (regex.IsMatch(name)) {
|
|
|
|
|
if (RandomNameChecker.isRandom(name))
|
|
|
|
|
return false;
|
|
|
|
|
if (!RandomNameChecker.isNonRandom(name))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return checkValidName(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool isValidNamespaceName(string ns) {
|
|
|
|
|
if (ns == null)
|
|
|
|
|
return false;
|
|
|
|
|
if (ns.Contains("."))
|
|
|
|
|
return base.isValidNamespaceName(ns);
|
|
|
|
|
return checkValidName(ns, isRandomNameTypes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool isValidTypeName(string name) {
|
|
|
|
|
return name != null && checkValidName(name, isRandomNameTypes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool isValidMethodName(string name) {
|
|
|
|
|
return name != null && checkValidName(name, isRandomNameMembers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool isValidPropertyName(string name) {
|
|
|
|
|
return name != null && checkValidName(name, isRandomNameMembers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool isValidEventName(string name) {
|
|
|
|
|
return name != null && checkValidName(name, isRandomNameMembers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool isValidFieldName(string name) {
|
|
|
|
|
return name != null && checkValidName(name, isRandomNameMembers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool isValidGenericParamName(string name) {
|
|
|
|
|
return name != null && checkValidName(name, isRandomNameMembers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool isValidMethodArgName(string name) {
|
|
|
|
|
return name != null && checkValidName(name, isRandomNameMembers);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-04 18:45:04 +08:00
|
|
|
|
public override bool isValidMethodReturnArgName(string name) {
|
|
|
|
|
return string.IsNullOrEmpty(name) || checkValidName(name, isRandomNameMembers);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-03 20:53:01 +08:00
|
|
|
|
public override bool isValidResourceKeyName(string name) {
|
|
|
|
|
return name != null && checkValidName(name, isRandomNameMembers);
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-21 04:47:45 +08:00
|
|
|
|
protected override int detectInternal() {
|
|
|
|
|
int val = 0;
|
|
|
|
|
|
2012-01-08 02:14:15 +08:00
|
|
|
|
int sum = toInt32(unpackedNativeFile) +
|
|
|
|
|
toInt32(decrypterType.Detected) +
|
|
|
|
|
toInt32(nativeLibSaver.Detected);
|
2011-12-21 07:31:27 +08:00
|
|
|
|
if (sum > 0)
|
|
|
|
|
val += 100 + 10 * (sum - 1);
|
|
|
|
|
|
2011-12-21 04:47:45 +08:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void scanForObfuscator() {
|
2011-12-21 07:31:27 +08:00
|
|
|
|
decrypterType = new DecrypterType(module);
|
|
|
|
|
decrypterType.find();
|
|
|
|
|
nativeLibSaver = new NativeLibSaver(module);
|
|
|
|
|
nativeLibSaver.find();
|
2011-12-21 04:47:45 +08:00
|
|
|
|
obfuscatorName = detectVersion();
|
|
|
|
|
if (unpackedNativeFile)
|
|
|
|
|
obfuscatorName += " (native)";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string detectVersion() {
|
|
|
|
|
return DeobfuscatorInfo.THE_NAME + " 3.x";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void deobfuscateBegin() {
|
|
|
|
|
base.deobfuscateBegin();
|
|
|
|
|
|
2011-12-22 01:04:49 +08:00
|
|
|
|
antiStrongName = new AntiStrongName();
|
|
|
|
|
|
2012-07-28 10:22:17 +08:00
|
|
|
|
staticStringInliner.add(decrypterType.StringDecrypter1, (method2, gim, args) => {
|
2011-12-21 07:31:27 +08:00
|
|
|
|
return decrypterType.decrypt1((string)args[0]);
|
|
|
|
|
});
|
2012-07-28 10:22:17 +08:00
|
|
|
|
staticStringInliner.add(decrypterType.StringDecrypter2, (method2, gim, args) => {
|
2011-12-21 07:31:27 +08:00
|
|
|
|
return decrypterType.decrypt2((string)args[0]);
|
|
|
|
|
});
|
2012-01-22 05:15:53 +08:00
|
|
|
|
DeobfuscatedFile.stringDecryptersAdded();
|
2011-12-21 07:31:27 +08:00
|
|
|
|
|
2011-12-27 03:32:42 +08:00
|
|
|
|
libAssemblyResolver = new LibAssemblyResolver(module);
|
|
|
|
|
libAssemblyResolver.find(DeobfuscatedFile, this);
|
|
|
|
|
|
2011-12-21 07:31:27 +08:00
|
|
|
|
if (Operations.DecryptStrings == OpDecryptString.None)
|
|
|
|
|
canRemoveDecrypterType = false;
|
|
|
|
|
|
2011-12-22 01:12:04 +08:00
|
|
|
|
removeInitCall(nativeLibSaver.InitMethod);
|
2011-12-21 07:31:27 +08:00
|
|
|
|
addResourceToBeRemoved(nativeLibSaver.Resource, "Native lib resource");
|
|
|
|
|
addTypeToBeRemoved(nativeLibSaver.Type, "Native lib saver type");
|
|
|
|
|
|
|
|
|
|
foreach (var initMethod in decrypterType.InitMethods)
|
2011-12-22 01:12:04 +08:00
|
|
|
|
removeInitCall(initMethod);
|
2011-12-21 07:31:27 +08:00
|
|
|
|
|
2011-12-21 04:47:45 +08:00
|
|
|
|
dumpUnpackedFiles();
|
2011-12-27 03:32:42 +08:00
|
|
|
|
dumpResourceFiles();
|
2011-12-21 04:47:45 +08:00
|
|
|
|
|
|
|
|
|
startedDeobfuscating = true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
void removeInitCall(MethodDef initMethod) {
|
2011-12-22 01:12:04 +08:00
|
|
|
|
addCctorInitCallToBeRemoved(initMethod);
|
|
|
|
|
addCtorInitCallToBeRemoved(initMethod);
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-21 04:47:45 +08:00
|
|
|
|
void dumpUnpackedFiles() {
|
|
|
|
|
foreach (var unpackedFile in unpackedFiles)
|
2011-12-27 03:32:42 +08:00
|
|
|
|
DeobfuscatedFile.createAssemblyFile(unpackedFile.data,
|
|
|
|
|
Win32Path.GetFileNameWithoutExtension(unpackedFile.filename),
|
|
|
|
|
Win32Path.GetExtension(unpackedFile.filename));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void dumpResourceFiles() {
|
|
|
|
|
foreach (var resource in libAssemblyResolver.Resources) {
|
|
|
|
|
var mod = ModuleDefinition.ReadModule(resource.GetResourceStream());
|
|
|
|
|
addResourceToBeRemoved(resource, string.Format("Embedded assembly: {0}", mod.Assembly.FullName));
|
|
|
|
|
DeobfuscatedFile.createAssemblyFile(resource.GetResourceData(),
|
|
|
|
|
Utils.getAssemblySimpleName(mod.Assembly.FullName),
|
|
|
|
|
DeobUtils.getExtension(mod.Kind));
|
|
|
|
|
}
|
|
|
|
|
removeInitCall(libAssemblyResolver.InitMethod);
|
|
|
|
|
addCallToBeRemoved(module.EntryPoint, libAssemblyResolver.InitMethod);
|
|
|
|
|
addTypeToBeRemoved(libAssemblyResolver.Type, "Assembly resolver type (library mode)");
|
2011-12-21 04:47:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-12-22 01:04:49 +08:00
|
|
|
|
public override void deobfuscateMethodEnd(Blocks blocks) {
|
|
|
|
|
if (options.RemoveAntiStrongName) {
|
|
|
|
|
if (antiStrongName.remove(blocks))
|
|
|
|
|
Log.v("Removed Anti Strong Name code");
|
|
|
|
|
}
|
|
|
|
|
base.deobfuscateMethodEnd(blocks);
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-21 04:47:45 +08:00
|
|
|
|
public override void deobfuscateEnd() {
|
|
|
|
|
removeInlinedMethods();
|
|
|
|
|
if (options.RestoreTypes)
|
|
|
|
|
new TypesRestorer(module).deobfuscate();
|
|
|
|
|
|
2011-12-21 07:41:09 +08:00
|
|
|
|
if (canRemoveDecrypterType && !isTypeCalled(decrypterType.Type)) {
|
2011-12-21 07:31:27 +08:00
|
|
|
|
addTypeToBeRemoved(decrypterType.Type, "Decrypter type");
|
2011-12-22 01:30:37 +08:00
|
|
|
|
addModuleReferencesToBeRemoved(decrypterType.ModuleReferences, "Native lib module reference");
|
|
|
|
|
addResourceToBeRemoved(decrypterType.LinkedResource, "Native lib linked resource");
|
2011-12-21 07:41:09 +08:00
|
|
|
|
}
|
2011-12-21 07:31:27 +08:00
|
|
|
|
|
2011-12-21 04:47:45 +08:00
|
|
|
|
base.deobfuscateEnd();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void removeInlinedMethods() {
|
|
|
|
|
if (!options.InlineMethods || !options.RemoveInlinedMethods)
|
|
|
|
|
return;
|
|
|
|
|
findAndRemoveInlinedMethods();
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-25 13:25:40 +08:00
|
|
|
|
public override IEnumerable<int> getStringDecrypterMethods() {
|
|
|
|
|
var list = new List<int>();
|
2011-12-21 07:31:27 +08:00
|
|
|
|
foreach (var method in decrypterType.StringDecrypters)
|
2012-11-02 22:57:11 +08:00
|
|
|
|
list.Add(method.MDToken.ToInt32());
|
2011-12-21 04:47:45 +08:00
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|