2011-11-06 21:42:52 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2011-11-06 21:42:52 +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;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
2011-11-06 21:42:52 +08:00
|
|
|
|
|
2011-12-09 16:02:06 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.Xenocode {
|
|
|
|
|
public class DeobfuscatorInfo : DeobfuscatorInfoBase {
|
2011-11-06 21:42:52 +08:00
|
|
|
|
public const string THE_NAME = "Xenocode";
|
2011-11-12 18:31:07 +08:00
|
|
|
|
public const string THE_TYPE = "xc";
|
2013-11-18 22:43:08 +08:00
|
|
|
|
const string DEFAULT_REGEX = @"!^[oO01l]{4,}$&!^(get_|set_|add_|remove_|_)?[x_][a-f0-9]{16,}$&" + DeobfuscatorBase.DEFAULT_ASIAN_VALID_NAME_REGEX;
|
|
|
|
|
|
2011-11-06 21:42:52 +08:00
|
|
|
|
public DeobfuscatorInfo()
|
|
|
|
|
: base(DEFAULT_REGEX) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Name {
|
|
|
|
|
get { return THE_NAME; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Type {
|
2011-11-12 18:31:07 +08:00
|
|
|
|
get { return THE_TYPE; }
|
2011-11-06 21:42:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override IDeobfuscator CreateDeobfuscator() {
|
2011-11-06 21:42:52 +08:00
|
|
|
|
return new Deobfuscator(new Deobfuscator.Options {
|
|
|
|
|
ValidNameRegex = validNameRegex.get(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Deobfuscator : DeobfuscatorBase {
|
|
|
|
|
bool foundXenocodeAttribute = false;
|
|
|
|
|
StringDecrypter stringDecrypter;
|
|
|
|
|
|
|
|
|
|
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-06 21:42:52 +08:00
|
|
|
|
get { return DeobfuscatorInfo.THE_NAME; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Name {
|
2011-11-12 18:31:07 +08:00
|
|
|
|
get { return TypeLong; }
|
2011-11-06 21:42:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Deobfuscator(Options options)
|
|
|
|
|
: base(options) {
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override int DetectInternal() {
|
2011-11-06 21:42:52 +08:00
|
|
|
|
int val = 0;
|
|
|
|
|
|
2012-02-29 07:13:57 +08:00
|
|
|
|
if (stringDecrypter.Detected)
|
2011-11-06 21:42:52 +08:00
|
|
|
|
val += 100;
|
2012-02-29 07:13:57 +08:00
|
|
|
|
if (foundXenocodeAttribute)
|
2011-11-06 21:42:52 +08:00
|
|
|
|
val += 10;
|
|
|
|
|
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override void ScanForObfuscator() {
|
|
|
|
|
FindXenocodeAttribute();
|
2011-11-06 21:42:52 +08:00
|
|
|
|
stringDecrypter = new StringDecrypter(module);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
stringDecrypter.Find();
|
2011-11-06 21:42:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void FindXenocodeAttribute() {
|
2011-11-06 21:42:52 +08:00
|
|
|
|
foreach (var type in module.Types) {
|
|
|
|
|
switch (type.FullName) {
|
|
|
|
|
case "Xenocode.Client.Attributes.AssemblyAttributes.ProcessedByXenocode":
|
|
|
|
|
case "Xenocode.Client.Attributes.AssemblyAttributes.SuppressDisassembly":
|
2012-12-30 19:34:21 +08:00
|
|
|
|
case "Xenocode.User.Attributes.AssemblyAttributes.ProcessedByXenoCode":
|
|
|
|
|
case "Xenocode.User.Attributes.AssemblyAttributes.SuppressDisassembly":
|
2013-01-19 20:03:57 +08:00
|
|
|
|
AddAttributeToBeRemoved(type, "Obfuscator attribute");
|
2011-11-06 21:42:52 +08:00
|
|
|
|
foundXenocodeAttribute = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override void DeobfuscateBegin() {
|
|
|
|
|
base.DeobfuscateBegin();
|
2011-11-06 21:42:52 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
staticStringInliner.Add(stringDecrypter.Method, (method, gim, args) => stringDecrypter.Decrypt((string)args[0], (int)args[1]));
|
|
|
|
|
DeobfuscatedFile.StringDecryptersAdded();
|
2011-11-06 21:42:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override void DeobfuscateEnd() {
|
2012-02-21 23:56:29 +08:00
|
|
|
|
if (CanRemoveStringDecrypterType)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
AddTypeToBeRemoved(stringDecrypter.Type, "String decrypter type");
|
|
|
|
|
var obfType = FindTypeWithThousandsOfMethods();
|
2012-12-30 19:34:21 +08:00
|
|
|
|
if (obfType != null)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
AddTypeToBeRemoved(obfType, "Obfuscator type with thousands of empty methods");
|
|
|
|
|
RemoveInvalidAttributes(module);
|
|
|
|
|
RemoveInvalidAttributes(module.Assembly);
|
|
|
|
|
base.DeobfuscateEnd();
|
2011-11-06 21:42:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
TypeDef FindTypeWithThousandsOfMethods() {
|
2012-12-30 19:34:21 +08:00
|
|
|
|
foreach (var type in module.Types) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (IsTypeWithThousandsOfMethods(type))
|
2012-12-30 19:34:21 +08:00
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool IsTypeWithThousandsOfMethods(TypeDef type) {
|
2012-12-30 19:34:21 +08:00
|
|
|
|
if (!type.IsNotPublic)
|
|
|
|
|
return false;
|
|
|
|
|
if (type.HasFields || type.HasEvents || type.HasProperties)
|
|
|
|
|
return false;
|
|
|
|
|
if (type.Methods.Count < 100)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (method.IsStaticConstructor)
|
|
|
|
|
return false;
|
|
|
|
|
if (method.IsConstructor) {
|
|
|
|
|
if (method.MethodSig.GetParamCount() != 0)
|
|
|
|
|
return false;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!method.IsPrivate || method.IsStatic)
|
|
|
|
|
return false;
|
|
|
|
|
if (method.Body == null)
|
|
|
|
|
return false;
|
|
|
|
|
if (method.Body.Instructions.Count != 1)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove the attribute Xenocode adds that has an invalid ctor
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void RemoveInvalidAttributes(IHasCustomAttribute hca) {
|
2012-12-30 19:34:21 +08:00
|
|
|
|
if (!CanRemoveTypes)
|
|
|
|
|
return;
|
|
|
|
|
if (hca == null)
|
|
|
|
|
return;
|
|
|
|
|
for (int i = hca.CustomAttributes.Count - 1; i >= 0; i--) {
|
|
|
|
|
var ca = hca.CustomAttributes[i];
|
|
|
|
|
if (ca.Constructor == null)
|
|
|
|
|
hca.CustomAttributes.RemoveAt(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-11-06 22:24:30 +08:00
|
|
|
|
if (stringDecrypter.Method != null)
|
2012-11-02 22:57:11 +08:00
|
|
|
|
list.Add(stringDecrypter.Method.MDToken.ToInt32());
|
2011-11-06 21:42:52 +08:00
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|