2017-02-13 17:14:22 +08:00
|
|
|
|
/*
|
|
|
|
|
Copyright (C) 2011-2017 TheProxy
|
|
|
|
|
|
|
|
|
|
This file is part of modified 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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-08-08 18:27:21 +08:00
|
|
|
|
using System.Collections.Generic;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
using de4dot.blocks.cflow;
|
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.ConfuserEx
|
|
|
|
|
{
|
|
|
|
|
public class DeobfuscatorInfo : DeobfuscatorInfoBase
|
|
|
|
|
{
|
2017-08-08 18:27:21 +08:00
|
|
|
|
internal const string THE_NAME = "ConfuserEx";
|
2017-08-19 22:40:14 +08:00
|
|
|
|
public const string THE_TYPE = "crx";
|
2017-08-08 18:27:21 +08:00
|
|
|
|
private const string DEFAULT_REGEX = DeobfuscatorBase.DEFAULT_ASIAN_VALID_NAME_REGEX;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
|
|
|
|
public DeobfuscatorInfo()
|
|
|
|
|
: base(DEFAULT_REGEX)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-08 18:27:21 +08:00
|
|
|
|
public override string Name => THE_NAME;
|
|
|
|
|
public override string Type => THE_TYPE;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
|
|
|
|
public override IDeobfuscator CreateDeobfuscator()
|
|
|
|
|
{
|
|
|
|
|
return new Deobfuscator(new Deobfuscator.Options
|
|
|
|
|
{
|
|
|
|
|
RenameResourcesInCode = false,
|
2017-08-08 18:27:21 +08:00
|
|
|
|
ValidNameRegex = validNameRegex.Get()
|
2017-02-13 17:14:22 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-08 18:27:21 +08:00
|
|
|
|
private class Deobfuscator : DeobfuscatorBase
|
2017-02-13 17:14:22 +08:00
|
|
|
|
{
|
2017-08-08 18:27:21 +08:00
|
|
|
|
private readonly ControlFlowFixer _controlFlowFixer = new ControlFlowFixer();
|
|
|
|
|
|
|
|
|
|
private bool _canRemoveLzma = true;
|
2017-07-25 22:37:41 +08:00
|
|
|
|
private ConstantsDecrypter _constantDecrypter;
|
2017-08-08 18:27:21 +08:00
|
|
|
|
private bool _detectedConfuserExAttribute, _deobfuscating;
|
|
|
|
|
private LzmaFinder _lzmaFinder;
|
2017-07-25 22:37:41 +08:00
|
|
|
|
private ProxyCallFixer _proxyCallFixer;
|
2017-08-08 18:27:21 +08:00
|
|
|
|
private ResourceDecrypter _resourceDecrypter;
|
|
|
|
|
private string _version = "";
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
2017-08-08 18:27:21 +08:00
|
|
|
|
public Deobfuscator(Options options)
|
|
|
|
|
: base(options)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-08 18:27:21 +08:00
|
|
|
|
public override string Type => THE_TYPE;
|
|
|
|
|
public override string TypeLong => THE_NAME;
|
|
|
|
|
public override string Name => $"{TypeLong} {_version}";
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
2017-08-08 18:27:21 +08:00
|
|
|
|
public override IEnumerable<IBlocksDeobfuscator> BlocksDeobfuscators
|
2017-02-13 17:14:22 +08:00
|
|
|
|
{
|
2017-08-08 18:27:21 +08:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var list = new List<IBlocksDeobfuscator>();
|
|
|
|
|
list.Add(_controlFlowFixer);
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
2017-08-08 18:27:21 +08:00
|
|
|
|
if (_deobfuscating && _int32ValueInliner != null)
|
2017-08-19 22:40:14 +08:00
|
|
|
|
{
|
|
|
|
|
var constantInliner = new ConstantsInliner(_sbyteValueInliner, _byteValueInliner,
|
|
|
|
|
_int16ValueInliner,
|
|
|
|
|
_uint16ValueInliner, _int32ValueInliner, _uint32ValueInliner, _int64ValueInliner,
|
|
|
|
|
_uint64ValueInliner, _singleValueInliner, _doubleValueInliner, _arrayValueInliner)
|
|
|
|
|
{
|
|
|
|
|
ExecuteIfNotModified = true
|
|
|
|
|
};
|
|
|
|
|
list.Add(constantInliner);
|
|
|
|
|
}
|
2017-08-08 18:27:21 +08:00
|
|
|
|
return list;
|
|
|
|
|
}
|
2017-02-13 17:14:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override int DetectInternal()
|
|
|
|
|
{
|
2017-08-08 18:27:21 +08:00
|
|
|
|
var val = 0;
|
2017-07-25 22:37:41 +08:00
|
|
|
|
if (_detectedConfuserExAttribute) val += 0;
|
|
|
|
|
if (_lzmaFinder.FoundLzma) val += 10;
|
|
|
|
|
if (_constantDecrypter.Detected) val += 10;
|
|
|
|
|
if (_resourceDecrypter.Detected) val += 10;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void ScanForObfuscator()
|
|
|
|
|
{
|
2017-07-25 22:37:41 +08:00
|
|
|
|
_lzmaFinder = new LzmaFinder(module, DeobfuscatedFile);
|
|
|
|
|
_lzmaFinder.Find();
|
|
|
|
|
_constantDecrypter = new ConstantsDecrypter(module, _lzmaFinder.Method, DeobfuscatedFile);
|
|
|
|
|
_resourceDecrypter = new ResourceDecrypter(module, _lzmaFinder.Method, DeobfuscatedFile);
|
|
|
|
|
if (_lzmaFinder.FoundLzma)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
{
|
2017-07-25 22:37:41 +08:00
|
|
|
|
_constantDecrypter.Find();
|
|
|
|
|
_resourceDecrypter.Find();
|
2017-02-13 17:14:22 +08:00
|
|
|
|
}
|
2017-07-25 22:37:41 +08:00
|
|
|
|
|
|
|
|
|
_proxyCallFixer = new ProxyCallFixer(module, DeobfuscatedFile);
|
|
|
|
|
_proxyCallFixer.FindDelegateCreatorMethod();
|
|
|
|
|
_proxyCallFixer.Find();
|
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
DetectConfuserExAttribute();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-25 22:37:41 +08:00
|
|
|
|
private void DetectConfuserExAttribute()
|
2017-02-13 17:14:22 +08:00
|
|
|
|
{
|
|
|
|
|
foreach (var attribute in module.CustomAttributes)
|
|
|
|
|
{
|
|
|
|
|
if (attribute.TypeFullName != "ConfusedByAttribute")
|
|
|
|
|
continue;
|
|
|
|
|
foreach (var argument in attribute.ConstructorArguments)
|
|
|
|
|
{
|
|
|
|
|
if (argument.Type.ElementType != ElementType.String)
|
|
|
|
|
continue;
|
|
|
|
|
var value = argument.Value.ToString();
|
|
|
|
|
if (!value.Contains("ConfuserEx"))
|
|
|
|
|
continue;
|
2017-07-25 22:37:41 +08:00
|
|
|
|
_detectedConfuserExAttribute = true;
|
|
|
|
|
_version = value.Replace("ConfuserEx", "");
|
2017-02-13 17:14:22 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void DeobfuscateBegin()
|
|
|
|
|
{
|
2017-07-25 22:37:41 +08:00
|
|
|
|
if (_constantDecrypter.Detected)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
{
|
2017-08-08 18:27:21 +08:00
|
|
|
|
Logger.w(
|
|
|
|
|
"Constants encryption detected! Please note that the decryption method has to be set manually!"); //TODO: Remove
|
|
|
|
|
|
|
|
|
|
_sbyteValueInliner = new SByteValueInliner();
|
|
|
|
|
_byteValueInliner = new ByteValueInliner();
|
|
|
|
|
_int16ValueInliner = new Int16ValueInliner();
|
|
|
|
|
_uint16ValueInliner = new UInt16ValueInliner();
|
|
|
|
|
_int32ValueInliner = new Int32ValueInliner();
|
|
|
|
|
_uint32ValueInliner = new UInt32ValueInliner();
|
|
|
|
|
_int64ValueInliner = new Int64ValueInliner();
|
|
|
|
|
_uint64ValueInliner = new UInt64ValueInliner();
|
|
|
|
|
_singleValueInliner = new SingleValueInliner();
|
|
|
|
|
_doubleValueInliner = new DoubleValueInliner();
|
|
|
|
|
_arrayValueInliner = new ArrayValueInliner(initializedDataCreator);
|
2017-07-25 22:37:41 +08:00
|
|
|
|
foreach (var info in _constantDecrypter.Decrypters)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
{
|
|
|
|
|
staticStringInliner.Add(info.Method,
|
2017-07-25 22:37:41 +08:00
|
|
|
|
(method, gim, args) => _constantDecrypter.DecryptString(info, gim, (uint) args[0]));
|
2017-08-08 18:27:21 +08:00
|
|
|
|
_sbyteValueInliner.Add(info.Method,
|
2017-07-25 22:37:41 +08:00
|
|
|
|
(method, gim, args) => _constantDecrypter.DecryptSByte(info, gim, (uint) args[0]));
|
2017-08-08 18:27:21 +08:00
|
|
|
|
_byteValueInliner.Add(info.Method,
|
2017-07-25 22:37:41 +08:00
|
|
|
|
(method, gim, args) => _constantDecrypter.DecryptByte(info, gim, (uint) args[0]));
|
2017-08-08 18:27:21 +08:00
|
|
|
|
_int16ValueInliner.Add(info.Method,
|
2017-07-25 22:37:41 +08:00
|
|
|
|
(method, gim, args) => _constantDecrypter.DecryptInt16(info, gim, (uint) args[0]));
|
2017-08-08 18:27:21 +08:00
|
|
|
|
_uint16ValueInliner.Add(info.Method,
|
2017-07-25 22:37:41 +08:00
|
|
|
|
(method, gim, args) => _constantDecrypter.DecryptUInt16(info, gim, (uint) args[0]));
|
2017-08-08 18:27:21 +08:00
|
|
|
|
_int32ValueInliner.Add(info.Method,
|
2017-07-25 22:37:41 +08:00
|
|
|
|
(method, gim, args) => _constantDecrypter.DecryptInt32(info, gim, (uint) args[0]));
|
2017-08-08 18:27:21 +08:00
|
|
|
|
_uint32ValueInliner.Add(info.Method,
|
2017-07-25 22:37:41 +08:00
|
|
|
|
(method, gim, args) => _constantDecrypter.DecryptUInt32(info, gim, (uint) args[0]));
|
2017-08-08 18:27:21 +08:00
|
|
|
|
_int64ValueInliner.Add(info.Method,
|
2017-07-25 22:37:41 +08:00
|
|
|
|
(method, gim, args) => _constantDecrypter.DecryptInt64(info, gim, (uint) args[0]));
|
2017-08-08 18:27:21 +08:00
|
|
|
|
_uint64ValueInliner.Add(info.Method,
|
2017-07-25 22:37:41 +08:00
|
|
|
|
(method, gim, args) => _constantDecrypter.DecryptUInt64(info, gim, (uint) args[0]));
|
2017-08-08 18:27:21 +08:00
|
|
|
|
_singleValueInliner.Add(info.Method,
|
2017-07-25 22:37:41 +08:00
|
|
|
|
(method, gim, args) => _constantDecrypter.DecryptSingle(info, gim, (uint) args[0]));
|
2017-08-08 18:27:21 +08:00
|
|
|
|
_doubleValueInliner.Add(info.Method,
|
2017-07-25 22:37:41 +08:00
|
|
|
|
(method, gim, args) => _constantDecrypter.DecryptDouble(info, gim, (uint) args[0]));
|
2017-08-08 18:27:21 +08:00
|
|
|
|
_arrayValueInliner.Add(info.Method,
|
2017-07-25 22:37:41 +08:00
|
|
|
|
(method, gim, args) => _constantDecrypter.DecryptArray(info, gim, (uint) args[0]));
|
2017-02-13 17:14:22 +08:00
|
|
|
|
}
|
2017-07-25 22:37:41 +08:00
|
|
|
|
_deobfuscating = true;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
}
|
2017-07-25 22:37:41 +08:00
|
|
|
|
if (_resourceDecrypter.Detected)
|
|
|
|
|
{
|
2017-08-08 18:27:21 +08:00
|
|
|
|
Logger.w(
|
|
|
|
|
"Resource encryption detected! Please note that the decryption method has to be set manually!"); //TODO: Remove
|
2017-07-25 22:37:41 +08:00
|
|
|
|
_resourceDecrypter.Fix();
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
base.DeobfuscateBegin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void DeobfuscateEnd()
|
|
|
|
|
{
|
|
|
|
|
FindAndRemoveInlinedMethods();
|
|
|
|
|
|
2017-08-08 18:27:21 +08:00
|
|
|
|
var toRemoveFromCctor = new List<MethodDef>();
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
2017-07-25 22:37:41 +08:00
|
|
|
|
if (_constantDecrypter.Detected)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
if (CanRemoveStringDecrypterType)
|
|
|
|
|
{
|
2017-07-25 22:37:41 +08:00
|
|
|
|
toRemoveFromCctor.Add(_constantDecrypter.Method);
|
|
|
|
|
AddMethodToBeRemoved(_constantDecrypter.Method, "Constant Decrypter Initializer");
|
|
|
|
|
foreach (var dec in _constantDecrypter.Decrypters)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
AddMethodToBeRemoved(dec.Method, "Constant Decrypter Method");
|
2017-07-25 22:37:41 +08:00
|
|
|
|
AddFieldsToBeRemoved(_constantDecrypter.Fields, "Constant Decrypter Fields");
|
|
|
|
|
AddTypeToBeRemoved(_constantDecrypter.Type, "Array field signature type");
|
2017-02-13 17:14:22 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
2017-08-08 18:27:21 +08:00
|
|
|
|
{
|
2017-07-25 22:37:41 +08:00
|
|
|
|
_canRemoveLzma = false;
|
2017-08-08 18:27:21 +08:00
|
|
|
|
}
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
2017-07-25 22:37:41 +08:00
|
|
|
|
if (_resourceDecrypter.Detected && _resourceDecrypter.CanRemoveLzma)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
{
|
2017-07-25 22:37:41 +08:00
|
|
|
|
toRemoveFromCctor.Add(_resourceDecrypter.Method);
|
|
|
|
|
AddMethodToBeRemoved(_resourceDecrypter.Method, "Resource decrypter Initializer method");
|
|
|
|
|
AddMethodToBeRemoved(_resourceDecrypter.AssembyResolveMethod,
|
2017-02-13 17:14:22 +08:00
|
|
|
|
"Resource decrypter AssemblyResolve method");
|
2017-07-25 22:37:41 +08:00
|
|
|
|
AddFieldsToBeRemoved(_resourceDecrypter.Fields, "Constant Decrypter Fields");
|
|
|
|
|
AddTypeToBeRemoved(_resourceDecrypter.Type, "Array field signature type");
|
2017-02-13 17:14:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-25 22:37:41 +08:00
|
|
|
|
if (!_constantDecrypter.CanRemoveLzma || !_resourceDecrypter.CanRemoveLzma)
|
|
|
|
|
_canRemoveLzma = false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
2017-07-25 22:37:41 +08:00
|
|
|
|
if (_lzmaFinder.FoundLzma && _canRemoveLzma)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
{
|
2017-07-25 22:37:41 +08:00
|
|
|
|
AddMethodToBeRemoved(_lzmaFinder.Method, "Lzma Decompress method");
|
|
|
|
|
AddTypesToBeRemoved(_lzmaFinder.Types, "Lzma Nested Types");
|
2017-02-13 17:14:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-25 22:37:41 +08:00
|
|
|
|
if (_proxyCallFixer.Detected)
|
|
|
|
|
{
|
|
|
|
|
AddTypesToBeRemoved(_proxyCallFixer.DelegateTypes, "Proxy delegates");
|
|
|
|
|
AddMethodsToBeRemoved(_proxyCallFixer.DelegateCreatorMethods, "Proxy creator methods");
|
|
|
|
|
AddTypesToBeRemoved(_proxyCallFixer.AttributeTypes, "Proxy creator attributes");
|
|
|
|
|
AddMethodsToBeRemoved(_proxyCallFixer.NativeMethods, "Proxy native methods");
|
|
|
|
|
}
|
2017-08-08 18:27:21 +08:00
|
|
|
|
|
2017-07-25 22:37:41 +08:00
|
|
|
|
AddMethodsToBeRemoved(_controlFlowFixer.NativeMethods, "Control flow native methods");
|
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
var moduleCctor = DotNetUtils.GetModuleTypeCctor(module);
|
|
|
|
|
foreach (var instr in moduleCctor.Body.Instructions)
|
2017-08-19 22:40:14 +08:00
|
|
|
|
if (instr.OpCode == OpCodes.Call && instr.Operand is MethodDef
|
|
|
|
|
&& toRemoveFromCctor.Contains((MethodDef)instr.Operand))
|
2017-02-13 17:14:22 +08:00
|
|
|
|
instr.OpCode = OpCodes.Nop;
|
|
|
|
|
|
|
|
|
|
//No more mixed!
|
|
|
|
|
module.IsILOnly = true;
|
|
|
|
|
|
|
|
|
|
base.DeobfuscateEnd();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override IEnumerable<int> GetStringDecrypterMethods()
|
|
|
|
|
{
|
2017-08-08 18:27:21 +08:00
|
|
|
|
return new List<int>();
|
2017-02-13 17:14:22 +08:00
|
|
|
|
}
|
2017-07-25 22:37:41 +08:00
|
|
|
|
|
|
|
|
|
public override void DeobfuscateMethodEnd(Blocks blocks)
|
|
|
|
|
{
|
|
|
|
|
_proxyCallFixer.Deobfuscate(blocks);
|
|
|
|
|
base.DeobfuscateMethodEnd(blocks);
|
|
|
|
|
}
|
2017-08-08 18:27:21 +08:00
|
|
|
|
|
|
|
|
|
internal class Options : OptionsBase
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region ConstantInliners
|
|
|
|
|
|
|
|
|
|
private SByteValueInliner _sbyteValueInliner;
|
|
|
|
|
private ByteValueInliner _byteValueInliner;
|
|
|
|
|
private Int16ValueInliner _int16ValueInliner;
|
|
|
|
|
private UInt16ValueInliner _uint16ValueInliner;
|
|
|
|
|
private Int32ValueInliner _int32ValueInliner;
|
|
|
|
|
private UInt32ValueInliner _uint32ValueInliner;
|
|
|
|
|
private Int64ValueInliner _int64ValueInliner;
|
|
|
|
|
private UInt64ValueInliner _uint64ValueInliner;
|
|
|
|
|
private SingleValueInliner _singleValueInliner;
|
|
|
|
|
private DoubleValueInliner _doubleValueInliner;
|
|
|
|
|
private ArrayValueInliner _arrayValueInliner;
|
|
|
|
|
|
|
|
|
|
#endregion
|
2017-02-13 17:14:22 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|