de4dot-cex/de4dot.code/deobfuscators/CryptoObfuscator/ResourceDecrypter.cs

410 lines
12 KiB
C#
Raw Normal View History

/*
2014-03-12 05:15:43 +08:00
Copyright (C) 2011-2014 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;
2011-12-21 13:39:56 +08:00
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2011-12-15 23:16:21 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.CryptoObfuscator {
class ResourceDecrypter {
const int BUFLEN = 0x8000;
2012-11-09 05:24:13 +08:00
ModuleDefMD module;
TypeDef resourceDecrypterType;
byte[] buffer1 = new byte[BUFLEN];
byte[] buffer2 = new byte[BUFLEN];
2011-12-15 23:16:21 +08:00
byte desEncryptedFlag;
byte deflatedFlag;
byte bitwiseNotEncryptedFlag;
2012-03-17 02:13:27 +08:00
FrameworkType frameworkType;
2012-03-31 19:26:11 +08:00
bool flipFlagsBits;
2012-04-16 05:42:11 +08:00
int skipBytes;
2011-12-15 23:16:21 +08:00
2012-11-09 05:24:13 +08:00
public ResourceDecrypter(ModuleDefMD module, ISimpleDeobfuscator simpleDeobfuscator) {
this.module = module;
2013-01-19 20:03:57 +08:00
frameworkType = DotNetUtils.GetFrameworkType(module);
Find(simpleDeobfuscator);
2011-12-15 23:16:21 +08:00
}
2013-01-19 20:03:57 +08:00
void Find(ISimpleDeobfuscator simpleDeobfuscator) {
2012-03-17 02:13:27 +08:00
switch (frameworkType) {
case FrameworkType.Silverlight:
2013-01-19 20:03:57 +08:00
FindSilverlight();
2012-03-16 05:36:23 +08:00
break;
2013-11-06 15:12:40 +08:00
case FrameworkType.Desktop:
2012-03-17 02:13:27 +08:00
case FrameworkType.CompactFramework:
2012-11-09 05:24:13 +08:00
if (!module.IsClr1x) {
2013-01-19 20:03:57 +08:00
if (FindDesktopOrCompactFramework())
2012-03-16 05:36:23 +08:00
break;
}
2013-01-19 20:03:57 +08:00
FindDesktopOrCompactFrameworkV1();
2012-03-16 05:36:23 +08:00
break;
}
2013-01-19 20:03:57 +08:00
InitializeHeaderInfo(simpleDeobfuscator);
2012-03-16 05:36:23 +08:00
}
2011-12-15 23:16:21 +08:00
2012-03-16 05:36:23 +08:00
static string[] requiredTypes = new string[] {
"System.IO.MemoryStream",
"System.Object",
"System.Int32",
};
2013-01-19 20:03:57 +08:00
bool FindDesktopOrCompactFramework() {
2011-12-15 23:16:21 +08:00
resourceDecrypterType = null;
foreach (var type in module.Types) {
if (type.Fields.Count < 5)
2011-12-15 23:16:21 +08:00
continue;
2013-01-19 20:03:57 +08:00
if (!new FieldTypes(type).All(requiredTypes))
2011-12-15 23:16:21 +08:00
continue;
2012-11-17 06:50:52 +08:00
var cctor = type.FindStaticConstructor();
2011-12-15 23:16:21 +08:00
if (cctor == null)
continue;
2013-01-19 20:03:57 +08:00
if (!CheckCctor(cctor))
2011-12-15 23:16:21 +08:00
continue;
resourceDecrypterType = type;
2012-03-16 05:36:23 +08:00
return true;
2011-12-15 23:16:21 +08:00
}
2012-03-16 05:36:23 +08:00
return false;
2011-12-15 23:16:21 +08:00
}
2013-01-19 20:03:57 +08:00
bool CheckCctor(MethodDef cctor) {
2011-12-15 23:16:21 +08:00
if (cctor.Body == null)
return false;
int stsfldCount = 0;
foreach (var instr in cctor.Body.Instructions) {
if (instr.OpCode.Code == Code.Stsfld) {
2012-11-09 05:24:13 +08:00
var field = instr.Operand as IField;
if (!new SigComparer().Equals(cctor.DeclaringType, field.DeclaringType))
2011-12-15 23:16:21 +08:00
return false;
stsfldCount++;
}
}
2012-03-15 16:15:12 +08:00
return stsfldCount >= cctor.DeclaringType.Fields.Count;
2011-12-15 23:16:21 +08:00
}
2012-03-16 05:36:23 +08:00
static string[] requiredLocals_v1 = new string[] {
"System.Boolean",
"System.Byte",
"System.Byte[]",
"System.Int32",
"System.Security.Cryptography.DESCryptoServiceProvider",
};
2013-01-19 20:03:57 +08:00
bool FindDesktopOrCompactFrameworkV1() {
2012-03-16 05:36:23 +08:00
resourceDecrypterType = null;
foreach (var type in module.Types) {
if (type.Fields.Count != 0)
continue;
2013-01-19 20:03:57 +08:00
foreach (var method in GetDecrypterMethods(type)) {
if (method == null)
continue;
2013-01-19 20:03:57 +08:00
if (!new LocalTypes(method).Exactly(requiredLocals_v1))
continue;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.CallsMethod(method, "System.Int64", "()"))
continue;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.CallsMethod(method, "System.Int32", "(System.Byte[],System.Int32,System.Int32)"))
continue;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.CallsMethod(method, "System.Void", "(System.Array,System.Int32,System.Array,System.Int32,System.Int32)"))
continue;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.CallsMethod(method, "System.Security.Cryptography.ICryptoTransform", "()"))
continue;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.CallsMethod(method, "System.Byte[]", "(System.Byte[],System.Int32,System.Int32)"))
continue;
resourceDecrypterType = type;
return true;
}
2012-03-16 05:36:23 +08:00
}
return false;
}
static string[] requiredLocals_sl = new string[] {
"System.Byte",
"System.Byte[]",
"System.Int32",
};
2013-01-19 20:03:57 +08:00
void FindSilverlight() {
2012-03-16 05:36:23 +08:00
foreach (var type in module.Types) {
if (type.Fields.Count > 0)
continue;
if (type.HasNestedTypes || type.HasGenericParameters)
continue;
2013-01-19 20:03:57 +08:00
foreach (var method in GetDecrypterMethods(type)) {
if (method == null)
continue;
2013-01-19 20:03:57 +08:00
if (!new LocalTypes(method).Exactly(requiredLocals_sl))
continue;
resourceDecrypterType = type;
return;
}
2012-03-16 05:36:23 +08:00
}
}
2013-01-19 20:03:57 +08:00
void InitializeHeaderInfo(ISimpleDeobfuscator simpleDeobfuscator) {
2012-04-16 05:42:11 +08:00
skipBytes = 0;
2013-01-19 20:03:57 +08:00
foreach (var method in GetDecrypterMethods(resourceDecrypterType)) {
if (UpdateFlags(method, simpleDeobfuscator))
2011-12-21 13:39:56 +08:00
return;
2011-12-15 23:16:21 +08:00
}
2011-12-21 13:39:56 +08:00
desEncryptedFlag = 1;
deflatedFlag = 2;
bitwiseNotEncryptedFlag = 4;
}
2011-12-15 23:16:21 +08:00
2013-04-30 18:00:03 +08:00
static bool CheckFlipBits(MethodDef method) {
2012-03-31 19:26:11 +08:00
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 1; i++) {
var ldloc = instrs[i];
2012-11-09 05:24:13 +08:00
if (!ldloc.IsLdloc())
2012-03-31 19:26:11 +08:00
continue;
var local = ldloc.GetLocal(method.Body.Variables);
2012-11-09 05:24:13 +08:00
if (local == null || local.Type.GetElementType().GetPrimitiveSize() < 0)
2012-03-31 19:26:11 +08:00
continue;
var not = instrs[i + 1];
if (not.OpCode.Code != Code.Not)
continue;
return true;
}
return false;
}
2013-01-19 20:03:57 +08:00
bool UpdateFlags(MethodDef method, ISimpleDeobfuscator simpleDeobfuscator) {
if (method == null || method.Body == null || method.Body.Variables.Count < 3)
2011-12-21 13:39:56 +08:00
return false;
var constants = new List<int>();
2013-01-19 20:03:57 +08:00
simpleDeobfuscator.Deobfuscate(method);
2011-12-21 13:39:56 +08:00
var instructions = method.Body.Instructions;
for (int i = 2; i < instructions.Count; i++) {
var and = instructions[i];
if (and.OpCode.Code != Code.And)
continue;
var ldci4 = instructions[i - 1];
2012-11-09 05:24:13 +08:00
if (!ldci4.IsLdcI4())
2011-12-21 13:39:56 +08:00
continue;
2012-11-09 05:24:13 +08:00
int flagValue = ldci4.GetLdcI4Value();
2013-01-19 20:03:57 +08:00
if (!IsFlag(flagValue))
continue;
2011-12-21 13:39:56 +08:00
var ldloc = instructions[i - 2];
2012-11-09 05:24:13 +08:00
if (!ldloc.IsLdloc())
2011-12-21 13:39:56 +08:00
continue;
var local = ldloc.GetLocal(method.Body.Variables);
2012-11-09 05:24:13 +08:00
if (local.Type.GetElementType().GetPrimitiveSize() < 0)
2011-12-21 13:39:56 +08:00
continue;
constants.Add(flagValue);
2011-12-21 13:39:56 +08:00
}
2013-04-30 18:00:03 +08:00
flipFlagsBits = CheckFlipBits(method);
2013-01-19 20:03:57 +08:00
skipBytes = GetHeaderSkipBytes(method);
2012-03-31 19:26:11 +08:00
2012-03-17 02:13:27 +08:00
switch (frameworkType) {
case FrameworkType.Desktop:
2012-11-09 05:24:13 +08:00
if (!module.IsClr1x) {
2012-03-16 05:36:23 +08:00
if (constants.Count == 2) {
desEncryptedFlag = (byte)constants[0];
deflatedFlag = (byte)constants[1];
return true;
}
}
2013-11-06 15:12:40 +08:00
if (constants.Count == 1) {
desEncryptedFlag = (byte)constants[0];
return true;
2012-03-16 05:36:23 +08:00
}
break;
2012-03-17 02:13:27 +08:00
case FrameworkType.Silverlight:
2012-03-16 05:36:23 +08:00
if (constants.Count == 1) {
bitwiseNotEncryptedFlag = (byte)constants[0];
return true;
}
break;
2012-03-17 02:13:27 +08:00
case FrameworkType.CompactFramework:
2012-03-16 05:36:23 +08:00
if (constants.Count == 1) {
desEncryptedFlag = (byte)constants[0];
return true;
}
break;
2011-12-21 13:39:56 +08:00
}
return false;
}
2013-01-19 20:03:57 +08:00
static int GetHeaderSkipBytes(MethodDef method) {
2012-04-16 05:42:11 +08:00
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 1; i++) {
var ldci4 = instrs[i];
2012-11-09 05:24:13 +08:00
if (!ldci4.IsLdcI4())
2012-04-16 05:42:11 +08:00
continue;
2012-11-09 05:24:13 +08:00
int loopCount = ldci4.GetLdcI4Value();
2013-12-04 22:37:02 +08:00
if (loopCount < 2 || loopCount > 4)
2012-04-16 05:42:11 +08:00
continue;
var blt = instrs[i + 1];
if (blt.OpCode.Code != Code.Blt && blt.OpCode.Code != Code.Blt_S && blt.OpCode.Code != Code.Clt)
2012-04-16 05:42:11 +08:00
continue;
2012-08-23 00:33:27 +08:00
return loopCount - 1;
2012-04-16 05:42:11 +08:00
}
return 0;
}
2013-01-19 20:03:57 +08:00
static bool IsFlag(int value) {
for (uint tmp = (uint)value; tmp != 0; tmp >>= 1) {
if ((tmp & 1) != 0)
return tmp == 1;
}
return false;
}
2013-01-19 20:03:57 +08:00
static IEnumerable<MethodDef> GetDecrypterMethods(TypeDef type) {
if (type == null)
yield break;
2012-03-16 05:36:23 +08:00
foreach (var method in type.Methods) {
2013-01-19 20:03:57 +08:00
if (DotNetUtils.IsMethod(method, "System.Byte[]", "(System.IO.Stream)"))
yield return method;
2013-01-19 20:03:57 +08:00
else if (DotNetUtils.IsMethod(method, "System.Byte[]", "(System.Int64,System.IO.Stream)"))
yield return method;
2013-11-06 15:12:40 +08:00
else if (DotNetUtils.IsMethod(method, "System.Byte[]", "(System.Int64,System.IO.Stream,System.UInt32)"))
yield return method;
2013-01-19 20:03:57 +08:00
else if (DotNetUtils.IsMethod(method, "System.Byte[]", "(System.Int32,System.IO.Stream)"))
yield return method;
2013-01-19 20:03:57 +08:00
else if (DotNetUtils.IsMethod(method, "System.Byte[]", "(System.Int16,System.IO.Stream)"))
yield return method;
2013-01-19 20:03:57 +08:00
else if (DotNetUtils.IsMethod(method, "System.Byte[]", "(System.Byte,System.IO.Stream)"))
yield return method;
2013-01-19 20:03:57 +08:00
else if (DotNetUtils.IsMethod(method, "System.Byte[]", "(System.SByte,System.IO.Stream)"))
yield return method;
2013-01-19 20:03:57 +08:00
else if (DotNetUtils.IsMethod(method, "System.Byte[]", "(System.Byte,System.IO.Stream,System.Int32)"))
yield return method;
2013-01-19 20:03:57 +08:00
else if (DotNetUtils.IsMethod(method, "System.Byte[]", "(System.SByte,System.IO.Stream,System.UInt32)"))
yield return method;
2013-01-19 20:03:57 +08:00
else if (DotNetUtils.IsMethod(method, "System.Byte[]", "(System.Char,System.IO.Stream)"))
2013-01-10 09:47:53 +08:00
yield return method;
2011-12-15 23:16:21 +08:00
}
}
2013-01-19 20:03:57 +08:00
public byte[] Decrypt(Stream resourceStream) {
byte flags = (byte)resourceStream.ReadByte();
2012-03-31 19:26:11 +08:00
if (flipFlagsBits)
flags = (byte)~flags;
Stream sourceStream = resourceStream;
2011-12-10 06:43:02 +08:00
int sourceStreamOffset = 1;
bool didSomething = false;
2012-04-16 05:42:11 +08:00
sourceStream.Position += skipBytes;
sourceStreamOffset += skipBytes;
2011-12-15 23:16:21 +08:00
byte allFlags = (byte)(desEncryptedFlag | deflatedFlag | bitwiseNotEncryptedFlag);
if ((flags & ~allFlags) != 0)
Logger.w("Found unknown resource encryption flags: 0x{0:X2}", flags);
2011-12-15 23:16:21 +08:00
if ((flags & desEncryptedFlag) != 0) {
var memStream = new MemoryStream((int)resourceStream.Length);
using (var provider = new DESCryptoServiceProvider()) {
var iv = new byte[8];
sourceStream.Read(iv, 0, 8);
provider.IV = iv;
2013-01-19 20:03:57 +08:00
provider.Key = GetKey(sourceStream);
using (var transform = provider.CreateDecryptor()) {
while (true) {
int count = sourceStream.Read(buffer1, 0, buffer1.Length);
if (count <= 0)
break;
int count2 = transform.TransformBlock(buffer1, 0, count, buffer2, 0);
memStream.Write(buffer2, 0, count2);
}
var finalData = transform.TransformFinalBlock(buffer1, 0, 0);
memStream.Write(finalData, 0, finalData.Length);
}
}
sourceStream = memStream;
2011-12-10 06:43:02 +08:00
sourceStreamOffset = 0;
didSomething = true;
}
2011-12-15 23:16:21 +08:00
if ((flags & deflatedFlag) != 0) {
var memStream = new MemoryStream((int)resourceStream.Length);
2011-12-10 06:43:02 +08:00
sourceStream.Position = sourceStreamOffset;
using (var inflater = new DeflateStream(sourceStream, CompressionMode.Decompress)) {
while (true) {
int count = inflater.Read(buffer1, 0, buffer1.Length);
if (count <= 0)
break;
memStream.Write(buffer1, 0, count);
}
}
sourceStream = memStream;
2011-12-10 06:43:02 +08:00
sourceStreamOffset = 0;
didSomething = true;
}
2011-12-15 23:16:21 +08:00
if ((flags & bitwiseNotEncryptedFlag) != 0) {
2011-10-23 15:19:50 +08:00
var memStream = new MemoryStream((int)resourceStream.Length);
2011-12-10 06:43:02 +08:00
sourceStream.Position = sourceStreamOffset;
for (int i = sourceStreamOffset; i < sourceStream.Length; i++)
2011-10-23 15:19:50 +08:00
memStream.WriteByte((byte)~sourceStream.ReadByte());
sourceStream = memStream;
2011-12-10 06:43:02 +08:00
sourceStreamOffset = 0;
didSomething = true;
2011-10-23 15:19:50 +08:00
}
if (didSomething && sourceStream is MemoryStream) {
var memStream = (MemoryStream)sourceStream;
return memStream.ToArray();
}
else {
int len = (int)(sourceStream.Length - sourceStream.Position);
byte[] data = new byte[len];
sourceStream.Read(data, 0, len);
return data;
}
}
2013-01-19 20:03:57 +08:00
byte[] GetKey(Stream resourceStream) {
byte[] key = new byte[8];
resourceStream.Read(key, 0, key.Length);
for (int i = 0; i < key.Length; i++) {
if (key[i] != 0)
return key;
}
2012-11-09 05:24:13 +08:00
key = PublicKeyBase.GetRawData(module.Assembly.PublicKeyToken);
if (key == null)
throw new ApplicationException("PublicKeyToken is null, can't decrypt resources");
return key;
}
}
}