2011-10-26 20:40:55 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
2011-10-26 20:40:55 +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;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using Mono.Cecil;
|
2011-11-12 18:19:10 +08:00
|
|
|
|
using Mono.Cecil.Cil;
|
2011-10-26 20:40:55 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
2011-12-22 01:55:36 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 {
|
2011-10-26 20:40:55 +08:00
|
|
|
|
class EncryptedResource {
|
|
|
|
|
ModuleDefinition module;
|
|
|
|
|
MethodDefinition resourceDecrypterMethod;
|
|
|
|
|
EmbeddedResource encryptedDataResource;
|
|
|
|
|
byte[] key, iv;
|
|
|
|
|
|
2011-11-09 04:27:03 +08:00
|
|
|
|
public TypeDefinition Type {
|
|
|
|
|
get { return resourceDecrypterMethod == null ? null : resourceDecrypterMethod.DeclaringType; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-06 22:24:30 +08:00
|
|
|
|
public MethodDefinition Method {
|
2011-10-26 20:40:55 +08:00
|
|
|
|
get { return resourceDecrypterMethod; }
|
|
|
|
|
set { resourceDecrypterMethod = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-06 22:24:30 +08:00
|
|
|
|
public EmbeddedResource Resource {
|
2011-10-27 05:00:01 +08:00
|
|
|
|
get { return encryptedDataResource; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-09 04:27:03 +08:00
|
|
|
|
public bool FoundResource {
|
|
|
|
|
get { return encryptedDataResource != null; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 20:40:55 +08:00
|
|
|
|
public EncryptedResource(ModuleDefinition module) {
|
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EncryptedResource(ModuleDefinition module, EncryptedResource oldOne) {
|
|
|
|
|
this.module = module;
|
2011-11-06 19:18:35 +08:00
|
|
|
|
resourceDecrypterMethod = lookup(oldOne.resourceDecrypterMethod, "Could not find resource decrypter method");
|
2011-10-27 01:49:25 +08:00
|
|
|
|
if (oldOne.encryptedDataResource != null)
|
|
|
|
|
encryptedDataResource = DotNetUtils.getResource(module, oldOne.encryptedDataResource.Name) as EmbeddedResource;
|
2011-10-26 20:40:55 +08:00
|
|
|
|
key = oldOne.key;
|
|
|
|
|
iv = oldOne.iv;
|
|
|
|
|
|
2011-10-27 01:49:25 +08:00
|
|
|
|
if (encryptedDataResource == null && oldOne.encryptedDataResource != null)
|
2011-10-26 20:40:55 +08:00
|
|
|
|
throw new ApplicationException("Could not initialize EncryptedResource");
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-06 19:18:35 +08:00
|
|
|
|
T lookup<T>(T def, string errorMessage) where T : MemberReference {
|
|
|
|
|
return DeobUtils.lookup(module, def, errorMessage);
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-07 13:11:32 +08:00
|
|
|
|
public bool couldBeResourceDecrypter(MethodDefinition method, IEnumerable<string> additionalTypes) {
|
|
|
|
|
return couldBeResourceDecrypter(method, additionalTypes, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool couldBeResourceDecrypter(MethodDefinition method, IEnumerable<string> additionalTypes, bool checkResource) {
|
2011-10-26 20:40:55 +08:00
|
|
|
|
if (!method.IsStatic)
|
|
|
|
|
return false;
|
|
|
|
|
if (method.Body == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
2011-10-27 01:49:25 +08:00
|
|
|
|
var localTypes = new LocalTypes(method);
|
|
|
|
|
var requiredTypes = new List<string> {
|
2011-10-26 20:40:55 +08:00
|
|
|
|
"System.Byte[]",
|
|
|
|
|
"System.IO.BinaryReader",
|
|
|
|
|
"System.IO.MemoryStream",
|
|
|
|
|
"System.Security.Cryptography.CryptoStream",
|
|
|
|
|
"System.Security.Cryptography.ICryptoTransform",
|
|
|
|
|
};
|
2011-10-27 01:49:25 +08:00
|
|
|
|
requiredTypes.AddRange(additionalTypes);
|
2011-10-26 20:40:55 +08:00
|
|
|
|
if (!localTypes.all(requiredTypes))
|
|
|
|
|
return false;
|
2011-11-24 17:08:29 +08:00
|
|
|
|
if (!localTypes.exists("System.Security.Cryptography.RijndaelManaged") &&
|
|
|
|
|
!localTypes.exists("System.Security.Cryptography.AesManaged"))
|
|
|
|
|
return false;
|
2011-10-26 20:40:55 +08:00
|
|
|
|
|
2011-11-09 02:32:10 +08:00
|
|
|
|
if (checkResource && findMethodsDecrypterResource(method) == null)
|
2011-10-26 20:40:55 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void init(ISimpleDeobfuscator simpleDeobfuscator) {
|
|
|
|
|
if (resourceDecrypterMethod == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
simpleDeobfuscator.deobfuscate(resourceDecrypterMethod);
|
|
|
|
|
|
|
|
|
|
encryptedDataResource = findMethodsDecrypterResource(resourceDecrypterMethod);
|
|
|
|
|
if (encryptedDataResource == null)
|
2011-11-09 04:27:03 +08:00
|
|
|
|
return;
|
2011-10-26 20:40:55 +08:00
|
|
|
|
|
2011-12-21 03:16:57 +08:00
|
|
|
|
key = ArrayFinder.getInitializedByteArray(resourceDecrypterMethod, 32);
|
2011-10-26 20:40:55 +08:00
|
|
|
|
if (key == null)
|
|
|
|
|
throw new ApplicationException("Could not find resource decrypter key");
|
2011-12-21 03:16:57 +08:00
|
|
|
|
iv = ArrayFinder.getInitializedByteArray(resourceDecrypterMethod, 16);
|
2011-10-26 20:40:55 +08:00
|
|
|
|
if (iv == null)
|
|
|
|
|
throw new ApplicationException("Could not find resource decrypter IV");
|
2011-11-12 18:19:10 +08:00
|
|
|
|
if (usesPublicKeyToken()) {
|
|
|
|
|
var publicKeyToken = module.Assembly.Name.PublicKeyToken;
|
|
|
|
|
if (publicKeyToken != null && publicKeyToken.Length > 0) {
|
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
|
iv[i * 2 + 1] = publicKeyToken[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int[] pktIndexes = new int[16] { 1, 0, 3, 1, 5, 2, 7, 3, 9, 4, 11, 5, 13, 6, 15, 7 };
|
|
|
|
|
bool usesPublicKeyToken() {
|
|
|
|
|
int pktIndex = 0;
|
|
|
|
|
foreach (var instr in resourceDecrypterMethod.Body.Instructions) {
|
|
|
|
|
if (instr.OpCode.FlowControl != FlowControl.Next) {
|
|
|
|
|
pktIndex = 0;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!DotNetUtils.isLdcI4(instr))
|
|
|
|
|
continue;
|
|
|
|
|
int val = DotNetUtils.getLdcI4Value(instr);
|
|
|
|
|
if (val != pktIndexes[pktIndex++]) {
|
|
|
|
|
pktIndex = 0;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (pktIndex >= pktIndexes.Length)
|
|
|
|
|
return true;
|
2011-10-26 20:40:55 +08:00
|
|
|
|
}
|
2011-11-12 18:19:10 +08:00
|
|
|
|
return false;
|
2011-10-26 20:40:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EmbeddedResource findMethodsDecrypterResource(MethodDefinition method) {
|
|
|
|
|
foreach (var s in DotNetUtils.getCodeStrings(method)) {
|
|
|
|
|
var resource = DotNetUtils.getResource(module, s) as EmbeddedResource;
|
|
|
|
|
if (resource != null)
|
|
|
|
|
return resource;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] decrypt() {
|
|
|
|
|
if (encryptedDataResource == null || key == null || iv == null)
|
|
|
|
|
throw new ApplicationException("Can't decrypt resource");
|
|
|
|
|
|
2011-12-31 22:12:41 +08:00
|
|
|
|
return DeobUtils.aesDecrypt(encryptedDataResource.GetResourceData(), key, iv);
|
2011-10-26 20:40:55 +08:00
|
|
|
|
}
|
2011-11-25 22:21:29 +08:00
|
|
|
|
|
|
|
|
|
public byte[] encrypt(byte[] data) {
|
|
|
|
|
if (key == null || iv == null)
|
|
|
|
|
throw new ApplicationException("Can't encrypt resource");
|
|
|
|
|
|
|
|
|
|
using (var aes = new RijndaelManaged { Mode = CipherMode.CBC }) {
|
|
|
|
|
using (var transform = aes.CreateEncryptor(key, iv)) {
|
|
|
|
|
return transform.TransformFinalBlock(data, 0, data.Length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void updateResource(byte[] encryptedData) {
|
|
|
|
|
for (int i = 0; i < module.Resources.Count; i++) {
|
|
|
|
|
if (module.Resources[i] == encryptedDataResource) {
|
|
|
|
|
encryptedDataResource = new EmbeddedResource(encryptedDataResource.Name, encryptedDataResource.Attributes, encryptedData);
|
|
|
|
|
module.Resources[i] = encryptedDataResource;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw new ApplicationException("Could not find encrypted resource");
|
|
|
|
|
}
|
2011-10-26 20:40:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|