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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-12-08 08:12:20 +08:00
|
|
|
|
using System;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
using System.Collections.Generic;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
using dot10.DotNet;
|
2012-12-08 08:12:20 +08:00
|
|
|
|
using dot10.DotNet.Emit;
|
2011-09-24 16:26:29 +08:00
|
|
|
|
using de4dot.blocks;
|
2012-12-07 22:07:30 +08:00
|
|
|
|
using de4dot.code.renamer;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2012-02-12 23:46:39 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.Eazfuscator_NET {
|
2011-12-09 16:02:06 +08:00
|
|
|
|
public class DeobfuscatorInfo : DeobfuscatorInfoBase {
|
2011-11-04 03:03:32 +08:00
|
|
|
|
public const string THE_NAME = "Eazfuscator.NET";
|
2011-12-08 16:53:13 +08:00
|
|
|
|
public const string THE_TYPE = "ef";
|
2011-12-08 16:52:23 +08:00
|
|
|
|
const string DEFAULT_REGEX = @"!^#=&!^dje_.+_ejd$&" + DeobfuscatorBase.DEFAULT_VALID_NAME_REGEX;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
public DeobfuscatorInfo()
|
2011-12-08 16:52:23 +08:00
|
|
|
|
: base(DEFAULT_REGEX) {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
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(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Deobfuscator : DeobfuscatorBase {
|
|
|
|
|
Options options;
|
2011-11-27 15:28:17 +08:00
|
|
|
|
string obfuscatorName = DeobfuscatorInfo.THE_NAME;
|
2011-12-21 13:41:06 +08:00
|
|
|
|
bool detectedVersion = false;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2012-02-27 19:55:37 +08:00
|
|
|
|
DecrypterType decrypterType;
|
2012-02-27 05:48:43 +08:00
|
|
|
|
StringDecrypter stringDecrypter;
|
2012-02-28 06:43:45 +08:00
|
|
|
|
AssemblyResolver assemblyResolver;
|
|
|
|
|
ResourceResolver resourceResolver;
|
2012-04-29 06:01:50 +08:00
|
|
|
|
ResourceMethodsRestorer resourceMethodsRestorer;
|
2012-02-27 05:48:43 +08:00
|
|
|
|
|
2011-09-22 10:55:30 +08:00
|
|
|
|
internal class Options : OptionsBase {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-27 15:28:17 +08:00
|
|
|
|
get { return obfuscatorName; }
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Deobfuscator(Options options)
|
|
|
|
|
: base(options) {
|
|
|
|
|
this.options = options;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 20:29:12 +08:00
|
|
|
|
protected override int detectInternal() {
|
2011-12-21 13:41:06 +08:00
|
|
|
|
int val = 0;
|
|
|
|
|
|
2012-02-28 06:43:45 +08:00
|
|
|
|
int sum = toInt32(stringDecrypter.Detected) +
|
|
|
|
|
toInt32(assemblyResolver.Detected) +
|
|
|
|
|
toInt32(resourceResolver.Detected);
|
|
|
|
|
if (sum > 0)
|
|
|
|
|
val += 100 + 10 * (sum - 1);
|
2011-12-21 13:41:06 +08:00
|
|
|
|
if (detectedVersion)
|
|
|
|
|
val += 10;
|
|
|
|
|
|
|
|
|
|
return val;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 20:29:12 +08:00
|
|
|
|
protected override void scanForObfuscator() {
|
2012-04-10 09:52:18 +08:00
|
|
|
|
decrypterType = new DecrypterType(module, DeobfuscatedFile);
|
2012-02-27 19:55:37 +08:00
|
|
|
|
stringDecrypter = new StringDecrypter(module, decrypterType);
|
|
|
|
|
stringDecrypter.find();
|
2012-02-28 06:43:45 +08:00
|
|
|
|
assemblyResolver = new AssemblyResolver(module, decrypterType);
|
|
|
|
|
assemblyResolver.find();
|
|
|
|
|
resourceResolver = new ResourceResolver(module, assemblyResolver);
|
|
|
|
|
resourceResolver.find();
|
2012-02-27 05:48:43 +08:00
|
|
|
|
if (stringDecrypter.Detected)
|
2011-11-27 15:28:17 +08:00
|
|
|
|
detectVersion();
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-27 15:28:17 +08:00
|
|
|
|
void detectVersion() {
|
2012-03-19 05:59:34 +08:00
|
|
|
|
var version = new VersionDetector(module, stringDecrypter).detect();
|
2012-02-29 10:17:45 +08:00
|
|
|
|
if (version == null)
|
2011-12-21 13:41:06 +08:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
detectedVersion = true;
|
2012-02-29 10:17:45 +08:00
|
|
|
|
obfuscatorName = DeobfuscatorInfo.THE_NAME + " " + version;
|
2011-12-21 13:41:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-27 05:48:43 +08:00
|
|
|
|
public override void deobfuscateBegin() {
|
|
|
|
|
base.deobfuscateBegin();
|
|
|
|
|
|
2012-02-27 19:55:37 +08:00
|
|
|
|
stringDecrypter.initialize(DeobfuscatedFile);
|
2012-07-28 10:22:17 +08:00
|
|
|
|
staticStringInliner.add(stringDecrypter.Method, (method2, gim, args) => {
|
2012-02-27 05:48:43 +08:00
|
|
|
|
return stringDecrypter.decrypt((int)args[0]);
|
|
|
|
|
});
|
|
|
|
|
DeobfuscatedFile.stringDecryptersAdded();
|
2012-02-28 06:43:45 +08:00
|
|
|
|
|
|
|
|
|
assemblyResolver.initialize(DeobfuscatedFile, this);
|
|
|
|
|
assemblyResolver.initializeEmbeddedFiles();
|
|
|
|
|
addModuleCctorInitCallToBeRemoved(assemblyResolver.InitMethod);
|
|
|
|
|
|
|
|
|
|
resourceResolver.initialize(DeobfuscatedFile, this);
|
|
|
|
|
foreach (var info in resourceResolver.mergeResources())
|
|
|
|
|
addResourceToBeRemoved(info.Resource, "Encrypted resources");
|
|
|
|
|
addModuleCctorInitCallToBeRemoved(resourceResolver.InitMethod);
|
|
|
|
|
|
2012-04-29 06:01:50 +08:00
|
|
|
|
resourceMethodsRestorer = new ResourceMethodsRestorer(module);
|
2012-12-07 22:07:30 +08:00
|
|
|
|
if ((Operations.RenamerFlags & (RenamerFlags.RenameTypes | RenamerFlags.RenameNamespaces)) != 0)
|
|
|
|
|
resourceMethodsRestorer.find(DeobfuscatedFile, this);
|
2012-02-29 01:17:33 +08:00
|
|
|
|
|
2012-02-28 06:43:45 +08:00
|
|
|
|
dumpEmbeddedAssemblies();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void dumpEmbeddedAssemblies() {
|
|
|
|
|
foreach (var info in assemblyResolver.AssemblyInfos) {
|
|
|
|
|
DeobfuscatedFile.createAssemblyFile(info.Data, info.SimpleName, info.Extension);
|
|
|
|
|
addResourceToBeRemoved(info.Resource, string.Format("Embedded assembly: {0}", info.AssemblyFullName));
|
|
|
|
|
}
|
2012-02-27 05:48:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-29 01:17:33 +08:00
|
|
|
|
public override void deobfuscateMethodEnd(Blocks blocks) {
|
2012-04-29 06:01:50 +08:00
|
|
|
|
resourceMethodsRestorer.deobfuscate(blocks);
|
2012-04-29 10:03:10 +08:00
|
|
|
|
assemblyResolver.deobfuscate(blocks);
|
2012-02-29 01:17:33 +08:00
|
|
|
|
base.deobfuscateMethodEnd(blocks);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-22 10:55:30 +08:00
|
|
|
|
public override void deobfuscateEnd() {
|
2012-02-27 05:48:43 +08:00
|
|
|
|
if (CanRemoveStringDecrypterType) {
|
|
|
|
|
addTypesToBeRemoved(stringDecrypter.Types, "String decrypter type");
|
2012-02-27 19:55:37 +08:00
|
|
|
|
addTypeToBeRemoved(decrypterType.Type, "Decrypter type");
|
2012-12-11 07:23:16 +08:00
|
|
|
|
addTypesToBeRemoved(stringDecrypter.DynocodeTypes, "Dynocode type");
|
2012-02-27 05:48:43 +08:00
|
|
|
|
addResourceToBeRemoved(stringDecrypter.Resource, "Encrypted strings");
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
2012-02-28 06:43:45 +08:00
|
|
|
|
addTypeToBeRemoved(assemblyResolver.Type, "Assembly resolver type");
|
2012-02-29 03:44:54 +08:00
|
|
|
|
addTypeToBeRemoved(assemblyResolver.OtherType, "Assembly resolver other type");
|
2012-02-28 06:43:45 +08:00
|
|
|
|
addTypeToBeRemoved(resourceResolver.Type, "Resource resolver type");
|
2012-04-29 06:01:50 +08:00
|
|
|
|
addTypeToBeRemoved(resourceMethodsRestorer.Type, "GetManifestResourceStream type");
|
|
|
|
|
addResourceToBeRemoved(resourceMethodsRestorer.Resource, "GetManifestResourceStream type resource");
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2012-02-29 06:57:48 +08:00
|
|
|
|
fixInterfaces();
|
2012-12-08 08:12:20 +08:00
|
|
|
|
stringDecrypterBugWorkaround();
|
2011-09-22 10:55:30 +08:00
|
|
|
|
base.deobfuscateEnd();
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-08 08:12:20 +08:00
|
|
|
|
void stringDecrypterBugWorkaround() {
|
|
|
|
|
// There's a bug in Eazfuscator.NET when the VM and string encryption features are
|
|
|
|
|
// enabled. The string decrypter's initialization code checks to make sure it's not
|
|
|
|
|
// called by eg. a dynamic method. When it's called from the VM code, it is
|
|
|
|
|
// called by MethodBase.Invoke() and the string decrypter antis set in causing it
|
|
|
|
|
// to fail.
|
|
|
|
|
// One way to work around this is to make sure the string decrypter has been called
|
|
|
|
|
// once. That way, any VM code calling it won't trigger a failure.
|
|
|
|
|
// We can put this code in <Module>::.cctor() since it gets executed before any
|
|
|
|
|
// other code.
|
|
|
|
|
// Note that we can't call the string decrypter from <Module>::.cctor() since
|
|
|
|
|
// its DeclaringType property will return null (since it's the global type). We
|
|
|
|
|
// must call another created class which calls the string decrypter.
|
|
|
|
|
|
|
|
|
|
// You must use --dont-rename --keep-types --preserve-tokens and decrypt strings
|
|
|
|
|
if (!Operations.KeepObfuscatorTypes || Operations.DecryptStrings == OpDecryptString.None ||
|
|
|
|
|
(Operations.RenamerFlags & (RenamerFlags.RenameNamespaces | RenamerFlags.RenameTypes)) != 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (stringDecrypter.ValidStringDecrypterValue == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var newType = module.UpdateRowId(new TypeDefUser(Guid.NewGuid().ToString("B"), module.CorLibTypes.Object.TypeDefOrRef));
|
|
|
|
|
module.Types.Add(newType);
|
|
|
|
|
var newMethod = module.UpdateRowId(new MethodDefUser("x", MethodSig.CreateStatic(module.CorLibTypes.Void), 0, MethodAttributes.Static | MethodAttributes.HideBySig));
|
|
|
|
|
newType.Methods.Add(newMethod);
|
|
|
|
|
newMethod.Body = new CilBody();
|
|
|
|
|
newMethod.Body.MaxStack = 1;
|
|
|
|
|
newMethod.Body.Instructions.Add(Instruction.CreateLdcI4(stringDecrypter.ValidStringDecrypterValue.Value));
|
2012-12-16 07:03:56 +08:00
|
|
|
|
newMethod.Body.Instructions.Add(OpCodes.Call.ToInstruction(stringDecrypter.Method));
|
|
|
|
|
newMethod.Body.Instructions.Add(OpCodes.Pop.ToInstruction());
|
|
|
|
|
newMethod.Body.Instructions.Add(OpCodes.Ret.ToInstruction());
|
2012-12-08 08:12:20 +08:00
|
|
|
|
|
|
|
|
|
var cctor = module.GlobalType.FindOrCreateStaticConstructor();
|
|
|
|
|
var blocks = new Blocks(cctor);
|
|
|
|
|
var block = blocks.MethodBlocks.getAllBlocks()[0];
|
2012-12-16 07:03:56 +08:00
|
|
|
|
block.insert(0, OpCodes.Call.ToInstruction(newMethod));
|
2012-12-08 08:12:20 +08:00
|
|
|
|
|
|
|
|
|
IList<Instruction> allInstructions;
|
|
|
|
|
IList<ExceptionHandler> allExceptionHandlers;
|
|
|
|
|
blocks.getCode(out allInstructions, out allExceptionHandlers);
|
|
|
|
|
DotNetUtils.restoreBody(cctor, allInstructions, allExceptionHandlers);
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-25 13:25:40 +08:00
|
|
|
|
public override IEnumerable<int> getStringDecrypterMethods() {
|
|
|
|
|
var list = new List<int>();
|
2012-02-27 05:48:43 +08:00
|
|
|
|
if (stringDecrypter.Method != null)
|
2012-11-02 22:57:11 +08:00
|
|
|
|
list.Add(stringDecrypter.Method.MDToken.ToInt32());
|
2011-09-22 10:55:30 +08:00
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|