2012-02-06 01:47:31 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2012-02-06 01:47:31 +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-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.IO;
|
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2012-02-06 01:47:31 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
2012-02-07 10:03:49 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.CodeVeil {
|
2012-02-06 01:47:31 +08:00
|
|
|
|
class StringDecrypter {
|
2012-11-08 16:48:05 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-02-08 22:40:11 +08:00
|
|
|
|
MainType mainType;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
TypeDef decrypterType;
|
|
|
|
|
FieldDef stringDataField;
|
|
|
|
|
MethodDef initMethod;
|
|
|
|
|
MethodDef decrypterMethod;
|
2012-02-06 01:47:31 +08:00
|
|
|
|
string[] decryptedStrings;
|
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return decrypterType != null; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef Type {
|
2012-02-09 01:58:06 +08:00
|
|
|
|
get { return decrypterType; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef InitMethod {
|
2012-02-09 01:58:06 +08:00
|
|
|
|
get { return initMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef DecryptMethod {
|
2012-02-06 01:47:31 +08:00
|
|
|
|
get { return decrypterMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-08 16:48:05 +08:00
|
|
|
|
public StringDecrypter(ModuleDefMD module, MainType mainType) {
|
2012-02-06 01:47:31 +08:00
|
|
|
|
this.module = module;
|
2012-02-08 22:40:11 +08:00
|
|
|
|
this.mainType = mainType;
|
2012-02-06 01:47:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-08 16:48:05 +08:00
|
|
|
|
public StringDecrypter(ModuleDefMD module, MainType mainType, StringDecrypter oldOne) {
|
2012-02-06 01:47:31 +08:00
|
|
|
|
this.module = module;
|
2012-02-08 22:40:11 +08:00
|
|
|
|
this.mainType = mainType;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
this.decrypterType = Lookup(oldOne.decrypterType, "Could not find string decrypter type");
|
|
|
|
|
this.stringDataField = Lookup(oldOne.stringDataField, "Could not find string data field");
|
|
|
|
|
this.initMethod = Lookup(oldOne.initMethod, "Could not find string decrypter init method");
|
|
|
|
|
this.decrypterMethod = Lookup(oldOne.decrypterMethod, "Could not find string decrypter method");
|
2012-02-06 01:47:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
T Lookup<T>(T def, string errorMessage) where T : class, ICodedToken {
|
|
|
|
|
return DeobUtils.Lookup(module, def, errorMessage);
|
2012-02-06 01:47:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Find() {
|
|
|
|
|
var cctor = DotNetUtils.GetModuleTypeCctor(module);
|
2012-02-06 01:47:31 +08:00
|
|
|
|
if (cctor == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-02-07 22:17:41 +08:00
|
|
|
|
// V3-V4 calls string decrypter init method in <Module>::.cctor().
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (Find(cctor))
|
2012-02-07 22:17:41 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
FindV5(cctor);
|
2012-02-07 22:17:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool Find(MethodDef method) {
|
2012-02-07 22:17:41 +08:00
|
|
|
|
if (method == null || method.Body == null || !method.IsStatic)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var instrs = method.Body.Instructions;
|
2012-02-06 01:47:31 +08:00
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
|
|
|
|
var call = instrs[i];
|
|
|
|
|
if (call.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
var initMethodTmp = call.Operand as MethodDef;
|
2012-02-06 01:47:31 +08:00
|
|
|
|
if (initMethodTmp == null || initMethodTmp.Body == null || !initMethodTmp.IsStatic)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(initMethodTmp, "System.Void", "()"))
|
2012-02-06 01:47:31 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!CheckType(initMethodTmp.DeclaringType))
|
2012-02-06 01:47:31 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
decrypterType = initMethodTmp.DeclaringType;
|
|
|
|
|
initMethod = initMethodTmp;
|
2012-02-07 22:17:41 +08:00
|
|
|
|
return true;
|
2012-02-06 01:47:31 +08:00
|
|
|
|
}
|
2012-02-07 11:45:59 +08:00
|
|
|
|
|
2012-02-07 22:17:41 +08:00
|
|
|
|
return false;
|
2012-02-06 01:47:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-07 22:17:41 +08:00
|
|
|
|
// The main decrypter type calls the string decrypter init method inside its init method
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void FindV5(MethodDef method) {
|
2012-02-08 22:40:11 +08:00
|
|
|
|
if (!mainType.Detected)
|
|
|
|
|
return;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var calledMethod in DotNetUtils.GetCalledMethods(module, mainType.InitMethod)) {
|
|
|
|
|
if (Find(calledMethod))
|
2012-02-08 22:40:11 +08:00
|
|
|
|
return;
|
2012-02-07 10:03:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool CheckType(TypeDef type) {
|
2012-02-06 01:47:31 +08:00
|
|
|
|
if (!type.HasNestedTypes)
|
|
|
|
|
return false;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var stringDataFieldTmp = CheckFields(type);
|
2012-02-06 01:47:31 +08:00
|
|
|
|
if (stringDataFieldTmp == null)
|
|
|
|
|
return false;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var fieldType = DotNetUtils.GetType(module, stringDataFieldTmp.FieldSig.GetFieldType());
|
2012-02-06 01:47:31 +08:00
|
|
|
|
if (fieldType == null || type.NestedTypes.IndexOf(fieldType) < 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var decrypterMethodTmp = GetDecrypterMethod(type);
|
2012-02-06 01:47:31 +08:00
|
|
|
|
if (decrypterMethodTmp == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
stringDataField = stringDataFieldTmp;
|
|
|
|
|
decrypterMethod = decrypterMethodTmp;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static MethodDef GetDecrypterMethod(TypeDef type) {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
MethodDef foundMethod = null;
|
2012-02-06 01:47:31 +08:00
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (method.Body == null || !method.IsStatic)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(method, "System.String", "(System.Int32)"))
|
2012-02-06 01:47:31 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (foundMethod != null)
|
|
|
|
|
return null;
|
|
|
|
|
foundMethod = method;
|
|
|
|
|
}
|
|
|
|
|
return foundMethod;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static string[] requiredFields = new string[] {
|
|
|
|
|
"System.Byte[]",
|
|
|
|
|
"System.Int32",
|
|
|
|
|
"System.Int32[]",
|
|
|
|
|
"System.String[]",
|
|
|
|
|
"System.UInt32[]",
|
|
|
|
|
};
|
2013-01-19 20:03:57 +08:00
|
|
|
|
FieldDef CheckFields(TypeDef type) {
|
|
|
|
|
if (!new FieldTypes(type).All(requiredFields))
|
2012-02-06 01:47:31 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
FieldDef stringData = null;
|
2012-02-06 01:47:31 +08:00
|
|
|
|
foreach (var field in type.Fields) {
|
|
|
|
|
if (field.RVA != 0) {
|
|
|
|
|
if (stringData != null)
|
|
|
|
|
return null;
|
|
|
|
|
stringData = field;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stringData == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var data = stringData.InitialValue;
|
|
|
|
|
if (data == null || data.Length == 0 || data.Length % 4 != 0)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return stringData;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Initialize() {
|
2012-02-07 11:45:59 +08:00
|
|
|
|
if (initMethod == null || stringDataField == null)
|
2012-02-06 01:47:31 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var key = GetKey(initMethod);
|
2012-02-06 01:47:31 +08:00
|
|
|
|
if (key == null)
|
|
|
|
|
throw new ApplicationException("Could not find string decrypter key");
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
DecryptStrings(key);
|
2012-02-09 01:58:06 +08:00
|
|
|
|
|
2012-11-08 16:48:05 +08:00
|
|
|
|
stringDataField.FieldSig.Type = module.CorLibTypes.Byte;
|
2012-02-09 01:58:06 +08:00
|
|
|
|
stringDataField.InitialValue = new byte[1];
|
2012-11-17 18:45:24 +08:00
|
|
|
|
stringDataField.RVA = 0;
|
2012-02-06 01:47:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static uint[] GetKey(MethodDef method) {
|
2012-02-06 01:47:31 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 1; i++) {
|
|
|
|
|
var ldci4 = instrs[i];
|
2012-11-08 16:48:05 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-02-06 01:47:31 +08:00
|
|
|
|
continue;
|
2012-11-08 16:48:05 +08:00
|
|
|
|
if (ldci4.GetLdcI4Value() != 4)
|
2012-02-06 01:47:31 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (instrs[i + 1].OpCode.Code != Code.Newarr)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
i++;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var key = ArrayFinder.GetInitializedUInt32Array(4, method, ref i);
|
2012-02-06 01:56:05 +08:00
|
|
|
|
if (key == null)
|
2012-02-06 01:47:31 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void DecryptStrings(uint[] key) {
|
2012-02-06 01:47:31 +08:00
|
|
|
|
var data = stringDataField.InitialValue;
|
|
|
|
|
|
|
|
|
|
var encryptedData = new uint[data.Length / 4];
|
|
|
|
|
Buffer.BlockCopy(data, 0, encryptedData, 0, data.Length);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
DeobUtils.XxteaDecrypt(encryptedData, key);
|
2012-02-06 01:47:31 +08:00
|
|
|
|
var decryptedData = new byte[data.Length];
|
|
|
|
|
Buffer.BlockCopy(encryptedData, 0, decryptedData, 0, data.Length);
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var inflated = DeobUtils.Inflate(decryptedData, 0, decryptedData.Length, true);
|
2012-11-08 16:48:05 +08:00
|
|
|
|
var reader = MemoryImageStream.Create(inflated);
|
|
|
|
|
int deflatedLength = (int)reader.ReadCompressedUInt32();
|
|
|
|
|
int numStrings = (int)reader.ReadCompressedUInt32();
|
2012-02-06 01:47:31 +08:00
|
|
|
|
decryptedStrings = new string[numStrings];
|
|
|
|
|
var offsets = new int[numStrings];
|
|
|
|
|
for (int i = 0; i < numStrings; i++)
|
2012-11-08 16:48:05 +08:00
|
|
|
|
offsets[i] = (int)reader.ReadCompressedUInt32();
|
|
|
|
|
int startOffset = (int)reader.Position;
|
2012-02-06 01:47:31 +08:00
|
|
|
|
for (int i = 0; i < numStrings; i++) {
|
2012-11-08 16:48:05 +08:00
|
|
|
|
reader.Position = startOffset + offsets[i];
|
2012-02-06 01:47:31 +08:00
|
|
|
|
decryptedStrings[i] = reader.ReadString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public string Decrypt(int index) {
|
2012-02-06 01:47:31 +08:00
|
|
|
|
return decryptedStrings[index];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|