de4dot-cex/de4dot.code/deobfuscators/CodeVeil/StringDecrypter.cs

249 lines
6.8 KiB
C#
Raw Normal View History

2012-02-06 01:47:31 +08:00
/*
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;
using System.IO;
2012-11-08 16:48:05 +08:00
using dot10.IO;
using dot10.DotNet;
using dot10.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;
MainType mainType;
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; }
}
public TypeDef Type {
2012-02-09 01:58:06 +08:00
get { return decrypterType; }
}
public MethodDef InitMethod {
2012-02-09 01:58:06 +08:00
get { return initMethod; }
}
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;
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;
this.mainType = mainType;
2012-02-06 01:47:31 +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-11-07 11:45:05 +08:00
T lookup<T>(T def, string errorMessage) where T : class, ICodedToken {
2012-02-06 01:47:31 +08:00
return DeobUtils.lookup(module, def, errorMessage);
}
public void find() {
var cctor = DotNetUtils.getModuleTypeCctor(module);
if (cctor == null)
return;
// V3-V4 calls string decrypter init method in <Module>::.cctor().
if (find(cctor))
return;
2012-02-13 18:26:20 +08:00
findV5(cctor);
}
bool find(MethodDef method) {
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;
var initMethodTmp = call.Operand as MethodDef;
2012-02-06 01:47:31 +08:00
if (initMethodTmp == null || initMethodTmp.Body == null || !initMethodTmp.IsStatic)
continue;
if (!DotNetUtils.isMethod(initMethodTmp, "System.Void", "()"))
continue;
if (!checkType(initMethodTmp.DeclaringType))
continue;
decrypterType = initMethodTmp.DeclaringType;
initMethod = initMethodTmp;
return true;
2012-02-06 01:47:31 +08:00
}
2012-02-07 11:45:59 +08:00
return false;
2012-02-06 01:47:31 +08:00
}
// The main decrypter type calls the string decrypter init method inside its init method
void findV5(MethodDef method) {
if (!mainType.Detected)
return;
2012-03-18 03:36:41 +08:00
foreach (var calledMethod in DotNetUtils.getCalledMethods(module, mainType.InitMethod)) {
if (find(calledMethod))
return;
2012-02-07 10:03:49 +08:00
}
}
bool checkType(TypeDef type) {
2012-02-06 01:47:31 +08:00
if (!type.HasNestedTypes)
return false;
var stringDataFieldTmp = checkFields(type);
if (stringDataFieldTmp == null)
return false;
2012-11-08 16:48:05 +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;
var decrypterMethodTmp = getDecrypterMethod(type);
if (decrypterMethodTmp == null)
return false;
stringDataField = stringDataFieldTmp;
decrypterMethod = decrypterMethodTmp;
return true;
}
static MethodDef getDecrypterMethod(TypeDef type) {
MethodDef foundMethod = null;
2012-02-06 01:47:31 +08:00
foreach (var method in type.Methods) {
if (method.Body == null || !method.IsStatic)
continue;
if (!DotNetUtils.isMethod(method, "System.String", "(System.Int32)"))
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[]",
};
FieldDef checkFields(TypeDef type) {
2012-02-06 01:47:31 +08:00
if (!new FieldTypes(type).all(requiredFields))
return null;
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;
}
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;
var key = getKey(initMethod);
if (key == null)
throw new ApplicationException("Could not find string decrypter key");
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];
stringDataField.RVA = 0;
2012-02-06 01:47:31 +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++;
var key = ArrayFinder.getInitializedUInt32Array(4, method, ref i);
if (key == null)
2012-02-06 01:47:31 +08:00
continue;
return key;
}
return null;
}
void decryptStrings(uint[] key) {
var data = stringDataField.InitialValue;
var encryptedData = new uint[data.Length / 4];
Buffer.BlockCopy(data, 0, encryptedData, 0, data.Length);
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);
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();
}
}
public string decrypt(int index) {
return decryptedStrings[index];
}
}
}