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

365 lines
9.9 KiB
C#
Raw Normal View History

2012-01-23 02:58:31 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 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/>.
*/
2012-05-10 01:00:21 +08:00
using System;
using System.Collections.Generic;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2012-01-24 10:22:59 +08:00
using de4dot.blocks;
2012-01-23 02:58:31 +08:00
namespace de4dot.code.deobfuscators.DeepSea {
class ResourceResolver : ResolverBase {
Data30 data30;
Data40 data40;
2012-05-10 01:00:21 +08:00
Data41 data41;
ResourceVersion version = ResourceVersion.Unknown;
enum ResourceVersion {
Unknown,
V3,
V40,
V41,
}
class Data30 {
public EmbeddedResource resource;
}
class Data40 {
public FieldDef resourceField;
public MethodDef resolveHandler2;
public MethodDef getDataMethod;
public int magic;
}
2012-05-10 01:00:21 +08:00
class Data41 {
public FieldDef resourceField;
public MethodDef resolveHandler2;
2012-05-10 01:00:21 +08:00
public int magic;
public bool isTrial;
}
class HandlerInfo {
public MethodDef handler;
2012-05-10 01:00:21 +08:00
public IList<object> args;
public HandlerInfo(MethodDef handler, IList<object> args) {
2012-05-10 01:00:21 +08:00
this.handler = handler;
this.args = args;
}
}
2012-01-23 02:58:31 +08:00
public MethodDef InitMethod2 {
get {
if (data40 != null)
return data40.resolveHandler2;
if (data41 != null)
return data41.resolveHandler2;
return null;
}
}
public MethodDef GetDataMethod {
get { return data40 != null ? data40.getDataMethod : null; }
}
public EmbeddedResource Resource {
get { return data30 != null ? data30.resource : null; }
}
2012-11-09 07:21:45 +08:00
public ResourceResolver(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
}
2013-01-19 20:03:57 +08:00
protected override bool CheckResolverInitMethodInternal(MethodDef resolverInitMethod) {
return DotNetUtils.CallsMethod(resolverInitMethod, "System.Void System.AppDomain::add_ResourceResolve(System.ResolveEventHandler)");
}
2013-01-19 20:03:57 +08:00
protected override bool CheckHandlerMethodDesktopInternal(MethodDef handler) {
if (CheckHandlerV3(handler)) {
2012-05-10 01:00:21 +08:00
version = ResourceVersion.V3;
2012-01-24 10:22:59 +08:00
return true;
}
2013-01-19 20:03:57 +08:00
simpleDeobfuscator.Deobfuscate(handler);
if ((data40 = CheckHandlerV40(handler)) != null) {
2012-05-10 01:00:21 +08:00
version = ResourceVersion.V40;
2012-01-24 10:22:59 +08:00
return true;
}
2013-01-19 20:03:57 +08:00
var info = GetHandlerArgs41(handler);
2012-05-10 01:00:21 +08:00
Data41 data41Tmp;
2013-01-19 20:03:57 +08:00
if (info != null && CheckHandlerV41(info, out data41Tmp)) {
2012-05-10 01:00:21 +08:00
version = ResourceVersion.V41;
data41 = data41Tmp;
return true;
}
2012-01-24 10:22:59 +08:00
return false;
}
2013-01-19 20:03:57 +08:00
HandlerInfo GetHandlerArgs41(MethodDef handler) {
2012-05-10 01:00:21 +08:00
var instrs = handler.Body.Instructions;
for (int i = 0; i < instrs.Count; i++) {
var instr = instrs[i];
if (instr.OpCode.Code != Code.Call)
continue;
var calledMethod = instr.Operand as MethodDef;
2012-05-10 01:00:21 +08:00
if (calledMethod == null)
continue;
2013-01-19 20:03:57 +08:00
if (GetLdtokenField(calledMethod) == null)
continue;
2013-01-19 20:03:57 +08:00
var args = DsUtils.GetArgValues(instrs, i);
2012-05-10 01:00:21 +08:00
if (args == null)
continue;
return new HandlerInfo(calledMethod, args);
}
return null;
}
2013-01-19 20:03:57 +08:00
bool CheckHandlerV41(HandlerInfo info, out Data41 data41) {
2012-05-10 01:00:21 +08:00
data41 = new Data41();
data41.resolveHandler2 = info.handler;
2013-01-19 20:03:57 +08:00
data41.resourceField = GetLdtokenField(info.handler);
2012-05-10 01:00:21 +08:00
if (data41.resourceField == null)
return false;
2012-12-14 19:40:44 +08:00
bool isOtherRetail;
2013-01-19 20:03:57 +08:00
int magicArgIndex = GetMagicArgIndex41Retail(info.handler, out isOtherRetail);
2012-05-10 01:00:21 +08:00
if (magicArgIndex < 0) {
2013-01-19 20:03:57 +08:00
magicArgIndex = GetMagicArgIndex41Trial(info.handler);
2012-05-10 01:00:21 +08:00
data41.isTrial = true;
}
2012-11-09 07:21:45 +08:00
var asmVer = module.Assembly.Version;
2012-05-10 01:00:21 +08:00
if (magicArgIndex < 0 || magicArgIndex >= info.args.Count)
return false;
var val = info.args[magicArgIndex];
if (!(val is int))
return false;
if (data41.isTrial)
data41.magic = (int)val >> 3;
2012-12-14 19:40:44 +08:00
else if (isOtherRetail)
data41.magic = data41.resourceField.InitialValue.Length - (int)val;
2012-05-10 01:00:21 +08:00
else
data41.magic = ((asmVer.Major << 3) | (asmVer.Minor << 2) | asmVer.Revision) - (int)val;
return true;
}
2013-01-19 20:03:57 +08:00
static int GetMagicArgIndex41Retail(MethodDef method, out bool isOtherRetail) {
2012-12-14 19:40:44 +08:00
isOtherRetail = false;
2012-05-10 01:00:21 +08:00
var instrs = method.Body.Instructions;
2012-12-14 19:40:44 +08:00
for (int i = 0; i < instrs.Count - 4; i++) {
isOtherRetail = false;
var ld = instrs[i];
if (ld.IsLdarg())
isOtherRetail = true;
else if (!ld.IsLdloc())
continue;
var add = instrs[i + 1];
2012-05-10 01:00:21 +08:00
if (add.OpCode.Code != Code.Add)
continue;
2012-12-14 19:40:44 +08:00
var ldarg = instrs[i + 2];
2012-11-09 07:21:45 +08:00
if (!ldarg.IsLdarg())
2012-05-10 01:00:21 +08:00
continue;
2012-12-14 19:40:44 +08:00
var sub = instrs[i + 3];
2012-05-10 01:00:21 +08:00
if (sub.OpCode.Code != Code.Sub)
continue;
2012-12-14 19:40:44 +08:00
var ldci4 = instrs[i + 4];
2012-11-09 07:21:45 +08:00
if (!ldci4.IsLdcI4() || ldci4.GetLdcI4Value() != 0xFF)
2012-05-10 01:00:21 +08:00
continue;
2012-11-09 07:21:45 +08:00
return ldarg.GetParameterIndex();
2012-05-10 01:00:21 +08:00
}
2012-12-14 19:40:44 +08:00
2012-05-10 01:00:21 +08:00
return -1;
}
2013-01-19 20:03:57 +08:00
static int GetMagicArgIndex41Trial(MethodDef method) {
2012-05-10 01:00:21 +08:00
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 2; i++) {
var ldarg = instrs[i];
2012-11-09 07:21:45 +08:00
if (!ldarg.IsLdarg())
2012-05-10 01:00:21 +08:00
continue;
2012-11-09 07:21:45 +08:00
if (!instrs[i + 1].IsLdcI4())
2012-05-10 01:00:21 +08:00
continue;
if (instrs[i + 2].OpCode.Code != Code.Shr)
continue;
2012-11-09 07:21:45 +08:00
return ldarg.GetParameterIndex();
2012-05-10 01:00:21 +08:00
}
return -1;
}
2013-01-19 20:03:57 +08:00
static FieldDef GetLdtokenField(MethodDef method) {
2012-05-10 01:00:21 +08:00
foreach (var instr in method.Body.Instructions) {
if (instr.OpCode.Code != Code.Ldtoken)
continue;
var field = instr.Operand as FieldDef;
2012-05-10 01:00:21 +08:00
if (field == null || field.InitialValue == null || field.InitialValue.Length == 0)
continue;
return field;
}
return null;
}
2012-01-24 10:22:59 +08:00
static string[] handlerLocalTypes_V3 = new string[] {
2012-01-23 02:58:31 +08:00
"System.AppDomain",
"System.Byte[]",
"System.Collections.Generic.Dictionary`2<System.String,System.String>",
"System.IO.Compression.DeflateStream",
"System.IO.MemoryStream",
"System.IO.Stream",
"System.Reflection.Assembly",
"System.String",
"System.String[]",
};
2013-01-19 20:03:57 +08:00
static bool CheckHandlerV3(MethodDef handler) {
return new LocalTypes(handler).All(handlerLocalTypes_V3);
2012-01-23 02:58:31 +08:00
}
2013-01-19 20:03:57 +08:00
static Data40 CheckHandlerV40(MethodDef handler) {
var data40 = new Data40();
var instrs = handler.Body.Instructions;
for (int i = 0; i < instrs.Count; i++) {
int index = i;
2012-01-24 10:22:59 +08:00
if (instrs[index++].OpCode.Code != Code.Ldarg_1)
2012-01-24 10:22:59 +08:00
continue;
var ldtoken = instrs[index++];
if (ldtoken.OpCode.Code != Code.Ldtoken)
2012-01-24 10:22:59 +08:00
continue;
var field = ldtoken.Operand as FieldDef;
string methodSig = "(System.ResolveEventArgs,System.RuntimeFieldHandle,System.Int32,System.String,System.Int32)";
var method = ldtoken.Operand as MethodDef;
if (method != null) {
// >= 4.0.4
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(method, "System.Byte[]", "()"))
continue;
2013-01-19 20:03:57 +08:00
field = GetResourceField(method);
methodSig = "(System.ResolveEventArgs,System.RuntimeMethodHandle,System.Int32,System.String,System.Int32)";
}
else {
// 4.0.1.18 .. 4.0.3
2012-01-24 10:22:59 +08:00
}
2012-05-10 01:00:21 +08:00
if (field == null || field.InitialValue == null || field.InitialValue.Length == 0)
continue;
var ldci4_len = instrs[index++];
2012-11-09 07:21:45 +08:00
if (!ldci4_len.IsLdcI4())
continue;
2012-11-09 07:21:45 +08:00
if (ldci4_len.GetLdcI4Value() != field.InitialValue.Length)
continue;
if (instrs[index++].OpCode.Code != Code.Ldstr)
continue;
var ldci4_magic = instrs[index++];
2012-11-09 07:21:45 +08:00
if (!ldci4_magic.IsLdcI4())
continue;
2012-11-09 07:21:45 +08:00
data40.magic = ldci4_magic.GetLdcI4Value();
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)
continue;
var resolveHandler2 = call.Operand as MethodDef;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(resolveHandler2, "System.Reflection.Assembly", methodSig))
continue;
data40.resourceField = field;
data40.getDataMethod = method;
data40.resolveHandler2 = resolveHandler2;
return data40;
2012-01-24 10:22:59 +08:00
}
return null;
2012-01-24 10:22:59 +08:00
}
2013-01-19 20:03:57 +08:00
static FieldDef GetResourceField(MethodDef method) {
2012-01-24 10:22:59 +08:00
foreach (var instr in method.Body.Instructions) {
if (instr.OpCode.Code != Code.Ldtoken)
continue;
var field = instr.Operand as FieldDef;
2012-01-24 10:22:59 +08:00
if (field == null || field.InitialValue == null || field.InitialValue.Length == 0)
continue;
return field;
}
return null;
}
2013-01-19 20:03:57 +08:00
public void Initialize() {
2012-01-23 02:58:31 +08:00
if (resolveHandler == null)
return;
2012-05-10 01:00:21 +08:00
if (version == ResourceVersion.V3) {
2013-01-19 20:03:57 +08:00
simpleDeobfuscator.Deobfuscate(resolveHandler);
simpleDeobfuscator.DecryptStrings(resolveHandler, deob);
data30 = new Data30();
2013-01-19 20:03:57 +08:00
data30.resource = DeobUtils.GetEmbeddedResourceFromCodeStrings(module, resolveHandler);
if (data30.resource == null) {
Logger.w("Could not find resource of encrypted resources");
2012-01-24 10:22:59 +08:00
return;
}
2012-01-23 02:58:31 +08:00
}
}
2013-01-19 20:03:57 +08:00
public bool MergeResources(out EmbeddedResource rsrc) {
2012-01-24 10:22:59 +08:00
rsrc = null;
2012-05-10 01:00:21 +08:00
switch (version) {
case ResourceVersion.V3:
if (data30.resource == null)
2012-01-24 10:22:59 +08:00
return false;
2012-01-23 02:58:31 +08:00
2013-01-19 20:03:57 +08:00
DeobUtils.DecryptAndAddResources(module, data30.resource.Name.String, () => DecryptResourceV3(data30.resource));
rsrc = data30.resource;
2012-05-10 01:00:21 +08:00
return true;
case ResourceVersion.V40:
2013-01-19 20:03:57 +08:00
return DecryptResource(data40.resourceField, data40.magic);
2012-05-10 01:00:21 +08:00
case ResourceVersion.V41:
2013-01-19 20:03:57 +08:00
return DecryptResource(data41.resourceField, data41.magic);
2012-01-24 10:22:59 +08:00
2012-05-10 01:00:21 +08:00
default:
return true;
2012-01-24 10:22:59 +08:00
}
2012-05-10 01:00:21 +08:00
}
2013-01-19 20:03:57 +08:00
bool DecryptResource(FieldDef resourceField, int magic) {
2012-05-10 01:00:21 +08:00
if (resourceField == null)
return false;
2012-11-09 07:21:45 +08:00
string name = string.Format("Embedded data field {0:X8} RVA {1:X8}", resourceField.MDToken.ToInt32(), (uint)resourceField.RVA);
2013-01-19 20:03:57 +08:00
DeobUtils.DecryptAndAddResources(module, name, () => DecryptResourceV4(resourceField.InitialValue, magic));
2012-05-10 01:00:21 +08:00
resourceField.InitialValue = new byte[1];
2012-11-09 07:21:45 +08:00
resourceField.FieldSig.Type = module.CorLibTypes.Byte;
resourceField.RVA = 0;
2012-01-24 10:22:59 +08:00
return true;
2012-01-23 02:58:31 +08:00
}
}
}