de4dot-cex/de4dot.code/deobfuscators/Dotfuscator/Deobfuscator.cs

137 lines
3.7 KiB
C#
Raw Normal View History

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/>.
*/
using System.Collections.Generic;
using dot10.DotNet;
2011-09-22 10:55:30 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.Dotfuscator {
public class DeobfuscatorInfo : DeobfuscatorInfoBase {
2011-11-04 03:03:32 +08:00
public const string THE_NAME = "Dotfuscator";
2011-11-12 18:31:07 +08:00
public const string THE_TYPE = "df";
2011-09-22 10:55:30 +08:00
const string DEFAULT_REGEX = @"!^[a-z][a-z0-9]{0,2}$&!^A_[0-9]+$&" + DeobfuscatorBase.DEFAULT_VALID_NAME_REGEX;
public DeobfuscatorInfo()
2011-11-04 03:03:32 +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 {
RenameResourcesInCode = false,
ValidNameRegex = validNameRegex.get(),
});
}
}
class Deobfuscator : DeobfuscatorBase {
Options options;
string obfuscatorName = "Dotfuscator";
2011-12-22 02:22:23 +08:00
StringDecrypter stringDecrypter;
bool foundDotfuscatorAttribute = false;
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 {
get { return obfuscatorName; }
}
public Deobfuscator(Options options)
: base(options) {
this.options = options;
}
protected override int detectInternal() {
2011-09-22 10:55:30 +08:00
int val = 0;
if (stringDecrypter.Detected)
val += 100;
if (foundDotfuscatorAttribute)
val += 10;
2011-09-22 10:55:30 +08:00
return val;
}
protected override void scanForObfuscator() {
2011-12-22 02:22:23 +08:00
stringDecrypter = new StringDecrypter(module);
stringDecrypter.find(DeobfuscatedFile);
2011-09-22 10:55:30 +08:00
findDotfuscatorAttribute();
}
void findDotfuscatorAttribute() {
foreach (var type in module.Types) {
if (type.FullName == "DotfuscatorAttribute") {
foundDotfuscatorAttribute = true;
addAttributeToBeRemoved(type, "Obfuscator attribute");
initializeVersion(type);
return;
}
}
}
void initializeVersion(TypeDef attr) {
2011-09-22 10:55:30 +08:00
var s = DotNetUtils.getCustomArgAsString(getAssemblyAttribute(attr), 0);
if (s == null)
return;
2011-10-11 00:39:42 +08:00
var val = System.Text.RegularExpressions.Regex.Match(s, @"^(\d+(?::\d+)*\.\d+(?:\.\d+)*)$");
2011-09-22 10:55:30 +08:00
if (val.Groups.Count < 2)
return;
obfuscatorName = "Dotfuscator " + val.Groups[1].ToString();
}
2011-12-22 02:22:23 +08:00
public override void deobfuscateBegin() {
base.deobfuscateBegin();
foreach (var info in stringDecrypter.StringDecrypterInfos)
2012-07-28 10:22:17 +08:00
staticStringInliner.add(info.method, (method, gim, args) => stringDecrypter.decrypt(method, (string)args[0], (int)args[1]));
DeobfuscatedFile.stringDecryptersAdded();
2011-09-22 10:55:30 +08:00
}
public override void deobfuscateEnd() {
if (CanRemoveStringDecrypterType)
2011-12-22 02:22:23 +08:00
addMethodsToBeRemoved(stringDecrypter.StringDecrypters, "String decrypter method");
2011-09-22 10:55:30 +08:00
base.deobfuscateEnd();
}
2012-02-25 13:25:40 +08:00
public override IEnumerable<int> getStringDecrypterMethods() {
var list = new List<int>();
2011-12-22 02:22:23 +08:00
foreach (var method in stringDecrypter.StringDecrypters)
list.Add(method.MDToken.ToInt32());
2011-09-22 10:55:30 +08:00
return list;
}
}
}