2012-07-28 10:45:27 +08:00
|
|
|
|
/*
|
|
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
using Mono.Cecil;
|
|
|
|
|
using Mono.Cecil.Cil;
|
|
|
|
|
using Mono.Cecil.Metadata;
|
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
using de4dot.PE;
|
|
|
|
|
|
|
|
|
|
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-07-28 10:45:27 +08:00
|
|
|
|
ModuleDefinition module;
|
|
|
|
|
byte[] fileData;
|
|
|
|
|
ISimpleDeobfuscator simpleDeobfuscator;
|
|
|
|
|
FieldDefinition dictField, dataField;
|
|
|
|
|
MethodDefinition installMethod;
|
|
|
|
|
MethodDefinitionAndDeclaringTypeDict<DecrypterInfo> decrypters = new MethodDefinitionAndDeclaringTypeDict<DecrypterInfo>();
|
|
|
|
|
uint key0, key0d;
|
|
|
|
|
MethodDefinition nativeMethod;
|
|
|
|
|
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,
|
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-07-28 10:45:27 +08:00
|
|
|
|
public readonly MethodDefinition method;
|
|
|
|
|
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-08-08 20:30:46 +08:00
|
|
|
|
public DecrypterInfo(ConstantsDecrypterV18 constantsDecrypter, MethodDefinition 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string decryptString(uint magic1, ulong magic2) {
|
|
|
|
|
return Encoding.UTF8.GetString(decrypt(magic1, magic2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int decryptInt32(uint magic1, ulong magic2) {
|
|
|
|
|
return BitConverter.ToInt32(decrypt(magic1, magic2), 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long decryptInt64(uint magic1, ulong magic2) {
|
|
|
|
|
return BitConverter.ToInt64(decrypt(magic1, magic2), 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float decryptSingle(uint magic1, ulong magic2) {
|
|
|
|
|
return BitConverter.ToSingle(decrypt(magic1, magic2), 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double decryptDouble(uint magic1, ulong magic2) {
|
|
|
|
|
return BitConverter.ToDouble(decrypt(magic1, magic2), 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] decrypt(uint magic1, ulong magic2) {
|
|
|
|
|
ulong info = hash(method.DeclaringType.MetadataToken.ToUInt32() * magic1) ^ magic2;
|
|
|
|
|
int offset = (int)(info >> 32);
|
|
|
|
|
int len = (int)info;
|
|
|
|
|
var decrypted = new byte[len];
|
|
|
|
|
byte[] key = BitConverter.GetBytes(method.MetadataToken.ToUInt32() ^ key0d);
|
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
|
decrypted[i] = (byte)(constantsDecrypter.constants[offset + i] ^ key[(offset + i) & 3]);
|
|
|
|
|
return decrypted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
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:
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return hash1(key0l * magic);
|
|
|
|
|
default:
|
|
|
|
|
throw new ApplicationException("Invalid version");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<TypeDefinition> Types {
|
|
|
|
|
get {
|
|
|
|
|
var types = new List<TypeDefinition>();
|
|
|
|
|
foreach (var info in decrypters.getValues())
|
|
|
|
|
types.Add(info.method.DeclaringType);
|
|
|
|
|
return types;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<FieldDefinition> Fields {
|
|
|
|
|
get {
|
|
|
|
|
return new List<FieldDefinition> {
|
|
|
|
|
dataField,
|
|
|
|
|
dictField,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MethodDefinition NativeMethod {
|
|
|
|
|
get { return nativeMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EmbeddedResource Resource {
|
|
|
|
|
get { return resource; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<DecrypterInfo> Decrypters {
|
|
|
|
|
get { return decrypters.getValues(); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return installMethod != null; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-08 07:33:26 +08:00
|
|
|
|
public ConstantsDecrypterV18(ModuleDefinition module, byte[] fileData, ISimpleDeobfuscator simpleDeobfuscator) {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
this.fileData = fileData;
|
|
|
|
|
this.simpleDeobfuscator = simpleDeobfuscator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void find() {
|
|
|
|
|
var cctor = DotNetUtils.getModuleTypeCctor(module);
|
|
|
|
|
if (cctor == null)
|
|
|
|
|
return;
|
|
|
|
|
simpleDeobfuscator.deobfuscate(cctor, true);
|
|
|
|
|
|
2012-08-06 02:25:43 +08:00
|
|
|
|
if ((dictField = ConstantsDecrypterUtils.findDictField(cctor, cctor.DeclaringType)) == null)
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return;
|
2012-08-06 02:25:43 +08:00
|
|
|
|
if ((dataField = ConstantsDecrypterUtils.findDataField(cctor, cctor.DeclaringType)) == null)
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2012-08-08 20:30:46 +08:00
|
|
|
|
nativeMethod = findNativeMethod(cctor, cctor.DeclaringType);
|
|
|
|
|
|
|
|
|
|
var method = getDecryptMethod();
|
|
|
|
|
if (method == null)
|
|
|
|
|
return;
|
|
|
|
|
simpleDeobfuscator.deobfuscate(method);
|
|
|
|
|
var info = new DecrypterInfo(this, method, ConfuserVersion.Unknown);
|
|
|
|
|
if (findKeys_v18_r75367(info))
|
|
|
|
|
initVersion(cctor, ConfuserVersion.v18_r75367_normal, ConfuserVersion.v18_r75367_dynamic, ConfuserVersion.v18_r75367_native);
|
2012-08-09 00:34:24 +08:00
|
|
|
|
else if (findKeys_v18_r75369(info))
|
|
|
|
|
initVersion(cctor, ConfuserVersion.v18_r75369_normal, ConfuserVersion.v18_r75369_dynamic, ConfuserVersion.v18_r75369_native);
|
2012-08-08 20:30:46 +08:00
|
|
|
|
else
|
|
|
|
|
return;
|
|
|
|
|
|
2012-07-28 10:45:27 +08:00
|
|
|
|
installMethod = cctor;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-08 20:30:46 +08:00
|
|
|
|
void initVersion(MethodDefinition installMethod, ConfuserVersion normal, ConfuserVersion dynamic, ConfuserVersion native) {
|
|
|
|
|
if (nativeMethod != null)
|
|
|
|
|
version = native;
|
|
|
|
|
else if (DeobUtils.hasInteger(installMethod, 0x10000))
|
|
|
|
|
version = normal;
|
|
|
|
|
else
|
|
|
|
|
version = dynamic;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-28 10:45:27 +08:00
|
|
|
|
public void initialize() {
|
|
|
|
|
if (installMethod == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!findKeys())
|
|
|
|
|
throw new ApplicationException("Could not find keys");
|
|
|
|
|
|
|
|
|
|
if ((resource = findResource(key0)) == null)
|
|
|
|
|
throw new ApplicationException("Could not find resource");
|
2012-08-08 20:30:46 +08:00
|
|
|
|
constants = decryptResource(resource.GetResourceData());
|
2012-07-28 10:45:27 +08:00
|
|
|
|
|
|
|
|
|
findDecrypters();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EmbeddedResource findResource(uint magic) {
|
|
|
|
|
var name = Encoding.UTF8.GetString(BitConverter.GetBytes(magic));
|
|
|
|
|
return DotNetUtils.getResource(module, name) as EmbeddedResource;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool findKeys() {
|
|
|
|
|
if (!findKey0(installMethod, out key0))
|
|
|
|
|
return false;
|
|
|
|
|
if (!findKey0d(installMethod, out key0d))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool findKey0(MethodDefinition method, out uint key) {
|
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
|
|
|
|
int index = ConfuserUtils.findCallMethod(instrs, i, Code.Call, "System.Text.Encoding System.Text.Encoding::get_UTF8()");
|
|
|
|
|
if (index < 0)
|
|
|
|
|
break;
|
|
|
|
|
int index2 = ConfuserUtils.findCallMethod(instrs, i, Code.Call, "System.Byte[] System.BitConverter::GetBytes(System.Int32)");
|
|
|
|
|
if (index2 - index != 2)
|
|
|
|
|
continue;
|
|
|
|
|
var ldci4 = instrs[index + 1];
|
|
|
|
|
if (!DotNetUtils.isLdcI4(ldci4))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
key = (uint)DotNetUtils.getLdcI4Value(ldci4);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool findKey0d(MethodDefinition method, out uint key) {
|
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
|
|
|
|
int index = ConfuserUtils.findCallMethod(instrs, i, Code.Callvirt, "System.Reflection.Module System.Reflection.MemberInfo::get_Module()");
|
|
|
|
|
if (index < 0)
|
|
|
|
|
break;
|
|
|
|
|
int index2 = ConfuserUtils.findCallMethod(instrs, i, Code.Callvirt, "System.Int32 System.Reflection.MemberInfo::get_MetadataToken()");
|
|
|
|
|
if (index2 - index != 3)
|
|
|
|
|
continue;
|
|
|
|
|
var ldci4 = instrs[index + 1];
|
|
|
|
|
if (!DotNetUtils.isLdcI4(ldci4))
|
|
|
|
|
continue;
|
|
|
|
|
if (!DotNetUtils.isLdloc(instrs[index + 2]))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
key = (uint)DotNetUtils.getLdcI4Value(ldci4);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static MethodDefinition findNativeMethod(MethodDefinition method, TypeDefinition declaringType) {
|
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
|
|
|
|
if (!DotNetUtils.isLdloc(instrs[i]))
|
|
|
|
|
continue;
|
|
|
|
|
var call = instrs[i + 1];
|
|
|
|
|
if (call.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
|
|
|
|
var calledMethod = call.Operand as MethodDefinition;
|
|
|
|
|
if (calledMethod == null || !calledMethod.IsStatic || !calledMethod.IsNative)
|
|
|
|
|
continue;
|
|
|
|
|
if (!DotNetUtils.isMethod(calledMethod, "System.Int32", "(System.Int32)"))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return calledMethod;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-08 20:30:46 +08:00
|
|
|
|
MethodDefinition 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;
|
|
|
|
|
if (!checkMethods(type.Methods))
|
|
|
|
|
continue;
|
2012-07-28 10:45:27 +08:00
|
|
|
|
foreach (var method in type.Methods) {
|
2012-08-08 20:30:46 +08:00
|
|
|
|
if (isDecryptMethodSignature(method))
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2012-08-08 20:30:46 +08:00
|
|
|
|
static bool checkMethods(IEnumerable<MethodDefinition> methods) {
|
|
|
|
|
int numMethods = 0;
|
|
|
|
|
foreach (var method in methods) {
|
|
|
|
|
if (method.Name == ".ctor" || method.Name == ".cctor")
|
|
|
|
|
return false;
|
|
|
|
|
if (!isDecryptMethodSignature(method))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
numMethods++;
|
|
|
|
|
}
|
|
|
|
|
return numMethods > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool isDecryptMethodSignature(MethodDefinition 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-07-28 10:45:27 +08:00
|
|
|
|
if (method.Parameters.Count != 2)
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
2012-07-28 10:45:27 +08:00
|
|
|
|
if (method.Parameters[0].ParameterType.EType != ElementType.U4)
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
2012-07-28 10:45:27 +08:00
|
|
|
|
if (method.Parameters[1].ParameterType.EType != ElementType.U8)
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
2012-07-28 10:45:27 +08:00
|
|
|
|
if (!(method.MethodReturnType.ReturnType is GenericParameter))
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
2012-07-28 10:45:27 +08:00
|
|
|
|
if (method.GenericParameters.Count != 1)
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void findDecrypters() {
|
|
|
|
|
foreach (var type in module.Types) {
|
|
|
|
|
if (type.Attributes != (TypeAttributes.Abstract | TypeAttributes.Sealed))
|
|
|
|
|
continue;
|
|
|
|
|
if (!checkMethods(type.Methods))
|
|
|
|
|
continue;
|
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
var info = createDecrypterInfo(method);
|
|
|
|
|
if (info != null)
|
|
|
|
|
decrypters.add(info.method, info);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DecrypterInfo createDecrypterInfo(MethodDefinition method) {
|
|
|
|
|
if (!isDecryptMethodSignature(method))
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
simpleDeobfuscator.deobfuscate(method);
|
2012-08-08 20:30:46 +08:00
|
|
|
|
var info = new DecrypterInfo(this, method, version);
|
|
|
|
|
if (!findKeys(info))
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-08 20:30:46 +08:00
|
|
|
|
bool findKeys(DecrypterInfo info) {
|
|
|
|
|
switch (version) {
|
|
|
|
|
case ConfuserVersion.v18_r75367_normal:
|
|
|
|
|
case ConfuserVersion.v18_r75367_dynamic:
|
|
|
|
|
case ConfuserVersion.v18_r75367_native:
|
|
|
|
|
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:
|
|
|
|
|
return findKeys_v18_r75369(info);
|
2012-08-08 20:30:46 +08:00
|
|
|
|
default:
|
|
|
|
|
throw new ApplicationException("Invalid version");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool findKeys_v18_r75367(DecrypterInfo info) {
|
|
|
|
|
if (!findLKeys_v18_r75367(info))
|
|
|
|
|
return false;
|
|
|
|
|
if (!findKey0_v18_r75367(info))
|
|
|
|
|
return false;
|
|
|
|
|
if (!findKey0d_v18_r75367(info))
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-09 00:34:24 +08:00
|
|
|
|
static bool findKeys_v18_r75369(DecrypterInfo info) {
|
|
|
|
|
if (!findLKeys_v18_r75369(info))
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
2012-08-09 00:34:24 +08:00
|
|
|
|
if (!findKey0_v18_r75369(info))
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return false;
|
|
|
|
|
if (!findKey0d_v18_r75367(info))
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
if (!DotNetUtils.isLdcI4(ldci4_1))
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (!DotNetUtils.isLdloc(instrs[i + 1]))
|
|
|
|
|
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;
|
|
|
|
|
if (!DotNetUtils.isStloc(instrs[i + 4]))
|
|
|
|
|
continue;
|
2012-08-08 20:30:46 +08:00
|
|
|
|
var ldci4_2 = instrs[i + 5];
|
|
|
|
|
if (!DotNetUtils.isLdcI4(ldci4_2))
|
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-08-08 20:30:46 +08:00
|
|
|
|
if (!DotNetUtils.isStloc(instrs[i + 7]))
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
2012-08-08 20:30:46 +08:00
|
|
|
|
var ldci4_3 = instrs[i + 8];
|
|
|
|
|
if (!DotNetUtils.isLdcI4(ldci4_3))
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 9].OpCode.Code != Code.Conv_I8)
|
|
|
|
|
continue;
|
|
|
|
|
if (!DotNetUtils.isStloc(instrs[i + 10]))
|
2012-07-28 10:45:27 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2012-08-08 20:30:46 +08:00
|
|
|
|
info.key0l = (uint)DotNetUtils.getLdcI4Value(ldci4_1);
|
|
|
|
|
info.key1l = (uint)DotNetUtils.getLdcI4Value(ldci4_2);
|
|
|
|
|
info.key2l = (uint)DotNetUtils.getLdcI4Value(ldci4_3);
|
2012-07-28 10:45:27 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-08 20:30:46 +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-07-28 10:45:27 +08:00
|
|
|
|
if (!DotNetUtils.isLdcI4(ldci4))
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
info.key0 = (uint)DotNetUtils.getLdcI4Value(ldci4);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-08 20:30:46 +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++) {
|
|
|
|
|
int index = ConfuserUtils.findCallMethod(instrs, i, Code.Callvirt, "System.Int32 System.Reflection.MemberInfo::get_MetadataToken()");
|
|
|
|
|
if (index < 0)
|
|
|
|
|
break;
|
|
|
|
|
int index2 = ConfuserUtils.findCallMethod(instrs, index, Code.Call, "System.Byte[] System.BitConverter::GetBytes(System.Int32)");
|
|
|
|
|
if (index2 < 0)
|
|
|
|
|
break;
|
|
|
|
|
if (index2 - index != 3)
|
|
|
|
|
continue;
|
|
|
|
|
var ldci4 = instrs[index + 1];
|
|
|
|
|
if (!DotNetUtils.isLdcI4(ldci4))
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[index + 2].OpCode.Code != Code.Xor)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
info.key0d = (uint)DotNetUtils.getLdcI4Value(ldci4);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-09 00:34:24 +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;
|
|
|
|
|
if (!DotNetUtils.isLdloc(instrs[i + 1]))
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 2].OpCode.Code != Code.Conv_U8)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 3].OpCode.Code != Code.Mul)
|
|
|
|
|
continue;
|
|
|
|
|
if (!DotNetUtils.isStloc(instrs[i + 4]))
|
|
|
|
|
continue;
|
|
|
|
|
var ldci8_2 = instrs[i + 5];
|
|
|
|
|
if (ldci8_2.OpCode.Code != Code.Ldc_I8)
|
|
|
|
|
continue;
|
|
|
|
|
if (!DotNetUtils.isStloc(instrs[i + 6]))
|
|
|
|
|
continue;
|
|
|
|
|
var ldci8_3 = instrs[i + 7];
|
|
|
|
|
if (ldci8_3.OpCode.Code != Code.Ldc_I8)
|
|
|
|
|
continue;
|
|
|
|
|
if (!DotNetUtils.isStloc(instrs[i + 8]))
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-09 00:34:24 +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++) {
|
|
|
|
|
if (!DotNetUtils.isLdloc(instrs[i]))
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 1].OpCode.Code != Code.Xor)
|
|
|
|
|
continue;
|
|
|
|
|
var ldci4 = instrs[i + 2];
|
|
|
|
|
if (!DotNetUtils.isLdcI4(ldci4))
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 3].OpCode.Code != Code.Conv_U8)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 4].OpCode.Code != Code.Mul)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
info.key0 = (uint)DotNetUtils.getLdcI4Value(ldci4);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] decryptResource(byte[] encrypted) {
|
|
|
|
|
switch (version) {
|
|
|
|
|
case ConfuserVersion.v18_r75367_normal:
|
2012-08-09 00:34:24 +08:00
|
|
|
|
case ConfuserVersion.v18_r75369_normal:
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return decryptResource_v18_r75367_normal(encrypted);
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v18_r75367_dynamic:
|
2012-08-09 00:34:24 +08:00
|
|
|
|
case ConfuserVersion.v18_r75369_dynamic:
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return decryptResource_v18_r75367_dynamic(encrypted);
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v18_r75367_native:
|
2012-08-09 00:34:24 +08:00
|
|
|
|
case ConfuserVersion.v18_r75369_native:
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return decryptResource_v18_r75367_native(encrypted);
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new ApplicationException("Unknown version");
|
2012-07-29 16:17:05 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] getSigKey() {
|
2012-07-28 10:45:27 +08:00
|
|
|
|
uint sigToken = key0d ^ installMethod.MetadataToken.ToUInt32();
|
|
|
|
|
if ((sigToken & 0xFF000000) != 0x11000000)
|
|
|
|
|
throw new ApplicationException("Invalid sig token");
|
2012-07-29 16:17:05 +08:00
|
|
|
|
return module.GetSignatureBlob(sigToken);
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-08 20:30:46 +08:00
|
|
|
|
byte[] decryptResource_v18_r75367_normal(byte[] encrypted) {
|
2012-07-29 16:17:05 +08:00
|
|
|
|
var key = getSigKey();
|
2012-08-03 01:13:42 +08:00
|
|
|
|
var decrypted = ConfuserUtils.decrypt(BitConverter.ToUInt32(key, 12) * (uint)key0, encrypted);
|
2012-07-29 16:17:05 +08:00
|
|
|
|
return DeobUtils.inflate(DeobUtils.aesDecrypt(decrypted, key, DeobUtils.md5Sum(key)), true);
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-29 20:23:27 +08:00
|
|
|
|
static int getDynamicStartIndex(IList<Instruction> instrs, int ldlocIndex) {
|
|
|
|
|
for (int i = ldlocIndex - 1; i >= 0; i--) {
|
|
|
|
|
if (instrs[i].OpCode.FlowControl != FlowControl.Next)
|
|
|
|
|
return i + 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int getDynamicEndIndex(int startIndex, VariableDefinition 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];
|
|
|
|
|
if (DotNetUtils.isStloc(instr) && DotNetUtils.getLocalVar(installMethod.Body.Variables, instr) == local)
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VariableDefinition getDynamicLocal(out int instrIndex) {
|
|
|
|
|
var instrs = installMethod.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
|
|
|
|
i = ConfuserUtils.findCallMethod(instrs, i, Code.Callvirt, "System.Void System.IO.BinaryWriter::Write(System.Byte)");
|
|
|
|
|
if (i < 0)
|
|
|
|
|
break;
|
|
|
|
|
int index = i - 2;
|
|
|
|
|
if (index < 0)
|
|
|
|
|
continue;
|
|
|
|
|
var ldloc = instrs[index];
|
|
|
|
|
if (!DotNetUtils.isLdloc(ldloc))
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[index + 1].OpCode.Code != Code.Conv_U1)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
instrIndex = index;
|
|
|
|
|
return DotNetUtils.getLocalVar(installMethod.Body.Variables, ldloc);
|
|
|
|
|
}
|
|
|
|
|
instrIndex = 0;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-08 20:30:46 +08:00
|
|
|
|
byte[] decryptResource_v18_r75367_dynamic(byte[] encrypted) {
|
2012-07-29 20:23:27 +08:00
|
|
|
|
int ldlocIndex;
|
|
|
|
|
var local = getDynamicLocal(out ldlocIndex);
|
|
|
|
|
if (local == null)
|
|
|
|
|
throw new ApplicationException("Could not find local");
|
|
|
|
|
|
|
|
|
|
var instrs = installMethod.Body.Instructions;
|
|
|
|
|
int startIndex = getDynamicStartIndex(instrs, ldlocIndex);
|
|
|
|
|
int endIndex = getDynamicEndIndex(startIndex, local);
|
|
|
|
|
if (endIndex < 0)
|
|
|
|
|
throw new ApplicationException("Could not find endIndex");
|
|
|
|
|
|
|
|
|
|
var constReader = new ConstantsReader(installMethod);
|
|
|
|
|
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return decryptResource(encrypted, magic => {
|
2012-07-31 07:16:35 +08:00
|
|
|
|
constReader.setConstantInt32(local, magic);
|
2012-07-29 20:23:27 +08:00
|
|
|
|
int index = startIndex, result;
|
|
|
|
|
if (!constReader.getNextInt32(ref index, out result))
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2012-08-08 20:30:46 +08:00
|
|
|
|
byte[] decryptResource_v18_r75367_native(byte[] encrypted) {
|
2012-07-29 20:23:27 +08:00
|
|
|
|
var x86Emu = new x86Emulator(new PeImage(fileData));
|
2012-08-08 20:30:46 +08:00
|
|
|
|
return decryptResource(encrypted, magic => (byte)x86Emu.emulate((uint)nativeMethod.RVA, magic));
|
2012-07-29 20:23:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-08 20:30:46 +08:00
|
|
|
|
byte[] decryptResource(byte[] encrypted, Func<uint, byte> decryptFunc) {
|
2012-07-29 16:17:05 +08:00
|
|
|
|
var key = getSigKey();
|
2012-07-28 10:45:27 +08:00
|
|
|
|
|
|
|
|
|
var decrypted = DeobUtils.aesDecrypt(encrypted, key, DeobUtils.md5Sum(key));
|
|
|
|
|
decrypted = DeobUtils.inflate(decrypted, true);
|
|
|
|
|
|
|
|
|
|
var reader = new BinaryReader(new MemoryStream(decrypted));
|
|
|
|
|
var result = new MemoryStream();
|
|
|
|
|
var writer = new BinaryWriter(result);
|
|
|
|
|
while (reader.BaseStream.Position < reader.BaseStream.Length) {
|
|
|
|
|
uint magic = Utils.readEncodedUInt32(reader);
|
2012-07-29 20:23:27 +08:00
|
|
|
|
writer.Write(decryptFunc(magic));
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool verifyGenericArg(GenericInstanceMethod gim, ElementType etype) {
|
|
|
|
|
if (gim == null || gim.GenericArguments.Count != 1)
|
|
|
|
|
return false;
|
|
|
|
|
return gim.GenericArguments[0].EType == etype;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string decryptString(MethodDefinition method, GenericInstanceMethod gim, uint magic1, ulong magic2) {
|
|
|
|
|
if (!verifyGenericArg(gim, ElementType.String))
|
|
|
|
|
return null;
|
|
|
|
|
var info = decrypters.find(method);
|
|
|
|
|
return info.decryptString(magic1, magic2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object decryptInt32(MethodDefinition method, GenericInstanceMethod gim, uint magic1, ulong magic2) {
|
|
|
|
|
if (!verifyGenericArg(gim, ElementType.I4))
|
|
|
|
|
return null;
|
|
|
|
|
var info = decrypters.find(method);
|
|
|
|
|
return info.decryptInt32(magic1, magic2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object decryptInt64(MethodDefinition method, GenericInstanceMethod gim, uint magic1, ulong magic2) {
|
|
|
|
|
if (!verifyGenericArg(gim, ElementType.I8))
|
|
|
|
|
return null;
|
|
|
|
|
var info = decrypters.find(method);
|
|
|
|
|
return info.decryptInt64(magic1, magic2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object decryptSingle(MethodDefinition method, GenericInstanceMethod gim, uint magic1, ulong magic2) {
|
|
|
|
|
if (!verifyGenericArg(gim, ElementType.R4))
|
|
|
|
|
return null;
|
|
|
|
|
var info = decrypters.find(method);
|
|
|
|
|
return info.decryptSingle(magic1, magic2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object decryptDouble(MethodDefinition method, GenericInstanceMethod gim, uint magic1, ulong magic2) {
|
|
|
|
|
if (!verifyGenericArg(gim, ElementType.R8))
|
|
|
|
|
return null;
|
|
|
|
|
var info = decrypters.find(method);
|
|
|
|
|
return info.decryptDouble(magic1, magic2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
public bool getRevisionRange(out int minRev, out int maxRev) {
|
|
|
|
|
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;
|
|
|
|
|
maxRev = int.MaxValue;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
default: throw new ApplicationException("Invalid version");
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-28 10:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|