2012-01-08 03:27:07 +08:00
|
|
|
|
/*
|
2012-01-10 06:04:52 +08:00
|
|
|
|
Copyright (C) 2011-2012 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-11-02 22:57:11 +08:00
|
|
|
|
using dot10.DotNet;
|
|
|
|
|
using dot10.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
|
|
|
|
}
|
|
|
|
|
|
2012-06-12 16:37:51 +08:00
|
|
|
|
public ResourceDecrypter create() {
|
|
|
|
|
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 {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] decrypt(byte[] encryptedData) {
|
|
|
|
|
byte[] key, iv;
|
|
|
|
|
var reader = new BinaryReader(new MemoryStream(encryptedData));
|
|
|
|
|
bool isCompressed = getHeaderData(reader, out key, out iv);
|
|
|
|
|
var data = DeobUtils.desDecrypt(encryptedData,
|
|
|
|
|
(int)reader.BaseStream.Position,
|
|
|
|
|
(int)(reader.BaseStream.Length - reader.BaseStream.Position),
|
|
|
|
|
key, iv);
|
|
|
|
|
if (isCompressed)
|
|
|
|
|
data = DeobUtils.inflate(data, true);
|
|
|
|
|
return data;
|
2012-01-14 17:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-06-12 16:37:51 +08:00
|
|
|
|
bool getHeaderData(BinaryReader reader, out byte[] key, out byte[] iv) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] decrypt(byte[] encryptedData) {
|
|
|
|
|
int index = 0;
|
|
|
|
|
byte[] key, iv;
|
|
|
|
|
bool isCompressed = getKeyIv(getHeaderData(encryptedData, ref index), out key, out iv);
|
|
|
|
|
var data = DeobUtils.desDecrypt(encryptedData, index, encryptedData.Length - index, key, iv);
|
|
|
|
|
if (isCompressed)
|
|
|
|
|
data = DeobUtils.inflate(data, true);
|
|
|
|
|
return data;
|
|
|
|
|
}
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
2012-06-12 16:37:51 +08:00
|
|
|
|
byte[] getHeaderData(byte[] encryptedData, ref int index) {
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool getKeyIv(byte[] headerData, out byte[] key, out byte[] iv) {
|
|
|
|
|
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;
|
|
|
|
|
this.inflater = InflaterCreator.create(decryptMethod, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] decrypt(byte[] encryptedData) {
|
|
|
|
|
int index = 0;
|
|
|
|
|
byte[] key, iv;
|
|
|
|
|
bool isCompressed = getKeyIv(getHeaderData(encryptedData, ref index), out key, out iv);
|
|
|
|
|
var data = DeobUtils.desDecrypt(encryptedData, index, encryptedData.Length - index, key, iv);
|
|
|
|
|
if (isCompressed)
|
|
|
|
|
data = DeobUtils.inflate(data, inflater);
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] getHeaderData(byte[] encryptedData, ref int index) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool getKeyIv(byte[] headerData, out byte[] key, out byte[] iv) {
|
|
|
|
|
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;
|
|
|
|
|
simpleDeobfuscator.deobfuscate(decryptMethod);
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +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;
|
|
|
|
|
if (!DotNetUtils.isMethod(calledMethod, "System.IO.MemoryStream", "(System.IO.Stream)"))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return calledMethod;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
2012-06-12 16:37:51 +08:00
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] decrypt(byte[] encryptedData) {
|
|
|
|
|
if (decrypter == null)
|
|
|
|
|
decrypter = createDecrypter(encryptedData);
|
|
|
|
|
return decrypter.decrypt(encryptedData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IDecrypter createDecrypter(byte[] encryptedData) {
|
|
|
|
|
if (decryptMethod != null && DeobUtils.hasInteger(decryptMethod, 6))
|
|
|
|
|
return new Decrypter3(module, decryptMethod);
|
|
|
|
|
if (isV30(encryptedData))
|
|
|
|
|
return new Decrypter1(module);
|
|
|
|
|
return new Decrypter2(module);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool isV30(byte[] data) {
|
|
|
|
|
return data.Length > 10 && data[0] == 8 && data[9] <= 1 && data[10] == 8;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|