2012-01-08 03:27:07 +08:00
|
|
|
|
/*
|
2015-10-30 05:45:26 +08:00
|
|
|
|
Copyright (C) 2011-2015 de4dot@gmail.com
|
2012-01-08 03:27:07 +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.IO;
|
2012-06-12 16:37:51 +08:00
|
|
|
|
using ICSharpCode.SharpZipLib.Zip.Compression;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2012-06-12 16:37:51 +08:00
|
|
|
|
using de4dot.blocks;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.Babel_NET {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
class ResourceDecrypterCreator {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-06-12 16:37:51 +08:00
|
|
|
|
ISimpleDeobfuscator simpleDeobfuscator;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
public ResourceDecrypterCreator(ModuleDefMD module, ISimpleDeobfuscator simpleDeobfuscator) {
|
2012-01-08 03:27:07 +08:00
|
|
|
|
this.module = module;
|
2012-06-12 16:37:51 +08:00
|
|
|
|
this.simpleDeobfuscator = simpleDeobfuscator;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public ResourceDecrypter Create() {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
return new ResourceDecrypter(module, simpleDeobfuscator);
|
2012-01-14 17:37:15 +08:00
|
|
|
|
}
|
2012-06-12 16:37:51 +08:00
|
|
|
|
}
|
2012-01-14 17:37:15 +08:00
|
|
|
|
|
2012-06-12 16:37:51 +08:00
|
|
|
|
class ResourceDecrypter {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-06-12 16:37:51 +08:00
|
|
|
|
ISimpleDeobfuscator simpleDeobfuscator;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
MethodDef decryptMethod;
|
2012-06-12 16:37:51 +08:00
|
|
|
|
IDecrypter decrypter;
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
public ResourceDecrypter(ModuleDefMD module, ISimpleDeobfuscator simpleDeobfuscator) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
this.simpleDeobfuscator = simpleDeobfuscator;
|
2012-01-14 17:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-06-12 16:37:51 +08:00
|
|
|
|
interface IDecrypter {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
byte[] Decrypt(byte[] encryptedData);
|
2012-01-14 17:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-06-12 16:37:51 +08:00
|
|
|
|
// v3.0
|
|
|
|
|
class Decrypter1 : IDecrypter {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-06-12 16:37:51 +08:00
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
public Decrypter1(ModuleDefMD module) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public byte[] Decrypt(byte[] encryptedData) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
byte[] key, iv;
|
|
|
|
|
var reader = new BinaryReader(new MemoryStream(encryptedData));
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool isCompressed = GetHeaderData(reader, out key, out iv);
|
|
|
|
|
var data = DeobUtils.DesDecrypt(encryptedData,
|
2012-06-12 16:37:51 +08:00
|
|
|
|
(int)reader.BaseStream.Position,
|
|
|
|
|
(int)(reader.BaseStream.Length - reader.BaseStream.Position),
|
|
|
|
|
key, iv);
|
|
|
|
|
if (isCompressed)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
data = DeobUtils.Inflate(data, true);
|
2012-06-12 16:37:51 +08:00
|
|
|
|
return data;
|
2012-01-14 17:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool GetHeaderData(BinaryReader reader, out byte[] key, out byte[] iv) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
iv = reader.ReadBytes(reader.ReadByte());
|
|
|
|
|
bool hasEmbeddedKey = reader.ReadBoolean();
|
|
|
|
|
if (hasEmbeddedKey)
|
|
|
|
|
key = reader.ReadBytes(reader.ReadByte());
|
|
|
|
|
else {
|
|
|
|
|
key = new byte[reader.ReadByte()];
|
2012-11-08 14:06:46 +08:00
|
|
|
|
Array.Copy(module.Assembly.PublicKey.Data, 0, key, 0, key.Length);
|
2012-06-12 16:37:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reader.ReadBytes(reader.ReadInt32()); // hash
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-01-14 17:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// v3.5+
|
2012-06-12 16:37:51 +08:00
|
|
|
|
class Decrypter2 : IDecrypter {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-06-12 16:37:51 +08:00
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
public Decrypter2(ModuleDefMD module) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public byte[] Decrypt(byte[] encryptedData) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
int index = 0;
|
|
|
|
|
byte[] key, iv;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool isCompressed = GetKeyIv(GetHeaderData(encryptedData, ref index), out key, out iv);
|
|
|
|
|
var data = DeobUtils.DesDecrypt(encryptedData, index, encryptedData.Length - index, key, iv);
|
2012-06-12 16:37:51 +08:00
|
|
|
|
if (isCompressed)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
data = DeobUtils.Inflate(data, true);
|
2012-06-12 16:37:51 +08:00
|
|
|
|
return data;
|
|
|
|
|
}
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
byte[] GetHeaderData(byte[] encryptedData, ref int index) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
bool xorDecrypt = encryptedData[index++] != 0;
|
|
|
|
|
var headerData = new byte[BitConverter.ToUInt16(encryptedData, index)];
|
|
|
|
|
Array.Copy(encryptedData, index + 2, headerData, 0, headerData.Length);
|
|
|
|
|
index += headerData.Length + 2;
|
|
|
|
|
if (!xorDecrypt)
|
|
|
|
|
return headerData;
|
|
|
|
|
|
|
|
|
|
var key = new byte[8];
|
|
|
|
|
Array.Copy(encryptedData, index, key, 0, key.Length);
|
|
|
|
|
index += key.Length;
|
|
|
|
|
for (int i = 0; i < headerData.Length; i++)
|
|
|
|
|
headerData[i] ^= key[i % key.Length];
|
2012-01-08 03:27:07 +08:00
|
|
|
|
return headerData;
|
2012-06-12 16:37:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool GetKeyIv(byte[] headerData, out byte[] key, out byte[] iv) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
var reader = new BinaryReader(new MemoryStream(headerData));
|
|
|
|
|
|
|
|
|
|
// 3.0 - 3.5 don't have this field
|
|
|
|
|
if (headerData[(int)reader.BaseStream.Position] != 8) {
|
|
|
|
|
var license = reader.ReadString();
|
|
|
|
|
}
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
2012-06-12 16:37:51 +08:00
|
|
|
|
// 4.2 (and earlier?) always compress the data
|
|
|
|
|
bool isCompressed = true;
|
|
|
|
|
if (headerData[(int)reader.BaseStream.Position] != 8)
|
|
|
|
|
isCompressed = reader.ReadBoolean();
|
|
|
|
|
|
|
|
|
|
iv = reader.ReadBytes(reader.ReadByte());
|
|
|
|
|
bool hasEmbeddedKey = reader.ReadBoolean();
|
|
|
|
|
if (hasEmbeddedKey)
|
|
|
|
|
key = reader.ReadBytes(reader.ReadByte());
|
|
|
|
|
else {
|
|
|
|
|
key = new byte[reader.ReadByte()];
|
2012-11-08 14:06:46 +08:00
|
|
|
|
Array.Copy(module.Assembly.PublicKey.Data, 12, key, 0, key.Length);
|
2012-06-12 16:37:51 +08:00
|
|
|
|
key[5] |= 0x80;
|
|
|
|
|
}
|
|
|
|
|
return isCompressed;
|
|
|
|
|
}
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-06-12 16:37:51 +08:00
|
|
|
|
// v5.0+ retail
|
|
|
|
|
class Decrypter3 : IDecrypter {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-06-12 16:37:51 +08:00
|
|
|
|
Inflater inflater;
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
public Decrypter3(ModuleDefMD module, MethodDef decryptMethod) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
this.module = module;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
this.inflater = InflaterCreator.Create(decryptMethod, true);
|
2012-06-12 16:37:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public byte[] Decrypt(byte[] encryptedData) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
int index = 0;
|
|
|
|
|
byte[] key, iv;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool isCompressed = GetKeyIv(GetHeaderData(encryptedData, ref index), out key, out iv);
|
|
|
|
|
var data = DeobUtils.DesDecrypt(encryptedData, index, encryptedData.Length - index, key, iv);
|
2012-06-12 16:37:51 +08:00
|
|
|
|
if (isCompressed)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
data = DeobUtils.Inflate(data, inflater);
|
2012-06-12 16:37:51 +08:00
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
byte[] GetHeaderData(byte[] encryptedData, ref int index) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
bool xorDecrypt = encryptedData[index++] != 0;
|
|
|
|
|
var headerData = new byte[BitConverter.ToUInt16(encryptedData, index)];
|
|
|
|
|
Array.Copy(encryptedData, index + 2, headerData, 0, headerData.Length);
|
|
|
|
|
index += headerData.Length + 2;
|
|
|
|
|
if (!xorDecrypt)
|
|
|
|
|
return headerData;
|
|
|
|
|
|
|
|
|
|
var key = new byte[6];
|
|
|
|
|
Array.Copy(encryptedData, index, key, 0, key.Length);
|
|
|
|
|
index += key.Length;
|
|
|
|
|
for (int i = 0; i < headerData.Length; i++)
|
|
|
|
|
headerData[i] ^= key[i % key.Length];
|
|
|
|
|
return headerData;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool GetKeyIv(byte[] headerData, out byte[] key, out byte[] iv) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
var reader = new BinaryReader(new MemoryStream(headerData));
|
2012-01-09 12:50:32 +08:00
|
|
|
|
|
|
|
|
|
var license = reader.ReadString();
|
2012-06-12 16:37:51 +08:00
|
|
|
|
bool isCompressed = reader.ReadBoolean();
|
|
|
|
|
|
|
|
|
|
var unkData = reader.ReadBytes(reader.ReadInt32());
|
|
|
|
|
|
|
|
|
|
bool hasEmbeddedKey = reader.ReadBoolean();
|
|
|
|
|
|
|
|
|
|
iv = reader.ReadBytes(reader.ReadByte());
|
|
|
|
|
if (hasEmbeddedKey)
|
|
|
|
|
key = reader.ReadBytes(reader.ReadByte());
|
|
|
|
|
else {
|
|
|
|
|
key = new byte[reader.ReadByte()];
|
2012-11-08 14:06:46 +08:00
|
|
|
|
Array.Copy(module.Assembly.PublicKey.Data, 12, key, 0, key.Length);
|
2012-06-12 16:37:51 +08:00
|
|
|
|
key[5] |= 0x80;
|
|
|
|
|
}
|
|
|
|
|
return isCompressed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef DecryptMethod {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
set {
|
|
|
|
|
if (value == null)
|
|
|
|
|
return;
|
|
|
|
|
if (decryptMethod == null) {
|
|
|
|
|
decryptMethod = value;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
simpleDeobfuscator.Deobfuscate(decryptMethod);
|
2012-06-12 16:37:51 +08:00
|
|
|
|
}
|
|
|
|
|
else if (decryptMethod != value)
|
|
|
|
|
throw new ApplicationException("Found another decrypter method");
|
2012-01-09 12:50:32 +08:00
|
|
|
|
}
|
2012-06-12 16:37:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public static MethodDef FindDecrypterMethod(MethodDef method) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
if (method == null || method.Body == null)
|
|
|
|
|
return null;
|
2012-01-09 04:48:37 +08:00
|
|
|
|
|
2012-06-12 16:37:51 +08:00
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
|
|
|
|
if (instr.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
var calledMethod = instr.Operand as MethodDef;
|
2012-06-12 16:37:51 +08:00
|
|
|
|
if (calledMethod == null || !calledMethod.IsStatic || calledMethod.Body == null)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(calledMethod, "System.IO.MemoryStream", "(System.IO.Stream)"))
|
2012-06-12 16:37:51 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return calledMethod;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
2012-06-12 16:37:51 +08:00
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public byte[] Decrypt(byte[] encryptedData) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
if (decrypter == null)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
decrypter = CreateDecrypter(encryptedData);
|
|
|
|
|
return decrypter.Decrypt(encryptedData);
|
2012-06-12 16:37:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
IDecrypter CreateDecrypter(byte[] encryptedData) {
|
|
|
|
|
if (decryptMethod != null && DeobUtils.HasInteger(decryptMethod, 6))
|
2012-06-12 16:37:51 +08:00
|
|
|
|
return new Decrypter3(module, decryptMethod);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (IsV30(encryptedData))
|
2012-06-12 16:37:51 +08:00
|
|
|
|
return new Decrypter1(module);
|
|
|
|
|
return new Decrypter2(module);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static bool IsV30(byte[] data) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
return data.Length > 10 && data[0] == 8 && data[9] <= 1 && data[10] == 8;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|