2012-07-28 10:45:27 +08:00
|
|
|
|
/*
|
2015-10-30 05:45:26 +08:00
|
|
|
|
Copyright (C) 2011-2015 de4dot@gmail.com
|
2012-07-28 10:45:27 +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;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
2012-12-23 04:08:29 +08:00
|
|
|
|
using dnlib.IO;
|
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.MD;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2012-07-28 10:45:27 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.Confuser {
|
2012-08-08 07:33:26 +08:00
|
|
|
|
// Since v1.8 r75367
|
2012-08-09 17:47:18 +08:00
|
|
|
|
class ConstantsDecrypterV18 : IVersionProvider {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-07-28 10:45:27 +08:00
|
|
|
|
byte[] fileData;
|
|
|
|
|
ISimpleDeobfuscator simpleDeobfuscator;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
FieldDef dictField, dataField;
|
|
|
|
|
MethodDef installMethod;
|
2012-12-01 04:05:54 +08:00
|
|
|
|
MethodDefAndDeclaringTypeDict<DecrypterInfo> decrypters = new MethodDefAndDeclaringTypeDict<DecrypterInfo>();
|
2012-07-28 10:45:27 +08:00
|
|
|
|
uint key0, key0d;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
MethodDef nativeMethod;
|
2013-01-20 22:59:30 +08:00
|
|
|
|
TypeDef lzmaType;
|
2012-07-28 10:45:27 +08:00
|
|
|
|
EmbeddedResource resource;
|
|
|
|
|
byte[] constants;
|
2012-08-08 20:30:46 +08:00
|
|
|
|
ConfuserVersion version = ConfuserVersion.Unknown;
|
2012-07-29 16:17:05 +08:00
|
|
|
|
|
2012-08-08 20:30:46 +08:00
|
|
|
|
public enum ConfuserVersion {
|
2012-07-29 16:17:05 +08:00
|
|
|
|
Unknown,
|
2012-08-08 20:30:46 +08:00
|
|
|
|
v18_r75367_normal,
|
|
|
|
|
v18_r75367_dynamic,
|
|
|
|
|
v18_r75367_native,
|
2012-08-09 00:34:24 +08:00
|
|
|
|
v18_r75369_normal,
|
|
|
|
|
v18_r75369_dynamic,
|
|
|
|
|
v18_r75369_native,
|
2013-01-20 22:59:30 +08:00
|
|
|
|
v19_r77172_normal,
|
|
|
|
|
v19_r77172_dynamic,
|
|
|
|
|
v19_r77172_native,
|
2013-01-21 01:02:19 +08:00
|
|
|
|
v19_r78056_normal,
|
|
|
|
|
v19_r78056_dynamic,
|
|
|
|
|
v19_r78056_native,
|
2013-02-03 00:03:56 +08:00
|
|
|
|
v19_r78363_normal,
|
|
|
|
|
v19_r78363_dynamic,
|
|
|
|
|
v19_r78363_native,
|
2013-09-23 00:35:58 +08:00
|
|
|
|
v19_r79630_normal,
|
|
|
|
|
v19_r79630_dynamic,
|
|
|
|
|
v19_r79630_native,
|
2012-07-29 16:17:05 +08:00
|
|
|
|
}
|
2012-07-28 10:45:27 +08:00
|
|
|
|
|
|
|
|
|
public class DecrypterInfo {
|
2012-08-08 07:33:26 +08:00
|
|
|
|
readonly ConstantsDecrypterV18 constantsDecrypter;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public readonly MethodDef method;
|
2012-07-28 10:45:27 +08:00
|
|
|
|
public ulong key0l, key1l, key2l;
|
|
|
|
|
public uint key0, key0d;
|
2012-08-08 20:30:46 +08:00
|
|
|
|
readonly ConfuserVersion version;
|
2012-07-28 10:45:27 +08:00
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public DecrypterInfo(ConstantsDecrypterV18 constantsDecrypter, MethodDef method, ConfuserVersion version) {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
this.constantsDecrypter = constantsDecrypter;
|
|
|
|
|
this.method = method;
|
2012-08-08 20:30:46 +08:00
|
|
|
|
this.version = version;
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public string DecryptString(uint magic1, ulong magic2) {
|
|
|
|
|
return Encoding.UTF8.GetString(Decrypt(magic1, magic2));
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public int DecryptInt32(uint magic1, ulong magic2) {
|
|
|
|
|
return BitConverter.ToInt32(Decrypt(magic1, magic2), 0);
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public long DecryptInt64(uint magic1, ulong magic2) {
|
|
|
|
|
return BitConverter.ToInt64(Decrypt(magic1, magic2), 0);
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public float DecryptSingle(uint magic1, ulong magic2) {
|
|
|
|
|
return BitConverter.ToSingle(Decrypt(magic1, magic2), 0);
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public double DecryptDouble(uint magic1, ulong magic2) {
|
|
|
|
|
return BitConverter.ToDouble(Decrypt(magic1, magic2), 0);
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
byte[] Decrypt(uint magic1, ulong magic2) {
|
|
|
|
|
ulong info = Hash(method.DeclaringType.MDToken.ToUInt32() * magic1) ^ magic2;
|
2012-07-28 10:45:27 +08:00
|
|
|
|
int offset = (int)(info >> 32);
|
|
|
|
|
int len = (int)info;
|
|
|
|
|
var decrypted = new byte[len];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
byte[] key = BitConverter.GetBytes(method.MDToken.ToUInt32() ^ key0d);
|
2012-07-28 10:45:27 +08:00
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
|
decrypted[i] = (byte)(constantsDecrypter.constants[offset + i] ^ key[(offset + i) & 3]);
|
|
|
|
|
return decrypted;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
ulong Hash(uint magic) {
|
2012-08-08 20:30:46 +08:00
|
|
|
|
switch (version) {
|
|
|
|
|
case ConfuserVersion.v18_r75367_normal:
|
|
|
|
|
case ConfuserVersion.v18_r75367_dynamic:
|
|
|
|
|
case ConfuserVersion.v18_r75367_native:
|
2013-01-19 20:09:49 +08:00
|
|
|
|
return Hash1(key0l ^ magic);
|
2012-08-09 00:34:24 +08:00
|
|
|
|
case ConfuserVersion.v18_r75369_normal:
|
|
|
|
|
case ConfuserVersion.v18_r75369_dynamic:
|
|
|
|
|
case ConfuserVersion.v18_r75369_native:
|
2013-01-20 22:59:30 +08:00
|
|
|
|
case ConfuserVersion.v19_r77172_normal:
|
|
|
|
|
case ConfuserVersion.v19_r77172_dynamic:
|
|
|
|
|
case ConfuserVersion.v19_r77172_native:
|
2013-01-21 01:02:19 +08:00
|
|
|
|
case ConfuserVersion.v19_r78056_normal:
|
|
|
|
|
case ConfuserVersion.v19_r78056_dynamic:
|
|
|
|
|
case ConfuserVersion.v19_r78056_native:
|
2013-02-03 00:03:56 +08:00
|
|
|
|
case ConfuserVersion.v19_r78363_normal:
|
|
|
|
|
case ConfuserVersion.v19_r78363_dynamic:
|
|
|
|
|
case ConfuserVersion.v19_r78363_native:
|
2013-09-23 00:35:58 +08:00
|
|
|
|
case ConfuserVersion.v19_r79630_normal:
|
|
|
|
|
case ConfuserVersion.v19_r79630_dynamic:
|
|
|
|
|
case ConfuserVersion.v19_r79630_native:
|
2013-01-19 20:09:49 +08:00
|
|
|
|
return Hash1(key0l * magic);
|
2012-08-08 20:30:46 +08:00
|
|
|
|
default:
|
|
|
|
|
throw new ApplicationException("Invalid version");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
ulong Hash1(ulong h0) {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
ulong h1 = key1l;
|
|
|
|
|
ulong h2 = key2l;
|
|
|
|
|
h1 *= h0;
|
|
|
|
|
h2 *= h0;
|
|
|
|
|
h0 *= h0;
|
|
|
|
|
ulong hash = 14695981039346656037UL;
|
|
|
|
|
while (h0 != 0) {
|
|
|
|
|
hash *= 1099511628211UL;
|
|
|
|
|
hash = (hash ^ h0) + (h1 ^ h2) * key0;
|
|
|
|
|
h1 *= 0x811C9DC5;
|
|
|
|
|
h2 *= 0xA2CEBAB2;
|
|
|
|
|
h0 >>= 8;
|
|
|
|
|
}
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public IEnumerable<TypeDef> Types {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
get {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var types = new List<TypeDef>();
|
2013-01-19 20:09:49 +08:00
|
|
|
|
foreach (var info in decrypters.GetValues())
|
2012-07-28 10:45:27 +08:00
|
|
|
|
types.Add(info.method.DeclaringType);
|
|
|
|
|
return types;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public IEnumerable<FieldDef> Fields {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
get {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
return new List<FieldDef> {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
dataField,
|
|
|
|
|
dictField,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public MethodDef NativeMethod {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
get { return nativeMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-20 22:59:30 +08:00
|
|
|
|
public TypeDef LzmaType {
|
|
|
|
|
get { return lzmaType; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-28 10:45:27 +08:00
|
|
|
|
public EmbeddedResource Resource {
|
|
|
|
|
get { return resource; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<DecrypterInfo> Decrypters {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
get { return decrypters.GetValues(); }
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return installMethod != null; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public ConstantsDecrypterV18(ModuleDefMD module, byte[] fileData, ISimpleDeobfuscator simpleDeobfuscator) {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
this.fileData = fileData;
|
|
|
|
|
this.simpleDeobfuscator = simpleDeobfuscator;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public void Find() {
|
|
|
|
|
var cctor = DotNetUtils.GetModuleTypeCctor(module);
|
2012-07-28 10:45:27 +08:00
|
|
|
|
if (cctor == null)
|
|
|
|
|
return;
|
2015-10-30 05:36:17 +08:00
|
|
|
|
simpleDeobfuscator.Deobfuscate(cctor, SimpleDeobfuscatorFlags.Force | SimpleDeobfuscatorFlags.DisableConstantsFolderExtraInstrs);
|
2012-07-28 10:45:27 +08:00
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if ((dictField = ConstantsDecrypterUtils.FindDictField(cctor, cctor.DeclaringType)) == null)
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return;
|
2013-01-20 22:59:30 +08:00
|
|
|
|
|
|
|
|
|
if ((dataField = ConstantsDecrypterUtils.FindDataField_v18_r75367(cctor, cctor.DeclaringType)) == null &&
|
|
|
|
|
(dataField = ConstantsDecrypterUtils.FindDataField_v19_r77172(cctor, cctor.DeclaringType)) == null)
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
nativeMethod = FindNativeMethod(cctor, cctor.DeclaringType);
|
2012-08-08 20:30:46 +08:00
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var method = GetDecryptMethod();
|
2012-08-08 20:30:46 +08:00
|
|
|
|
if (method == null)
|
|
|
|
|
return;
|
2015-10-30 05:36:17 +08:00
|
|
|
|
simpleDeobfuscator.Deobfuscate(method, SimpleDeobfuscatorFlags.DisableConstantsFolderExtraInstrs);
|
2012-08-08 20:30:46 +08:00
|
|
|
|
var info = new DecrypterInfo(this, method, ConfuserVersion.Unknown);
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (FindKeys_v18_r75367(info))
|
|
|
|
|
InitVersion(cctor, ConfuserVersion.v18_r75367_normal, ConfuserVersion.v18_r75367_dynamic, ConfuserVersion.v18_r75367_native);
|
2013-01-20 22:59:30 +08:00
|
|
|
|
else if (FindKeys_v18_r75369(info)) {
|
|
|
|
|
lzmaType = ConfuserUtils.FindLzmaType(cctor);
|
|
|
|
|
if (lzmaType == null)
|
|
|
|
|
InitVersion(cctor, ConfuserVersion.v18_r75369_normal, ConfuserVersion.v18_r75369_dynamic, ConfuserVersion.v18_r75369_native);
|
2013-01-21 01:02:19 +08:00
|
|
|
|
else if (!DotNetUtils.CallsMethod(method, "System.Void System.Threading.Monitor::Exit(System.Object)"))
|
2013-01-20 22:59:30 +08:00
|
|
|
|
InitVersion(cctor, ConfuserVersion.v19_r77172_normal, ConfuserVersion.v19_r77172_dynamic, ConfuserVersion.v19_r77172_native);
|
2013-09-23 00:35:58 +08:00
|
|
|
|
else if (DotNetUtils.CallsMethod(method, "System.Void System.Diagnostics.StackFrame::.ctor(System.Int32)"))
|
2013-02-03 00:03:56 +08:00
|
|
|
|
InitVersion(cctor, ConfuserVersion.v19_r78363_normal, ConfuserVersion.v19_r78363_dynamic, ConfuserVersion.v19_r78363_native);
|
2013-09-23 00:35:58 +08:00
|
|
|
|
else {
|
|
|
|
|
int index1 = ConfuserUtils.FindCallMethod(cctor.Body.Instructions, 0, Code.Callvirt, "System.Reflection.Module System.Reflection.MemberInfo::get_Module()");
|
|
|
|
|
int index2 = ConfuserUtils.FindCallMethod(cctor.Body.Instructions, 0, Code.Callvirt, "System.Int32 System.Reflection.MemberInfo::get_MetadataToken()");
|
|
|
|
|
if (index1 < 0 || index2 < 0) {
|
|
|
|
|
}
|
|
|
|
|
if (index2 - index1 == 3)
|
|
|
|
|
InitVersion(cctor, ConfuserVersion.v19_r78056_normal, ConfuserVersion.v19_r78056_dynamic, ConfuserVersion.v19_r78056_native);
|
|
|
|
|
else if (index2 - index1 == -4)
|
|
|
|
|
InitVersion(cctor, ConfuserVersion.v19_r79630_normal, ConfuserVersion.v19_r79630_dynamic, ConfuserVersion.v19_r79630_native);
|
|
|
|
|
}
|
2013-01-20 22:59:30 +08:00
|
|
|
|
}
|
2012-08-08 20:30:46 +08:00
|
|
|
|
else
|
|
|
|
|
return;
|
|
|
|
|
|
2012-07-28 10:45:27 +08:00
|
|
|
|
installMethod = cctor;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
void InitVersion(MethodDef installMethod, ConfuserVersion normal, ConfuserVersion dynamic, ConfuserVersion native) {
|
2012-08-08 20:30:46 +08:00
|
|
|
|
if (nativeMethod != null)
|
|
|
|
|
version = native;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
else if (DeobUtils.HasInteger(installMethod, 0x10000))
|
2012-08-08 20:30:46 +08:00
|
|
|
|
version = normal;
|
|
|
|
|
else
|
|
|
|
|
version = dynamic;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public void Initialize() {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
if (installMethod == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!FindKeys())
|
2012-07-28 10:45:27 +08:00
|
|
|
|
throw new ApplicationException("Could not find keys");
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if ((resource = FindResource(key0)) == null)
|
2012-07-28 10:45:27 +08:00
|
|
|
|
throw new ApplicationException("Could not find resource");
|
2013-01-19 20:09:49 +08:00
|
|
|
|
constants = DecryptResource(resource.GetResourceData());
|
2012-07-28 10:45:27 +08:00
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
FindDecrypters();
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
EmbeddedResource FindResource(uint magic) {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
var name = Encoding.UTF8.GetString(BitConverter.GetBytes(magic));
|
2013-01-19 20:09:49 +08:00
|
|
|
|
return DotNetUtils.GetResource(module, name) as EmbeddedResource;
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
bool FindKeys() {
|
|
|
|
|
if (!FindKey0(installMethod, out key0))
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!FindKey0d(installMethod, out key0d))
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool FindKey0(MethodDef method, out uint key) {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
int index = ConfuserUtils.FindCallMethod(instrs, i, Code.Call, "System.Text.Encoding System.Text.Encoding::get_UTF8()");
|
2012-07-28 10:45:27 +08:00
|
|
|
|
if (index < 0)
|
|
|
|
|
break;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
int index2 = ConfuserUtils.FindCallMethod(instrs, i, Code.Call, "System.Byte[] System.BitConverter::GetBytes(System.Int32)");
|
2012-07-28 10:45:27 +08:00
|
|
|
|
if (index2 - index != 2)
|
|
|
|
|
continue;
|
|
|
|
|
var ldci4 = instrs[index + 1];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
key = (uint)ldci4.GetLdcI4Value();
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool FindKey0d(MethodDef method, out uint key) {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
int index = ConfuserUtils.FindCallMethod(instrs, i, Code.Callvirt, "System.Reflection.Module System.Reflection.MemberInfo::get_Module()");
|
2012-07-28 10:45:27 +08:00
|
|
|
|
if (index < 0)
|
|
|
|
|
break;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
int index2 = ConfuserUtils.FindCallMethod(instrs, i, Code.Callvirt, "System.Int32 System.Reflection.MemberInfo::get_MetadataToken()");
|
2013-09-23 00:35:58 +08:00
|
|
|
|
int ldci4Index;
|
|
|
|
|
switch (index2 - index) {
|
|
|
|
|
case 3:
|
|
|
|
|
// rev <= r79440
|
|
|
|
|
ldci4Index = index + 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case -4:
|
|
|
|
|
// rev >= r79630
|
|
|
|
|
ldci4Index = index2 - 2;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
2013-09-23 00:35:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var ldci4 = instrs[ldci4Index];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
2013-09-23 00:35:58 +08:00
|
|
|
|
if (!instrs[ldci4Index + 1].IsLdloc())
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
key = (uint)ldci4.GetLdcI4Value();
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static MethodDef FindNativeMethod(MethodDef method, TypeDef declaringType) {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i].IsLdloc())
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
|
|
|
|
var call = instrs[i + 1];
|
|
|
|
|
if (call.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var calledMethod = call.Operand as MethodDef;
|
2012-07-28 10:45:27 +08:00
|
|
|
|
if (calledMethod == null || !calledMethod.IsStatic || !calledMethod.IsNative)
|
|
|
|
|
continue;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(calledMethod, "System.Int32", "(System.Int32)"))
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return calledMethod;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
MethodDef GetDecryptMethod() {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
foreach (var type in module.Types) {
|
2012-08-08 20:30:46 +08:00
|
|
|
|
if (type.Attributes != (TypeAttributes.Abstract | TypeAttributes.Sealed))
|
|
|
|
|
continue;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!CheckMethods(type.Methods))
|
2012-08-08 20:30:46 +08:00
|
|
|
|
continue;
|
2012-07-28 10:45:27 +08:00
|
|
|
|
foreach (var method in type.Methods) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (IsDecryptMethodSignature(method))
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return method;
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return null;
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool CheckMethods(IEnumerable<MethodDef> methods) {
|
2012-08-08 20:30:46 +08:00
|
|
|
|
int numMethods = 0;
|
|
|
|
|
foreach (var method in methods) {
|
|
|
|
|
if (method.Name == ".ctor" || method.Name == ".cctor")
|
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!IsDecryptMethodSignature(method))
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
numMethods++;
|
|
|
|
|
}
|
|
|
|
|
return numMethods > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool IsDecryptMethodSignature(MethodDef method) {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
if (method == null || method.Body == null)
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
|
|
|
|
if (method.Attributes != (MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.CompilerControlled))
|
|
|
|
|
return false;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var sig = method.MethodSig;
|
|
|
|
|
if (sig == null)
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (sig.Params.Count != 2)
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (sig.Params[0].GetElementType() != ElementType.U4)
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (sig.Params[1].GetElementType() != ElementType.U8)
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!(sig.RetType.RemovePinnedAndModifiers() is GenericMVar))
|
|
|
|
|
return false;
|
|
|
|
|
if (sig.GenParamCount != 1)
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
void FindDecrypters() {
|
2012-08-08 20:30:46 +08:00
|
|
|
|
foreach (var type in module.Types) {
|
|
|
|
|
if (type.Attributes != (TypeAttributes.Abstract | TypeAttributes.Sealed))
|
|
|
|
|
continue;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!CheckMethods(type.Methods))
|
2012-08-08 20:30:46 +08:00
|
|
|
|
continue;
|
|
|
|
|
foreach (var method in type.Methods) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var info = CreateDecrypterInfo(method);
|
2012-08-08 20:30:46 +08:00
|
|
|
|
if (info != null)
|
2013-01-19 20:09:49 +08:00
|
|
|
|
decrypters.Add(info.method, info);
|
2012-08-08 20:30:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
DecrypterInfo CreateDecrypterInfo(MethodDef method) {
|
|
|
|
|
if (!IsDecryptMethodSignature(method))
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
2015-10-30 05:36:17 +08:00
|
|
|
|
simpleDeobfuscator.Deobfuscate(method, SimpleDeobfuscatorFlags.DisableConstantsFolderExtraInstrs);
|
2012-08-08 20:30:46 +08:00
|
|
|
|
var info = new DecrypterInfo(this, method, version);
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!FindKeys(info))
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
bool FindKeys(DecrypterInfo info) {
|
2012-08-08 20:30:46 +08:00
|
|
|
|
switch (version) {
|
|
|
|
|
case ConfuserVersion.v18_r75367_normal:
|
|
|
|
|
case ConfuserVersion.v18_r75367_dynamic:
|
|
|
|
|
case ConfuserVersion.v18_r75367_native:
|
2013-01-19 20:09:49 +08:00
|
|
|
|
return FindKeys_v18_r75367(info);
|
2012-08-09 00:34:24 +08:00
|
|
|
|
case ConfuserVersion.v18_r75369_normal:
|
|
|
|
|
case ConfuserVersion.v18_r75369_dynamic:
|
|
|
|
|
case ConfuserVersion.v18_r75369_native:
|
2013-01-20 22:59:30 +08:00
|
|
|
|
case ConfuserVersion.v19_r77172_normal:
|
|
|
|
|
case ConfuserVersion.v19_r77172_dynamic:
|
|
|
|
|
case ConfuserVersion.v19_r77172_native:
|
2013-01-21 01:02:19 +08:00
|
|
|
|
case ConfuserVersion.v19_r78056_normal:
|
|
|
|
|
case ConfuserVersion.v19_r78056_dynamic:
|
|
|
|
|
case ConfuserVersion.v19_r78056_native:
|
2013-02-03 00:03:56 +08:00
|
|
|
|
case ConfuserVersion.v19_r78363_normal:
|
|
|
|
|
case ConfuserVersion.v19_r78363_dynamic:
|
|
|
|
|
case ConfuserVersion.v19_r78363_native:
|
2013-09-23 00:35:58 +08:00
|
|
|
|
case ConfuserVersion.v19_r79630_normal:
|
|
|
|
|
case ConfuserVersion.v19_r79630_dynamic:
|
|
|
|
|
case ConfuserVersion.v19_r79630_native:
|
2013-01-19 20:09:49 +08:00
|
|
|
|
return FindKeys_v18_r75369(info);
|
2012-08-08 20:30:46 +08:00
|
|
|
|
default:
|
|
|
|
|
throw new ApplicationException("Invalid version");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool FindKeys_v18_r75367(DecrypterInfo info) {
|
|
|
|
|
if (!FindLKeys_v18_r75367(info))
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!FindKey0_v18_r75367(info))
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!FindKey0d_v18_r75367(info))
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool FindKeys_v18_r75369(DecrypterInfo info) {
|
|
|
|
|
if (!FindLKeys_v18_r75369(info))
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!FindKey0_v18_r75369(info))
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!FindKey0d_v18_r75367(info))
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool FindLKeys_v18_r75367(DecrypterInfo info) {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
var instrs = info.method.Body.Instructions;
|
2012-08-08 20:30:46 +08:00
|
|
|
|
for (int i = 0; i < instrs.Count - 10; i++) {
|
|
|
|
|
var ldci4_1 = instrs[i];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4_1.IsLdcI4())
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 1].IsLdloc())
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
2012-08-08 20:30:46 +08:00
|
|
|
|
if (instrs[i + 2].OpCode.Code != Code.Xor)
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
2012-08-08 20:30:46 +08:00
|
|
|
|
if (instrs[i + 3].OpCode.Code != Code.Conv_U8)
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 4].IsStloc())
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
2012-08-08 20:30:46 +08:00
|
|
|
|
var ldci4_2 = instrs[i + 5];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4_2.IsLdcI4())
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
2012-08-08 20:30:46 +08:00
|
|
|
|
if (instrs[i + 6].OpCode.Code != Code.Conv_I8)
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 7].IsStloc())
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
2012-08-08 20:30:46 +08:00
|
|
|
|
var ldci4_3 = instrs[i + 8];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4_3.IsLdcI4())
|
2012-08-08 20:30:46 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 9].OpCode.Code != Code.Conv_I8)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 10].IsStloc())
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
info.key0l = (uint)ldci4_1.GetLdcI4Value();
|
|
|
|
|
info.key1l = (uint)ldci4_2.GetLdcI4Value();
|
|
|
|
|
info.key2l = (uint)ldci4_3.GetLdcI4Value();
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool FindKey0_v18_r75367(DecrypterInfo info) {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
var instrs = info.method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 4; i++) {
|
2012-08-08 20:30:46 +08:00
|
|
|
|
if (instrs[i].OpCode.Code != Code.Xor)
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
2012-08-08 20:30:46 +08:00
|
|
|
|
var ldci4 = instrs[i + 1];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
2012-08-08 20:30:46 +08:00
|
|
|
|
if (instrs[i + 2].OpCode.Code != Code.Conv_I8)
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
2012-08-08 20:30:46 +08:00
|
|
|
|
if (instrs[i + 3].OpCode.Code != Code.Mul)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 4].OpCode.Code != Code.Add)
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
info.key0 = (uint)ldci4.GetLdcI4Value();
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool FindKey0d_v18_r75367(DecrypterInfo info) {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
var instrs = info.method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
int index = ConfuserUtils.FindCallMethod(instrs, i, Code.Callvirt, "System.Int32 System.Reflection.MemberInfo::get_MetadataToken()");
|
2012-07-28 10:45:27 +08:00
|
|
|
|
if (index < 0)
|
|
|
|
|
break;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
int index2 = ConfuserUtils.FindCallMethod(instrs, index, Code.Call, "System.Byte[] System.BitConverter::GetBytes(System.Int32)");
|
2012-07-28 10:45:27 +08:00
|
|
|
|
if (index2 < 0)
|
|
|
|
|
break;
|
|
|
|
|
if (index2 - index != 3)
|
|
|
|
|
continue;
|
|
|
|
|
var ldci4 = instrs[index + 1];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[index + 2].OpCode.Code != Code.Xor)
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
info.key0d = (uint)ldci4.GetLdcI4Value();
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool FindLKeys_v18_r75369(DecrypterInfo info) {
|
2012-08-08 20:30:46 +08:00
|
|
|
|
var instrs = info.method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 8; i++) {
|
|
|
|
|
var ldci8_1 = instrs[i];
|
|
|
|
|
if (ldci8_1.OpCode.Code != Code.Ldc_I8)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 1].IsLdloc())
|
2012-08-08 20:30:46 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 2].OpCode.Code != Code.Conv_U8)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 3].OpCode.Code != Code.Mul)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 4].IsStloc())
|
2012-08-08 20:30:46 +08:00
|
|
|
|
continue;
|
|
|
|
|
var ldci8_2 = instrs[i + 5];
|
|
|
|
|
if (ldci8_2.OpCode.Code != Code.Ldc_I8)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 6].IsStloc())
|
2012-08-08 20:30:46 +08:00
|
|
|
|
continue;
|
|
|
|
|
var ldci8_3 = instrs[i + 7];
|
|
|
|
|
if (ldci8_3.OpCode.Code != Code.Ldc_I8)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 8].IsStloc())
|
2012-08-08 20:30:46 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
info.key0l = (ulong)(long)ldci8_1.Operand;
|
|
|
|
|
info.key1l = (ulong)(long)ldci8_2.Operand;
|
|
|
|
|
info.key2l = (ulong)(long)ldci8_3.Operand;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool FindKey0_v18_r75369(DecrypterInfo info) {
|
2012-08-08 20:30:46 +08:00
|
|
|
|
var instrs = info.method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 4; i++) {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i].IsLdloc())
|
2012-08-08 20:30:46 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 1].OpCode.Code != Code.Xor)
|
|
|
|
|
continue;
|
|
|
|
|
var ldci4 = instrs[i + 2];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-08-08 20:30:46 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 3].OpCode.Code != Code.Conv_U8)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 4].OpCode.Code != Code.Mul)
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
info.key0 = (uint)ldci4.GetLdcI4Value();
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-20 22:59:30 +08:00
|
|
|
|
byte[] Decompress(byte[] compressed) {
|
|
|
|
|
if (lzmaType != null)
|
|
|
|
|
return ConfuserUtils.SevenZipDecompress(compressed);
|
|
|
|
|
return DeobUtils.Inflate(compressed, true);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
byte[] DecryptResource(byte[] encrypted) {
|
2012-08-08 20:30:46 +08:00
|
|
|
|
switch (version) {
|
|
|
|
|
case ConfuserVersion.v18_r75367_normal:
|
2012-08-09 00:34:24 +08:00
|
|
|
|
case ConfuserVersion.v18_r75369_normal:
|
2013-01-20 22:59:30 +08:00
|
|
|
|
case ConfuserVersion.v19_r77172_normal:
|
2013-01-21 01:02:19 +08:00
|
|
|
|
case ConfuserVersion.v19_r78056_normal:
|
2013-02-03 00:03:56 +08:00
|
|
|
|
case ConfuserVersion.v19_r78363_normal:
|
2013-09-23 00:35:58 +08:00
|
|
|
|
case ConfuserVersion.v19_r79630_normal:
|
2013-01-19 20:09:49 +08:00
|
|
|
|
return DecryptResource_v18_r75367_normal(encrypted);
|
2012-08-08 20:30:46 +08:00
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v18_r75367_dynamic:
|
2012-08-09 00:34:24 +08:00
|
|
|
|
case ConfuserVersion.v18_r75369_dynamic:
|
2013-01-20 22:59:30 +08:00
|
|
|
|
case ConfuserVersion.v19_r77172_dynamic:
|
2013-01-21 01:02:19 +08:00
|
|
|
|
case ConfuserVersion.v19_r78056_dynamic:
|
2013-02-03 00:03:56 +08:00
|
|
|
|
case ConfuserVersion.v19_r78363_dynamic:
|
2013-09-23 00:35:58 +08:00
|
|
|
|
case ConfuserVersion.v19_r79630_dynamic:
|
2013-01-19 20:09:49 +08:00
|
|
|
|
return DecryptResource_v18_r75367_dynamic(encrypted);
|
2012-08-08 20:30:46 +08:00
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v18_r75367_native:
|
2012-08-09 00:34:24 +08:00
|
|
|
|
case ConfuserVersion.v18_r75369_native:
|
2013-01-20 22:59:30 +08:00
|
|
|
|
case ConfuserVersion.v19_r77172_native:
|
2013-01-21 01:02:19 +08:00
|
|
|
|
case ConfuserVersion.v19_r78056_native:
|
2013-02-03 00:03:56 +08:00
|
|
|
|
case ConfuserVersion.v19_r78363_native:
|
2013-09-23 00:35:58 +08:00
|
|
|
|
case ConfuserVersion.v19_r79630_native:
|
2013-01-19 20:09:49 +08:00
|
|
|
|
return DecryptResource_v18_r75367_native(encrypted);
|
2012-08-08 20:30:46 +08:00
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new ApplicationException("Unknown version");
|
2012-07-29 16:17:05 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
byte[] GetSigKey() {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
return module.ReadBlob(key0d ^ installMethod.MDToken.ToUInt32());
|
2012-07-29 16:17:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
byte[] DecryptResource_v18_r75367_normal(byte[] encrypted) {
|
|
|
|
|
var key = GetSigKey();
|
|
|
|
|
var decrypted = ConfuserUtils.Decrypt(BitConverter.ToUInt32(key, 12) * (uint)key0, encrypted);
|
2013-01-20 22:59:30 +08:00
|
|
|
|
return Decompress(DeobUtils.AesDecrypt(decrypted, key, DeobUtils.Md5Sum(key)));
|
2012-07-29 16:17:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static int GetDynamicStartIndex(IList<Instruction> instrs, int ldlocIndex) {
|
2012-07-29 20:23:27 +08:00
|
|
|
|
for (int i = ldlocIndex - 1; i >= 0; i--) {
|
|
|
|
|
if (instrs[i].OpCode.FlowControl != FlowControl.Next)
|
|
|
|
|
return i + 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
int GetDynamicEndIndex(int startIndex, Local local) {
|
2012-08-02 14:07:26 +08:00
|
|
|
|
if (startIndex < 0)
|
|
|
|
|
return -1;
|
2012-07-29 20:23:27 +08:00
|
|
|
|
var instrs = installMethod.Body.Instructions;
|
|
|
|
|
for (int i = startIndex; i < instrs.Count; i++) {
|
|
|
|
|
var instr = instrs[i];
|
2012-11-20 15:02:10 +08:00
|
|
|
|
if (instr.IsStloc() && instr.GetLocal(installMethod.Body.Variables) == local)
|
2012-07-29 20:23:27 +08:00
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
Local GetDynamicLocal(out int instrIndex) {
|
2012-07-29 20:23:27 +08:00
|
|
|
|
var instrs = installMethod.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
i = ConfuserUtils.FindCallMethod(instrs, i, Code.Callvirt, "System.Void System.IO.BinaryWriter::Write(System.Byte)");
|
2012-07-29 20:23:27 +08:00
|
|
|
|
if (i < 0)
|
|
|
|
|
break;
|
|
|
|
|
int index = i - 2;
|
|
|
|
|
if (index < 0)
|
|
|
|
|
continue;
|
|
|
|
|
var ldloc = instrs[index];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldloc.IsLdloc())
|
2012-07-29 20:23:27 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[index + 1].OpCode.Code != Code.Conv_U1)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
instrIndex = index;
|
2012-11-20 15:02:10 +08:00
|
|
|
|
return ldloc.GetLocal(installMethod.Body.Variables);
|
2012-07-29 20:23:27 +08:00
|
|
|
|
}
|
|
|
|
|
instrIndex = 0;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
byte[] DecryptResource_v18_r75367_dynamic(byte[] encrypted) {
|
2012-07-29 20:23:27 +08:00
|
|
|
|
int ldlocIndex;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var local = GetDynamicLocal(out ldlocIndex);
|
2012-07-29 20:23:27 +08:00
|
|
|
|
if (local == null)
|
|
|
|
|
throw new ApplicationException("Could not find local");
|
|
|
|
|
|
|
|
|
|
var instrs = installMethod.Body.Instructions;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
int startIndex = GetDynamicStartIndex(instrs, ldlocIndex);
|
|
|
|
|
int endIndex = GetDynamicEndIndex(startIndex, local);
|
2012-07-29 20:23:27 +08:00
|
|
|
|
if (endIndex < 0)
|
|
|
|
|
throw new ApplicationException("Could not find endIndex");
|
|
|
|
|
|
|
|
|
|
var constReader = new ConstantsReader(installMethod);
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
return DecryptResource(encrypted, magic => {
|
|
|
|
|
constReader.SetConstantInt32(local, magic);
|
2012-07-29 20:23:27 +08:00
|
|
|
|
int index = startIndex, result;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!constReader.GetNextInt32(ref index, out result))
|
2012-07-29 20:23:27 +08:00
|
|
|
|
throw new ApplicationException("Could not get constant");
|
|
|
|
|
if (index != endIndex)
|
|
|
|
|
throw new ApplicationException("Wrong constant");
|
|
|
|
|
return (byte)result;
|
|
|
|
|
});
|
2012-07-29 16:17:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
byte[] DecryptResource_v18_r75367_native(byte[] encrypted) {
|
2012-12-01 04:05:54 +08:00
|
|
|
|
using (var x86Emu = new x86Emulator(fileData))
|
2013-01-19 20:09:49 +08:00
|
|
|
|
return DecryptResource(encrypted, magic => (byte)x86Emu.Emulate((uint)nativeMethod.RVA, magic));
|
2012-07-29 20:23:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
byte[] DecryptResource(byte[] encrypted, Func<uint, byte> decryptFunc) {
|
|
|
|
|
var key = GetSigKey();
|
2012-07-28 10:45:27 +08:00
|
|
|
|
|
2013-01-20 22:59:30 +08:00
|
|
|
|
byte[] decrypted = DecryptAndDecompress(encrypted, key);
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var reader = MemoryImageStream.Create(decrypted);
|
2012-07-28 10:45:27 +08:00
|
|
|
|
var result = new MemoryStream();
|
|
|
|
|
var writer = new BinaryWriter(result);
|
2012-11-19 06:42:43 +08:00
|
|
|
|
while (reader.Position < reader.Length) {
|
|
|
|
|
uint magic = reader.Read7BitEncodedUInt32();
|
2012-07-29 20:23:27 +08:00
|
|
|
|
writer.Write(decryptFunc(magic));
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-20 22:59:30 +08:00
|
|
|
|
byte[] DecryptAndDecompress(byte[] encrypted, byte[] key) {
|
|
|
|
|
byte[] iv = DeobUtils.Md5Sum(key);
|
|
|
|
|
try {
|
|
|
|
|
return Decompress(DeobUtils.AesDecrypt(encrypted, key, iv));
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
return DeobUtils.AesDecrypt(Decompress(encrypted), key, iv);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool VerifyGenericArg(MethodSpec gim, ElementType etype) {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (gim == null)
|
|
|
|
|
return false;
|
|
|
|
|
var gims = gim.GenericInstMethodSig;
|
|
|
|
|
if (gims == null || gims.GenericArguments.Count != 1)
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return false;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
return gims.GenericArguments[0].GetElementType() == etype;
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public string DecryptString(MethodDef method, MethodSpec gim, uint magic1, ulong magic2) {
|
|
|
|
|
if (!VerifyGenericArg(gim, ElementType.String))
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return null;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var info = decrypters.Find(method);
|
|
|
|
|
return info.DecryptString(magic1, magic2);
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public object DecryptInt32(MethodDef method, MethodSpec gim, uint magic1, ulong magic2) {
|
|
|
|
|
if (!VerifyGenericArg(gim, ElementType.I4))
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return null;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var info = decrypters.Find(method);
|
|
|
|
|
return info.DecryptInt32(magic1, magic2);
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public object DecryptInt64(MethodDef method, MethodSpec gim, uint magic1, ulong magic2) {
|
|
|
|
|
if (!VerifyGenericArg(gim, ElementType.I8))
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return null;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var info = decrypters.Find(method);
|
|
|
|
|
return info.DecryptInt64(magic1, magic2);
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public object DecryptSingle(MethodDef method, MethodSpec gim, uint magic1, ulong magic2) {
|
|
|
|
|
if (!VerifyGenericArg(gim, ElementType.R4))
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return null;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var info = decrypters.Find(method);
|
|
|
|
|
return info.DecryptSingle(magic1, magic2);
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public object DecryptDouble(MethodDef method, MethodSpec gim, uint magic1, ulong magic2) {
|
|
|
|
|
if (!VerifyGenericArg(gim, ElementType.R8))
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return null;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var info = decrypters.Find(method);
|
|
|
|
|
return info.DecryptDouble(magic1, magic2);
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public void CleanUp() {
|
2012-07-29 16:17:05 +08:00
|
|
|
|
if (installMethod == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-07-28 10:45:27 +08:00
|
|
|
|
//TODO: Only remove its code
|
|
|
|
|
installMethod.Body.Instructions.Clear();
|
|
|
|
|
installMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
|
|
|
|
|
}
|
2012-08-09 17:47:18 +08:00
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public bool GetRevisionRange(out int minRev, out int maxRev) {
|
2012-08-09 17:47:18 +08:00
|
|
|
|
switch (version) {
|
|
|
|
|
case ConfuserVersion.Unknown:
|
|
|
|
|
minRev = maxRev = 0;
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v18_r75367_normal:
|
|
|
|
|
case ConfuserVersion.v18_r75367_dynamic:
|
|
|
|
|
case ConfuserVersion.v18_r75367_native:
|
|
|
|
|
minRev = 75367;
|
|
|
|
|
maxRev = 75367;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v18_r75369_normal:
|
|
|
|
|
case ConfuserVersion.v18_r75369_dynamic:
|
|
|
|
|
case ConfuserVersion.v18_r75369_native:
|
|
|
|
|
minRev = 75369;
|
2013-01-20 22:59:30 +08:00
|
|
|
|
maxRev = 77124;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v19_r77172_normal:
|
|
|
|
|
case ConfuserVersion.v19_r77172_dynamic:
|
|
|
|
|
case ConfuserVersion.v19_r77172_native:
|
|
|
|
|
minRev = 77172;
|
2013-01-21 01:02:19 +08:00
|
|
|
|
maxRev = 77501;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v19_r78056_normal:
|
|
|
|
|
case ConfuserVersion.v19_r78056_dynamic:
|
|
|
|
|
case ConfuserVersion.v19_r78056_native:
|
|
|
|
|
minRev = 78056;
|
2013-09-22 23:04:45 +08:00
|
|
|
|
// r78964 removed code that made it impossible to differentiate it from this
|
|
|
|
|
// version. All we know is that it can't be r78363-r78963.
|
2013-09-23 00:35:58 +08:00
|
|
|
|
maxRev = 79440;
|
2013-02-03 00:03:56 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v19_r78363_normal:
|
|
|
|
|
case ConfuserVersion.v19_r78363_dynamic:
|
|
|
|
|
case ConfuserVersion.v19_r78363_native:
|
|
|
|
|
minRev = 78363;
|
2013-09-22 23:04:45 +08:00
|
|
|
|
maxRev = 78963;
|
2012-08-09 17:47:18 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
2013-09-23 00:35:58 +08:00
|
|
|
|
case ConfuserVersion.v19_r79630_normal:
|
|
|
|
|
case ConfuserVersion.v19_r79630_dynamic:
|
|
|
|
|
case ConfuserVersion.v19_r79630_native:
|
|
|
|
|
minRev = 79630;
|
|
|
|
|
maxRev = int.MaxValue;
|
|
|
|
|
return true;
|
|
|
|
|
|
2012-08-09 17:47:18 +08:00
|
|
|
|
default: throw new ApplicationException("Invalid version");
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|