2012-01-23 02:58:31 +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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-01-24 06:13:04 +08:00
|
|
|
|
using System;
|
2012-01-23 02:58:31 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
using dot10.DotNet;
|
|
|
|
|
using dot10.DotNet.Emit;
|
2012-01-23 02:58:31 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.DeepSea {
|
|
|
|
|
class StringDecrypter {
|
2012-11-09 07:21:45 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-01-24 06:13:04 +08:00
|
|
|
|
MethodDefinitionAndDeclaringTypeDict<IDecrypterInfo> methodToInfo = new MethodDefinitionAndDeclaringTypeDict<IDecrypterInfo>();
|
2012-01-24 07:41:09 +08:00
|
|
|
|
DecrypterVersion version = DecrypterVersion.Unknown;
|
|
|
|
|
|
|
|
|
|
public enum DecrypterVersion {
|
|
|
|
|
Unknown,
|
|
|
|
|
V1_3,
|
2012-04-30 14:33:01 +08:00
|
|
|
|
V4_0,
|
|
|
|
|
V4_1,
|
2012-01-24 07:41:09 +08:00
|
|
|
|
}
|
2012-01-23 02:58:31 +08:00
|
|
|
|
|
2012-01-24 06:13:04 +08:00
|
|
|
|
interface IDecrypterInfo {
|
2012-01-24 07:41:09 +08:00
|
|
|
|
DecrypterVersion Version { get; }
|
2012-11-02 22:57:11 +08:00
|
|
|
|
MethodDef Method { get; }
|
2012-01-24 06:13:04 +08:00
|
|
|
|
string decrypt(object[] args);
|
|
|
|
|
void cleanup();
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
static short[] findKey(MethodDef initMethod, FieldDef keyField) {
|
2012-05-09 23:30:31 +08:00
|
|
|
|
var fields = new FieldDefinitionAndDeclaringTypeDict<bool>();
|
|
|
|
|
fields.add(keyField, true);
|
|
|
|
|
return findKey(initMethod, fields);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
static short[] findKey(MethodDef initMethod, FieldDefinitionAndDeclaringTypeDict<bool> fields) {
|
2012-01-24 06:13:04 +08:00
|
|
|
|
var instrs = initMethod.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 2; i++) {
|
|
|
|
|
var ldci4 = instrs[i];
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-01-24 06:13:04 +08:00
|
|
|
|
continue;
|
|
|
|
|
var newarr = instrs[i + 1];
|
|
|
|
|
if (newarr.OpCode.Code != Code.Newarr)
|
|
|
|
|
continue;
|
|
|
|
|
if (newarr.Operand.ToString() != "System.Char")
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var stloc = instrs[i + 2];
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (!stloc.IsStloc())
|
2012-01-24 06:13:04 +08:00
|
|
|
|
continue;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
var local = stloc.GetLocal(initMethod.Body.LocalList);
|
2012-01-24 06:13:04 +08:00
|
|
|
|
|
|
|
|
|
int startInitIndex = i;
|
|
|
|
|
i++;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
var array = ArrayFinder.getInitializedInt16Array(ldci4.GetLdcI4Value(), initMethod, ref i);
|
2012-01-24 06:13:04 +08:00
|
|
|
|
if (array == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var field = getStoreField(initMethod, startInitIndex, local);
|
|
|
|
|
if (field == null)
|
|
|
|
|
continue;
|
2012-05-09 23:30:31 +08:00
|
|
|
|
if (fields.find(field))
|
2012-01-24 06:13:04 +08:00
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 07:21:45 +08:00
|
|
|
|
static FieldDef getStoreField(MethodDef method, int startIndex, Local local) {
|
2012-01-24 06:13:04 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 1; i++) {
|
|
|
|
|
var ldloc = instrs[i];
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (!ldloc.IsLdloc())
|
2012-01-24 06:13:04 +08:00
|
|
|
|
continue;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (ldloc.GetLocal(method.Body.LocalList) != local)
|
2012-01-24 06:13:04 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var stsfld = instrs[i + 1];
|
|
|
|
|
if (stsfld.OpCode.Code != Code.Stsfld)
|
|
|
|
|
continue;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
return stsfld.Operand as FieldDef;
|
2012-01-24 06:13:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
static bool findMagic(MethodDef method, out int magic) {
|
2012-04-30 14:33:01 +08:00
|
|
|
|
int arg1, arg2;
|
|
|
|
|
return findMagic(method, out arg1, out arg2, out magic);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
static bool findMagic(MethodDef method, out int arg1, out int arg2, out int magic) {
|
2012-04-30 14:33:01 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 3; i++) {
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if ((arg1 = instrs[i].GetParameterIndex()) < 0)
|
2012-04-30 14:33:01 +08:00
|
|
|
|
continue;
|
|
|
|
|
var ldci4 = instrs[i + 1];
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-04-30 14:33:01 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 2].OpCode.Code != Code.Xor)
|
|
|
|
|
continue;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if ((arg2 = instrs[i + 3].GetParameterIndex()) < 0)
|
2012-04-30 14:33:01 +08:00
|
|
|
|
continue;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
magic = ldci4.GetLdcI4Value();
|
2012-04-30 14:33:01 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
arg1 = arg2 = 0;
|
|
|
|
|
magic = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
static void removeInitializeArrayCall(MethodDef method, FieldDef field) {
|
2012-07-18 20:39:27 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 1; i++) {
|
|
|
|
|
var ldtoken = instrs[i];
|
|
|
|
|
if (ldtoken.OpCode.Code != Code.Ldtoken)
|
|
|
|
|
continue;
|
|
|
|
|
if (ldtoken.Operand != field)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var call = instrs[i + 1];
|
|
|
|
|
if (call.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
var calledMethod = call.Operand as IMethod;
|
2012-07-18 20:39:27 +08:00
|
|
|
|
if (calledMethod == null)
|
|
|
|
|
continue;
|
|
|
|
|
if (calledMethod.ToString() != "System.Void System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(System.Array,System.RuntimeFieldHandle)")
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
instrs[i] = Instruction.Create(OpCodes.Pop);
|
|
|
|
|
instrs[i + 1] = Instruction.Create(OpCodes.Nop);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-30 14:33:01 +08:00
|
|
|
|
class DecrypterInfo41 : IDecrypterInfo {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
MethodDef cctor;
|
2012-04-30 14:33:01 +08:00
|
|
|
|
int magic;
|
|
|
|
|
int arg1, arg2;
|
|
|
|
|
FieldDefinitionAndDeclaringTypeDict<bool> fields;
|
|
|
|
|
ArrayInfo arrayInfo;
|
|
|
|
|
ushort[] encryptedData;
|
2012-05-09 23:30:31 +08:00
|
|
|
|
short[] key;
|
|
|
|
|
int keyShift;
|
|
|
|
|
bool isTrial;
|
2012-04-30 14:33:01 +08:00
|
|
|
|
|
|
|
|
|
class ArrayInfo {
|
|
|
|
|
public int sizeInElems;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
public ITypeDefOrRef elementType;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public FieldDef initField;
|
|
|
|
|
public FieldDef field;
|
2012-04-30 14:33:01 +08:00
|
|
|
|
|
2012-11-09 07:21:45 +08:00
|
|
|
|
public ArrayInfo(int sizeInElems, ITypeDefOrRef elementType, FieldDef initField, FieldDef field) {
|
2012-04-30 14:33:01 +08:00
|
|
|
|
this.sizeInElems = sizeInElems;
|
|
|
|
|
this.elementType = elementType;
|
|
|
|
|
this.initField = initField;
|
|
|
|
|
this.field = field;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DecrypterVersion Version {
|
|
|
|
|
get { return DecrypterVersion.V4_1; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef Method { get; private set; }
|
2012-04-30 14:33:01 +08:00
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public DecrypterInfo41(MethodDef cctor, MethodDef method) {
|
2012-04-30 14:33:01 +08:00
|
|
|
|
this.cctor = cctor;
|
|
|
|
|
Method = method;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-11 14:54:26 +08:00
|
|
|
|
public static bool isPossibleDecrypterMethod(MethodDef method, bool firstTime) {
|
2012-04-30 14:33:01 +08:00
|
|
|
|
if (!checkMethodSignature(method))
|
|
|
|
|
return false;
|
|
|
|
|
var fields = getFields(method);
|
|
|
|
|
if (fields == null || fields.Count != 3)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
static bool checkMethodSignature(MethodDef method) {
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (method.MethodSig.GetRetType().GetElementType() != ElementType.String)
|
2012-04-30 14:33:01 +08:00
|
|
|
|
return false;
|
|
|
|
|
int count = 0;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
foreach (var arg in method.MethodSig.GetParams()) {
|
|
|
|
|
if (arg.ElementType == ElementType.I4)
|
2012-04-30 14:33:01 +08:00
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
return count >= 2;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
static FieldDefinitionAndDeclaringTypeDict<bool> getFields(MethodDef method) {
|
2012-04-30 14:33:01 +08:00
|
|
|
|
var fields = new FieldDefinitionAndDeclaringTypeDict<bool>();
|
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
|
|
|
|
if (instr.OpCode.Code != Code.Ldsfld && instr.OpCode.Code != Code.Stsfld)
|
|
|
|
|
continue;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
var field = instr.Operand as FieldDef;
|
2012-04-30 14:33:01 +08:00
|
|
|
|
if (field == null)
|
|
|
|
|
continue;
|
|
|
|
|
if (field.DeclaringType != method.DeclaringType)
|
|
|
|
|
continue;
|
|
|
|
|
fields.add(field, true);
|
|
|
|
|
}
|
|
|
|
|
return fields;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool initialize() {
|
|
|
|
|
if (!findMagic(Method, out arg1, out arg2, out magic))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
fields = getFields(Method);
|
|
|
|
|
if (fields == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
arrayInfo = getArrayInfo(cctor);
|
|
|
|
|
if (arrayInfo == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (arrayInfo.initField.InitialValue.Length % 2 == 1)
|
|
|
|
|
return false;
|
|
|
|
|
encryptedData = new ushort[arrayInfo.initField.InitialValue.Length / 2];
|
|
|
|
|
Buffer.BlockCopy(arrayInfo.initField.InitialValue, 0, encryptedData, 0, arrayInfo.initField.InitialValue.Length);
|
|
|
|
|
|
2012-05-09 23:30:31 +08:00
|
|
|
|
isTrial = !DeobUtils.hasInteger(Method, 0xFFF0);
|
|
|
|
|
keyShift = findKeyShift(cctor);
|
2012-04-30 14:33:01 +08:00
|
|
|
|
key = findKey();
|
|
|
|
|
if (key == null || key.Length == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
int findKeyShift(MethodDef method) {
|
2012-05-09 23:30:31 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 3; i++) {
|
|
|
|
|
int index = i;
|
|
|
|
|
|
|
|
|
|
var ldci4 = instrs[index++];
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-05-09 23:30:31 +08:00
|
|
|
|
continue;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (ldci4.GetLdcI4Value() != 0xFF)
|
2012-05-09 23:30:31 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (instrs[index++].OpCode.Code != Code.And)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[index++].OpCode.Code != Code.Dup)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var ldci4_2 = instrs[index++];
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (!ldci4_2.IsLdcI4())
|
2012-05-09 23:30:31 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (findNextFieldUse(method, index) < 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-09 07:21:45 +08:00
|
|
|
|
return ldci4_2.GetLdcI4Value();
|
2012-05-09 23:30:31 +08:00
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
int findNextFieldUse(MethodDef method, int index) {
|
2012-05-09 23:30:31 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = index; i < instrs.Count; i++) {
|
|
|
|
|
var instr = instrs[i];
|
|
|
|
|
if (instr.OpCode.Code != Code.Ldsfld && instr.OpCode.Code != Code.Stsfld)
|
|
|
|
|
continue;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
var field = instr.Operand as IField;
|
2012-05-09 23:30:31 +08:00
|
|
|
|
if (!fields.find(field))
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
ArrayInfo getArrayInfo(MethodDef method) {
|
2012-04-30 14:33:01 +08:00
|
|
|
|
var instructions = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instructions.Count; i++) {
|
|
|
|
|
var ldci4_arraySizeInBytes = instructions[i];
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (!ldci4_arraySizeInBytes.IsLdcI4())
|
2012-04-30 14:33:01 +08:00
|
|
|
|
continue;
|
|
|
|
|
i++;
|
|
|
|
|
var instrs = DotNetUtils.getInstructions(instructions, i, OpCodes.Newarr, OpCodes.Dup, OpCodes.Ldtoken, OpCodes.Call, OpCodes.Stsfld);
|
|
|
|
|
if (instrs == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-09 07:21:45 +08:00
|
|
|
|
int sizeInBytes = ldci4_arraySizeInBytes.GetLdcI4Value();
|
|
|
|
|
var elementType = instrs[0].Operand as ITypeDefOrRef;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
var initField = instrs[2].Operand as FieldDef;
|
|
|
|
|
var field = instrs[4].Operand as FieldDef;
|
2012-04-30 14:33:01 +08:00
|
|
|
|
if (elementType == null)
|
|
|
|
|
continue;
|
|
|
|
|
if (initField == null || initField.InitialValue == null || initField.InitialValue.Length == 0)
|
|
|
|
|
continue;
|
|
|
|
|
if (!fields.find(field))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return new ArrayInfo(sizeInBytes, elementType, initField, field);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-09 23:30:31 +08:00
|
|
|
|
short[] findKey() {
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (cctor.OwnerModule.Assembly == null)
|
2012-04-30 14:33:01 +08:00
|
|
|
|
return null;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
var pkt = cctor.OwnerModule.Assembly.PublicKeyToken;
|
|
|
|
|
if (!PublicKeyBase.IsNullOrEmpty2(pkt))
|
|
|
|
|
return getPublicKeyTokenKey(pkt.Data);
|
2012-04-30 14:33:01 +08:00
|
|
|
|
return findKey(cctor);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
short[] findKey(MethodDef initMethod) {
|
2012-05-09 23:30:31 +08:00
|
|
|
|
return StringDecrypter.findKey(initMethod, fields);
|
2012-04-30 14:33:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-05-09 23:30:31 +08:00
|
|
|
|
short[] getPublicKeyTokenKey(byte[] publicKeyToken) {
|
|
|
|
|
if (keyShift < 0)
|
|
|
|
|
throw new ApplicationException("Could not find shift value");
|
|
|
|
|
var key = new short[publicKeyToken.Length];
|
2012-04-30 14:33:01 +08:00
|
|
|
|
for (int i = 0; i < publicKeyToken.Length; i++) {
|
|
|
|
|
int b = publicKeyToken[i];
|
2012-05-09 23:30:31 +08:00
|
|
|
|
key[i] = (short)((b << keyShift) ^ b);
|
2012-04-30 14:33:01 +08:00
|
|
|
|
}
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string decrypt(object[] args) {
|
2012-05-09 23:30:31 +08:00
|
|
|
|
if (isTrial)
|
|
|
|
|
return decryptTrial((int)args[arg1], (int)args[arg2]);
|
|
|
|
|
return decryptRetail((int)args[arg1], (int)args[arg2]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string decryptTrial(int magic2, int magic3) {
|
|
|
|
|
int offset = magic ^ magic2 ^ magic3;
|
|
|
|
|
var keyChar = encryptedData[offset + 1];
|
|
|
|
|
int cachedIndex = encryptedData[offset] ^ keyChar;
|
|
|
|
|
int numChars = ((keyChar ^ encryptedData[offset + 2]) << 16) + (keyChar ^ encryptedData[offset + 3]);
|
|
|
|
|
offset += 4;
|
|
|
|
|
var sb = new StringBuilder(numChars);
|
|
|
|
|
for (int i = 0; i < numChars; i++)
|
|
|
|
|
sb.Append((char)(keyChar ^ encryptedData[offset + i] ^ key[(offset + i) % key.Length]));
|
|
|
|
|
return sb.ToString();
|
2012-04-30 14:33:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-05-09 23:30:31 +08:00
|
|
|
|
string decryptRetail(int magic2, int magic3) {
|
2012-04-30 14:33:01 +08:00
|
|
|
|
int offset = magic ^ magic2 ^ magic3;
|
|
|
|
|
var keyChar = encryptedData[offset + 2];
|
|
|
|
|
int cachedIndex = encryptedData[offset + 1] ^ keyChar;
|
|
|
|
|
int flags = encryptedData[offset] ^ keyChar;
|
|
|
|
|
int numChars = (flags >> 1) & ~7 | (flags & 7);
|
|
|
|
|
if ((flags & 8) != 0) {
|
|
|
|
|
numChars <<= 15;
|
|
|
|
|
numChars |= encryptedData[offset + 3] ^ keyChar;
|
|
|
|
|
offset++;
|
|
|
|
|
}
|
|
|
|
|
offset += 3;
|
2012-04-30 18:49:43 +08:00
|
|
|
|
var sb = new StringBuilder(numChars);
|
2012-04-30 14:33:01 +08:00
|
|
|
|
for (int i = 0; i < numChars; i++)
|
|
|
|
|
sb.Append((char)(keyChar ^ encryptedData[offset + numChars - i - 1] ^ key[(i + 1 + offset) % key.Length]));
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void cleanup() {
|
|
|
|
|
arrayInfo.initField.InitialValue = new byte[1];
|
2012-11-09 07:21:45 +08:00
|
|
|
|
arrayInfo.initField.FieldSig.Type = arrayInfo.initField.OwnerModule.CorLibTypes.Byte;
|
2012-07-18 20:39:27 +08:00
|
|
|
|
removeInitializeArrayCall(cctor, arrayInfo.initField);
|
2012-04-30 14:33:01 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DecrypterInfo40 : IDecrypterInfo {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
MethodDef cctor;
|
2012-01-24 06:13:04 +08:00
|
|
|
|
int magic;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
FieldDef cachedStringsField;
|
|
|
|
|
FieldDef keyField;
|
|
|
|
|
FieldDef encryptedStringsField;
|
|
|
|
|
FieldDef encryptedDataField;
|
2012-01-24 06:13:04 +08:00
|
|
|
|
short[] key;
|
|
|
|
|
ushort[] encryptedData;
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef Method { get; private set; }
|
2012-04-30 14:33:01 +08:00
|
|
|
|
|
2012-01-24 07:41:09 +08:00
|
|
|
|
public DecrypterVersion Version {
|
2012-04-30 14:33:01 +08:00
|
|
|
|
get { return DecrypterVersion.V4_0; }
|
2012-01-24 07:41:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public DecrypterInfo40(MethodDef cctor, MethodDef method) {
|
2012-01-24 06:13:04 +08:00
|
|
|
|
this.cctor = cctor;
|
|
|
|
|
this.Method = method;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-11 14:54:26 +08:00
|
|
|
|
public static bool isPossibleDecrypterMethod(MethodDef method, bool firstTime) {
|
|
|
|
|
if (!firstTime || !checkFields(method.DeclaringType.Fields))
|
2012-04-30 14:33:01 +08:00
|
|
|
|
return false;
|
|
|
|
|
return DotNetUtils.isMethod(method, "System.String", "(System.Int32,System.Int32)");
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-24 06:13:04 +08:00
|
|
|
|
public bool initialize() {
|
|
|
|
|
if (!findMagic(Method, out magic))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var charArrayFields = findFields();
|
|
|
|
|
if (charArrayFields == null || charArrayFields.Count != 2)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
encryptedStringsField = findEncryptedStrings(cctor, charArrayFields, out encryptedDataField);
|
|
|
|
|
if (encryptedStringsField == null)
|
|
|
|
|
return false;
|
|
|
|
|
if (encryptedDataField.InitialValue.Length % 2 == 1)
|
|
|
|
|
return false;
|
|
|
|
|
encryptedData = new ushort[encryptedDataField.InitialValue.Length / 2];
|
|
|
|
|
Buffer.BlockCopy(encryptedDataField.InitialValue, 0, encryptedData, 0, encryptedDataField.InitialValue.Length);
|
|
|
|
|
|
|
|
|
|
charArrayFields.Remove(encryptedStringsField);
|
|
|
|
|
keyField = charArrayFields[0];
|
|
|
|
|
|
|
|
|
|
key = findKey();
|
|
|
|
|
if (key == null || key.Length == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
List<FieldDef> findFields() {
|
|
|
|
|
var charArrayFields = new List<FieldDef>();
|
2012-01-24 06:13:04 +08:00
|
|
|
|
|
|
|
|
|
foreach (var instr in Method.Body.Instructions) {
|
|
|
|
|
if (instr.OpCode.Code != Code.Stsfld && instr.OpCode.Code != Code.Ldsfld)
|
|
|
|
|
continue;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
var field = instr.Operand as FieldDef;
|
2012-01-24 06:13:04 +08:00
|
|
|
|
if (field == null)
|
|
|
|
|
continue;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (!new SigComparer().Equals(Method.DeclaringType, field.DeclaringType))
|
2012-01-24 06:13:04 +08:00
|
|
|
|
continue;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
switch (field.FieldSig.GetFieldType().GetFullName()) {
|
2012-01-24 06:13:04 +08:00
|
|
|
|
case "System.Char[]":
|
|
|
|
|
if (!charArrayFields.Contains(field))
|
|
|
|
|
charArrayFields.Add(field);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "System.String[]":
|
|
|
|
|
if (cachedStringsField != null && cachedStringsField != field)
|
|
|
|
|
return null;
|
|
|
|
|
cachedStringsField = field;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cachedStringsField == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return charArrayFields;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
static FieldDef findEncryptedStrings(MethodDef initMethod, List<FieldDef> ourFields, out FieldDef dataField) {
|
2012-01-24 06:13:04 +08:00
|
|
|
|
for (int i = 0; i < initMethod.Body.Instructions.Count; i++) {
|
|
|
|
|
var instrs = DotNetUtils.getInstructions(initMethod.Body.Instructions, i, OpCodes.Ldtoken, OpCodes.Call, OpCodes.Stsfld);
|
|
|
|
|
if (instrs == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
dataField = instrs[0].Operand as FieldDef;
|
2012-01-24 06:13:04 +08:00
|
|
|
|
if (dataField == null || dataField.InitialValue == null || dataField.InitialValue.Length == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
var savedField = instrs[2].Operand as FieldDef;
|
2012-01-24 06:13:04 +08:00
|
|
|
|
if (savedField == null || !matches(ourFields, savedField))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return savedField;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dataField = null;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 07:21:45 +08:00
|
|
|
|
static bool matches(IEnumerable<FieldDef> ourFields, FieldDef field) {
|
2012-01-24 06:13:04 +08:00
|
|
|
|
foreach (var ourField in ourFields) {
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (FieldEqualityComparer.CompareDeclaringTypes.Equals(ourField, field))
|
2012-01-24 06:13:04 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
short[] findKey() {
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (cctor.OwnerModule.Assembly == null)
|
2012-02-09 05:01:10 +08:00
|
|
|
|
return null;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
var pkt = cctor.OwnerModule.Assembly.PublicKeyToken;
|
|
|
|
|
if (!PublicKeyBase.IsNullOrEmpty2(pkt))
|
|
|
|
|
return getPublicKeyTokenKey(pkt.Data);
|
2012-01-24 06:13:04 +08:00
|
|
|
|
return findKey(cctor);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
short[] findKey(MethodDef initMethod) {
|
2012-01-24 06:13:04 +08:00
|
|
|
|
return StringDecrypter.findKey(initMethod, keyField);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static short[] getPublicKeyTokenKey(byte[] publicKeyToken) {
|
|
|
|
|
var key = new short[publicKeyToken.Length];
|
|
|
|
|
for (int i = 0; i < publicKeyToken.Length; i++) {
|
|
|
|
|
int b = publicKeyToken[i];
|
|
|
|
|
key[i] = (short)((b << 4) ^ b);
|
|
|
|
|
}
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string decrypt(object[] args) {
|
|
|
|
|
return decrypt((int)args[0], (int)args[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string decrypt(int magic2, int magic3) {
|
|
|
|
|
int index = magic ^ magic2 ^ magic3;
|
2012-01-25 13:22:47 +08:00
|
|
|
|
int cachedIndex = encryptedData[index++];
|
|
|
|
|
int stringLen = encryptedData[index++] + ((int)encryptedData[index++] << 16);
|
2012-01-24 06:13:04 +08:00
|
|
|
|
var sb = new StringBuilder(stringLen);
|
|
|
|
|
for (int i = 0; i < stringLen; i++)
|
|
|
|
|
sb.Append((char)(encryptedData[index++] ^ key[cachedIndex++ % key.Length]));
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void cleanup() {
|
2012-01-24 16:06:54 +08:00
|
|
|
|
encryptedDataField.InitialValue = new byte[1];
|
2012-11-09 07:21:45 +08:00
|
|
|
|
encryptedDataField.FieldSig.Type = encryptedDataField.OwnerModule.CorLibTypes.Byte;
|
2012-07-18 20:39:27 +08:00
|
|
|
|
removeInitializeArrayCall(cctor, encryptedDataField);
|
2012-01-24 06:13:04 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-30 14:33:01 +08:00
|
|
|
|
class DecrypterInfo13 : IDecrypterInfo {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
MethodDef cctor;
|
|
|
|
|
FieldDef cachedStringsField;
|
|
|
|
|
FieldDef keyField;
|
2012-01-23 02:58:31 +08:00
|
|
|
|
int magic;
|
|
|
|
|
string[] encryptedStrings;
|
|
|
|
|
short[] key;
|
2012-01-24 07:41:09 +08:00
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef Method { get; private set; }
|
2012-04-30 14:33:01 +08:00
|
|
|
|
|
2012-01-24 07:41:09 +08:00
|
|
|
|
public DecrypterVersion Version {
|
|
|
|
|
get { return DecrypterVersion.V1_3; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-11 14:54:26 +08:00
|
|
|
|
public static bool isPossibleDecrypterMethod(MethodDef method, bool firstTime) {
|
|
|
|
|
if (!firstTime || !checkFields(method.DeclaringType.Fields))
|
2012-04-30 14:33:01 +08:00
|
|
|
|
return false;
|
|
|
|
|
return DotNetUtils.isMethod(method, "System.String", "(System.Int32)");
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public DecrypterInfo13(MethodDef cctor, MethodDef method) {
|
2012-01-23 02:58:31 +08:00
|
|
|
|
this.cctor = cctor;
|
2012-01-24 06:13:04 +08:00
|
|
|
|
this.Method = method;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string decrypt(object[] args) {
|
|
|
|
|
return decrypt((int)args[0]);
|
2012-01-23 02:58:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-01-24 06:13:04 +08:00
|
|
|
|
string decrypt(int magic2) {
|
2012-01-23 02:58:31 +08:00
|
|
|
|
var es = encryptedStrings[magic ^ magic2];
|
|
|
|
|
var sb = new StringBuilder(es.Length);
|
|
|
|
|
for (int i = 0; i < es.Length; i++)
|
|
|
|
|
sb.Append((char)(es[i] ^ key[(magic2 + i) % key.Length]));
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool initialize() {
|
2012-01-24 06:13:04 +08:00
|
|
|
|
if (!findMagic(Method, out magic))
|
2012-01-23 02:58:31 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!findFields())
|
|
|
|
|
return false;
|
|
|
|
|
|
2012-01-24 06:13:04 +08:00
|
|
|
|
if (!findEncryptedStrings(Method))
|
2012-01-23 02:58:31 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
key = findKey();
|
2012-02-09 05:01:10 +08:00
|
|
|
|
if (key == null || key.Length == 0)
|
2012-01-23 02:58:31 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool findFields() {
|
2012-01-24 06:13:04 +08:00
|
|
|
|
foreach (var instr in Method.Body.Instructions) {
|
2012-01-23 02:58:31 +08:00
|
|
|
|
if (instr.OpCode.Code != Code.Stsfld && instr.OpCode.Code != Code.Ldsfld)
|
|
|
|
|
continue;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
var field = instr.Operand as FieldDef;
|
2012-01-23 02:58:31 +08:00
|
|
|
|
if (field == null)
|
|
|
|
|
continue;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (!new SigComparer().Equals(Method.DeclaringType, field.DeclaringType))
|
2012-01-23 02:58:31 +08:00
|
|
|
|
continue;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
switch (field.FieldSig.GetFieldType().GetFullName()) {
|
2012-01-23 02:58:31 +08:00
|
|
|
|
case "System.Char[]":
|
|
|
|
|
if (keyField != null && keyField != field)
|
|
|
|
|
return false;
|
|
|
|
|
keyField = field;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "System.String[]":
|
|
|
|
|
if (cachedStringsField != null && cachedStringsField != field)
|
|
|
|
|
return false;
|
|
|
|
|
cachedStringsField = field;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return keyField != null && cachedStringsField != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
short[] findKey() {
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (cctor.OwnerModule.Assembly == null)
|
2012-02-09 05:01:10 +08:00
|
|
|
|
return null;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
var pkt = cctor.OwnerModule.Assembly.PublicKeyToken;
|
|
|
|
|
if (!PublicKeyBase.IsNullOrEmpty2(pkt))
|
|
|
|
|
return getPublicKeyTokenKey(pkt.Data);
|
2012-01-23 02:58:31 +08:00
|
|
|
|
return findKey(cctor);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
short[] findKey(MethodDef initMethod) {
|
2012-01-24 06:13:04 +08:00
|
|
|
|
return StringDecrypter.findKey(initMethod, keyField);
|
2012-01-23 02:58:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static short[] getPublicKeyTokenKey(byte[] publicKeyToken) {
|
|
|
|
|
var key = new short[publicKeyToken.Length];
|
|
|
|
|
for (int i = 0; i < publicKeyToken.Length; i++) {
|
|
|
|
|
int b = publicKeyToken[i];
|
|
|
|
|
key[i] = (short)((b << 4) ^ b);
|
|
|
|
|
}
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
static bool findMagic(MethodDef method, out int magic) {
|
2012-01-23 02:58:31 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 2; i++) {
|
|
|
|
|
var ldarg = instrs[i];
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (ldarg.GetParameterIndex() < 0)
|
2012-01-23 02:58:31 +08:00
|
|
|
|
continue;
|
|
|
|
|
var ldci4 = instrs[i + 1];
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-01-23 02:58:31 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 2].OpCode.Code != Code.Xor)
|
|
|
|
|
continue;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
magic = ldci4.GetLdcI4Value();
|
2012-01-23 02:58:31 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
magic = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
bool findEncryptedStrings(MethodDef method) {
|
2012-01-23 02:58:31 +08:00
|
|
|
|
var switchInstr = getOnlySwitchInstruction(method);
|
|
|
|
|
if (switchInstr == null)
|
|
|
|
|
return false;
|
|
|
|
|
var targets = (Instruction[])switchInstr.Operand;
|
|
|
|
|
encryptedStrings = new string[targets.Length];
|
|
|
|
|
for (int i = 0; i < targets.Length; i++) {
|
|
|
|
|
var target = targets[i];
|
|
|
|
|
if (target.OpCode.Code != Code.Ldstr)
|
|
|
|
|
return false;
|
|
|
|
|
encryptedStrings[i] = (string)target.Operand;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
static Instruction getOnlySwitchInstruction(MethodDef method) {
|
2012-01-23 02:58:31 +08:00
|
|
|
|
Instruction switchInstr = null;
|
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
|
|
|
|
if (instr.OpCode.Code != Code.Switch)
|
|
|
|
|
continue;
|
|
|
|
|
if (switchInstr != null)
|
|
|
|
|
return null;
|
|
|
|
|
switchInstr = instr;
|
|
|
|
|
}
|
|
|
|
|
return switchInstr;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-24 06:13:04 +08:00
|
|
|
|
public void cleanup() {
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-23 02:58:31 +08:00
|
|
|
|
public override string ToString() {
|
|
|
|
|
return string.Format("M:{0:X8} N:{1}", magic, encryptedStrings.Length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return methodToInfo.Count != 0; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-24 07:41:09 +08:00
|
|
|
|
public DecrypterVersion Version {
|
|
|
|
|
get { return version; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public List<MethodDef> DecrypterMethods {
|
2012-01-23 02:58:31 +08:00
|
|
|
|
get {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
var methods = new List<MethodDef>(methodToInfo.Count);
|
2012-01-23 02:58:31 +08:00
|
|
|
|
foreach (var info in methodToInfo.getValues())
|
2012-01-24 06:13:04 +08:00
|
|
|
|
methods.Add(info.Method);
|
2012-01-23 02:58:31 +08:00
|
|
|
|
return methods;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 07:21:45 +08:00
|
|
|
|
public StringDecrypter(ModuleDefMD module) {
|
2012-01-23 02:58:31 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void find(ISimpleDeobfuscator simpleDeobfuscator) {
|
2012-02-09 05:01:10 +08:00
|
|
|
|
if (module.Assembly == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-11-09 07:21:45 +08:00
|
|
|
|
var pkt = module.Assembly.PublicKeyToken;
|
|
|
|
|
bool hasPublicKeyToken = !PublicKeyBase.IsNullOrEmpty2(pkt);
|
2012-01-23 02:58:31 +08:00
|
|
|
|
foreach (var type in module.GetTypes()) {
|
2012-11-09 07:21:45 +08:00
|
|
|
|
var cctor = type.FindClassConstructor();
|
2012-01-23 02:58:31 +08:00
|
|
|
|
if (cctor == null)
|
|
|
|
|
continue;
|
2012-01-24 06:13:04 +08:00
|
|
|
|
|
2012-04-30 14:33:01 +08:00
|
|
|
|
bool deobfuscatedCctor = false;
|
2012-11-11 14:54:26 +08:00
|
|
|
|
bool firstTime = true;
|
2012-01-24 06:13:04 +08:00
|
|
|
|
foreach (var method in type.Methods) {
|
2012-04-30 14:33:01 +08:00
|
|
|
|
if (!method.IsStatic || method.Body == null)
|
2012-01-24 06:13:04 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
IDecrypterInfo info = null;
|
|
|
|
|
|
2012-11-11 14:54:26 +08:00
|
|
|
|
if (DecrypterInfo13.isPossibleDecrypterMethod(method, firstTime)) {
|
2012-04-30 14:33:01 +08:00
|
|
|
|
deobfuscateCctor(simpleDeobfuscator, cctor, ref deobfuscatedCctor, hasPublicKeyToken);
|
|
|
|
|
simpleDeobfuscator.deobfuscate(method);
|
|
|
|
|
info = getInfoV13(cctor, method);
|
|
|
|
|
}
|
2012-11-11 14:54:26 +08:00
|
|
|
|
else if (DecrypterInfo40.isPossibleDecrypterMethod(method, firstTime)) {
|
2012-04-30 14:33:01 +08:00
|
|
|
|
deobfuscateCctor(simpleDeobfuscator, cctor, ref deobfuscatedCctor, hasPublicKeyToken);
|
2012-01-24 06:13:04 +08:00
|
|
|
|
simpleDeobfuscator.deobfuscate(method);
|
2012-04-30 14:33:01 +08:00
|
|
|
|
info = getInfoV40(cctor, method);
|
2012-01-24 06:13:04 +08:00
|
|
|
|
}
|
2012-11-11 14:54:26 +08:00
|
|
|
|
else if (DecrypterInfo41.isPossibleDecrypterMethod(method, firstTime)) {
|
2012-04-30 14:33:01 +08:00
|
|
|
|
deobfuscateCctor(simpleDeobfuscator, cctor, ref deobfuscatedCctor, hasPublicKeyToken);
|
2012-01-24 06:13:04 +08:00
|
|
|
|
simpleDeobfuscator.deobfuscate(method);
|
2012-04-30 14:33:01 +08:00
|
|
|
|
info = getInfoV41(cctor, method);
|
2012-01-24 06:13:04 +08:00
|
|
|
|
}
|
2012-11-11 14:54:26 +08:00
|
|
|
|
firstTime = false;
|
2012-01-24 06:13:04 +08:00
|
|
|
|
|
|
|
|
|
if (info == null)
|
|
|
|
|
continue;
|
|
|
|
|
methodToInfo.add(method, info);
|
2012-01-24 07:41:09 +08:00
|
|
|
|
version = info.Version;
|
2012-01-24 06:13:04 +08:00
|
|
|
|
}
|
2012-01-23 02:58:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
static void deobfuscateCctor(ISimpleDeobfuscator simpleDeobfuscator, MethodDef cctor, ref bool deobfuscatedCctor, bool hasPublicKeyToken) {
|
2012-04-30 14:33:01 +08:00
|
|
|
|
if (deobfuscatedCctor || hasPublicKeyToken)
|
|
|
|
|
return;
|
|
|
|
|
simpleDeobfuscator.deobfuscate(cctor);
|
|
|
|
|
deobfuscatedCctor = true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
static bool checkFields(IEnumerable<FieldDef> fields) {
|
2012-01-23 02:58:31 +08:00
|
|
|
|
bool foundCharAry = false, foundStringAry = false;
|
|
|
|
|
foreach (var field in fields) {
|
|
|
|
|
if (foundCharAry && foundStringAry)
|
|
|
|
|
break;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
switch (field.FieldSig.GetFieldType().GetFullName()) {
|
2012-01-23 02:58:31 +08:00
|
|
|
|
case "System.Char[]":
|
|
|
|
|
foundCharAry = true;
|
|
|
|
|
break;
|
|
|
|
|
case "System.String[]":
|
|
|
|
|
foundStringAry = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return foundCharAry && foundStringAry;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
DecrypterInfo13 getInfoV13(MethodDef cctor, MethodDef method) {
|
2012-04-30 14:33:01 +08:00
|
|
|
|
var info = new DecrypterInfo13(cctor, method);
|
|
|
|
|
if (!info.initialize())
|
|
|
|
|
return null;
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
DecrypterInfo40 getInfoV40(MethodDef cctor, MethodDef method) {
|
2012-04-30 14:33:01 +08:00
|
|
|
|
var info = new DecrypterInfo40(cctor, method);
|
2012-01-24 06:13:04 +08:00
|
|
|
|
if (!info.initialize())
|
2012-01-23 02:58:31 +08:00
|
|
|
|
return null;
|
2012-01-24 06:13:04 +08:00
|
|
|
|
return info;
|
|
|
|
|
}
|
2012-01-23 02:58:31 +08:00
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
DecrypterInfo41 getInfoV41(MethodDef cctor, MethodDef method) {
|
2012-04-30 14:33:01 +08:00
|
|
|
|
var info = new DecrypterInfo41(cctor, method);
|
2012-01-23 02:58:31 +08:00
|
|
|
|
if (!info.initialize())
|
|
|
|
|
return null;
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 07:21:45 +08:00
|
|
|
|
public string decrypt(IMethod method, object[] args) {
|
2012-01-23 02:58:31 +08:00
|
|
|
|
var info = methodToInfo.find(method);
|
2012-01-24 06:13:04 +08:00
|
|
|
|
return info.decrypt(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void cleanup() {
|
|
|
|
|
foreach (var info in methodToInfo.getValues())
|
|
|
|
|
info.cleanup();
|
2012-01-23 02:58:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|