2011-10-27 01:49:25 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2011-10-27 01:49:25 +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.Text;
|
|
|
|
|
using System.Collections.Generic;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.PE;
|
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2011-10-27 01:49:25 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
2011-12-22 01:55:36 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 {
|
2011-10-27 01:49:25 +08:00
|
|
|
|
class StringDecrypter {
|
2012-11-18 01:57:36 +08:00
|
|
|
|
ModuleDefMD module;
|
2011-10-27 01:49:25 +08:00
|
|
|
|
EncryptedResource encryptedResource;
|
|
|
|
|
List<DecrypterInfo> decrypterInfos = new List<DecrypterInfo>();
|
2012-11-02 22:57:11 +08:00
|
|
|
|
MethodDef otherStringDecrypter;
|
2011-10-27 01:49:25 +08:00
|
|
|
|
byte[] decryptedData;
|
2012-11-21 18:14:20 +08:00
|
|
|
|
MyPEImage peImage;
|
2011-10-28 07:33:05 +08:00
|
|
|
|
byte[] fileData;
|
|
|
|
|
StringDecrypterVersion stringDecrypterVersion;
|
|
|
|
|
|
|
|
|
|
enum StringDecrypterVersion {
|
|
|
|
|
UNKNOWN = 0,
|
|
|
|
|
VER_37, // 3.7-
|
|
|
|
|
VER_38, // 3.8+
|
|
|
|
|
}
|
2011-10-27 01:49:25 +08:00
|
|
|
|
|
|
|
|
|
public class DecrypterInfo {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef method;
|
2011-10-27 01:49:25 +08:00
|
|
|
|
public byte[] key;
|
|
|
|
|
public byte[] iv;
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public DecrypterInfo(MethodDef method, byte[] key, byte[] iv) {
|
2011-10-27 01:49:25 +08:00
|
|
|
|
this.method = method;
|
|
|
|
|
this.key = key;
|
|
|
|
|
this.iv = iv;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Detected {
|
2011-11-06 22:24:30 +08:00
|
|
|
|
get { return encryptedResource.Method != null; }
|
2011-10-27 01:49:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef DecrypterType {
|
2011-11-09 04:27:03 +08:00
|
|
|
|
get { return encryptedResource.Type; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-06 22:24:30 +08:00
|
|
|
|
public EmbeddedResource Resource {
|
|
|
|
|
get { return encryptedResource.Resource; }
|
2011-10-27 05:00:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-27 01:49:25 +08:00
|
|
|
|
public IEnumerable<DecrypterInfo> DecrypterInfos {
|
|
|
|
|
get { return decrypterInfos; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef OtherStringDecrypter {
|
2011-10-28 04:25:44 +08:00
|
|
|
|
get { return otherStringDecrypter; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-18 01:57:36 +08:00
|
|
|
|
public StringDecrypter(ModuleDefMD module) {
|
2011-10-27 01:49:25 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
this.encryptedResource = new EncryptedResource(module);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-18 01:57:36 +08:00
|
|
|
|
public StringDecrypter(ModuleDefMD module, StringDecrypter oldOne) {
|
2011-10-27 01:49:25 +08:00
|
|
|
|
this.module = module;
|
2011-10-28 07:33:05 +08:00
|
|
|
|
this.stringDecrypterVersion = oldOne.stringDecrypterVersion;
|
2011-10-27 01:49:25 +08:00
|
|
|
|
this.encryptedResource = new EncryptedResource(module, oldOne.encryptedResource);
|
|
|
|
|
foreach (var oldInfo in oldOne.decrypterInfos) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var method = Lookup(oldInfo.method, "Could not find string decrypter method");
|
2011-10-27 01:49:25 +08:00
|
|
|
|
decrypterInfos.Add(new DecrypterInfo(method, oldInfo.key, oldInfo.iv));
|
|
|
|
|
}
|
2013-01-19 20:03:57 +08:00
|
|
|
|
otherStringDecrypter = Lookup(oldOne.otherStringDecrypter, "Could not find string decrypter method");
|
2011-11-06 19:18:35 +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);
|
2011-10-27 01:49:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Find(ISimpleDeobfuscator simpleDeobfuscator) {
|
2011-10-27 01:49:25 +08:00
|
|
|
|
var additionalTypes = new string[] {
|
|
|
|
|
"System.String",
|
|
|
|
|
};
|
|
|
|
|
EmbeddedResource stringsResource = null;
|
|
|
|
|
foreach (var type in module.Types) {
|
2011-10-28 04:25:44 +08:00
|
|
|
|
if (decrypterInfos.Count > 0)
|
|
|
|
|
break;
|
2011-10-27 01:49:25 +08:00
|
|
|
|
if (type.BaseType == null || type.BaseType.FullName != "System.Object")
|
|
|
|
|
continue;
|
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (!method.IsStatic || !method.HasBody)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(method, "System.String", "(System.Int32)"))
|
2011-10-27 01:49:25 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!encryptedResource.CouldBeResourceDecrypter(method, additionalTypes))
|
2011-10-27 01:49:25 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var resource = DotNetUtils.GetResource(module, DotNetUtils.GetCodeStrings(method)) as EmbeddedResource;
|
2011-10-27 01:49:25 +08:00
|
|
|
|
if (resource == null)
|
|
|
|
|
throw new ApplicationException("Could not find strings resource");
|
|
|
|
|
if (stringsResource != null && stringsResource != resource)
|
|
|
|
|
throw new ApplicationException("Two different string resources found");
|
|
|
|
|
|
|
|
|
|
stringsResource = resource;
|
2011-11-06 22:24:30 +08:00
|
|
|
|
encryptedResource.Method = method;
|
2011-10-30 02:28:29 +08:00
|
|
|
|
|
|
|
|
|
var info = new DecrypterInfo(method, null, null);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
simpleDeobfuscator.Deobfuscate(info.method);
|
|
|
|
|
FindKeyIv(info.method, out info.key, out info.iv);
|
2011-10-30 02:28:29 +08:00
|
|
|
|
|
|
|
|
|
decrypterInfos.Add(info);
|
2011-10-27 01:49:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-10-28 04:25:44 +08:00
|
|
|
|
|
|
|
|
|
if (decrypterInfos.Count > 0)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
FindOtherStringDecrypter(decrypterInfos[0].method.DeclaringType);
|
2011-10-28 04:25:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void FindOtherStringDecrypter(TypeDef type) {
|
2011-10-28 04:25:44 +08:00
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (!method.IsStatic || !method.HasBody)
|
|
|
|
|
continue;
|
2012-11-18 01:57:36 +08:00
|
|
|
|
var sig = method.MethodSig;
|
|
|
|
|
if (sig == null)
|
2011-10-28 04:25:44 +08:00
|
|
|
|
continue;
|
2012-11-18 01:57:36 +08:00
|
|
|
|
if (sig.RetType.GetElementType() != ElementType.String)
|
2011-10-28 04:25:44 +08:00
|
|
|
|
continue;
|
2012-11-18 01:57:36 +08:00
|
|
|
|
if (sig.Params.Count != 1)
|
|
|
|
|
continue;
|
|
|
|
|
if (sig.Params[0].GetElementType() != ElementType.Object &&
|
|
|
|
|
sig.Params[0].GetElementType() != ElementType.String)
|
2011-10-28 04:25:44 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
otherStringDecrypter = method;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-10-27 01:49:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Initialize(MyPEImage peImage, byte[] fileData, ISimpleDeobfuscator simpleDeobfuscator) {
|
2011-11-06 22:24:30 +08:00
|
|
|
|
if (encryptedResource.Method == null)
|
2011-10-27 01:49:25 +08:00
|
|
|
|
return;
|
|
|
|
|
this.peImage = peImage;
|
2011-10-28 07:33:05 +08:00
|
|
|
|
this.fileData = fileData;
|
2011-10-27 01:49:25 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
encryptedResource.Initialize(simpleDeobfuscator);
|
2011-11-09 04:27:03 +08:00
|
|
|
|
if (!encryptedResource.FoundResource)
|
|
|
|
|
return;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
Logger.v("Adding string decrypter. Resource: {0}", Utils.ToCsharpString(encryptedResource.Resource.Name));
|
|
|
|
|
decryptedData = encryptedResource.Decrypt();
|
2011-10-27 01:49:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void FindKeyIv(MethodDef method, out byte[] key, out byte[] iv) {
|
2011-10-27 01:49:25 +08:00
|
|
|
|
key = null;
|
|
|
|
|
iv = null;
|
|
|
|
|
|
|
|
|
|
var requiredTypes = new string[] {
|
|
|
|
|
"System.Byte[]",
|
|
|
|
|
"System.IO.MemoryStream",
|
|
|
|
|
"System.Security.Cryptography.CryptoStream",
|
|
|
|
|
"System.Security.Cryptography.Rijndael",
|
|
|
|
|
};
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var calledMethod in DotNetUtils.GetCalledMethods(module, method)) {
|
2011-10-27 01:49:25 +08:00
|
|
|
|
if (calledMethod.DeclaringType != method.DeclaringType)
|
|
|
|
|
continue;
|
2012-11-18 01:57:36 +08:00
|
|
|
|
if (calledMethod.MethodSig.GetRetType().GetFullName() != "System.Byte[]")
|
2011-10-27 08:08:30 +08:00
|
|
|
|
continue;
|
2011-10-27 01:49:25 +08:00
|
|
|
|
var localTypes = new LocalTypes(calledMethod);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!localTypes.All(requiredTypes))
|
2011-10-27 01:49:25 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var instructions = calledMethod.Body.Instructions;
|
|
|
|
|
byte[] newKey = null, newIv = null;
|
|
|
|
|
for (int i = 0; i < instructions.Count && (newKey == null || newIv == null); i++) {
|
|
|
|
|
var instr = instructions[i];
|
|
|
|
|
if (instr.OpCode.Code != Code.Ldtoken)
|
|
|
|
|
continue;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
var field = instr.Operand as FieldDef;
|
2011-10-27 01:49:25 +08:00
|
|
|
|
if (field == null)
|
|
|
|
|
continue;
|
|
|
|
|
if (field.InitialValue == null)
|
|
|
|
|
continue;
|
|
|
|
|
if (field.InitialValue.Length == 32)
|
|
|
|
|
newKey = field.InitialValue;
|
|
|
|
|
else if (field.InitialValue.Length == 16)
|
|
|
|
|
newIv = field.InitialValue;
|
|
|
|
|
}
|
|
|
|
|
if (newKey == null || newIv == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
InitializeStringDecrypterVersion(method);
|
2011-10-27 01:49:25 +08:00
|
|
|
|
key = newKey;
|
|
|
|
|
iv = newIv;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void InitializeStringDecrypterVersion(MethodDef method) {
|
2011-10-28 07:33:05 +08:00
|
|
|
|
var localTypes = new LocalTypes(method);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (localTypes.Exists("System.IntPtr"))
|
2011-10-28 07:33:05 +08:00
|
|
|
|
stringDecrypterVersion = StringDecrypterVersion.VER_38;
|
|
|
|
|
else
|
|
|
|
|
stringDecrypterVersion = StringDecrypterVersion.VER_37;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
DecrypterInfo GetDecrypterInfo(MethodDef method) {
|
2011-10-27 01:49:25 +08:00
|
|
|
|
foreach (var info in decrypterInfos) {
|
|
|
|
|
if (info.method == method)
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
throw new ApplicationException("Invalid string decrypter method");
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public string Decrypt(MethodDef method, int offset) {
|
|
|
|
|
var info = GetDecrypterInfo(method);
|
2011-10-27 01:49:25 +08:00
|
|
|
|
|
|
|
|
|
if (info.key == null) {
|
|
|
|
|
int length = BitConverter.ToInt32(decryptedData, offset);
|
|
|
|
|
return Encoding.Unicode.GetString(decryptedData, offset + 4, length);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2011-10-28 07:33:05 +08:00
|
|
|
|
byte[] encryptedStringData;
|
|
|
|
|
if (stringDecrypterVersion == StringDecrypterVersion.VER_37) {
|
|
|
|
|
int fileOffset = BitConverter.ToInt32(decryptedData, offset);
|
|
|
|
|
int length = BitConverter.ToInt32(fileData, fileOffset);
|
|
|
|
|
encryptedStringData = new byte[length];
|
|
|
|
|
Array.Copy(fileData, fileOffset + 4, encryptedStringData, 0, length);
|
|
|
|
|
}
|
|
|
|
|
else if (stringDecrypterVersion == StringDecrypterVersion.VER_38) {
|
|
|
|
|
uint rva = BitConverter.ToUInt32(decryptedData, offset);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
int length = peImage.ReadInt32(rva);
|
|
|
|
|
encryptedStringData = peImage.ReadBytes(rva + 4, length);
|
2011-10-28 07:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw new ApplicationException("Unknown string decrypter version");
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
return Encoding.Unicode.GetString(DeobUtils.AesDecrypt(encryptedStringData, info.key, info.iv));
|
2011-10-27 01:49:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-10-28 04:25:44 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public string Decrypt(string s) {
|
2011-10-28 07:33:05 +08:00
|
|
|
|
return Encoding.Unicode.GetString(Convert.FromBase64String(s));
|
2011-10-28 04:25:44 +08:00
|
|
|
|
}
|
2011-10-27 01:49:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|