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
/*
2014-03-12 05:15:43 +08:00
Copyright (C) 2011-2014 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 dnlib.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";
2014-04-14 12:26:06 +08:00
const string DEFAULT_REGEX = @"!^(?:eval_)?[a-z][a-z0-9]{0,2}$&!^A_[0-9]+$&" + DeobfuscatorBase.DEFAULT_ASIAN_VALID_NAME_REGEX;
2011-09-22 10:55:30 +08:00
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
}
2013-01-19 20:03:57 +08:00
public override IDeobfuscator CreateDeobfuscator() {
2011-09-22 10:55:30 +08:00
return new Deobfuscator(new Deobfuscator.Options {
RenameResourcesInCode = false,
2013-11-18 23:28:44 +08:00
ValidNameRegex = validNameRegex.Get(),
2011-09-22 10:55:30 +08:00
});
}
}
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;
}
2013-01-19 20:03:57 +08:00
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;
}
2013-01-19 20:03:57 +08:00
protected override void ScanForObfuscator() {
2011-12-22 02:22:23 +08:00
stringDecrypter = new StringDecrypter(module);
2013-01-19 20:03:57 +08:00
stringDecrypter.Find(DeobfuscatedFile);
FindDotfuscatorAttribute();
2011-09-22 10:55:30 +08:00
}
2013-01-19 20:03:57 +08:00
void FindDotfuscatorAttribute() {
2011-09-22 10:55:30 +08:00
foreach (var type in module.Types) {
if (type.FullName == "DotfuscatorAttribute") {
foundDotfuscatorAttribute = true;
2013-01-19 20:03:57 +08:00
AddAttributeToBeRemoved(type, "Obfuscator attribute");
InitializeVersion(type);
2011-09-22 10:55:30 +08:00
return;
}
}
}
2013-01-19 20:03:57 +08:00
void InitializeVersion(TypeDef attr) {
var s = DotNetUtils.GetCustomArgAsString(GetAssemblyAttribute(attr), 0);
2011-09-22 10:55:30 +08:00
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();
}
2013-01-19 20:03:57 +08:00
public override void DeobfuscateBegin() {
base.DeobfuscateBegin();
2011-12-22 02:22:23 +08:00
foreach (var info in stringDecrypter.StringDecrypterInfos)
2013-01-19 20:03:57 +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
}
2013-01-19 20:03:57 +08:00
public override void DeobfuscateEnd() {
if (CanRemoveStringDecrypterType)
2013-01-19 20:03:57 +08:00
AddMethodsToBeRemoved(stringDecrypter.StringDecrypters, "String decrypter method");
2011-09-22 10:55:30 +08:00
2013-01-19 20:03:57 +08:00
base.DeobfuscateEnd();
2011-09-22 10:55:30 +08:00
}
2013-01-19 20:03:57 +08:00
public override IEnumerable<int> GetStringDecrypterMethods() {
2012-02-25 13:25:40 +08:00
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;
}
}
}