2012-07-11 08:18:01 +08:00
|
|
|
|
/*
|
2015-10-30 05:45:26 +08:00
|
|
|
|
Copyright (C) 2011-2015 de4dot@gmail.com
|
2012-07-11 08:18:01 +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.Collections.Generic;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.IO;
|
|
|
|
|
using dnlib.DotNet;
|
2013-09-25 07:37:53 +08:00
|
|
|
|
using dnlib.DotNet.Emit;
|
2012-07-11 08:18:01 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.CryptoObfuscator {
|
|
|
|
|
class ConstantsDecrypter {
|
2012-11-09 05:24:13 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
TypeDef decrypterType;
|
|
|
|
|
MethodDef methodI4;
|
|
|
|
|
MethodDef methodI8;
|
|
|
|
|
MethodDef methodR4;
|
|
|
|
|
MethodDef methodR8;
|
2013-09-25 07:37:53 +08:00
|
|
|
|
MethodDef methodArray;
|
|
|
|
|
InitializedDataCreator initializedDataCreator;
|
2012-07-11 08:18:01 +08:00
|
|
|
|
EmbeddedResource encryptedResource;
|
|
|
|
|
byte[] constantsData;
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef Type {
|
2012-07-11 08:18:01 +08:00
|
|
|
|
get { return decrypterType; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EmbeddedResource Resource {
|
|
|
|
|
get { return encryptedResource; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef Int32Decrypter {
|
2012-07-11 08:18:01 +08:00
|
|
|
|
get { return methodI4; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef Int64Decrypter {
|
2012-07-11 08:18:01 +08:00
|
|
|
|
get { return methodI8; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef SingleDecrypter {
|
2012-07-11 08:18:01 +08:00
|
|
|
|
get { return methodR4; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef DoubleDecrypter {
|
2012-07-11 08:18:01 +08:00
|
|
|
|
get { return methodR8; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return decrypterType != null; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-25 07:37:53 +08:00
|
|
|
|
public ConstantsDecrypter(ModuleDefMD module, InitializedDataCreator initializedDataCreator) {
|
2012-07-11 08:18:01 +08:00
|
|
|
|
this.module = module;
|
2013-09-25 07:37:53 +08:00
|
|
|
|
this.initializedDataCreator = initializedDataCreator;
|
2012-07-11 08:18:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Find() {
|
2012-07-11 08:18:01 +08:00
|
|
|
|
foreach (var type in module.Types) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!CheckType(type))
|
2012-07-11 08:18:01 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
decrypterType = type;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static readonly string[] requiredTypes = new string[] {
|
|
|
|
|
"System.Byte[]",
|
|
|
|
|
};
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool CheckType(TypeDef type) {
|
2012-07-11 08:18:01 +08:00
|
|
|
|
if (type.Methods.Count != 7)
|
|
|
|
|
return false;
|
2012-08-21 21:06:42 +08:00
|
|
|
|
if (type.Fields.Count < 1 || type.Fields.Count > 2)
|
2012-07-11 08:18:01 +08:00
|
|
|
|
return false;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!new FieldTypes(type).All(requiredTypes))
|
2012-07-11 08:18:01 +08:00
|
|
|
|
return false;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!CheckMethods(type))
|
2012-07-11 08:18:01 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool CheckMethods(TypeDef type) {
|
|
|
|
|
methodI4 = DotNetUtils.GetMethod(type, "System.Int32", "(System.Int32)");
|
|
|
|
|
methodI8 = DotNetUtils.GetMethod(type, "System.Int64", "(System.Int32)");
|
|
|
|
|
methodR4 = DotNetUtils.GetMethod(type, "System.Single", "(System.Int32)");
|
|
|
|
|
methodR8 = DotNetUtils.GetMethod(type, "System.Double", "(System.Int32)");
|
2013-09-25 07:37:53 +08:00
|
|
|
|
methodArray = DotNetUtils.GetMethod(type, "System.Void", "(System.Array,System.Int32)");
|
2012-07-11 08:18:01 +08:00
|
|
|
|
|
|
|
|
|
return methodI4 != null && methodI8 != null &&
|
2013-09-25 07:37:53 +08:00
|
|
|
|
methodR4 != null && methodR8 != null &&
|
|
|
|
|
methodArray != null;
|
2012-07-11 08:18:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Initialize(ResourceDecrypter resourceDecrypter) {
|
2012-07-11 08:18:01 +08:00
|
|
|
|
if (decrypterType == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2016-02-07 01:35:17 +08:00
|
|
|
|
MethodDef cctor = decrypterType.FindStaticConstructor();
|
|
|
|
|
encryptedResource = CoUtils.GetResource(module, DotNetUtils.GetCodeStrings(cctor));
|
|
|
|
|
|
|
|
|
|
//if the return value is null, it is possible that resource name is encrypted
|
|
|
|
|
if (encryptedResource == null) {
|
2016-02-12 03:50:54 +08:00
|
|
|
|
var Resources = new string[] { CoUtils.DecryptResourceName(module, cctor) };
|
|
|
|
|
encryptedResource = CoUtils.GetResource(module, Resources);
|
2016-02-07 01:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-20 08:15:27 +08:00
|
|
|
|
encryptedResource.Data.Position = 0;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
constantsData = resourceDecrypter.Decrypt(encryptedResource.Data.CreateStream());
|
2012-07-11 08:18:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public int DecryptInt32(int index) {
|
2012-07-11 08:18:01 +08:00
|
|
|
|
return BitConverter.ToInt32(constantsData, index);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public long DecryptInt64(int index) {
|
2012-07-11 08:18:01 +08:00
|
|
|
|
return BitConverter.ToInt64(constantsData, index);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public float DecryptSingle(int index) {
|
2012-07-11 08:18:01 +08:00
|
|
|
|
return BitConverter.ToSingle(constantsData, index);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public double DecryptDouble(int index) {
|
2012-07-11 08:18:01 +08:00
|
|
|
|
return BitConverter.ToDouble(constantsData, index);
|
|
|
|
|
}
|
2013-09-25 07:37:53 +08:00
|
|
|
|
|
|
|
|
|
struct ArrayInfo {
|
|
|
|
|
public CorLibTypeSig arrayType;
|
|
|
|
|
public int start, len;
|
|
|
|
|
public int arySize, index;
|
|
|
|
|
|
|
|
|
|
public ArrayInfo(int start, int len, CorLibTypeSig arrayType, int arySize, int index) {
|
|
|
|
|
this.start = start;
|
|
|
|
|
this.len = len;
|
|
|
|
|
this.arrayType = arrayType;
|
|
|
|
|
this.arySize = arySize;
|
|
|
|
|
this.index = index;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Deobfuscate(Blocks blocks) {
|
|
|
|
|
var infos = new List<ArrayInfo>();
|
|
|
|
|
foreach (var block in blocks.MethodBlocks.GetAllBlocks()) {
|
|
|
|
|
var instrs = block.Instructions;
|
|
|
|
|
infos.Clear();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < instrs.Count - 5; i++) {
|
|
|
|
|
int index = i;
|
|
|
|
|
|
|
|
|
|
var ldci4_arySize = instrs[index++];
|
|
|
|
|
if (!ldci4_arySize.IsLdcI4())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var newarr = instrs[index++];
|
|
|
|
|
if (newarr.OpCode.Code != Code.Newarr)
|
|
|
|
|
continue;
|
|
|
|
|
var arrayType = module.CorLibTypes.GetCorLibTypeSig(newarr.Operand as ITypeDefOrRef);
|
|
|
|
|
if (arrayType == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (instrs[index++].OpCode.Code != Code.Dup)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var ldci4_index = instrs[index++];
|
|
|
|
|
if (!ldci4_index.IsLdcI4())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var call = instrs[index++];
|
|
|
|
|
if (call.OpCode.Code != Code.Call && call.OpCode.Code != Code.Callvirt)
|
|
|
|
|
continue;
|
|
|
|
|
if (!MethodEqualityComparer.CompareDeclaringTypes.Equals(call.Operand as IMethod, methodArray))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (arrayType.ElementType.GetPrimitiveSize() == -1) {
|
|
|
|
|
Logger.w("Can't decrypt non-primitive type array in method {0:X8}", blocks.Method.MDToken.ToInt32());
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
infos.Add(new ArrayInfo(i, index - i, arrayType, ldci4_arySize.GetLdcI4Value(),
|
|
|
|
|
ldci4_index.GetLdcI4Value()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
infos.Reverse();
|
|
|
|
|
foreach (var info in infos) {
|
|
|
|
|
var elemSize = info.arrayType.ElementType.GetPrimitiveSize();
|
|
|
|
|
var decrypted = DecryptArray(info);
|
|
|
|
|
initializedDataCreator.AddInitializeArrayCode(block, info.start, info.len, info.arrayType.ToTypeDefOrRef(), decrypted);
|
|
|
|
|
Logger.v("Decrypted {0} array: {1} elements", info.arrayType.ToString(), decrypted.Length / elemSize);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] DecryptArray(ArrayInfo aryInfo) {
|
|
|
|
|
var ary = new byte[aryInfo.arySize * aryInfo.arrayType.ElementType.GetPrimitiveSize()];
|
|
|
|
|
int dataIndex = aryInfo.index;
|
|
|
|
|
int len = DeobUtils.ReadVariableLengthInt32(constantsData, ref dataIndex);
|
|
|
|
|
Buffer.BlockCopy(constantsData, dataIndex, ary, 0, len);
|
|
|
|
|
return ary;
|
|
|
|
|
}
|
2012-07-11 08:18:01 +08:00
|
|
|
|
}
|
|
|
|
|
}
|