Support the latest MC versions

This commit is contained in:
de4dot 2012-07-20 14:49:47 +02:00
parent 1eaa9f8c51
commit 8b82f8b47d
4 changed files with 640 additions and 52 deletions

View File

@ -68,6 +68,8 @@
<Compile Include="deobfuscators\Babel_NET\MethodBodyReader.cs" />
<Compile Include="deobfuscators\Babel_NET\MethodReferenceReader.cs" />
<Compile Include="deobfuscators\DeepSea\ArrayBlockState.cs" />
<Compile Include="deobfuscators\MaxtoCode\CryptDecrypter.cs" />
<Compile Include="deobfuscators\MaxtoCode\Decrypter6.cs" />
<Compile Include="deobfuscators\MethodBodyReaderBase.cs" />
<Compile Include="deobfuscators\Babel_NET\MethodsDecrypter.cs" />
<Compile Include="deobfuscators\Babel_NET\ProxyDelegateFinder.cs" />

View File

@ -0,0 +1,375 @@
/*
Copyright (C) 2011-2012 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;
namespace de4dot.code.deobfuscators.MaxtoCode {
class CryptDecrypter {
byte[] key;
static readonly byte[] sbox = new byte[8 * 8 * 8] {
14, 4, 13, 1, 2, 15, 11, 8,
3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1,
10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11,
15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7,
5, 11, 3, 14, 10, 0, 6, 13,
15, 1, 8, 14, 6, 11, 3, 4,
9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14,
12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1,
5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2,
11, 6, 7, 12, 0, 5, 14, 9,
10, 0, 9, 14, 6, 3, 15, 5,
1, 13, 12, 7, 11, 4, 2, 8,
13, 7, 0, 9, 3, 4, 6, 10,
2, 8, 5, 14, 12, 11, 15, 1,
13, 6, 4, 9, 8, 15, 3, 0,
11, 1, 2, 12, 5, 10, 14, 7,
1, 10, 13, 0, 6, 9, 8, 7,
4, 15, 14, 3, 11, 5, 2, 12,
7, 13, 14, 3, 0, 6, 9, 10,
1, 2, 8, 5, 11, 12, 4, 15,
13, 8, 11, 5, 6, 15, 0, 3,
4, 7, 2, 12, 1, 10, 14, 9,
10, 6, 9, 0, 12, 11, 7, 13,
15, 1, 3, 14, 5, 2, 8, 4,
3, 15, 0, 6, 10, 1, 13, 8,
9, 4, 5, 11, 12, 7, 2, 14,
2, 12, 4, 1, 7, 10, 11, 6,
8, 5, 3, 15, 13, 0, 14, 9,
14, 11, 2, 12, 4, 7, 13, 1,
5, 0, 15, 10, 3, 9, 8, 6,
4, 2, 1, 11, 10, 13, 7, 8,
15, 9, 12, 5, 6, 3, 0, 14,
11, 8, 12, 7, 1, 14, 2, 13,
6, 15, 0, 9, 10, 4, 5, 3,
12, 1, 10, 15, 9, 2, 6, 8,
0, 13, 3, 4, 14, 7, 5, 11,
10, 15, 4, 2, 7, 12, 9, 5,
6, 1, 13, 14, 0, 11, 3, 8,
9, 14, 15, 5, 2, 8, 12, 3,
7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10,
11, 14, 1, 7, 6, 0, 8, 13,
4, 11, 2, 14, 15, 0, 8, 13,
3, 12, 9, 7, 5, 10, 6, 1,
13, 0, 11, 7, 4, 9, 1, 10,
14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14,
10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7,
9, 5, 0, 15, 14, 2, 3, 12,
13, 2, 8, 4, 6, 15, 11, 1,
10, 9, 3, 14, 5, 0, 12, 7,
1, 15, 13, 8, 10, 3, 7, 4,
12, 5, 6, 11, 0, 14, 9, 2,
7, 11, 4, 1, 9, 12, 14, 2,
0, 6, 10, 13, 15, 3, 5, 8,
2, 1, 14, 7, 4, 10, 8, 13,
15, 12, 9, 0, 3, 5, 6, 11,
};
static readonly byte[] perm = new byte[32] {
16, 7, 20, 21, 29, 12, 28, 17,
1, 15, 23, 26, 5, 18, 31, 10,
2, 8, 24, 14, 32, 27, 3, 9,
19, 13, 30, 6, 22, 11, 4, 25,
};
static readonly byte[] esel = new byte[48] {
32, 1, 2, 3, 4, 5, 4, 5,
6, 7, 8, 9, 8, 9, 10, 11,
12, 13, 12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21, 20, 21,
22, 23, 24, 25, 24, 25, 26, 27,
28, 29, 28, 29, 30, 31, 32, 1,
};
static readonly byte[] ip = new byte[64] {
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7,
};
static readonly byte[] final = new byte[64] {
40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25,
};
static readonly byte[] pc1 = new byte[56] {
57, 49, 41, 33, 25, 17, 9, 1,
58, 50, 42, 34, 26, 18, 10, 2,
59, 51, 43, 35, 27, 19, 11, 3,
60, 52, 44, 36, 63, 55, 47, 39,
31, 23, 15, 7, 62, 54, 46, 38,
30, 22, 14, 6, 61, 53, 45, 37,
29, 21, 13, 5, 28, 20, 12, 4,
};
static readonly byte[] pc2 = new byte[48] {
14, 17, 11, 24, 1, 5, 3, 28,
15, 6, 21, 10, 23, 19, 12, 4,
26, 8, 16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55, 30, 40,
51, 45, 33, 48, 44, 49, 39, 56,
34, 53, 46, 42, 50, 36, 29, 32,
};
static readonly byte[] rots = new byte[16] {
1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1,
};
struct Bits {
readonly byte[] byteBits;
public static Bits fromBytes(byte[] bytes) {
return fromBytes(bytes, 0, bytes.Length * 8);
}
public static Bits fromBytes(byte[] bytes, int index, int numBits) {
return new Bits(bytes, index, numBits);
}
public static Bits fromByteBits(byte[] byteBits1, byte[] byteBits2) {
return new Bits(byteBits1, byteBits2);
}
public static Bits fromByteBits(byte[] byteBits) {
return fromByteBits(byteBits, 0, byteBits.Length);
}
public static Bits fromByteBits(byte[] byteBits, int index, int numBits) {
var bits = new Bits(numBits);
for (int i = 0; i < numBits; i++)
bits.byteBits[i] = byteBits[index + i];
return bits;
}
public byte this[int index] {
get { return byteBits[index]; }
}
public byte[] ByteBits {
get { return byteBits; }
}
Bits(int numBits) {
this.byteBits = new byte[numBits];
}
Bits(byte[] bytes1, byte[] bytes2) {
this.byteBits = concat(bytes1, bytes2);
}
Bits(byte[] bytes, int index, int numBits) {
this.byteBits = toByteBits(bytes, index, numBits);
}
static byte[] toByteBits(byte[] bytes, int index, int numBits) {
var byteBits = new byte[numBits];
for (int i = 0; i < numBits; i++) {
int j = i / 8;
int k = i & 7;
byteBits[i] = (byte)(((bytes[index + j] >> k) & 1) != 0 ? 1 : 0);
}
return byteBits;
}
static byte[] concat(byte[] bytes1, byte[] bytes2) {
var bytes = new byte[bytes1.Length + bytes2.Length];
Array.Copy(bytes1, 0, bytes, 0, bytes1.Length);
Array.Copy(bytes2, 0, bytes, bytes1.Length, bytes2.Length);
return bytes;
}
public Bits transpose(byte[] bits) {
var result = new Bits(bits.Length);
for (int i = 0; i < bits.Length; i++)
result.byteBits[i] = byteBits[bits[i] - 1];
return result;
}
public void rol() {
if (byteBits.Length == 0)
return;
var first = byteBits[0];
for (int i = 1; i < byteBits.Length; i++)
byteBits[i - 1] = byteBits[i];
byteBits[byteBits.Length - 1] = first;
}
public void rol(int num) {
for (int i = 0; i < num; i++)
rol();
}
public Bits extract(int index, int numBits) {
return fromByteBits(byteBits, index, numBits);
}
public void toBits(byte[] dest, int index) {
var bits = toBits();
Array.Copy(bits, 0, dest, index, bits.Length);
}
public byte[] toBits() {
var bits = new byte[(byteBits.Length + 7) / 8];
for (int i = 0; i < bits.Length; i++) {
byte val = 0;
for (int j = i * 8, k = 1; j < byteBits.Length; j++, k <<= 1) {
if (byteBits[j] != 0)
val |= (byte)k;
}
bits[i] = val;
}
return bits;
}
public Bits clone() {
return fromByteBits(byteBits, 0, byteBits.Length);
}
public void set(int destIndex, Bits other) {
for (int i = 0; i < other.byteBits.Length; i++)
byteBits[destIndex + i] = other.byteBits[i];
}
public void xor(Bits other) {
if (byteBits.Length != other.byteBits.Length)
throw new ArgumentException("other");
for (int i = 0; i < byteBits.Length; i++)
byteBits[i] ^= other.byteBits[i];
}
public void copyTo(byte[] dest, int index) {
for (int i = 0; i < byteBits.Length; i++)
dest[index + i] = byteBits[i];
}
}
public CryptDecrypter(byte[] key) {
if (key.Length <= 8)
throw new ArgumentException("Invalid size", "key");
this.key = key;
}
public static byte[] decrypt(byte[] key, byte[] encrypted) {
return new CryptDecrypter(key).decrypt(encrypted);
}
byte[] decrypt(byte[] encrypted) {
if (encrypted.Length % 8 != 0)
throw new ArgumentException("encrypted");
var key1 = createKey(key, 0);
var key2 = createKey(key, 8);
var decrypted = new byte[encrypted.Length];
int count = encrypted.Length / 8;
for (int i = 0; i < count; i++) {
var buf = new byte[8];
Array.Copy(encrypted, i * 8, buf, 0, buf.Length);
buf = decrypt(buf, key1, true);
buf = decrypt(buf, key2, false);
buf = decrypt(buf, key1, true);
Array.Copy(buf, 0, decrypted, i * 8, buf.Length);
}
return decrypted;
}
byte[] decrypt(byte[] data, Bits key, bool flag) {
var bits = Bits.fromBytes(data).transpose(ip);
if (flag) {
for (int i = 0, ki = key.ByteBits.Length - 48; i < 16; i++, ki -= 48) {
var oldBits = bits.extract(0, 32);
var tmp = decrypt(oldBits.clone(), key.extract(ki, 48));
tmp.xor(bits.extract(32, 32));
bits.set(32, oldBits);
bits.set(0, tmp);
}
}
else {
for (int i = 0, ki = 0; i < 16; i++, ki += 48) {
var oldBits = bits.extract(32, 32);
var tmp = decrypt(oldBits.clone(), key.extract(ki, 48));
tmp.xor(bits.extract(0, 32));
bits.set(0, oldBits);
bits.set(32, tmp);
}
}
bits = bits.transpose(final);
return bits.toBits();
}
Bits decrypt(Bits data, Bits key) {
var newData = data.clone().transpose(esel);
newData.xor(key);
return Bits.fromByteBits(getSbox(newData)).transpose(perm);
}
byte[] getSbox(Bits data) {
var sboxByteBits = new byte[32];
for (int i = 0; i < 8; i++) {
int di = i * 6;
int index = (data[di + 0] << 5) + (data[di + 5] << 4) + (data[di + 1] << 3) +
(data[di + 2] << 2) + (data[di + 3] << 1) + data[di + 4] + i * 64;
Bits.fromBytes(sbox, index, 4).copyTo(sboxByteBits, i * 4);
}
return sboxByteBits;
}
static Bits createKey(byte[] data, int index) {
Bits key1, key2;
createKeys(data, index, out key1, out key2);
byte[] newKey = new byte[16 * 6];
byte[] tmpData = new byte[28 * 2];
for (int i = 0; i < 16; i++) {
int rolCount = rots[i];
key1.rol(rolCount);
key2.rol(rolCount);
Bits.fromByteBits(key1.ByteBits, key2.ByteBits).transpose(pc2).toBits(newKey, i * 6);
}
return Bits.fromBytes(newKey);
}
static void createKeys(byte[] data, int index, out Bits key1, out Bits key2) {
var tmpKey = new byte[8];
int len = Math.Min(tmpKey.Length, data.Length - index);
if (len == 0)
throw new ArgumentException("data");
Array.Copy(data, index, tmpKey, 0, len);
var bits = Bits.fromBytes(tmpKey).transpose(pc1);
key1 = Bits.fromByteBits(bits.ByteBits, 0, 28);
key2 = Bits.fromByteBits(bits.ByteBits, 28, 28);
}
}
}

View File

@ -0,0 +1,123 @@
/*
Copyright (C) 2011-2012 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;
namespace de4dot.code.deobfuscators.MaxtoCode {
class Decrypter6 {
readonly uint[] key;
readonly byte[] gen1 = new byte[0x100];
readonly byte[] gen2 = new byte[0x100];
readonly byte[] gen3 = new byte[0x100];
readonly byte[] gen4 = new byte[0x100];
static readonly byte[] d1 = new byte[16] { 14, 4, 13, 21, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 };
static readonly byte[] d2 = new byte[16] { 15, 1, 8, 14, 6, 11, 3, 4, 30, 7, 2, 13, 12, 0, 5, 10 };
static readonly byte[] d3 = new byte[16] { 10, 0, 9, 14, 6, 3, 15, 5, 23, 13, 12, 7, 11, 4, 2, 8 };
static readonly byte[] d4 = new byte[16] { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 };
static readonly byte[] d5 = new byte[16] { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 };
static readonly byte[] d6 = new byte[16] { 12, 1, 10, 15, 9, 2, 6, 8, 2, 13, 3, 4, 14, 7, 5, 11 };
static readonly byte[] d7 = new byte[16] { 4, 11, 12, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 };
static readonly byte[] d8 = new byte[16] { 13, 2, 8, 14, 6, 7, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 };
public static byte[] decrypt(byte[] key, byte[] encrypted) {
return new Decrypter6(key).decrypt(encrypted);
}
Decrypter6(byte[] key) {
if (key.Length != 32)
throw new ArgumentException("Invalid key size", "key");
this.key = new uint[8];
Buffer.BlockCopy(key, 0, this.key, 0, key.Length);
initialize();
}
byte[] decrypt(byte[] encrypted) {
if ((encrypted.Length & 7) != 0)
throw new ArgumentException("Invalid data length", "encrypted");
var decrypted = new byte[encrypted.Length];
int count = decrypted.Length / 8;
for (int i = 0; i < count; i++) {
uint x, y;
decrypt(BitConverter.ToUInt32(encrypted, i * 8), BitConverter.ToUInt32(encrypted, i * 8 + 4), out x, out y);
for (int j = 1; j < 100; j++)
decrypt(x, y, out x, out y);
writeUInt32(decrypted, i * 8, x);
writeUInt32(decrypted, i * 8 + 4, y);
}
return decrypted;
}
static void writeUInt32(byte[] data, int index, uint value) {
data[index] = (byte)value;
data[index + 1] = (byte)(value >> 8);
data[index + 2] = (byte)(value >> 16);
data[index + 3] = (byte)(value >> 24);
}
void initialize() {
for (int i = 0; i < 0x100; i++) {
gen1[i] = (byte)((d1[i / 16] << 4) | d2[i & 0x0F]);
gen2[i] = (byte)((d3[i / 16] << 4) | d4[i & 0x0F]);
gen3[i] = (byte)((d5[i / 16] << 4) | d6[i & 0x0F]);
gen4[i] = (byte)((d7[i / 16] << 4) | d8[i & 0x0F]);
}
}
void decrypt(uint i0, uint i1, out uint o0, out uint o1) {
uint x = i0;
uint y = decrypt(x + key[0]);
y ^= i1;
x ^= decrypt(y + key[1]);
y ^= decrypt(x + key[2]);
x ^= decrypt(y + key[3]);
y ^= decrypt(x + key[4]);
x ^= decrypt(y + key[5]);
y ^= decrypt(x + key[6]);
x ^= decrypt(y + key[7]);
for (int i = 0; i < 3; i++) {
y ^= decrypt(x + key[7]);
x ^= decrypt(y + key[6]);
y ^= decrypt(x + key[5]);
x ^= decrypt(y + key[4]);
y ^= decrypt(x + key[3]);
x ^= decrypt(y + key[2]);
y ^= decrypt(x + key[1]);
x ^= decrypt(y + key[0]);
}
o0 = y;
o1 = x;
}
uint decrypt(uint val) {
uint x = (uint)((gen1[(byte)(val >> 24)] << 24) |
(gen2[(byte)(val >> 16)] << 16) |
(gen3[(byte)(val >> 8)] << 8) |
gen4[(byte)val]);
return ror(x, 21);
}
static uint ror(uint val, int n) {
return (val << (32 - n)) + (val >> n);
}
}
}

View File

@ -111,7 +111,18 @@ namespace de4dot.code.deobfuscators.MaxtoCode {
public McKey(PeImage peImage, PeHeader peHeader) {
this.peHeader = peHeader;
this.data = peImage.readBytes(peHeader.getMcKeyRva(), 0x2000);
try {
this.data = peImage.readBytes(peHeader.getMcKeyRva(), 0x2000);
}
catch (IOException) {
this.data = peImage.readBytes(peHeader.getMcKeyRva(), 0x1000);
}
}
public byte[] readBytes(int offset, int len) {
byte[] bytes = new byte[len];
Array.Copy(data, offset, bytes, 0, len);
return bytes;
}
public byte readByte(int offset) {
@ -253,9 +264,17 @@ namespace de4dot.code.deobfuscators.MaxtoCode {
uint encryptedDataOffset;
uint xorKey;
Dictionary<uint, DecryptedMethodInfo> infos = new Dictionary<uint, DecryptedMethodInfo>();
IDecrypter decrypter;
List<IDecrypter> decrypters = new List<IDecrypter>();
const int ENCRYPTED_DATA_INFO_SIZE = 0x13;
delegate byte[] Decrypt(byte[] encrypted);
readonly Decrypt[] decryptHandlersV1;
readonly Decrypt[] decryptHandlersV2;
readonly Decrypt[] decryptHandlersV3;
readonly Decrypt[] decryptHandlersV4;
readonly Decrypt[] decryptHandlersV5a;
readonly Decrypt[] decryptHandlersV5b;
public class DecryptedMethodInfo {
public uint bodyRva;
public byte[] body;
@ -272,6 +291,13 @@ namespace de4dot.code.deobfuscators.MaxtoCode {
this.peHeader = peHeader;
this.mcKey = mcKey;
decryptHandlersV1 = new Decrypt[] { decrypt1a, decrypt4a, decrypt2a, decrypt3a, decrypt5, decrypt6, decrypt7 };
decryptHandlersV2 = new Decrypt[] { decrypt3a, decrypt2a, decrypt1a, decrypt4a, decrypt5, decrypt6, decrypt7 };
decryptHandlersV3 = new Decrypt[] { decrypt1a, decrypt2a, decrypt3a, decrypt4a, decrypt5, decrypt6, decrypt7 };
decryptHandlersV4 = new Decrypt[] { decrypt2a, decrypt1a, decrypt3a, decrypt4a, decrypt5, decrypt6, decrypt7 };
decryptHandlersV5a = new Decrypt[] { decrypt4a, decrypt2a, decrypt3a, decrypt1a, decrypt5, decrypt6, decrypt7 };
decryptHandlersV5b = new Decrypt[] { decrypt4b, decrypt2b, decrypt3b, decrypt1b, decrypt6, decrypt7, decrypt5 };
structSize = getStructSize(mcKey);
uint methodInfosRva = peHeader.getRva2(0x0FF8, mcKey.readUInt32(0x005A));
@ -346,42 +372,29 @@ namespace de4dot.code.deobfuscators.MaxtoCode {
}
class Decrypter : IDecrypter {
MethodInfos methodInfos;
int[] typeToMethod;
Decrypt[] decrypterHandlers;
public Decrypter(MethodInfos methodInfos, int[] typeToMethod) {
this.methodInfos = methodInfos;
this.typeToMethod = typeToMethod;
public Decrypter(Decrypt[] decrypterHandlers) {
this.decrypterHandlers = decrypterHandlers;
}
public byte[] decrypt(int type, byte[] encrypted) {
if (0 <= type && type < typeToMethod.Length) {
switch (typeToMethod[type]) {
case 1: return methodInfos.decrypt1(encrypted);
case 2: return methodInfos.decrypt2(encrypted);
case 3: return methodInfos.decrypt3(encrypted);
case 4: return methodInfos.decrypt4(encrypted);
case 5: return methodInfos.decrypt5(encrypted);
case 6: return methodInfos.decrypt6(encrypted);
case 7: return methodInfos.decrypt7(encrypted);
}
}
if (1 <= type && type <= decrypterHandlers.Length)
return decrypterHandlers[type - 1](encrypted);
throw new ApplicationException(string.Format("Invalid encryption type: {0:X2}", type));
}
}
static readonly int[] typeToTypesV1 = new int[] { -1, 1, 4, 2, 3, 5, 6, 7 };
static readonly int[] typeToTypesV2 = new int[] { -1, 3, 2, 1, 4, 5, 6, 7 };
static readonly int[] typeToTypesV3 = new int[] { -1, 1, 2, 3, 4, 5, 6, 7 };
static readonly int[] typeToTypesV4 = new int[] { -1, 2, 1, 3, 4, 5, 6, 7 };
static readonly int[] typeToTypesV5 = new int[] { -1, 4, 2, 3, 1, 5, 6, 7 };
void initializeDecrypter() {
switch (getVersion()) {
case EncryptionVersion.V1: decrypter = new Decrypter(this, typeToTypesV1); break;
case EncryptionVersion.V2: decrypter = new Decrypter(this, typeToTypesV2); break;
case EncryptionVersion.V3: decrypter = new Decrypter(this, typeToTypesV3); break;
case EncryptionVersion.V4: decrypter = new Decrypter(this, typeToTypesV4); break;
case EncryptionVersion.V5: decrypter = new Decrypter(this, typeToTypesV5); break;
case EncryptionVersion.V1: decrypters.Add(new Decrypter(decryptHandlersV1)); break;
case EncryptionVersion.V2: decrypters.Add(new Decrypter(decryptHandlersV2)); break;
case EncryptionVersion.V3: decrypters.Add(new Decrypter(decryptHandlersV3)); break;
case EncryptionVersion.V4: decrypters.Add(new Decrypter(decryptHandlersV4)); break;
case EncryptionVersion.V5:
decrypters.Add(new Decrypter(decryptHandlersV5a));
decrypters.Add(new Decrypter(decryptHandlersV5b));
break;
case EncryptionVersion.Unknown:
default:
@ -391,7 +404,23 @@ namespace de4dot.code.deobfuscators.MaxtoCode {
public void initializeInfos() {
initializeDecrypter();
if (!initializeInfos2())
throw new ApplicationException("Could not decrypt methods");
}
bool initializeInfos2() {
foreach (var decrypter in decrypters) {
try {
if (initializeInfos2(decrypter))
return true;
}
catch {
}
}
return false;
}
bool initializeInfos2(IDecrypter decrypter) {
int numMethods = readInt32(0) ^ readInt32(4);
if (numMethods < 0)
throw new ApplicationException("Invalid number of encrypted methods");
@ -406,8 +435,6 @@ namespace de4dot.code.deobfuscators.MaxtoCode {
uint totalSize = readEncryptedUInt32(offset + 4);
uint methodInstructionRva = readEncryptedUInt32(offset + 8);
var decryptedData = new byte[totalSize];
// Read the method body header and method body (instrs + exception handlers).
// The method body header is always in the first one. The instrs + ex handlers
// are always in the last 4, and evenly divided (each byte[] is totalLen / 4).
@ -425,18 +452,24 @@ namespace de4dot.code.deobfuscators.MaxtoCode {
if (j == 1 && exOffset == 0)
encryptedDataInfos[j] = null;
else
encryptedDataInfos[j] = decrypt(encryptionType, dataOffset, encryptedSize, realSize);
encryptedDataInfos[j] = decrypt(decrypter, encryptionType, dataOffset, encryptedSize, realSize);
}
var decryptedData = new byte[totalSize];
int copyOffset = 0;
copyOffset = copyData(decryptedData, encryptedDataInfos[0], copyOffset);
for (int j = 2; j < encryptedDataInfos.Length; j++)
copyOffset = copyData(decryptedData, encryptedDataInfos[j], copyOffset);
copyData(decryptedData, encryptedDataInfos[1], exOffset); // Exceptions or padding
if (!MethodBodyParser.verify(decryptedData))
throw new InvalidMethodBody();
var info = new DecryptedMethodInfo(methodBodyRva, decryptedData);
infos[info.bodyRva] = info;
}
return true;
}
static int copyData(byte[] dest, byte[] source, int offset) {
@ -450,7 +483,7 @@ namespace de4dot.code.deobfuscators.MaxtoCode {
return peImage.offsetReadBytes(encryptedDataOffset + offset, size);
}
byte[] decrypt(int type, uint dataOffset, uint encryptedSize, uint realSize) {
byte[] decrypt(IDecrypter decrypter, int type, uint dataOffset, uint encryptedSize, uint realSize) {
if (realSize == 0)
return null;
if (realSize > encryptedSize)
@ -464,17 +497,35 @@ namespace de4dot.code.deobfuscators.MaxtoCode {
return decrypted;
}
byte[] decrypt1(byte[] encrypted) {
byte[] decrypt1a(byte[] encrypted) {
return decrypt1(encrypted, 0, 0x2000);
}
byte[] decrypt1b(byte[] encrypted) {
return decrypt1(encrypted, 6, 0x500);
}
byte[] decrypt1(byte[] encrypted, int keyStart, int keyEnd) {
var decrypted = new byte[encrypted.Length];
for (int i = 0; i < decrypted.Length; i++)
decrypted[i] = (byte)(encrypted[i] ^ mcKey.readByte(i % 0x2000));
for (int i = 0, ki = keyStart; i < decrypted.Length; i++) {
decrypted[i] = (byte)(encrypted[i] ^ mcKey.readByte(ki));
if (++ki == keyEnd)
ki = keyStart;
}
return decrypted;
}
byte[] decrypt2(byte[] encrypted) {
byte[] decrypt2a(byte[] encrypted) {
return decrypt2(encrypted, 0x00FA);
}
byte[] decrypt2b(byte[] encrypted) {
return decrypt2(encrypted, 0x00FA + 9);
}
byte[] decrypt2(byte[] encrypted, int offset) {
if ((encrypted.Length & 7) != 0)
throw new ApplicationException("Invalid encryption #2 length");
const int offset = 0x00FA;
uint key4 = mcKey.readUInt32(offset + 4 * 4);
uint key5 = mcKey.readUInt32(offset + 5 * 4);
@ -495,11 +546,18 @@ namespace de4dot.code.deobfuscators.MaxtoCode {
return decrypted;
}
static byte[] decrypt3Shifts = new byte[16] { 5, 11, 14, 21, 6, 20, 17, 29, 4, 10, 3, 2, 7, 1, 26, 18 };
byte[] decrypt3(byte[] encrypted) {
byte[] decrypt3a(byte[] encrypted) {
return decrypt3(encrypted, 0x015E);
}
byte[] decrypt3b(byte[] encrypted) {
return decrypt3(encrypted, 0x015E + 0xE5);
}
static readonly byte[] decrypt3Shifts = new byte[16] { 5, 11, 14, 21, 6, 20, 17, 29, 4, 10, 3, 2, 7, 1, 26, 18 };
byte[] decrypt3(byte[] encrypted, int offset) {
if ((encrypted.Length & 7) != 0)
throw new ApplicationException("Invalid encryption #3 length");
const int offset = 0x015E;
uint key0 = mcKey.readUInt32(offset + 0 * 4);
uint key3 = mcKey.readUInt32(offset + 3 * 4);
@ -525,37 +583,67 @@ namespace de4dot.code.deobfuscators.MaxtoCode {
return decrypted;
}
byte[] decrypt4(byte[] encrypted) {
byte[] decrypt4a(byte[] encrypted) {
return decrypt4(encrypted, 0, 0x2000);
}
byte[] decrypt4b(byte[] encrypted) {
return decrypt4(encrypted, 0x14, 0x1000);
}
byte[] decrypt4(byte[] encrypted, int keyStart, int keyEnd) {
var decrypted = new byte[encrypted.Length / 3 * 2 + 1];
int count = encrypted.Length / 3;
int i = 0, j = 0, k = 0;
int i = 0, ki = keyStart, j = 0;
while (count-- > 0) {
byte k1 = mcKey.readByte(j + 1);
byte k2 = mcKey.readByte(j + 2);
byte k3 = mcKey.readByte(j + 3);
decrypted[k++] = (byte)(((encrypted[i + 1] ^ k2) >> 4) | ((encrypted[i] ^ k1) & 0xF0));
decrypted[k++] = (byte)(((encrypted[i + 1] ^ k2) << 4) + ((encrypted[i + 2] ^ k3) & 0x0F));
byte k1 = mcKey.readByte(ki + 1);
byte k2 = mcKey.readByte(ki + 2);
byte k3 = mcKey.readByte(ki + 3);
decrypted[j++] = (byte)(((encrypted[i + 1] ^ k2) >> 4) | ((encrypted[i] ^ k1) & 0xF0));
decrypted[j++] = (byte)(((encrypted[i + 1] ^ k2) << 4) + ((encrypted[i + 2] ^ k3) & 0x0F));
i += 3;
j = (j + 4) % 0x2000;
ki += 4;
if (ki == keyEnd)
ki = keyStart;
}
if ((encrypted.Length % 3) != 0)
decrypted[k] = (byte)(encrypted[i] ^ mcKey.readByte(j));
decrypted[j] = (byte)(encrypted[i] ^ mcKey.readByte(ki));
return decrypted;
}
byte[] decrypt5(byte[] encrypted) {
throw new NotImplementedException("Encryption type #5 not implemented yet");
return CryptDecrypter.decrypt(mcKey.readBytes(0x0032, 15), encrypted);
}
byte[] decrypt6(byte[] encrypted) {
throw new NotImplementedException("Encryption type #6 not implemented yet");
return Decrypter6.decrypt(mcKey.readBytes(0x0096, 32), encrypted);
}
byte[] decrypt7(byte[] encrypted) {
throw new NotImplementedException("Encryption type #7 not implemented yet");
var decrypted = (byte[])encrypted.Clone();
new Blowfish(getBlowfishKey()).decrypt_LE(decrypted);
return decrypted;
}
byte[] blowfishKey;
byte[] getBlowfishKey() {
if (blowfishKey != null)
return blowfishKey;
var key = new byte[100];
int i;
for (i = 0; i < key.Length; i++) {
byte b = mcKey.readByte(i);
if (b == 0)
break;
key[i] = b;
}
for (; i < key.Length; i++)
key[i] = 0;
key[key.Length - 1] = 0;
return blowfishKey = key;
}
}