de4dot-cex/de4dot.code/deobfuscators/DeepSea/AssemblyResolver.cs

521 lines
14 KiB
C#
Raw Normal View History

2012-01-23 02:58:31 +08:00
/*
2014-03-12 05:15:43 +08:00
Copyright (C) 2011-2014 de4dot@gmail.com
2012-01-23 02:58: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.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2012-01-24 12:08:24 +08:00
using de4dot.blocks;
2012-01-23 02:58:31 +08:00
namespace de4dot.code.deobfuscators.DeepSea {
class AssemblyResolver : ResolverBase {
2012-03-17 21:05:54 +08:00
Version version;
2012-01-24 12:08:24 +08:00
List<FieldInfo> fieldInfos;
MethodDef decryptMethod;
2012-01-24 12:08:24 +08:00
2012-03-17 21:05:54 +08:00
enum Version {
Unknown,
V3Old,
V3,
V4,
2012-05-10 04:24:39 +08:00
V404,
V41,
2012-05-12 01:38:31 +08:00
V41SL,
2012-03-17 21:05:54 +08:00
}
2012-01-23 02:58:31 +08:00
public class AssemblyInfo {
public byte[] data;
public string fullName;
public string simpleName;
public string extension;
public EmbeddedResource resource;
public AssemblyInfo(byte[] data, string fullName, string simpleName, string extension, EmbeddedResource resource) {
this.data = data;
this.fullName = fullName;
this.simpleName = simpleName;
this.extension = extension;
this.resource = resource;
}
2012-01-24 12:08:24 +08:00
public override string ToString() {
return fullName;
}
}
class FieldInfo {
public FieldDef field;
2012-01-24 12:08:24 +08:00
public int magic;
public FieldInfo(FieldDef field, int magic) {
2012-01-24 12:08:24 +08:00
this.field = field;
this.magic = magic;
}
2012-01-23 02:58:31 +08:00
}
public MethodDef DecryptMethod {
get { return decryptMethod; }
}
2012-11-09 07:21:45 +08:00
public AssemblyResolver(ModuleDefMD module, ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob)
2012-01-24 10:22:59 +08:00
: base(module, simpleDeobfuscator, deob) {
2012-01-23 02:58:31 +08:00
}
2012-03-17 21:50:56 +08:00
static string[] requiredLocals_sl = new string[] {
"System.Byte[]",
"System.IO.Stream",
"System.Reflection.Assembly",
"System.Security.Cryptography.SHA1Managed",
"System.Windows.AssemblyPart",
};
2013-01-19 20:03:57 +08:00
protected override bool CheckResolverInitMethodSilverlight(MethodDef resolverInitMethod) {
2012-03-17 21:50:56 +08:00
if (resolverInitMethod.Body.ExceptionHandlers.Count != 1)
return false;
2013-01-19 20:03:57 +08:00
foreach (var method in DotNetUtils.GetCalledMethods(module, resolverInitMethod)) {
2012-03-17 21:50:56 +08:00
if (!method.IsStatic || method.Body == null)
continue;
if (!method.IsPublic || method.HasGenericParameters)
continue;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(method, "System.Void", "(System.String)"))
2012-03-17 21:50:56 +08:00
continue;
2013-01-19 20:03:57 +08:00
simpleDeobfuscator.Deobfuscate(method);
if (!new LocalTypes(method).All(requiredLocals_sl))
2012-03-17 21:50:56 +08:00
continue;
initMethod = resolverInitMethod;
resolveHandler = method;
2013-01-19 20:03:57 +08:00
UpdateVersion(resolveHandler);
2012-03-17 21:50:56 +08:00
return true;
}
return false;
}
2013-01-19 20:03:57 +08:00
void UpdateVersion(MethodDef handler) {
if (IsV3Old(handler)) {
2012-03-17 21:50:56 +08:00
version = Version.V3Old;
2012-05-12 01:38:31 +08:00
return;
}
2013-01-19 20:03:57 +08:00
if (IsV3SL(handler)) {
2012-05-12 01:38:31 +08:00
version = Version.V3; // 3.x-4.0.4
return;
}
2013-01-19 20:03:57 +08:00
if (IsV41SL(handler)) {
2012-05-12 01:38:31 +08:00
version = Version.V41SL;
return;
}
}
2013-01-19 20:03:57 +08:00
static bool IsV3SL(MethodDef handler) {
2012-05-12 01:38:31 +08:00
var instrs = handler.Body.Instructions;
for (int i = 0; i < instrs.Count - 3; i++) {
2012-11-09 07:21:45 +08:00
if (!instrs[i].IsLdloc())
2012-05-12 01:38:31 +08:00
continue;
if (instrs[i + 1].OpCode.Code != Code.Add)
continue;
2012-11-09 07:21:45 +08:00
if (!instrs[i + 2].IsLdcI4())
2012-05-12 01:38:31 +08:00
continue;
if (instrs[i + 3].OpCode.Code != Code.And)
continue;
return true;
}
return false;
}
2013-01-19 20:03:57 +08:00
static bool IsV41SL(MethodDef handler) {
2012-05-12 01:38:31 +08:00
var instrs = handler.Body.Instructions;
for (int i = 0; i < instrs.Count; i++) {
2012-11-09 07:21:45 +08:00
if (!instrs[i].IsLdcI4() || instrs[i].GetLdcI4Value() != 5)
2012-05-12 01:38:31 +08:00
continue;
if (instrs[i + 1].OpCode.Code != Code.And)
continue;
2012-11-09 07:21:45 +08:00
if (!instrs[i + 2].IsLdcI4() || instrs[i + 2].GetLdcI4Value() != 0x1F)
2012-05-12 01:38:31 +08:00
continue;
if (instrs[i + 3].OpCode.Code != Code.And)
continue;
return true;
}
return false;
2012-03-17 21:50:56 +08:00
}
2013-01-19 20:03:57 +08:00
static bool IsV3Old(MethodDef method) {
return DotNetUtils.CallsMethod(method, "System.Int32 System.IO.Stream::Read(System.Byte[],System.Int32,System.Int32)") &&
!DotNetUtils.CallsMethod(method, "System.Int32 System.IO.Stream::ReadByte()") &&
2012-03-17 21:50:56 +08:00
// Obfuscated System.Int32 System.IO.Stream::ReadByte()
2013-01-19 20:03:57 +08:00
!DotNetUtils.CallsMethod(method, "System.Int32", "(System.IO.Stream,System.Int32,System.Int32)");
2012-03-17 21:50:56 +08:00
}
2013-01-19 20:03:57 +08:00
protected override bool CheckResolverInitMethodInternal(MethodDef resolverInitMethod) {
return DotNetUtils.CallsMethod(resolverInitMethod, "System.Void System.AppDomain::add_AssemblyResolve(System.ResolveEventHandler)");
}
2013-01-19 20:03:57 +08:00
protected override bool CheckHandlerMethodDesktopInternal(MethodDef handler) {
if (CheckHandlerV3(handler) || checkHandlerSL(handler)) {
UpdateVersion(handler);
2012-01-24 12:08:24 +08:00
return true;
}
2013-01-19 20:03:57 +08:00
simpleDeobfuscator.Deobfuscate(handler);
2012-01-24 12:08:24 +08:00
List<FieldInfo> fieldInfosTmp;
MethodDef decryptMethodTmp;
2013-01-19 20:03:57 +08:00
if (CheckHandlerV4(handler, out fieldInfosTmp, out decryptMethodTmp)) {
2012-03-17 21:05:54 +08:00
version = Version.V4;
2012-01-24 12:08:24 +08:00
fieldInfos = fieldInfosTmp;
decryptMethod = decryptMethodTmp;
2012-01-24 12:08:24 +08:00
return true;
}
2013-01-19 20:03:57 +08:00
Version versionTmp = CheckHandlerV404_41(handler, out fieldInfosTmp, out decryptMethodTmp);
2012-05-10 04:24:39 +08:00
if (fieldInfosTmp.Count != 0) {
version = versionTmp;
fieldInfos = fieldInfosTmp;
decryptMethod = decryptMethodTmp;
2012-05-10 04:24:39 +08:00
return true;
}
2012-01-24 12:08:24 +08:00
return false;
}
static string[] handlerLocalTypes_NET = new string[] {
2012-01-23 02:58:31 +08:00
"System.Byte[]",
"System.IO.Compression.DeflateStream",
"System.IO.MemoryStream",
"System.IO.Stream",
"System.Reflection.Assembly",
"System.Security.Cryptography.SHA1CryptoServiceProvider",
2012-01-23 02:58:31 +08:00
"System.String",
};
2013-01-19 20:03:57 +08:00
static bool CheckHandlerV3(MethodDef handler) {
return new LocalTypes(handler).All(handlerLocalTypes_NET);
}
static string[] handlerLocalTypes_SL = new string[] {
"System.Byte[]",
"System.IO.Stream",
"System.Reflection.Assembly",
"System.Security.Cryptography.SHA1Managed",
"System.String",
"System.Windows.AssemblyPart",
};
static bool checkHandlerSL(MethodDef handler) {
2013-01-19 20:03:57 +08:00
return new LocalTypes(handler).All(handlerLocalTypes_SL);
2012-01-23 02:58:31 +08:00
}
// 4.0.1.18 .. 4.0.3
2013-01-19 20:03:57 +08:00
bool CheckHandlerV4(MethodDef handler, out List<FieldInfo> fieldInfos, out MethodDef decryptMethod) {
2012-01-24 12:08:24 +08:00
fieldInfos = new List<FieldInfo>();
decryptMethod = null;
2012-01-24 12:08:24 +08:00
var instrs = handler.Body.Instructions;
for (int i = 0; i < instrs.Count - 3; i++) {
int index = i;
var ldtoken = instrs[index++];
2012-01-24 12:08:24 +08:00
if (ldtoken.OpCode.Code != Code.Ldtoken)
continue;
var field = ldtoken.Operand as FieldDef;
2012-01-24 12:08:24 +08:00
if (field == null || field.InitialValue == null || field.InitialValue.Length == 0)
return false;
var ldci4_len = instrs[index++];
2012-11-09 07:21:45 +08:00
if (!ldci4_len.IsLdcI4())
2012-01-24 12:08:24 +08:00
return false;
2012-11-09 07:21:45 +08:00
if (ldci4_len.GetLdcI4Value() != field.InitialValue.Length)
2012-01-24 12:08:24 +08:00
return false;
var ldci4_magic = instrs[index++];
2012-11-09 07:21:45 +08:00
if (!ldci4_magic.IsLdcI4())
2012-01-24 12:08:24 +08:00
return false;
2012-11-09 07:21:45 +08:00
int magic = ldci4_magic.GetLdcI4Value();
2012-01-24 12:08:24 +08:00
var call = instrs[index++];
2012-11-09 07:21:45 +08:00
if (call.OpCode.Code == Code.Tailcall)
call = instrs[index++];
if (call.OpCode.Code != Code.Call)
return false;
var decryptMethodTmp = call.Operand as MethodDef;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(decryptMethodTmp, "System.Reflection.Assembly", "(System.RuntimeFieldHandle,System.Int32,System.Int32)"))
return false;
decryptMethod = decryptMethodTmp;
fieldInfos.Add(new FieldInfo(field, magic));
}
return fieldInfos.Count != 0;
}
2012-05-10 04:24:39 +08:00
// 4.0.4, 4.1+
2013-01-19 20:03:57 +08:00
Version CheckHandlerV404_41(MethodDef handler, out List<FieldInfo> fieldInfos, out MethodDef decryptMethod) {
2012-05-10 04:24:39 +08:00
Version version = Version.Unknown;
fieldInfos = new List<FieldInfo>();
decryptMethod = null;
var instrs = handler.Body.Instructions;
2012-05-10 04:24:39 +08:00
for (int i = 0; i < instrs.Count - 6; i++) {
int index = i;
var ldci4_len = instrs[index++];
2012-11-09 07:21:45 +08:00
if (!ldci4_len.IsLdcI4())
continue;
if (instrs[index++].OpCode.Code != Code.Newarr)
continue;
2012-11-09 07:21:45 +08:00
if (!instrs[index++].IsStloc())
continue;
2012-11-09 07:21:45 +08:00
if (!instrs[index++].IsLdloc())
continue;
var ldtoken = instrs[index++];
if (ldtoken.OpCode.Code != Code.Ldtoken)
continue;
var field = ldtoken.Operand as FieldDef;
if (field == null || field.InitialValue == null || field.InitialValue.Length == 0)
continue;
var call1 = instrs[index++];
if (call1.OpCode.Code != Code.Call)
continue;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(call1.Operand as IMethod, "System.Void", "(System.Array,System.RuntimeFieldHandle)"))
continue;
2013-01-19 20:03:57 +08:00
int callIndex = GetCallDecryptMethodIndex(instrs, index);
2012-05-10 04:24:39 +08:00
if (callIndex < 0)
continue;
2013-01-19 20:03:57 +08:00
var args = DsUtils.GetArgValues(instrs, callIndex);
2012-05-10 04:24:39 +08:00
if (args == null)
continue;
var decryptMethodTmp = instrs[callIndex].Operand as MethodDef;
if (decryptMethodTmp == null)
2012-05-10 04:24:39 +08:00
continue;
int magic;
Version versionTmp;
2013-01-19 20:03:57 +08:00
GetMagic(decryptMethodTmp, args, out versionTmp, out magic);
2012-05-10 04:24:39 +08:00
version = versionTmp;
decryptMethod = decryptMethodTmp;
2012-05-10 04:24:39 +08:00
fieldInfos.Add(new FieldInfo(field, magic));
}
return version;
}
2013-01-19 20:03:57 +08:00
static bool GetMagic(MethodDef method, IList<object> args, out Version version, out int magic) {
2012-05-10 04:24:39 +08:00
magic = 0;
2013-01-19 20:03:57 +08:00
int magicIndex = GetMagicIndex(method, out version);
2012-05-10 04:24:39 +08:00
if (magicIndex < 0 || magicIndex >= args.Count)
return false;
var val = args[magicIndex];
if (!(val is int))
return false;
magic = (int)val;
return true;
}
2013-01-19 20:03:57 +08:00
static int GetMagicIndex(MethodDef method, out Version version) {
int magicIndex = GetMagicIndex404(method);
2012-05-10 04:24:39 +08:00
if (magicIndex >= 0) {
version = Version.V404;
return magicIndex;
}
2013-01-19 20:03:57 +08:00
magicIndex = GetMagicIndex41Trial(method);
2012-05-10 04:24:39 +08:00
if (magicIndex >= 0) {
version = Version.V41;
return magicIndex;
}
version = Version.Unknown;
return -1;
}
2013-01-19 20:03:57 +08:00
static int GetMagicIndex404(MethodDef method) {
2012-05-10 04:24:39 +08:00
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 4; i++) {
int index = i;
2012-11-09 07:21:45 +08:00
if (!instrs[index++].IsLdloc())
continue;
2012-05-10 04:24:39 +08:00
var ldarg = instrs[index++];
2012-11-09 07:21:45 +08:00
if (!ldarg.IsLdarg())
2012-05-10 04:24:39 +08:00
continue;
if (instrs[index++].OpCode.Code != Code.Add)
continue;
var ldci4 = instrs[index++];
2012-11-09 07:21:45 +08:00
if (!ldci4.IsLdcI4())
2012-05-10 04:24:39 +08:00
continue;
2012-11-09 07:21:45 +08:00
if (ldci4.GetLdcI4Value() != 0xFF)
2012-05-10 04:24:39 +08:00
continue;
2012-11-09 07:21:45 +08:00
return ldarg.GetParameterIndex();
2012-05-10 04:24:39 +08:00
}
return -1;
}
2013-01-19 20:03:57 +08:00
static int GetMagicIndex41Trial(MethodDef method) {
2012-05-10 04:24:39 +08:00
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 4; i++) {
int index = i;
if (instrs[index++].OpCode.Code != Code.Div)
continue;
2012-05-10 04:24:39 +08:00
var ldarg = instrs[index++];
2012-11-09 07:21:45 +08:00
if (!ldarg.IsLdarg())
2012-05-10 04:24:39 +08:00
continue;
if (instrs[index++].OpCode.Code != Code.Add)
continue;
var ldci4 = instrs[index++];
2012-11-09 07:21:45 +08:00
if (!ldci4.IsLdcI4())
2012-05-10 04:24:39 +08:00
continue;
2012-11-09 07:21:45 +08:00
if (ldci4.GetLdcI4Value() != 0xFF)
2012-05-10 04:24:39 +08:00
continue;
2012-11-09 07:21:45 +08:00
return ldarg.GetParameterIndex();
2012-05-10 04:24:39 +08:00
}
return -1;
}
2013-01-19 20:03:57 +08:00
static int GetCallDecryptMethodIndex(IList<Instruction> instrs, int index) {
index = GetRetIndex(instrs, index);
2012-05-10 04:24:39 +08:00
if (index < 0)
return -1;
for (int i = index - 1; i >= 0; i--) {
var instr = instrs[i];
2013-01-19 20:03:57 +08:00
if (!IsCallOrNext(instr))
2012-05-10 04:24:39 +08:00
break;
if (instr.OpCode.Code != Code.Call)
continue;
2012-11-09 07:21:45 +08:00
var calledMethod = instr.Operand as IMethod;
if (calledMethod == null || calledMethod.MethodSig.GetParamCount() < 2)
continue;
2012-05-10 04:24:39 +08:00
return i;
2012-01-24 12:08:24 +08:00
}
2012-05-10 04:24:39 +08:00
return -1;
}
2012-01-24 12:08:24 +08:00
2013-01-19 20:03:57 +08:00
static int GetRetIndex(IList<Instruction> instrs, int index) {
2012-05-10 04:24:39 +08:00
for (int i = index; i < instrs.Count; i++) {
var instr = instrs[i];
if (instr.OpCode.Code == Code.Ret)
return i;
2013-01-19 20:03:57 +08:00
if (!IsCallOrNext(instr))
2012-05-10 04:24:39 +08:00
break;
}
return -1;
}
2013-01-19 20:03:57 +08:00
static bool IsCallOrNext(Instruction instr) {
2012-05-10 04:24:39 +08:00
switch (instr.OpCode.FlowControl) {
case FlowControl.Call:
case FlowControl.Next:
return true;
default:
return false;
}
2012-01-24 12:08:24 +08:00
}
2013-01-19 20:03:57 +08:00
public IEnumerable<AssemblyInfo> GetAssemblyInfos() {
2012-03-17 21:05:54 +08:00
if (!Detected)
return new List<AssemblyInfo>();
switch (version) {
case Version.V3Old:
2013-01-19 20:03:57 +08:00
return GetAssemblyInfos(resource => DecryptResourceV3Old(resource));
2012-03-17 21:05:54 +08:00
case Version.V3:
2013-01-19 20:03:57 +08:00
return GetAssemblyInfos(resource => DecryptResourceV3(resource));
2012-03-17 21:05:54 +08:00
case Version.V4:
2012-05-10 04:24:39 +08:00
case Version.V404:
2013-01-19 20:03:57 +08:00
return GetAssemblyInfosV4();
2012-05-10 04:24:39 +08:00
case Version.V41:
2013-01-19 20:03:57 +08:00
return GetAssemblyInfosV41();
2012-05-12 01:38:31 +08:00
case Version.V41SL:
2013-01-19 20:03:57 +08:00
return GetAssemblyInfos(resource => DecryptResourceV41SL(resource));
2012-03-17 21:05:54 +08:00
default:
throw new ApplicationException("Unknown version");
}
2012-01-24 12:08:24 +08:00
}
2013-01-19 20:03:57 +08:00
IEnumerable<AssemblyInfo> GetAssemblyInfos(Func<EmbeddedResource, byte[]> decrypter) {
2012-01-23 02:58:31 +08:00
var infos = new List<AssemblyInfo>();
foreach (var tmp in module.Resources) {
var resource = tmp as EmbeddedResource;
if (resource == null)
continue;
2012-11-09 07:21:45 +08:00
if (!Regex.IsMatch(resource.Name.String, @"^[0-9A-F]{40}$"))
2012-01-23 02:58:31 +08:00
continue;
2013-01-19 20:03:57 +08:00
var info = GetAssemblyInfo(resource, decrypter);
2012-01-23 02:58:31 +08:00
if (info == null)
continue;
infos.Add(info);
}
return infos;
}
2013-01-19 20:03:57 +08:00
AssemblyInfo GetAssemblyInfo(EmbeddedResource resource, Func<EmbeddedResource, byte[]> decrypter) {
2012-01-23 02:58:31 +08:00
try {
2012-05-12 01:38:31 +08:00
var decrypted = decrypter(resource);
2013-01-19 20:03:57 +08:00
return GetAssemblyInfo(decrypted, resource);
2012-01-23 02:58:31 +08:00
}
catch (Exception) {
return null;
}
}
2012-01-24 12:08:24 +08:00
2013-01-19 20:03:57 +08:00
AssemblyInfo GetAssemblyInfo(byte[] decryptedData, EmbeddedResource resource) {
2012-11-09 07:21:45 +08:00
var asm = AssemblyDef.Load(decryptedData);
var fullName = asm.FullName;
var simpleName = asm.Name.String;
2013-01-19 20:03:57 +08:00
var extension = DeobUtils.GetExtension(asm.Modules[0].Kind);
2012-01-24 12:08:24 +08:00
return new AssemblyInfo(decryptedData, fullName, simpleName, extension, resource);
}
2013-01-19 20:03:57 +08:00
IEnumerable<AssemblyInfo> GetAssemblyInfos(Func<byte[], int, byte[]> decrypter) {
2012-01-24 12:08:24 +08:00
var infos = new List<AssemblyInfo>();
if (fieldInfos == null)
return infos;
foreach (var fieldInfo in fieldInfos) {
2012-05-10 04:24:39 +08:00
var decrypted = decrypter(fieldInfo.field.InitialValue, fieldInfo.magic);
2013-01-19 20:03:57 +08:00
infos.Add(GetAssemblyInfo(decrypted, null));
fieldInfo.field.InitialValue = new byte[1];
2012-11-09 07:21:45 +08:00
fieldInfo.field.FieldSig.Type = module.CorLibTypes.Byte;
fieldInfo.field.RVA = 0;
2012-01-24 12:08:24 +08:00
}
return infos;
}
2012-05-10 04:24:39 +08:00
2013-01-19 20:03:57 +08:00
IEnumerable<AssemblyInfo> GetAssemblyInfosV4() {
return GetAssemblyInfos((data, magic) => DecryptResourceV4(data, magic));
2012-05-10 04:24:39 +08:00
}
2013-01-19 20:03:57 +08:00
IEnumerable<AssemblyInfo> GetAssemblyInfosV41() {
return GetAssemblyInfos((data, magic) => InflateIfNeeded(Decrypt41Trial(data, magic)));
2012-05-10 04:24:39 +08:00
}
2013-01-19 20:03:57 +08:00
static byte[] Decrypt41Trial(byte[] data, int magic) {
2012-05-10 04:24:39 +08:00
for (int i = 0; i < data.Length; i++)
data[i] ^= (byte)(i / 3 + magic);
return data;
}
2012-01-23 02:58:31 +08:00
}
}