2011-09-22 10:55:30 +08:00
|
|
|
|
/*
|
2014-03-12 05:15:43 +08:00
|
|
|
|
Copyright (C) 2011-2014 de4dot@gmail.com
|
2011-09-22 10:55:30 +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.Text;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2011-09-24 16:26:29 +08:00
|
|
|
|
using de4dot.blocks;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2011-12-09 16:02:06 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.SmartAssembly {
|
2011-11-13 04:04:24 +08:00
|
|
|
|
public class EmbeddedAssemblyInfo {
|
|
|
|
|
public string assemblyName;
|
|
|
|
|
public string simpleName;
|
|
|
|
|
public string resourceName;
|
|
|
|
|
public EmbeddedResource resource;
|
|
|
|
|
public bool isCompressed = false;
|
|
|
|
|
public bool isTempFile = false;
|
|
|
|
|
public string flags = "";
|
|
|
|
|
|
|
|
|
|
public override string ToString() {
|
|
|
|
|
return assemblyName ?? base.ToString();
|
|
|
|
|
}
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public static EmbeddedAssemblyInfo Create(ModuleDefMD module, string encName, string rsrcName) {
|
2011-11-13 04:04:24 +08:00
|
|
|
|
var info = new EmbeddedAssemblyInfo();
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2011-11-13 04:04:24 +08:00
|
|
|
|
try {
|
|
|
|
|
if (encName == "" || Convert.ToBase64String(Convert.FromBase64String(encName)) != encName)
|
2011-09-22 10:55:30 +08:00
|
|
|
|
return null;
|
2011-11-13 04:04:24 +08:00
|
|
|
|
}
|
|
|
|
|
catch (FormatException) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rsrcName.Length > 0 && rsrcName[0] == '[') {
|
|
|
|
|
int i = rsrcName.IndexOf(']');
|
|
|
|
|
if (i < 0)
|
2011-09-22 10:55:30 +08:00
|
|
|
|
return null;
|
2011-11-13 04:04:24 +08:00
|
|
|
|
info.flags = rsrcName.Substring(1, i - 1);
|
|
|
|
|
info.isTempFile = info.flags.IndexOf('t') >= 0;
|
|
|
|
|
info.isCompressed = info.flags.IndexOf('z') >= 0;
|
|
|
|
|
rsrcName = rsrcName.Substring(i + 1);
|
|
|
|
|
}
|
|
|
|
|
if (rsrcName == "")
|
|
|
|
|
return null;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2011-11-13 04:04:24 +08:00
|
|
|
|
info.assemblyName = Encoding.UTF8.GetString(Convert.FromBase64String(encName));
|
|
|
|
|
info.resourceName = rsrcName;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
info.resource = DotNetUtils.GetResource(module, rsrcName) as EmbeddedResource;
|
|
|
|
|
info.simpleName = Utils.GetAssemblySimpleName(info.assemblyName);
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2011-11-13 04:04:24 +08:00
|
|
|
|
return info;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
2011-11-13 04:04:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class AssemblyResolverInfo : ResolverInfoBase {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
MethodDef simpleZipTypeMethod;
|
2011-11-13 04:04:24 +08:00
|
|
|
|
List<EmbeddedAssemblyInfo> embeddedAssemblyInfos = new List<EmbeddedAssemblyInfo>();
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef SimpleZipTypeMethod {
|
2012-01-17 09:54:48 +08:00
|
|
|
|
get { return simpleZipTypeMethod; }
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IList<EmbeddedAssemblyInfo> EmbeddedAssemblyInfos {
|
|
|
|
|
get { return embeddedAssemblyInfos; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 00:07:02 +08:00
|
|
|
|
public AssemblyResolverInfo(ModuleDefMD module, ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob)
|
2011-09-22 10:55:30 +08:00
|
|
|
|
: base(module, simpleDeobfuscator, deob) {
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public bool ResolveResources() {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
|
|
foreach (var info in embeddedAssemblyInfos) {
|
|
|
|
|
if (info.resource != null)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
info.resource = DotNetUtils.GetResource(module, info.resourceName) as EmbeddedResource;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
if (info.resource == null)
|
|
|
|
|
ok = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override bool CheckResolverType(TypeDef type) {
|
2013-11-19 04:28:12 +08:00
|
|
|
|
if (DotNetUtils.FindFieldType(type, "System.Collections.Hashtable", true) != null ||
|
|
|
|
|
DotNetUtils.FindFieldType(type, "System.Collections.Generic.Dictionary`2<System.String,System.Reflection.Assembly>", true) != null)
|
2011-09-22 10:55:30 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
foreach (var field in type.Fields) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (DotNetUtils.DerivesFromDelegate(DotNetUtils.GetType(module, field.FieldType)))
|
2011-12-27 03:40:18 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (field.IsLiteral && field.FieldType.ToString() == "System.String")
|
|
|
|
|
continue;
|
|
|
|
|
return false;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override bool CheckHandlerMethod(MethodDef method) {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
if (!method.IsStatic || !method.HasBody)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var infos = new List<EmbeddedAssemblyInfo>();
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var s in DotNetUtils.GetCodeStrings(method)) {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
if (string.IsNullOrEmpty(s))
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!InitInfos(infos, s.Split(',')))
|
2011-09-22 10:55:30 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
embeddedAssemblyInfos = infos;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
FindSimpleZipType(method);
|
2011-09-22 10:55:30 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool InitInfos(IList<EmbeddedAssemblyInfo> list, string[] strings) {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
list.Clear();
|
|
|
|
|
if (strings.Length % 2 == 1)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < strings.Length; i += 2) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var info = EmbeddedAssemblyInfo.Create(module, strings[i], strings[i + 1]);
|
2011-09-22 10:55:30 +08:00
|
|
|
|
if (info == null)
|
|
|
|
|
return false;
|
|
|
|
|
list.Add(info);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-11 12:31:11 +08:00
|
|
|
|
Logger.v("Found embedded assemblies:");
|
2013-01-19 20:03:57 +08:00
|
|
|
|
Logger.Instance.Indent();
|
2011-09-22 10:55:30 +08:00
|
|
|
|
foreach (var info in list)
|
2012-11-11 12:31:11 +08:00
|
|
|
|
Logger.v("{0}", info.assemblyName);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
Logger.Instance.DeIndent();
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void FindSimpleZipType(MethodDef method) {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
if (method == null || !method.HasBody)
|
|
|
|
|
return;
|
|
|
|
|
foreach (var call in method.Body.Instructions) {
|
|
|
|
|
if (call.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-19 00:07:02 +08:00
|
|
|
|
var calledMethod = call.Operand as IMethod;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
if (calledMethod == null)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!SimpleZipInfo.IsSimpleZipDecryptMethod_QuickCheck(module, calledMethod, out simpleZipTypeMethod))
|
2011-09-22 10:55:30 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-11-13 04:04:24 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public EmbeddedAssemblyInfo Find(string simpleName) {
|
2011-11-13 04:04:24 +08:00
|
|
|
|
foreach (var info in embeddedAssemblyInfos) {
|
|
|
|
|
if (info.simpleName == simpleName)
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2012-01-10 09:36:39 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public bool RemoveEmbeddedAssemblyInfo(EmbeddedAssemblyInfo info) {
|
2012-01-10 09:36:39 +08:00
|
|
|
|
bool removed = false;
|
|
|
|
|
for (int i = 0; i < EmbeddedAssemblyInfos.Count; i++) {
|
|
|
|
|
var other = EmbeddedAssemblyInfos[i];
|
|
|
|
|
if (info.simpleName == other.simpleName) {
|
|
|
|
|
EmbeddedAssemblyInfos.RemoveAt(i--);
|
|
|
|
|
removed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return removed;
|
|
|
|
|
}
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
}
|