2012-07-30 02:02:12 +08:00
|
|
|
|
/*
|
2015-10-30 05:45:26 +08:00
|
|
|
|
Copyright (C) 2011-2015 de4dot@gmail.com
|
2012-07-30 02:02:12 +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.Security.Cryptography;
|
2012-12-23 04:08:29 +08:00
|
|
|
|
using dnlib.PE;
|
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2012-07-30 02:02:12 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.Confuser {
|
2012-08-09 17:47:18 +08:00
|
|
|
|
abstract class MethodsDecrypterBase : IVersionProvider {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
protected ModuleDefMD module;
|
2012-07-30 02:02:12 +08:00
|
|
|
|
protected ISimpleDeobfuscator simpleDeobfuscator;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
protected MethodDef initMethod;
|
|
|
|
|
protected MethodDef decryptMethod;
|
2012-07-30 02:02:12 +08:00
|
|
|
|
protected ulong lkey0;
|
|
|
|
|
protected uint key0, key1, key2, key3, key4, key5, key6;
|
|
|
|
|
protected byte[] methodsData;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public MethodDef InitMethod {
|
2012-07-30 02:02:12 +08:00
|
|
|
|
get { return initMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public TypeDef Type {
|
2012-07-30 02:02:12 +08:00
|
|
|
|
get { return initMethod != null ? initMethod.DeclaringType : null; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return initMethod != null; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
protected MethodsDecrypterBase(ModuleDefMD module, ISimpleDeobfuscator simpleDeobfuscator) {
|
2012-07-30 02:02:12 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
this.simpleDeobfuscator = simpleDeobfuscator;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
protected MethodsDecrypterBase(ModuleDefMD module, ISimpleDeobfuscator simpleDeobfuscator, MethodsDecrypterBase other) {
|
2012-07-30 02:02:12 +08:00
|
|
|
|
this.module = module;
|
2012-08-01 01:56:10 +08:00
|
|
|
|
this.simpleDeobfuscator = simpleDeobfuscator;
|
2012-07-30 02:02:12 +08:00
|
|
|
|
if (other != null)
|
2013-01-19 20:09:49 +08:00
|
|
|
|
this.initMethod = Lookup(other.initMethod, "Could not find initMethod");
|
2012-07-30 02:02:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
T Lookup<T>(T def, string errorMessage) where T : class, ICodedToken {
|
|
|
|
|
return DeobUtils.Lookup(module, def, errorMessage);
|
2012-07-30 02:02:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public abstract bool GetRevisionRange(out int minRev, out int maxRev);
|
2012-08-09 17:47:18 +08:00
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public void Find() {
|
|
|
|
|
Find(DotNetUtils.GetModuleTypeCctor(module));
|
2012-07-30 02:02:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
bool Find(MethodDef method) {
|
2012-07-30 02:02:12 +08:00
|
|
|
|
if (method == null || method.Body == null)
|
|
|
|
|
return false;
|
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
|
|
|
|
if (instr.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var calledMethod = instr.Operand as MethodDef;
|
2012-07-31 23:17:16 +08:00
|
|
|
|
try {
|
|
|
|
|
// If the body is encrypted, this could throw
|
|
|
|
|
if (calledMethod == null || calledMethod.Body == null)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
catch {
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
2012-07-31 23:17:16 +08:00
|
|
|
|
}
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(calledMethod, "System.Void", "()"))
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!CheckType(calledMethod.DeclaringType, calledMethod))
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
initMethod = calledMethod;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
protected abstract bool CheckType(TypeDef type, MethodDef initMethod);
|
2012-07-30 02:02:12 +08:00
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
protected static MethodDef FindDecryptMethod(TypeDef type) {
|
2012-07-30 02:02:12 +08:00
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (!method.IsStatic || method.Body == null)
|
|
|
|
|
continue;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(method, "System.Byte[]", "(System.Byte[],System.Byte[],System.Byte[])"))
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return method;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
protected static bool FindLKey0(MethodDef method, out ulong key) {
|
2012-07-30 02:02:12 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int index = 0; index < instrs.Count; index++) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
index = FindCallvirtReadUInt64(instrs, index);
|
2012-07-30 02:02:12 +08:00
|
|
|
|
if (index < 0)
|
|
|
|
|
break;
|
|
|
|
|
if (index + 1 >= instrs.Count)
|
|
|
|
|
continue;
|
|
|
|
|
var ldci8 = instrs[index + 1];
|
|
|
|
|
if (ldci8.OpCode.Code != Code.Ldc_I8)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
key = (ulong)(long)ldci8.Operand;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
protected static bool FindKey0_v16_r71742(MethodDef method, out uint key) {
|
2012-07-30 02:02:12 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i + 5 < instrs.Count; i++) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
i = FindCallvirtReadUInt32(instrs, i);
|
2012-07-30 02:02:12 +08:00
|
|
|
|
if (i < 0)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
int index = i + 1;
|
|
|
|
|
var ldci4_1 = instrs[index++];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4_1.IsLdcI4())
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[index++].OpCode.Code != Code.Xor)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[index++].IsStloc())
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[index++].IsLdloc())
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
|
|
|
|
var ldci4_2 = instrs[index++];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4_2.IsLdcI4())
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (ldci4_1.GetLdcI4Value() != ldci4_2.GetLdcI4Value())
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
key = (uint)ldci4_1.GetLdcI4Value();
|
2012-07-30 02:02:12 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
protected static bool FindKey0_v14_r58564(MethodDef method, out uint key) {
|
2012-07-31 23:17:16 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i + 5 < instrs.Count; i++) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
i = ConfuserUtils.FindCallMethod(instrs, i, Code.Callvirt, "System.Int32 System.IO.BinaryReader::ReadInt32()");
|
2012-07-31 23:17:16 +08:00
|
|
|
|
if (i < 0)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
int index = i + 1;
|
|
|
|
|
var ldci4_1 = instrs[index++];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4_1.IsLdcI4())
|
2012-07-31 23:17:16 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[index++].OpCode.Code != Code.Xor)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[index++].IsStloc())
|
2012-07-31 23:17:16 +08:00
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[index++].IsLdloc())
|
2012-07-31 23:17:16 +08:00
|
|
|
|
continue;
|
|
|
|
|
var ldci4_2 = instrs[index++];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4_2.IsLdcI4())
|
2012-07-31 23:17:16 +08:00
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (ldci4_2.GetLdcI4Value() != 0 && ldci4_1.GetLdcI4Value() != ldci4_2.GetLdcI4Value())
|
2012-07-31 23:17:16 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
key = (uint)ldci4_1.GetLdcI4Value();
|
2012-07-31 23:17:16 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
protected static bool FindKey1(MethodDef method, out uint key) {
|
2012-07-30 02:02:12 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int index = 0; index < instrs.Count; index++) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
index = FindCallvirtReadUInt32(instrs, index);
|
2012-07-30 02:02:12 +08:00
|
|
|
|
if (index < 0)
|
|
|
|
|
break;
|
|
|
|
|
if (index == 0)
|
|
|
|
|
continue;
|
|
|
|
|
int i = index - 1;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!CheckCallvirtReadUInt32(instrs, ref i))
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!CheckCallvirtReadUInt32(instrs, ref i))
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!CheckCallvirtReadUInt32(instrs, ref i))
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!CheckCallvirtReadUInt32(instrs, ref i))
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (i + 1 >= instrs.Count)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i].IsLdloc())
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
|
|
|
|
var ldci4 = instrs[i + 1];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
key = (uint)ldci4.GetLdcI4Value();
|
2012-07-30 02:02:12 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool CheckCallvirtReadUInt32(IList<Instruction> instrs, ref int index) {
|
2012-07-30 02:02:12 +08:00
|
|
|
|
if (index + 2 >= instrs.Count)
|
|
|
|
|
return false;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[index].IsLdloc())
|
2012-07-30 02:02:12 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!ConfuserUtils.IsCallMethod(instrs[index + 1], Code.Callvirt, "System.UInt32 System.IO.BinaryReader::ReadUInt32()"))
|
2012-07-30 02:02:12 +08:00
|
|
|
|
return false;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[index + 2].IsStloc() && instrs[index + 2].OpCode.Code != Code.Pop)
|
2012-07-30 02:02:12 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
index += 3;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
protected static bool FindKey2Key3(MethodDef method, out uint key2, out uint key3) {
|
2012-07-30 02:02:12 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
|
|
|
|
int index = i;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!FindKey2OrKey3(instrs, ref index, out key2))
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!FindKey2OrKey3(instrs, ref index, out key3))
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key2 = 0;
|
|
|
|
|
key3 = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool FindKey2OrKey3(IList<Instruction> instrs, ref int index, out uint key) {
|
2012-07-30 02:02:12 +08:00
|
|
|
|
key = 0;
|
|
|
|
|
if (index + 6 >= instrs.Count)
|
|
|
|
|
return false;
|
|
|
|
|
int i = index;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i++].IsLdloc())
|
2012-07-30 02:02:12 +08:00
|
|
|
|
return false;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i++].IsLdloc())
|
2012-07-30 02:02:12 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!ConfuserUtils.IsCallMethod(instrs[i++], Code.Callvirt, "System.Int32 System.IO.BinaryReader::ReadInt32()"))
|
2012-07-30 02:02:12 +08:00
|
|
|
|
return false;
|
|
|
|
|
var ldci4 = instrs[i++];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-07-30 02:02:12 +08:00
|
|
|
|
return false;
|
|
|
|
|
if (instrs[i++].OpCode.Code != Code.Xor)
|
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!ConfuserUtils.IsCallMethod(instrs[i++], Code.Callvirt, "System.Byte[] System.IO.BinaryReader::ReadBytes(System.Int32)"))
|
2012-07-30 02:02:12 +08:00
|
|
|
|
return false;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i++].IsStloc())
|
2012-07-30 02:02:12 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
key = (uint)ldci4.GetLdcI4Value();
|
2012-07-30 02:02:12 +08:00
|
|
|
|
index = i;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
protected static bool FindKey6(MethodDef method, out uint key) {
|
2012-07-30 02:02:12 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i + 4 < instrs.Count; i++) {
|
|
|
|
|
int index = i;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[index++].IsLdloc())
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[index++].OpCode.Code != Code.Sub)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[index++].OpCode.Code != Code.Ldelem_U1)
|
|
|
|
|
continue;
|
|
|
|
|
var ldci4 = instrs[index++];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-07-30 02:02:12 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[index++].OpCode.Code != Code.Xor)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[index++].OpCode.Code != Code.Conv_U1)
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
key = (uint)ldci4.GetLdcI4Value();
|
2012-07-30 02:02:12 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
protected static int FindCallvirtReadUInt32(IList<Instruction> instrs, int index) {
|
|
|
|
|
return ConfuserUtils.FindCallMethod(instrs, index, Code.Callvirt, "System.UInt32 System.IO.BinaryReader::ReadUInt32()");
|
2012-07-30 02:02:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static int FindCallvirtReadUInt64(IList<Instruction> instrs, int index) {
|
|
|
|
|
return ConfuserUtils.FindCallMethod(instrs, index, Code.Callvirt, "System.UInt64 System.IO.BinaryReader::ReadUInt64()");
|
2012-07-30 02:02:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
protected byte[] DecryptMethodsData_v17_r73404(MyPEImage peImage) {
|
|
|
|
|
return DecryptMethodsData_v16_r71742(peImage, GetEncryptedHeaderOffset_vXX(peImage.Sections));
|
2012-08-02 17:12:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
protected byte[] DecryptMethodsData_v16_r71742(MyPEImage peImage, uint encryptedHeaderOffset) {
|
2012-12-01 04:05:54 +08:00
|
|
|
|
uint mdRva = peImage.OptionalHeader.CheckSum ^ (uint)key0;
|
|
|
|
|
if ((RVA)mdRva != peImage.Cor20Header.MetaData.VirtualAddress)
|
2012-07-30 02:02:12 +08:00
|
|
|
|
throw new ApplicationException("Invalid metadata rva");
|
|
|
|
|
var reader = peImage.Reader;
|
2012-12-01 04:05:54 +08:00
|
|
|
|
reader.Position = encryptedHeaderOffset;
|
2012-07-30 02:02:12 +08:00
|
|
|
|
ulong checkSum = reader.ReadUInt64() ^ lkey0;
|
|
|
|
|
reader.ReadInt32(); // strong name RVA
|
|
|
|
|
reader.ReadInt32(); // strong name len
|
|
|
|
|
var iv = reader.ReadBytes(reader.ReadInt32() ^ (int)key2);
|
|
|
|
|
var encrypted = reader.ReadBytes(reader.ReadInt32() ^ (int)key3);
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var streamsBuffer = GetStreamsBuffer(peImage);
|
|
|
|
|
if (checkSum != CalcChecksum(streamsBuffer))
|
2012-07-30 02:02:12 +08:00
|
|
|
|
throw new ApplicationException("Invalid checksum. File has been modified.");
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var decrypted = Decrypt(encrypted, iv, streamsBuffer);
|
2012-07-30 02:02:12 +08:00
|
|
|
|
if (BitConverter.ToInt16(decrypted, 0) != 0x6FD6)
|
|
|
|
|
throw new ApplicationException("Invalid magic");
|
|
|
|
|
return decrypted;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
protected uint GetEncryptedHeaderOffset_v16_r71742(IList<ImageSectionHeader> sections) {
|
2012-08-02 17:12:20 +08:00
|
|
|
|
for (int i = sections.Count - 1; i >= 0; i--) {
|
|
|
|
|
var section = sections[i];
|
2012-12-01 04:05:54 +08:00
|
|
|
|
if (section.DisplayName == ".confuse")
|
|
|
|
|
return section.PointerToRawData;
|
2012-08-02 17:12:20 +08:00
|
|
|
|
}
|
|
|
|
|
throw new ApplicationException("Could not find encrypted section");
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
uint GetEncryptedHeaderOffset_vXX(IList<ImageSectionHeader> sections) {
|
2012-07-30 02:02:12 +08:00
|
|
|
|
for (int i = sections.Count - 1; i >= 0; i--) {
|
|
|
|
|
var section = sections[i];
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (GetSectionNameHash(section) == (uint)key1)
|
2012-12-01 04:05:54 +08:00
|
|
|
|
return section.PointerToRawData;
|
2012-07-30 02:02:12 +08:00
|
|
|
|
}
|
|
|
|
|
throw new ApplicationException("Could not find encrypted section");
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static byte[] GetStreamsBuffer(MyPEImage peImage) {
|
2012-07-30 02:02:12 +08:00
|
|
|
|
var memStream = new MemoryStream();
|
|
|
|
|
var writer = new BinaryWriter(memStream);
|
|
|
|
|
var reader = peImage.Reader;
|
2015-10-30 04:36:57 +08:00
|
|
|
|
foreach (var mdStream in peImage.MetaData.AllStreams) {
|
2012-12-01 04:05:54 +08:00
|
|
|
|
reader.Position = (long)mdStream.StartOffset;
|
|
|
|
|
writer.Write(reader.ReadBytes((int)(mdStream.EndOffset - mdStream.StartOffset)));
|
2012-07-30 02:02:12 +08:00
|
|
|
|
}
|
|
|
|
|
return memStream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
protected static ulong CalcChecksum(byte[] data) {
|
|
|
|
|
var sum = DeobUtils.Md5Sum(data);
|
2012-07-30 02:02:12 +08:00
|
|
|
|
return BitConverter.ToUInt64(sum, 0) ^ BitConverter.ToUInt64(sum, 8);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static uint GetSectionNameHash(ImageSectionHeader section) {
|
2012-07-30 02:02:12 +08:00
|
|
|
|
uint hash = 0;
|
2012-12-01 04:05:54 +08:00
|
|
|
|
foreach (var c in section.Name)
|
2012-07-30 02:02:12 +08:00
|
|
|
|
hash += c;
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
protected byte[] Decrypt(byte[] encrypted, byte[] iv, byte[] streamsBuffer) {
|
|
|
|
|
var decrypted = DeobUtils.AesDecrypt(encrypted, DeobUtils.Sha256Sum(streamsBuffer), iv);
|
2012-07-30 02:02:12 +08:00
|
|
|
|
var sha = SHA512.Create();
|
|
|
|
|
var hash = sha.ComputeHash(streamsBuffer);
|
|
|
|
|
for (int i = 0; i < decrypted.Length; i += 64) {
|
|
|
|
|
int j;
|
|
|
|
|
for (j = 0; j < 64 && i + j < decrypted.Length; j++)
|
|
|
|
|
decrypted[i + j] ^= (byte)(hash[j] ^ key6);
|
|
|
|
|
hash = sha.ComputeHash(decrypted, i, j);
|
|
|
|
|
}
|
|
|
|
|
return decrypted;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|