de4dot-cex/de4dot.code/deobfuscators/CodeVeil/v3_v4/Deobfuscator.cs

161 lines
4.1 KiB
C#
Raw Normal View History

2012-02-05 23:17:47 +08:00
/*
Copyright (C) 2011-2012 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;
using Mono.MyStuff;
2012-02-06 22:47:10 +08:00
namespace de4dot.code.deobfuscators.CodeVeil.v3_v4 {
2012-02-05 23:17:47 +08:00
public class DeobfuscatorInfo : DeobfuscatorInfoBase {
public const string THE_NAME = "CodeVeil";
2012-02-06 22:47:10 +08:00
public const string THE_TYPE = "cv4";
2012-02-06 15:20:04 +08:00
const string DEFAULT_REGEX = @"!^[A-Za-z]{1,2}$&" + DeobfuscatorBase.DEFAULT_VALID_NAME_REGEX;
2012-02-05 23:17:47 +08:00
public DeobfuscatorInfo()
2012-02-06 15:20:04 +08:00
: base(DEFAULT_REGEX) {
2012-02-05 23:17:47 +08:00
}
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(),
});
}
protected override IEnumerable<Option> getOptionsInternal() {
return new List<Option>() {
};
}
}
class Deobfuscator : DeobfuscatorBase {
Options options;
2012-02-06 22:55:14 +08:00
string obfuscatorName = DeobfuscatorInfo.THE_NAME + " 3.x - 4.x";
2012-02-05 23:17:47 +08:00
bool foundKillType = false;
MethodsDecrypter methodsDecrypter;
2012-02-06 01:47:31 +08:00
StringDecrypter stringDecrypter;
2012-02-05 23:17:47 +08:00
internal class Options : OptionsBase {
}
public override string Type {
get { return DeobfuscatorInfo.THE_TYPE; }
}
public override string TypeLong {
get { return DeobfuscatorInfo.THE_NAME; }
}
public override string Name {
get { return obfuscatorName; }
}
public Deobfuscator(Options options)
: base(options) {
this.options = options;
}
protected override int detectInternal() {
int val = 0;
2012-02-06 02:01:49 +08:00
int sum = toInt32(methodsDecrypter.Detected) +
2012-02-06 01:47:31 +08:00
toInt32(stringDecrypter.Detected);
2012-02-05 23:17:47 +08:00
if (sum > 0)
val += 100 + 10 * (sum - 1);
2012-02-06 02:01:49 +08:00
if (foundKillType)
val += 10;
2012-02-05 23:17:47 +08:00
return val;
}
protected override void scanForObfuscator() {
findKillType();
methodsDecrypter = new MethodsDecrypter(module);
methodsDecrypter.find();
2012-02-06 01:47:31 +08:00
stringDecrypter = new StringDecrypter(module);
stringDecrypter.find();
2012-02-05 23:17:47 +08:00
}
void findKillType() {
foreach (var type in module.Types) {
if (type.FullName == "____KILL") {
addTypeToBeRemoved(type, "KILL type");
2012-02-06 01:47:31 +08:00
foundKillType = true;
2012-02-05 23:17:47 +08:00
break;
}
}
}
public override bool getDecryptedModule(ref byte[] newFileData, ref Dictionary<uint, DumpedMethod> dumpedMethods) {
if (!methodsDecrypter.Detected)
return false;
var fileData = DeobUtils.readModule(module);
if (!methodsDecrypter.decrypt(fileData, ref dumpedMethods))
return false;
newFileData = fileData;
return true;
}
public override IDeobfuscator moduleReloaded(ModuleDefinition module) {
var newOne = new Deobfuscator(options);
newOne.setModule(module);
newOne.methodsDecrypter = new MethodsDecrypter(module, methodsDecrypter);
2012-02-06 01:47:31 +08:00
newOne.stringDecrypter = new StringDecrypter(module, stringDecrypter);
2012-02-05 23:17:47 +08:00
return newOne;
}
public override void deobfuscateBegin() {
base.deobfuscateBegin();
2012-02-06 01:47:31 +08:00
if (Operations.DecryptStrings != OpDecryptString.None) {
stringDecrypter.initialize();
staticStringInliner.add(stringDecrypter.DecryptMethod, (method, args) => {
return stringDecrypter.decrypt((int)args[0]);
});
DeobfuscatedFile.stringDecryptersAdded();
}
2012-02-05 23:17:47 +08:00
//TODO:
}
public override void deobfuscateEnd() {
//TODO:
base.deobfuscateEnd();
}
public override IEnumerable<string> getStringDecrypterMethods() {
var list = new List<string>();
if (stringDecrypter.DecryptMethod != null)
list.Add(stringDecrypter.DecryptMethod.MetadataToken.ToInt32().ToString("X8"));
2012-02-05 23:17:47 +08:00
return list;
}
}
}