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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2011-10-27 01:49:25 +08:00
|
|
|
|
using System.IO;
|
2011-10-25 01:44:49 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Mono.Cecil;
|
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.deobfuscators.dotNET_Reactor {
|
|
|
|
|
class DeobfuscatorInfo : DeobfuscatorInfoBase {
|
|
|
|
|
public DeobfuscatorInfo()
|
|
|
|
|
: base("dr") {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static string ObfuscatorType {
|
2011-10-26 20:29:12 +08:00
|
|
|
|
get { return "dotNetReactor"; }
|
2011-10-25 01:44:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Type {
|
|
|
|
|
get { return ObfuscatorType; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override IDeobfuscator createDeobfuscator() {
|
|
|
|
|
return new Deobfuscator(new Deobfuscator.Options {
|
|
|
|
|
ValidNameRegex = validNameRegex.get(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override IEnumerable<Option> getOptionsInternal() {
|
|
|
|
|
return new List<Option>() {
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Deobfuscator : DeobfuscatorBase {
|
|
|
|
|
Options options;
|
|
|
|
|
|
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-25 14:27:36 +08:00
|
|
|
|
|
2011-10-25 01:44:49 +08:00
|
|
|
|
internal class Options : OptionsBase {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Type {
|
|
|
|
|
get { return DeobfuscatorInfo.ObfuscatorType; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Name {
|
|
|
|
|
get { return ".NET Reactor"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-25 14:27:36 +08:00
|
|
|
|
if (methodsDecrypter.Detected)
|
2011-10-27 01:49:25 +08:00
|
|
|
|
val += 100;
|
|
|
|
|
else if (stringDecrypter.Detected)
|
|
|
|
|
val += 100;
|
|
|
|
|
if (methodsDecrypter.Detected && stringDecrypter.Detected)
|
|
|
|
|
val += 10;
|
2011-10-25 14:27:36 +08:00
|
|
|
|
|
2011-10-25 01:44:49 +08:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
stringDecrypter.find();
|
2011-10-25 01:44:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 20:40:55 +08:00
|
|
|
|
public override byte[] getDecryptedModule() {
|
2011-10-27 01:49:25 +08:00
|
|
|
|
using (var fileStream = new FileStream(module.FullyQualifiedName, FileMode.Open, FileAccess.Read, FileShare.Read)) {
|
|
|
|
|
fileData = new byte[(int)fileStream.Length];
|
|
|
|
|
fileStream.Read(fileData, 0, fileData.Length);
|
|
|
|
|
}
|
|
|
|
|
peImage = new PE.PeImage(fileData);
|
|
|
|
|
|
|
|
|
|
if (!methodsDecrypter.decrypt(peImage, DeobfuscatedFile))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return fileData;
|
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 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-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
|
|
|
|
|
|
|
|
|
stringDecrypter.init(peImage, DeobfuscatedFile);
|
|
|
|
|
|
|
|
|
|
foreach (var info in stringDecrypter.DecrypterInfos) {
|
|
|
|
|
staticStringDecrypter.add(info.method, (method2, args) => {
|
|
|
|
|
return stringDecrypter.decrypt(method2, (int)args[0]);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
DeobfuscatedFile.stringDecryptersAdded();
|
2011-10-25 01:44:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void deobfuscateMethodEnd(Blocks blocks) {
|
|
|
|
|
base.deobfuscateMethodEnd(blocks);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void deobfuscateEnd() {
|
|
|
|
|
base.deobfuscateEnd();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-25 01:44:49 +08:00
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|