2017-08-08 18:27:21 +08:00
|
|
|
|
using System;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
using System.Collections.Generic;
|
2017-08-20 21:25:25 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
2017-08-08 18:27:21 +08:00
|
|
|
|
using de4dot.blocks;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
using de4dot.blocks.cflow;
|
|
|
|
|
using de4dot.code.deobfuscators.ConfuserEx.x86;
|
2017-08-08 18:27:21 +08:00
|
|
|
|
using dnlib.DotNet;
|
2017-08-20 21:25:25 +08:00
|
|
|
|
using dnlib.DotNet.Writer;
|
|
|
|
|
using FieldAttributes = dnlib.DotNet.FieldAttributes;
|
|
|
|
|
using MethodAttributes = dnlib.DotNet.MethodAttributes;
|
|
|
|
|
using OpCodes = dnlib.DotNet.Emit.OpCodes;
|
|
|
|
|
using TypeAttributes = dnlib.DotNet.TypeAttributes;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.ConfuserEx
|
|
|
|
|
{
|
|
|
|
|
public class ConstantDecrypterBase
|
|
|
|
|
{
|
|
|
|
|
private readonly InstructionEmulator _instructionEmulator = new InstructionEmulator();
|
2017-08-08 18:27:21 +08:00
|
|
|
|
private X86Method _nativeMethod;
|
|
|
|
|
|
|
|
|
|
public MethodDef Method { get; set; }
|
|
|
|
|
public MethodDef NativeMethod { get; set; }
|
|
|
|
|
public byte[] Decrypted { get; set; }
|
|
|
|
|
public uint Magic1 { get; set; }
|
|
|
|
|
public uint Magic2 { get; set; }
|
|
|
|
|
public bool CanRemove { get; set; } = true;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
|
|
|
|
private int? CalculateKey()
|
|
|
|
|
{
|
|
|
|
|
var popValue = _instructionEmulator.Peek();
|
|
|
|
|
|
|
|
|
|
if (popValue == null || !popValue.IsInt32() || !(popValue as Int32Value).AllBitsValid())
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
_instructionEmulator.Pop();
|
2017-08-08 18:27:21 +08:00
|
|
|
|
var result = _nativeMethod.Execute(((Int32Value) popValue).Value);
|
2017-02-13 17:14:22 +08:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private uint CalculateMagic(uint index)
|
|
|
|
|
{
|
2017-08-08 18:27:21 +08:00
|
|
|
|
_instructionEmulator.Push(new Int32Value((int) index));
|
2017-02-13 17:14:22 +08:00
|
|
|
|
_nativeMethod = new X86Method(NativeMethod, Method.Module as ModuleDefMD); //TODO: Possible null
|
2017-08-08 18:27:21 +08:00
|
|
|
|
var key = CalculateKey();
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
2017-08-08 18:27:21 +08:00
|
|
|
|
var uint_0 = (uint) key.Value;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
uint_0 &= 0x3fffffff;
|
|
|
|
|
uint_0 <<= 2;
|
|
|
|
|
return uint_0;
|
|
|
|
|
}
|
2017-08-08 18:27:21 +08:00
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
public string DecryptString(uint index)
|
|
|
|
|
{
|
|
|
|
|
index = CalculateMagic(index);
|
2017-08-08 18:27:21 +08:00
|
|
|
|
var count = BitConverter.ToInt32(Decrypted, (int) index);
|
|
|
|
|
return string.Intern(Encoding.UTF8.GetString(Decrypted, (int) index + 4, count));
|
2017-02-13 17:14:22 +08:00
|
|
|
|
}
|
2017-08-08 18:27:21 +08:00
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
public T DecryptConstant<T>(uint index)
|
|
|
|
|
{
|
|
|
|
|
index = CalculateMagic(index);
|
2017-08-08 18:27:21 +08:00
|
|
|
|
var array = new T[1];
|
|
|
|
|
Buffer.BlockCopy(Decrypted, (int) index, array, 0, Marshal.SizeOf(typeof(T)));
|
2017-02-13 17:14:22 +08:00
|
|
|
|
return array[0];
|
|
|
|
|
}
|
2017-08-08 18:27:21 +08:00
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
public byte[] DecryptArray(uint index)
|
|
|
|
|
{
|
|
|
|
|
index = CalculateMagic(index);
|
2017-08-08 18:27:21 +08:00
|
|
|
|
var count = BitConverter.ToInt32(Decrypted, (int) index);
|
2017-02-13 17:14:22 +08:00
|
|
|
|
//int lengt = BitConverter.ToInt32(Decrypted, (int)index+4); we actualy dont need that
|
2017-08-08 18:27:21 +08:00
|
|
|
|
var buffer = new byte[count - 4];
|
|
|
|
|
Buffer.BlockCopy(Decrypted, (int) index + 8, buffer, 0, count - 4);
|
2017-02-13 17:14:22 +08:00
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ConstantsDecrypter
|
|
|
|
|
{
|
2017-08-08 18:27:21 +08:00
|
|
|
|
private readonly ISimpleDeobfuscator _deobfuscator;
|
|
|
|
|
private readonly MethodDef _lzmaMethod;
|
|
|
|
|
|
|
|
|
|
private readonly ModuleDef _module;
|
|
|
|
|
|
|
|
|
|
private readonly string[] _strDecryptCalledMethods =
|
2017-02-13 17:14:22 +08:00
|
|
|
|
{
|
2017-08-08 18:27:21 +08:00
|
|
|
|
"System.Text.Encoding System.Text.Encoding::get_UTF8()",
|
|
|
|
|
"System.String System.Text.Encoding::GetString(System.Byte[],System.Int32,System.Int32)",
|
|
|
|
|
"System.Array System.Array::CreateInstance(System.Type,System.Int32)",
|
|
|
|
|
"System.String System.String::Intern(System.String)",
|
|
|
|
|
"System.Void System.Buffer::BlockCopy(System.Array,System.Int32,System.Array,System.Int32,System.Int32)",
|
|
|
|
|
"System.Type System.Type::GetTypeFromHandle(System.RuntimeTypeHandle)",
|
|
|
|
|
"System.Type System.Type::GetElementType()"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private byte[] _decryptedBytes;
|
|
|
|
|
private FieldDef _decryptedField, _arrayField;
|
|
|
|
|
internal TypeDef ArrayType;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
|
|
|
|
public ConstantsDecrypter(ModuleDef module, MethodDef lzmaMethod, ISimpleDeobfuscator deobfsucator)
|
|
|
|
|
{
|
2017-08-08 18:27:21 +08:00
|
|
|
|
_module = module;
|
|
|
|
|
_lzmaMethod = lzmaMethod;
|
|
|
|
|
_deobfuscator = deobfsucator;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-20 21:25:25 +08:00
|
|
|
|
public bool CanRemoveLzma { get; private set; }
|
2017-08-08 18:27:21 +08:00
|
|
|
|
|
|
|
|
|
public TypeDef Type => ArrayType;
|
|
|
|
|
|
|
|
|
|
public MethodDef Method { get; private set; }
|
|
|
|
|
|
|
|
|
|
public List<FieldDef> Fields => new List<FieldDef> {_decryptedField, _arrayField};
|
|
|
|
|
|
|
|
|
|
public List<ConstantDecrypterBase> Decrypters { get; } = new List<ConstantDecrypterBase>();
|
|
|
|
|
|
|
|
|
|
public bool Detected => Method != null && _decryptedBytes != null && Decrypters.Count != 0 &&
|
|
|
|
|
_decryptedField != null && _arrayField != null;
|
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
public void Find()
|
|
|
|
|
{
|
2017-08-08 18:27:21 +08:00
|
|
|
|
var moduleCctor = DotNetUtils.GetModuleTypeCctor(_module);
|
2017-02-13 17:14:22 +08:00
|
|
|
|
if (moduleCctor == null)
|
|
|
|
|
return;
|
|
|
|
|
foreach (var inst in moduleCctor.Body.Instructions)
|
|
|
|
|
{
|
|
|
|
|
if (inst.OpCode != OpCodes.Call)
|
|
|
|
|
continue;
|
|
|
|
|
if (!(inst.Operand is MethodDef))
|
|
|
|
|
continue;
|
2017-08-08 18:27:21 +08:00
|
|
|
|
var method = (MethodDef) inst.Operand;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
if (!method.HasBody || !method.IsStatic)
|
|
|
|
|
continue;
|
|
|
|
|
if (!DotNetUtils.IsMethod(method, "System.Void", "()"))
|
|
|
|
|
continue;
|
2017-08-20 21:25:25 +08:00
|
|
|
|
|
2017-08-08 18:27:21 +08:00
|
|
|
|
_deobfuscator.Deobfuscate(method, SimpleDeobfuscatorFlags.Force);
|
2017-08-20 21:25:25 +08:00
|
|
|
|
|
|
|
|
|
if (!IsStringDecrypterInit(method, out FieldDef aField, out FieldDef dField))
|
2017-02-13 17:14:22 +08:00
|
|
|
|
continue;
|
2017-08-20 21:25:25 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_decryptedBytes = DecryptArray(method, aField.InitialValue);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e.Message);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_arrayField = aField;
|
|
|
|
|
_decryptedField = dField;
|
|
|
|
|
ArrayType = DotNetUtils.GetType(_module, _arrayField.FieldSig.Type);
|
2017-08-08 18:27:21 +08:00
|
|
|
|
Method = method;
|
2017-08-20 21:25:25 +08:00
|
|
|
|
Decrypters.AddRange(FindStringDecrypters(moduleCctor.DeclaringType));
|
|
|
|
|
CanRemoveLzma = true;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-20 21:25:25 +08:00
|
|
|
|
private bool IsStringDecrypterInit(MethodDef method, out FieldDef aField, out FieldDef dField)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
{
|
2017-08-20 21:25:25 +08:00
|
|
|
|
aField = null;
|
|
|
|
|
dField = null;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
var instructions = method.Body.Instructions;
|
|
|
|
|
if (instructions.Count < 15)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!instructions[0].IsLdcI4())
|
|
|
|
|
return false;
|
2017-08-08 18:27:21 +08:00
|
|
|
|
if (!instructions[1].IsStloc()) //uint num = 96u;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!instructions[2].IsLdcI4())
|
|
|
|
|
return false;
|
|
|
|
|
if (instructions[0].GetLdcI4Value() != instructions[2].GetLdcI4Value())
|
|
|
|
|
return false;
|
|
|
|
|
if (instructions[3].OpCode != OpCodes.Newarr)
|
|
|
|
|
return false;
|
|
|
|
|
if (instructions[3].Operand.ToString() != "System.UInt32")
|
|
|
|
|
return false;
|
|
|
|
|
if (instructions[4].OpCode != OpCodes.Dup)
|
|
|
|
|
return false;
|
|
|
|
|
if (instructions[5].OpCode != OpCodes.Ldtoken)
|
|
|
|
|
return false;
|
2017-08-20 21:25:25 +08:00
|
|
|
|
aField = instructions[5].Operand as FieldDef;
|
2017-08-08 18:27:21 +08:00
|
|
|
|
if (aField?.InitialValue == null)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
return false;
|
|
|
|
|
if (aField.Attributes != (FieldAttributes.Assembly | FieldAttributes.Static | FieldAttributes.HasFieldRVA))
|
|
|
|
|
return false;
|
|
|
|
|
if (instructions[6].OpCode != OpCodes.Call)
|
|
|
|
|
return false;
|
2017-08-08 18:27:21 +08:00
|
|
|
|
if (instructions[6].Operand.ToString() !=
|
|
|
|
|
"System.Void System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(System.Array,System.RuntimeFieldHandle)"
|
|
|
|
|
)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
return false;
|
2017-08-08 18:27:21 +08:00
|
|
|
|
if (!instructions[7].IsStloc()) // uint[] array = new uint[] {.....};
|
2017-02-13 17:14:22 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var l = instructions.Count;
|
|
|
|
|
if (!instructions[l - 4].IsLdloc())
|
|
|
|
|
return false;
|
|
|
|
|
if (instructions[l - 3].OpCode != OpCodes.Call)
|
|
|
|
|
return false;
|
2017-08-08 18:27:21 +08:00
|
|
|
|
if (instructions[l - 3].Operand != _lzmaMethod)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
return false;
|
2017-08-08 18:27:21 +08:00
|
|
|
|
if (instructions[l - 2].OpCode != OpCodes.Stsfld) //<Module>.byte_0 = <Module>.smethod_0(array4);
|
2017-02-13 17:14:22 +08:00
|
|
|
|
return false;
|
2017-08-20 21:25:25 +08:00
|
|
|
|
dField = instructions[l - 2].Operand as FieldDef;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
if (dField == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return true;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-20 21:25:25 +08:00
|
|
|
|
private byte[] DecryptArray(MethodDef method, byte[] encryptedArray)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
{
|
2017-08-20 21:25:25 +08:00
|
|
|
|
ModuleDefUser tempModule = new ModuleDefUser("TempModule");
|
|
|
|
|
|
|
|
|
|
AssemblyDef tempAssembly = new AssemblyDefUser("TempAssembly");
|
|
|
|
|
tempAssembly.Modules.Add(tempModule);
|
|
|
|
|
|
|
|
|
|
var tempType = new TypeDefUser("", "TempType", tempModule.CorLibTypes.Object.TypeDefOrRef);
|
|
|
|
|
tempType.Attributes = TypeAttributes.Public | TypeAttributes.Class;
|
|
|
|
|
MethodDef tempMethod = Utils.Clone(method);
|
|
|
|
|
|
|
|
|
|
tempMethod.ReturnType = new SZArraySig(tempModule.CorLibTypes.Byte);
|
|
|
|
|
tempMethod.MethodSig.Params.Add(new SZArraySig(tempModule.CorLibTypes.Byte));
|
|
|
|
|
tempMethod.Attributes = MethodAttributes.Public | MethodAttributes.Static;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 5; i++)
|
|
|
|
|
tempMethod.Body.Instructions.RemoveAt(2); // read encrypted array from argument
|
|
|
|
|
tempMethod.Body.Instructions.Insert(2, OpCodes.Ldarg_0.ToInstruction());
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
|
tempMethod.Body.Instructions.RemoveAt(tempMethod.Body.Instructions.Count -
|
|
|
|
|
2); // make return decrypted array
|
|
|
|
|
|
|
|
|
|
tempType.Methods.Add(tempMethod);
|
|
|
|
|
tempModule.Types.Add(tempType);
|
|
|
|
|
|
|
|
|
|
using (MemoryStream memoryStream = new MemoryStream())
|
2017-02-13 17:14:22 +08:00
|
|
|
|
{
|
2017-08-20 21:25:25 +08:00
|
|
|
|
ModuleWriterOptions moduleWriterOptions = new ModuleWriterOptions();
|
|
|
|
|
moduleWriterOptions.MetaDataOptions = new MetaDataOptions();
|
|
|
|
|
|
|
|
|
|
tempModule.Write(memoryStream, moduleWriterOptions);
|
|
|
|
|
|
|
|
|
|
Assembly patchedAssembly = Assembly.Load(memoryStream.ToArray());
|
|
|
|
|
var type = patchedAssembly.ManifestModule.GetType("TempType");
|
|
|
|
|
var methods = type.GetMethods();
|
|
|
|
|
MethodInfo patchedMethod = methods.First(m => m.IsPublic && m.IsStatic);
|
|
|
|
|
byte[] decryptedBytes = (byte[]) patchedMethod.Invoke(null, new object[]{encryptedArray});
|
|
|
|
|
return Lzma.Decompress(decryptedBytes);
|
2017-02-13 17:14:22 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-20 21:25:25 +08:00
|
|
|
|
private IEnumerable<ConstantDecrypterBase> FindStringDecrypters(TypeDef type)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
{
|
|
|
|
|
foreach (var method in type.Methods)
|
|
|
|
|
{
|
|
|
|
|
if (!method.HasBody)
|
|
|
|
|
continue;
|
2017-08-08 18:27:21 +08:00
|
|
|
|
if (!method.Signature.ContainsGenericParameter)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
continue;
|
|
|
|
|
var sig = method.MethodSig;
|
2017-08-08 18:27:21 +08:00
|
|
|
|
if (sig?.Params.Count != 1)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (sig.Params[0].GetElementType() != ElementType.U4)
|
|
|
|
|
continue;
|
|
|
|
|
if (!(sig.RetType.RemovePinnedAndModifiers() is GenericMVar))
|
|
|
|
|
continue;
|
|
|
|
|
if (sig.GenParamCount != 1)
|
|
|
|
|
continue;
|
2017-08-20 21:25:25 +08:00
|
|
|
|
|
2017-08-08 18:27:21 +08:00
|
|
|
|
_deobfuscator.Deobfuscate(method, SimpleDeobfuscatorFlags.Force);
|
2017-08-20 21:25:25 +08:00
|
|
|
|
|
|
|
|
|
if (IsStringDecrypter(method, out MethodDef nativeMethod))
|
|
|
|
|
{
|
|
|
|
|
yield return new ConstantDecrypterBase
|
|
|
|
|
{
|
|
|
|
|
Decrypted = _decryptedBytes,
|
|
|
|
|
Method = method,
|
|
|
|
|
NativeMethod = nativeMethod
|
|
|
|
|
};
|
|
|
|
|
}
|
2017-02-13 17:14:22 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-20 21:25:25 +08:00
|
|
|
|
private bool IsStringDecrypter(MethodDef method, out MethodDef nativeMethod)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
{
|
2017-08-20 21:25:25 +08:00
|
|
|
|
nativeMethod = null;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
var instr = method.Body.Instructions;
|
|
|
|
|
if (instr.Count < 25)
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
2017-08-08 18:27:21 +08:00
|
|
|
|
var i = 0;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
|
|
|
|
if (!instr[i++].IsLdarg())
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
|
|
|
|
if (instr[i].OpCode != OpCodes.Call)
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
2017-08-20 21:25:25 +08:00
|
|
|
|
nativeMethod = instr[i++].Operand as MethodDef;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
|
|
|
|
if (nativeMethod == null || !nativeMethod.IsStatic || !nativeMethod.IsNative)
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(nativeMethod, "System.Int32", "(System.Int32)"))
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
2017-08-08 18:27:21 +08:00
|
|
|
|
if (!instr[i++].IsStarg()) //uint_0 = (uint_0 * 2857448701u ^ 1196001109u);
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
|
|
|
|
if (!instr[i++].IsLdarg())
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
if (!instr[i].IsLdcI4() || instr[i++].GetLdcI4Value() != 0x1E)
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
if (instr[i++].OpCode != OpCodes.Shr_Un)
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
if (!instr[i++].IsStloc()) //uint num = uint_0 >> 30;
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
i++;
|
|
|
|
|
//TODO: Implement
|
|
|
|
|
//if (!instr[10].IsLdloca())
|
|
|
|
|
// return;
|
|
|
|
|
if (instr[i++].OpCode != OpCodes.Initobj)
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
if (!instr[i++].IsLdarg())
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
if (!instr[i].IsLdcI4() || instr[i++].GetLdcI4Value() != 0x3FFFFFFF)
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
if (instr[i++].OpCode != OpCodes.And)
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-08-08 18:27:21 +08:00
|
|
|
|
if (!instr[i++].IsStarg()) //uint_0 &= 1073741823u;
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
|
|
|
|
if (!instr[i++].IsLdarg())
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
if (!instr[i].IsLdcI4() || instr[i++].GetLdcI4Value() != 2)
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
if (instr[i++].OpCode != OpCodes.Shl)
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-08-08 18:27:21 +08:00
|
|
|
|
if (!instr[i++].IsStarg()) //uint_0 <<= 2;
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
|
2017-08-08 18:27:21 +08:00
|
|
|
|
foreach (var mtd in _strDecryptCalledMethods)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
if (!DotNetUtils.CallsMethod(method, mtd))
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return false;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
//TODO: Implement
|
|
|
|
|
//if (!DotNetUtils.LoadsField(method, decryptedField))
|
|
|
|
|
// return;
|
2017-08-20 21:25:25 +08:00
|
|
|
|
return true;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-08 18:27:21 +08:00
|
|
|
|
private static bool VerifyGenericArg(MethodSpec gim, ElementType etype)
|
2017-02-13 17:14:22 +08:00
|
|
|
|
{
|
2017-08-08 18:27:21 +08:00
|
|
|
|
var gims = gim?.GenericInstMethodSig;
|
2017-02-13 17:14:22 +08:00
|
|
|
|
if (gims == null || gims.GenericArguments.Count != 1)
|
|
|
|
|
return false;
|
|
|
|
|
return gims.GenericArguments[0].GetElementType() == etype;
|
|
|
|
|
}
|
2017-08-08 18:27:21 +08:00
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
public string DecryptString(ConstantDecrypterBase info, MethodSpec gim, uint magic1)
|
|
|
|
|
{
|
|
|
|
|
if (!VerifyGenericArg(gim, ElementType.String))
|
|
|
|
|
return null;
|
|
|
|
|
return info.DecryptString(magic1);
|
|
|
|
|
}
|
2017-08-08 18:27:21 +08:00
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
public object DecryptSByte(ConstantDecrypterBase info, MethodSpec gim, uint magic1)
|
|
|
|
|
{
|
|
|
|
|
if (!VerifyGenericArg(gim, ElementType.I1))
|
|
|
|
|
return null;
|
|
|
|
|
return info.DecryptConstant<sbyte>(magic1);
|
|
|
|
|
}
|
2017-08-08 18:27:21 +08:00
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
public object DecryptByte(ConstantDecrypterBase info, MethodSpec gim, uint magic1)
|
|
|
|
|
{
|
|
|
|
|
if (!VerifyGenericArg(gim, ElementType.U1))
|
|
|
|
|
return null;
|
|
|
|
|
return info.DecryptConstant<byte>(magic1);
|
|
|
|
|
}
|
2017-08-08 18:27:21 +08:00
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
public object DecryptInt16(ConstantDecrypterBase info, MethodSpec gim, uint magic1)
|
|
|
|
|
{
|
|
|
|
|
if (!VerifyGenericArg(gim, ElementType.I2))
|
|
|
|
|
return null;
|
|
|
|
|
return info.DecryptConstant<short>(magic1);
|
|
|
|
|
}
|
2017-08-08 18:27:21 +08:00
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
public object DecryptUInt16(ConstantDecrypterBase info, MethodSpec gim, uint magic1)
|
|
|
|
|
{
|
|
|
|
|
if (!VerifyGenericArg(gim, ElementType.U2))
|
|
|
|
|
return null;
|
|
|
|
|
return info.DecryptConstant<ushort>(magic1);
|
|
|
|
|
}
|
2017-08-08 18:27:21 +08:00
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
public object DecryptInt32(ConstantDecrypterBase info, MethodSpec gim, uint magic1)
|
|
|
|
|
{
|
|
|
|
|
if (!VerifyGenericArg(gim, ElementType.I4))
|
|
|
|
|
return null;
|
|
|
|
|
return info.DecryptConstant<int>(magic1);
|
|
|
|
|
}
|
2017-08-08 18:27:21 +08:00
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
public object DecryptUInt32(ConstantDecrypterBase info, MethodSpec gim, uint magic1)
|
|
|
|
|
{
|
|
|
|
|
if (!VerifyGenericArg(gim, ElementType.U4))
|
|
|
|
|
return null;
|
|
|
|
|
return info.DecryptConstant<uint>(magic1);
|
|
|
|
|
}
|
2017-08-08 18:27:21 +08:00
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
public object DecryptInt64(ConstantDecrypterBase info, MethodSpec gim, uint magic1)
|
|
|
|
|
{
|
|
|
|
|
if (!VerifyGenericArg(gim, ElementType.I8))
|
|
|
|
|
return null;
|
|
|
|
|
return info.DecryptConstant<long>(magic1);
|
|
|
|
|
}
|
2017-08-08 18:27:21 +08:00
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
public object DecryptUInt64(ConstantDecrypterBase info, MethodSpec gim, uint magic1)
|
|
|
|
|
{
|
|
|
|
|
if (!VerifyGenericArg(gim, ElementType.U8))
|
|
|
|
|
return null;
|
|
|
|
|
return info.DecryptConstant<ulong>(magic1);
|
|
|
|
|
}
|
2017-08-08 18:27:21 +08:00
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
public object DecryptSingle(ConstantDecrypterBase info, MethodSpec gim, uint magic1)
|
|
|
|
|
{
|
|
|
|
|
if (!VerifyGenericArg(gim, ElementType.R4))
|
|
|
|
|
return null;
|
|
|
|
|
return info.DecryptConstant<float>(magic1);
|
|
|
|
|
}
|
2017-08-08 18:27:21 +08:00
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
public object DecryptDouble(ConstantDecrypterBase info, MethodSpec gim, uint magic1)
|
|
|
|
|
{
|
|
|
|
|
if (!VerifyGenericArg(gim, ElementType.R8))
|
|
|
|
|
return null;
|
|
|
|
|
return info.DecryptConstant<double>(magic1);
|
|
|
|
|
}
|
2017-08-08 18:27:21 +08:00
|
|
|
|
|
2017-02-13 17:14:22 +08:00
|
|
|
|
public object DecryptArray(ConstantDecrypterBase info, MethodSpec gim, uint magic1)
|
|
|
|
|
{
|
|
|
|
|
if (!VerifyGenericArg(gim, ElementType.SZArray))
|
|
|
|
|
return null;
|
|
|
|
|
return info.DecryptArray(magic1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|