2011-10-25 01:44:49 +08:00
|
|
|
|
/*
|
|
|
|
|
Copyright (C) 2011 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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Mono.Cecil;
|
2011-10-30 13:15:52 +08:00
|
|
|
|
using Mono.Cecil.Cil;
|
2011-10-29 08:27:34 +08:00
|
|
|
|
using Mono.MyStuff;
|
2011-10-25 01:44:49 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.deobfuscators.dotNET_Reactor {
|
|
|
|
|
class DeobfuscatorInfo : DeobfuscatorInfoBase {
|
2011-11-04 03:03:32 +08:00
|
|
|
|
public const string THE_NAME = ".NET Reactor";
|
2011-11-04 18:01:21 +08:00
|
|
|
|
const string DEFAULT_REGEX = @"!^[a-zA-Z0-9]{6,}$&" + DeobfuscatorBase.DEFAULT_VALID_NAME_REGEX;
|
2011-10-27 04:24:31 +08:00
|
|
|
|
BoolOption decryptMethods;
|
|
|
|
|
BoolOption decryptBools;
|
2011-11-01 02:41:38 +08:00
|
|
|
|
BoolOption restoreTypes;
|
2011-11-01 21:23:30 +08:00
|
|
|
|
BoolOption inlineMethods;
|
|
|
|
|
BoolOption removeInlinedMethods;
|
2011-10-27 04:24:31 +08:00
|
|
|
|
|
2011-10-25 01:44:49 +08:00
|
|
|
|
public DeobfuscatorInfo()
|
2011-11-04 07:35:07 +08:00
|
|
|
|
: base(DEFAULT_REGEX) {
|
2011-10-27 04:24:31 +08:00
|
|
|
|
decryptMethods = new BoolOption(null, makeArgName("methods"), "Decrypt methods", true);
|
|
|
|
|
decryptBools = new BoolOption(null, makeArgName("bools"), "Decrypt booleans", true);
|
2011-11-01 02:41:38 +08:00
|
|
|
|
restoreTypes = new BoolOption(null, makeArgName("types"), "Restore types (object -> real type)", true);
|
2011-11-01 21:23:30 +08:00
|
|
|
|
inlineMethods = new BoolOption(null, makeArgName("inline"), "Inline short methods", true);
|
|
|
|
|
removeInlinedMethods = new BoolOption(null, makeArgName("remove-inlined"), "Remove inlined methods", true);
|
2011-10-25 01:44:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-04 03:03:32 +08:00
|
|
|
|
public override string Name {
|
|
|
|
|
get { return THE_NAME; }
|
2011-10-25 01:44:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Type {
|
2011-11-04 03:03:32 +08:00
|
|
|
|
get { return "dr"; }
|
2011-10-25 01:44:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override IDeobfuscator createDeobfuscator() {
|
|
|
|
|
return new Deobfuscator(new Deobfuscator.Options {
|
|
|
|
|
ValidNameRegex = validNameRegex.get(),
|
2011-10-27 04:24:31 +08:00
|
|
|
|
DecryptMethods = decryptMethods.get(),
|
|
|
|
|
DecryptBools = decryptBools.get(),
|
2011-11-01 02:41:38 +08:00
|
|
|
|
RestoreTypes = restoreTypes.get(),
|
2011-11-01 21:23:30 +08:00
|
|
|
|
InlineMethods = inlineMethods.get(),
|
|
|
|
|
RemoveInlinedMethods = removeInlinedMethods.get(),
|
2011-10-25 01:44:49 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override IEnumerable<Option> getOptionsInternal() {
|
|
|
|
|
return new List<Option>() {
|
2011-10-27 04:24:31 +08:00
|
|
|
|
decryptMethods,
|
|
|
|
|
decryptBools,
|
2011-11-01 02:41:38 +08:00
|
|
|
|
restoreTypes,
|
2011-11-01 21:23:30 +08:00
|
|
|
|
inlineMethods,
|
|
|
|
|
removeInlinedMethods,
|
2011-10-25 01:44:49 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Deobfuscator : DeobfuscatorBase {
|
|
|
|
|
Options options;
|
2011-10-30 02:28:29 +08:00
|
|
|
|
string obfuscatorName = ".NET Reactor";
|
2011-10-25 01:44:49 +08:00
|
|
|
|
|
2011-10-27 01:49:25 +08:00
|
|
|
|
PE.PeImage peImage;
|
|
|
|
|
byte[] fileData;
|
2011-10-25 14:27:36 +08:00
|
|
|
|
MethodsDecrypter methodsDecrypter;
|
2011-10-27 01:49:25 +08:00
|
|
|
|
StringDecrypter stringDecrypter;
|
2011-10-27 02:23:45 +08:00
|
|
|
|
BooleanDecrypter booleanDecrypter;
|
2011-10-27 03:05:35 +08:00
|
|
|
|
BoolValueInliner boolValueInliner;
|
2011-11-01 22:17:26 +08:00
|
|
|
|
MetadataTokenObfuscator metadataTokenObfuscator;
|
2011-10-25 14:27:36 +08:00
|
|
|
|
|
2011-11-01 21:23:30 +08:00
|
|
|
|
bool canRemoveDecrypterType = true;
|
|
|
|
|
bool startedDeobfuscating = false;
|
|
|
|
|
|
2011-10-25 01:44:49 +08:00
|
|
|
|
internal class Options : OptionsBase {
|
2011-10-27 04:24:31 +08:00
|
|
|
|
public bool DecryptMethods { get; set; }
|
|
|
|
|
public bool DecryptBools { get; set; }
|
2011-11-01 02:41:38 +08:00
|
|
|
|
public bool RestoreTypes { get; set; }
|
2011-11-01 21:23:30 +08:00
|
|
|
|
public bool InlineMethods { get; set; }
|
|
|
|
|
public bool RemoveInlinedMethods { get; set; }
|
2011-10-25 01:44:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Type {
|
2011-11-04 03:03:32 +08:00
|
|
|
|
get { return DeobfuscatorInfo.THE_NAME; }
|
2011-10-25 01:44:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Name {
|
2011-10-30 02:28:29 +08:00
|
|
|
|
get { return obfuscatorName; }
|
2011-10-25 01:44:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-01 21:23:30 +08:00
|
|
|
|
public override bool CanInlineMethods {
|
|
|
|
|
get { return startedDeobfuscating ? options.InlineMethods : true; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-25 01:44:49 +08:00
|
|
|
|
public Deobfuscator(Options options)
|
|
|
|
|
: base(options) {
|
|
|
|
|
this.options = options;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void init(ModuleDefinition module) {
|
|
|
|
|
base.init(module);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 20:29:12 +08:00
|
|
|
|
protected override int detectInternal() {
|
2011-10-25 01:44:49 +08:00
|
|
|
|
int val = 0;
|
|
|
|
|
|
2011-10-27 02:23:45 +08:00
|
|
|
|
if (methodsDecrypter.Detected || stringDecrypter.Detected || booleanDecrypter.Detected)
|
2011-10-27 01:49:25 +08:00
|
|
|
|
val += 100;
|
2011-10-27 02:23:45 +08:00
|
|
|
|
|
|
|
|
|
int sum = convert(methodsDecrypter.Detected) +
|
|
|
|
|
convert(stringDecrypter.Detected) +
|
|
|
|
|
convert(booleanDecrypter.Detected);
|
|
|
|
|
if (sum > 1)
|
|
|
|
|
val += 10 * (sum - 1);
|
2011-10-25 14:27:36 +08:00
|
|
|
|
|
2011-10-25 01:44:49 +08:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-27 02:23:45 +08:00
|
|
|
|
static int convert(bool b) {
|
|
|
|
|
return b ? 1 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 20:29:12 +08:00
|
|
|
|
protected override void scanForObfuscator() {
|
2011-10-25 14:27:36 +08:00
|
|
|
|
methodsDecrypter = new MethodsDecrypter(module);
|
|
|
|
|
methodsDecrypter.find();
|
2011-10-27 01:49:25 +08:00
|
|
|
|
stringDecrypter = new StringDecrypter(module);
|
2011-10-30 02:28:29 +08:00
|
|
|
|
stringDecrypter.find(DeobfuscatedFile);
|
2011-10-27 02:23:45 +08:00
|
|
|
|
booleanDecrypter = new BooleanDecrypter(module);
|
|
|
|
|
booleanDecrypter.find();
|
2011-10-30 02:28:29 +08:00
|
|
|
|
obfuscatorName = detectVersion();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string detectVersion() {
|
|
|
|
|
/*
|
|
|
|
|
Methods decrypter locals (not showing its own types):
|
|
|
|
|
3.7.0.3:
|
|
|
|
|
"System.Byte[]"
|
|
|
|
|
"System.Int32"
|
|
|
|
|
"System.Int32[]"
|
|
|
|
|
"System.IntPtr"
|
|
|
|
|
"System.IO.BinaryReader"
|
|
|
|
|
"System.IO.MemoryStream"
|
|
|
|
|
"System.Object"
|
|
|
|
|
"System.Reflection.Assembly"
|
|
|
|
|
"System.Security.Cryptography.CryptoStream"
|
|
|
|
|
"System.Security.Cryptography.ICryptoTransform"
|
|
|
|
|
"System.Security.Cryptography.RijndaelManaged"
|
|
|
|
|
"System.String"
|
|
|
|
|
|
|
|
|
|
3.9.8.0:
|
|
|
|
|
- "System.Int32[]"
|
|
|
|
|
+ "System.Diagnostics.StackFrame"
|
|
|
|
|
|
2011-10-30 13:15:52 +08:00
|
|
|
|
4.0.0.0: (jitter)
|
2011-10-30 02:28:29 +08:00
|
|
|
|
- "System.Diagnostics.StackFrame"
|
|
|
|
|
- "System.Object"
|
|
|
|
|
+ "System.Boolean"
|
|
|
|
|
+ "System.Collections.IEnumerator"
|
|
|
|
|
+ "System.Delegate"
|
|
|
|
|
+ "System.Diagnostics.Process"
|
|
|
|
|
+ "System.Diagnostics.ProcessModule"
|
|
|
|
|
+ "System.Diagnostics.ProcessModuleCollection"
|
|
|
|
|
+ "System.IDisposable"
|
|
|
|
|
+ "System.Int64"
|
|
|
|
|
+ "System.UInt32"
|
|
|
|
|
+ "System.UInt64"
|
2011-10-30 13:15:52 +08:00
|
|
|
|
|
|
|
|
|
4.1.0.0: (jitter)
|
|
|
|
|
+ "System.Reflection.Assembly"
|
|
|
|
|
|
|
|
|
|
4.3.1.0: (jitter)
|
|
|
|
|
+ "System.Byte&"
|
2011-10-30 02:28:29 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
LocalTypes localTypes;
|
|
|
|
|
int minVer = -1;
|
|
|
|
|
foreach (var info in stringDecrypter.DecrypterInfos) {
|
|
|
|
|
if (info.key == null)
|
|
|
|
|
continue;
|
|
|
|
|
localTypes = new LocalTypes(info.method);
|
|
|
|
|
if (!localTypes.exists("System.IntPtr"))
|
|
|
|
|
return ".NET Reactor <= 3.7";
|
|
|
|
|
minVer = 3800;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-06 22:24:30 +08:00
|
|
|
|
if (methodsDecrypter.Method == null) {
|
2011-10-30 02:28:29 +08:00
|
|
|
|
if (minVer >= 3800)
|
|
|
|
|
return ".NET Reactor >= 3.8";
|
|
|
|
|
return ".NET Reactor";
|
|
|
|
|
}
|
2011-11-06 22:24:30 +08:00
|
|
|
|
localTypes = new LocalTypes(methodsDecrypter.Method);
|
2011-10-30 02:28:29 +08:00
|
|
|
|
|
|
|
|
|
if (localTypes.exists("System.Int32[]")) {
|
|
|
|
|
if (minVer >= 3800)
|
|
|
|
|
return ".NET Reactor 3.8.4.1 - 3.9.0.1";
|
|
|
|
|
return ".NET Reactor <= 3.9.0.1";
|
|
|
|
|
}
|
2011-10-30 13:15:52 +08:00
|
|
|
|
if (!localTypes.exists("System.Diagnostics.Process")) { // If < 4.0
|
|
|
|
|
if (localTypes.exists("System.Diagnostics.StackFrame"))
|
|
|
|
|
return ".NET Reactor 3.9.8.0";
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-06 22:24:30 +08:00
|
|
|
|
var compileMethod = MethodsDecrypter.findDnrCompileMethod(methodsDecrypter.Method.DeclaringType);
|
2011-10-30 13:15:52 +08:00
|
|
|
|
if (compileMethod == null)
|
|
|
|
|
return ".NET Reactor < 4.0";
|
|
|
|
|
DeobfuscatedFile.deobfuscate(compileMethod);
|
|
|
|
|
bool compileMethodHasConstant_0x70000000 = findConstant(compileMethod, 0x70000000); // 4.0-4.1
|
2011-11-06 22:24:30 +08:00
|
|
|
|
DeobfuscatedFile.deobfuscate(methodsDecrypter.Method);
|
|
|
|
|
bool hasCorEnableProfilingString = findString(methodsDecrypter.Method, "Cor_Enable_Profiling"); // 4.1-4.4
|
2011-10-30 13:15:52 +08:00
|
|
|
|
|
|
|
|
|
if (compileMethodHasConstant_0x70000000) {
|
|
|
|
|
if (hasCorEnableProfilingString)
|
|
|
|
|
return ".NET Reactor 4.1";
|
|
|
|
|
return ".NET Reactor 4.0";
|
|
|
|
|
}
|
|
|
|
|
if (!hasCorEnableProfilingString)
|
|
|
|
|
return ".NET Reactor";
|
|
|
|
|
// 4.2-4.4
|
2011-10-30 02:28:29 +08:00
|
|
|
|
|
|
|
|
|
if (!localTypes.exists("System.Byte&"))
|
2011-10-30 13:15:52 +08:00
|
|
|
|
return ".NET Reactor 4.2";
|
|
|
|
|
|
|
|
|
|
localTypes = new LocalTypes(compileMethod);
|
|
|
|
|
if (localTypes.exists("System.Object"))
|
|
|
|
|
return ".NET Reactor 4.4";
|
|
|
|
|
return ".NET Reactor 4.3";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool findString(MethodDefinition method, string s) {
|
2011-10-31 07:09:38 +08:00
|
|
|
|
foreach (var cs in DotNetUtils.getCodeStrings(method)) {
|
|
|
|
|
if (cs == s)
|
2011-10-30 13:15:52 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool findConstant(MethodDefinition method, int constant) {
|
|
|
|
|
if (method == null || method.Body == null)
|
|
|
|
|
return false;
|
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
|
|
|
|
if (instr.OpCode.Code != Code.Ldc_I4)
|
|
|
|
|
continue;
|
|
|
|
|
if (constant == (int)instr.Operand)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-29 08:27:34 +08:00
|
|
|
|
public override bool getDecryptedModule(ref byte[] newFileData, ref Dictionary<uint, DumpedMethod> dumpedMethods) {
|
2011-11-06 20:46:50 +08:00
|
|
|
|
fileData = DeobUtils.readModule(module);
|
2011-10-27 01:49:25 +08:00
|
|
|
|
peImage = new PE.PeImage(fileData);
|
|
|
|
|
|
2011-10-27 04:24:31 +08:00
|
|
|
|
if (!options.DecryptMethods)
|
2011-10-29 08:27:34 +08:00
|
|
|
|
return false;
|
2011-10-27 04:24:31 +08:00
|
|
|
|
|
2011-10-29 08:27:34 +08:00
|
|
|
|
if (!methodsDecrypter.decrypt(peImage, DeobfuscatedFile, ref dumpedMethods))
|
|
|
|
|
return false;
|
2011-10-27 01:49:25 +08:00
|
|
|
|
|
2011-10-29 08:27:34 +08:00
|
|
|
|
newFileData = fileData;
|
|
|
|
|
return true;
|
2011-10-26 20:40:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override IDeobfuscator moduleReloaded(ModuleDefinition module) {
|
|
|
|
|
var newOne = new Deobfuscator(options);
|
|
|
|
|
newOne.setModule(module);
|
2011-10-27 02:23:45 +08:00
|
|
|
|
newOne.fileData = fileData;
|
2011-10-27 01:49:25 +08:00
|
|
|
|
newOne.peImage = new PE.PeImage(fileData);
|
2011-10-26 20:40:55 +08:00
|
|
|
|
newOne.methodsDecrypter = new MethodsDecrypter(module, methodsDecrypter);
|
2011-10-27 01:49:25 +08:00
|
|
|
|
newOne.stringDecrypter = new StringDecrypter(module, stringDecrypter);
|
2011-10-27 02:23:45 +08:00
|
|
|
|
newOne.booleanDecrypter = new BooleanDecrypter(module, booleanDecrypter);
|
2011-10-26 20:40:55 +08:00
|
|
|
|
return newOne;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-25 01:44:49 +08:00
|
|
|
|
public override void deobfuscateBegin() {
|
|
|
|
|
base.deobfuscateBegin();
|
2011-10-27 01:49:25 +08:00
|
|
|
|
|
2011-10-28 07:33:05 +08:00
|
|
|
|
stringDecrypter.init(peImage, fileData, DeobfuscatedFile);
|
2011-10-27 02:23:45 +08:00
|
|
|
|
booleanDecrypter.init(fileData, DeobfuscatedFile);
|
2011-10-27 03:05:35 +08:00
|
|
|
|
boolValueInliner = new BoolValueInliner();
|
|
|
|
|
|
2011-10-27 04:24:31 +08:00
|
|
|
|
if (options.DecryptBools) {
|
2011-11-06 22:24:30 +08:00
|
|
|
|
boolValueInliner.add(booleanDecrypter.Method, (method, args) => {
|
2011-10-27 04:24:31 +08:00
|
|
|
|
return booleanDecrypter.decrypt((int)args[0]);
|
|
|
|
|
});
|
|
|
|
|
}
|
2011-10-27 01:49:25 +08:00
|
|
|
|
|
|
|
|
|
foreach (var info in stringDecrypter.DecrypterInfos) {
|
|
|
|
|
staticStringDecrypter.add(info.method, (method2, args) => {
|
|
|
|
|
return stringDecrypter.decrypt(method2, (int)args[0]);
|
|
|
|
|
});
|
|
|
|
|
}
|
2011-10-28 04:25:44 +08:00
|
|
|
|
if (stringDecrypter.OtherStringDecrypter != null) {
|
|
|
|
|
staticStringDecrypter.add(stringDecrypter.OtherStringDecrypter, (method2, args) => {
|
|
|
|
|
return stringDecrypter.decrypt((string)args[0]);
|
|
|
|
|
});
|
|
|
|
|
}
|
2011-10-27 01:49:25 +08:00
|
|
|
|
DeobfuscatedFile.stringDecryptersAdded();
|
2011-10-27 05:00:01 +08:00
|
|
|
|
|
2011-11-01 22:17:26 +08:00
|
|
|
|
metadataTokenObfuscator = new MetadataTokenObfuscator(module);
|
|
|
|
|
|
2011-10-27 05:00:01 +08:00
|
|
|
|
if (Operations.DecryptStrings != OpDecryptString.None)
|
2011-11-06 22:24:30 +08:00
|
|
|
|
addResourceToBeRemoved(stringDecrypter.Resource, "Encrypted strings");
|
2011-11-04 14:21:12 +08:00
|
|
|
|
else
|
|
|
|
|
canRemoveDecrypterType = false;
|
|
|
|
|
|
2011-10-27 05:00:01 +08:00
|
|
|
|
if (options.DecryptMethods) {
|
2011-11-06 22:24:30 +08:00
|
|
|
|
addResourceToBeRemoved(methodsDecrypter.Resource, "Encrypted methods");
|
|
|
|
|
addCctorInitCallToBeRemoved(methodsDecrypter.Method);
|
2011-10-27 05:00:01 +08:00
|
|
|
|
}
|
2011-11-04 14:21:12 +08:00
|
|
|
|
else
|
|
|
|
|
canRemoveDecrypterType = false;
|
|
|
|
|
|
2011-10-27 05:00:01 +08:00
|
|
|
|
if (options.DecryptBools)
|
2011-11-06 22:24:30 +08:00
|
|
|
|
addResourceToBeRemoved(booleanDecrypter.Resource, "Encrypted booleans");
|
2011-11-04 14:21:12 +08:00
|
|
|
|
else
|
|
|
|
|
canRemoveDecrypterType = false;
|
|
|
|
|
|
|
|
|
|
// The inlined methods may contain calls to the decrypter class
|
|
|
|
|
if (!options.InlineMethods)
|
2011-11-01 21:23:30 +08:00
|
|
|
|
canRemoveDecrypterType = false;
|
|
|
|
|
|
|
|
|
|
// Set it to false until we've removed all references to it
|
|
|
|
|
canRemoveDecrypterType = false;
|
|
|
|
|
|
|
|
|
|
startedDeobfuscating = true;
|
2011-10-25 01:44:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-27 04:16:51 +08:00
|
|
|
|
public override bool deobfuscateOther(Blocks blocks) {
|
2011-10-27 03:05:35 +08:00
|
|
|
|
if (boolValueInliner.HasHandlers)
|
2011-10-27 04:16:51 +08:00
|
|
|
|
return boolValueInliner.decrypt(blocks) > 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-10-27 03:05:35 +08:00
|
|
|
|
|
2011-10-27 04:16:51 +08:00
|
|
|
|
public override void deobfuscateMethodEnd(Blocks blocks) {
|
2011-11-01 22:17:26 +08:00
|
|
|
|
metadataTokenObfuscator.deobfuscate(blocks);
|
2011-10-25 01:44:49 +08:00
|
|
|
|
base.deobfuscateMethodEnd(blocks);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void deobfuscateEnd() {
|
2011-11-01 21:23:30 +08:00
|
|
|
|
removeInlinedMethods();
|
2011-11-01 02:41:38 +08:00
|
|
|
|
if (options.RestoreTypes)
|
|
|
|
|
new TypesRestorer(module).deobfuscate();
|
2011-11-06 22:24:30 +08:00
|
|
|
|
if (canRemoveDecrypterType && methodsDecrypter.Method != null) {
|
|
|
|
|
addTypeToBeRemoved(methodsDecrypter.Method.DeclaringType, "Decrypter type");
|
2011-11-01 22:17:26 +08:00
|
|
|
|
addTypeToBeRemoved(metadataTokenObfuscator.Type, "Metadata token obfuscator");
|
|
|
|
|
}
|
2011-10-25 01:44:49 +08:00
|
|
|
|
base.deobfuscateEnd();
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-01 21:23:30 +08:00
|
|
|
|
class UnusedMethodsFinder {
|
|
|
|
|
ModuleDefinition module;
|
|
|
|
|
Dictionary<MethodDefinition, bool> possiblyUnusedMethods = new Dictionary<MethodDefinition, bool>();
|
|
|
|
|
Stack<MethodDefinition> notUnusedStack = new Stack<MethodDefinition>();
|
|
|
|
|
|
|
|
|
|
public UnusedMethodsFinder(ModuleDefinition module, IEnumerable<MethodDefinition> possiblyUnusedMethods) {
|
|
|
|
|
this.module = module;
|
|
|
|
|
foreach (var method in possiblyUnusedMethods)
|
|
|
|
|
this.possiblyUnusedMethods[method] = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<MethodDefinition> find() {
|
|
|
|
|
if (possiblyUnusedMethods.Count == 0)
|
|
|
|
|
return possiblyUnusedMethods.Keys;
|
|
|
|
|
|
|
|
|
|
foreach (var type in module.GetTypes()) {
|
|
|
|
|
foreach (var method in type.Methods)
|
|
|
|
|
check(method);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (notUnusedStack.Count > 0) {
|
|
|
|
|
var method = notUnusedStack.Pop();
|
|
|
|
|
if (!possiblyUnusedMethods.Remove(method))
|
|
|
|
|
continue;
|
|
|
|
|
check(method);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return possiblyUnusedMethods.Keys;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void check(MethodDefinition method) {
|
|
|
|
|
if (method.Body == null)
|
|
|
|
|
return;
|
|
|
|
|
if (possiblyUnusedMethods.ContainsKey(method))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
|
|
|
|
switch (instr.OpCode.Code) {
|
|
|
|
|
case Code.Call:
|
|
|
|
|
case Code.Calli:
|
|
|
|
|
case Code.Callvirt:
|
|
|
|
|
case Code.Newobj:
|
|
|
|
|
case Code.Ldtoken:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var calledMethod = DotNetUtils.getMethod(module, instr.Operand as MethodReference);
|
|
|
|
|
if (calledMethod == null)
|
|
|
|
|
continue;
|
|
|
|
|
if (possiblyUnusedMethods.ContainsKey(calledMethod))
|
|
|
|
|
notUnusedStack.Push(calledMethod);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void removeInlinedMethods() {
|
|
|
|
|
if (!options.InlineMethods || !options.RemoveInlinedMethods)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Not all garbage methods are inlined, possibly because we remove some code that calls
|
|
|
|
|
// the garbage method before the methods inliner has a chance to inline it. Try to find
|
|
|
|
|
// all garbage methods and other code will figure out if there are any calls left.
|
|
|
|
|
|
|
|
|
|
var inlinedMethods = new List<MethodDefinition>();
|
|
|
|
|
foreach (var type in module.GetTypes()) {
|
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (!method.IsStatic || !method.IsAssembly)
|
|
|
|
|
continue;
|
|
|
|
|
if (method.Body == null)
|
|
|
|
|
continue;
|
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
if (instrs.Count < 2)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
switch (instrs[0].OpCode.Code) {
|
|
|
|
|
case Code.Ldc_I4:
|
|
|
|
|
case Code.Ldc_I4_0:
|
|
|
|
|
case Code.Ldc_I4_1:
|
|
|
|
|
case Code.Ldc_I4_2:
|
|
|
|
|
case Code.Ldc_I4_3:
|
|
|
|
|
case Code.Ldc_I4_4:
|
|
|
|
|
case Code.Ldc_I4_5:
|
|
|
|
|
case Code.Ldc_I4_6:
|
|
|
|
|
case Code.Ldc_I4_7:
|
|
|
|
|
case Code.Ldc_I4_8:
|
|
|
|
|
case Code.Ldc_I4_M1:
|
|
|
|
|
case Code.Ldc_I4_S:
|
|
|
|
|
case Code.Ldc_I8:
|
|
|
|
|
case Code.Ldc_R4:
|
|
|
|
|
case Code.Ldc_R8:
|
|
|
|
|
case Code.Ldftn:
|
|
|
|
|
case Code.Ldnull:
|
|
|
|
|
case Code.Ldstr:
|
|
|
|
|
case Code.Ldtoken:
|
|
|
|
|
case Code.Ldsfld:
|
|
|
|
|
case Code.Ldsflda:
|
|
|
|
|
if (instrs[1].OpCode.Code != Code.Ret)
|
|
|
|
|
continue;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Ldarg:
|
|
|
|
|
case Code.Ldarg_S:
|
|
|
|
|
case Code.Ldarg_0:
|
|
|
|
|
case Code.Ldarg_1:
|
|
|
|
|
case Code.Ldarg_2:
|
|
|
|
|
case Code.Ldarg_3:
|
|
|
|
|
case Code.Call:
|
|
|
|
|
if (!isCallMethod(method))
|
|
|
|
|
continue;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inlinedMethods.Add(method);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
addMethodsToBeRemoved(new UnusedMethodsFinder(module, inlinedMethods).find(), "Inlined method");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isCallMethod(MethodDefinition method) {
|
|
|
|
|
int loadIndex = 0;
|
|
|
|
|
int methodArgsCount = DotNetUtils.getArgsCount(method);
|
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
int i = 0;
|
2011-11-04 14:21:12 +08:00
|
|
|
|
for (; i < instrs.Count && i < methodArgsCount; i++) {
|
2011-11-01 21:23:30 +08:00
|
|
|
|
var instr = instrs[i];
|
|
|
|
|
switch (instr.OpCode.Code) {
|
|
|
|
|
case Code.Ldarg:
|
|
|
|
|
case Code.Ldarg_S:
|
|
|
|
|
case Code.Ldarg_0:
|
|
|
|
|
case Code.Ldarg_1:
|
|
|
|
|
case Code.Ldarg_2:
|
|
|
|
|
case Code.Ldarg_3:
|
|
|
|
|
if (DotNetUtils.getArgIndex(method, instr) != loadIndex)
|
|
|
|
|
return false;
|
|
|
|
|
loadIndex++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (loadIndex != methodArgsCount)
|
|
|
|
|
return false;
|
|
|
|
|
if (i + 1 >= instrs.Count)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (instrs[i].OpCode.Code != Code.Call && instrs[i].OpCode.Code != Code.Callvirt)
|
|
|
|
|
return false;
|
|
|
|
|
if (instrs[i + 1].OpCode.Code != Code.Ret)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-25 01:44:49 +08:00
|
|
|
|
public override IEnumerable<string> getStringDecrypterMethods() {
|
|
|
|
|
var list = new List<string>();
|
2011-10-27 01:49:25 +08:00
|
|
|
|
foreach (var info in stringDecrypter.DecrypterInfos)
|
|
|
|
|
list.Add(info.method.MetadataToken.ToInt32().ToString("X8"));
|
2011-10-31 02:28:13 +08:00
|
|
|
|
if (stringDecrypter.OtherStringDecrypter != null)
|
|
|
|
|
list.Add(stringDecrypter.OtherStringDecrypter.MetadataToken.ToInt32().ToString("X8"));
|
2011-10-25 01:44:49 +08:00
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|