de4dot-cex/de4dot.code/deobfuscators/Babel_NET/BabelUtils.cs

134 lines
4.1 KiB
C#
Raw Normal View History

2012-01-18 14:38:35 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2012-01-18 14:38:35 +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-06-12 16:37:51 +08:00
using System.Collections.Generic;
using System.Text;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2012-01-18 14:38:35 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.Babel_NET {
static class BabelUtils {
2013-01-19 20:03:57 +08:00
public static EmbeddedResource FindEmbeddedResource(ModuleDefMD module, TypeDef decrypterType) {
return FindEmbeddedResource(module, decrypterType, (method) => { });
2012-01-18 14:38:35 +08:00
}
2013-01-19 20:03:57 +08:00
public static EmbeddedResource FindEmbeddedResource(ModuleDefMD module, TypeDef decrypterType, ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob) {
return FindEmbeddedResource(module, decrypterType, (method) => {
simpleDeobfuscator.Deobfuscate(method);
simpleDeobfuscator.DecryptStrings(method, deob);
2012-01-18 14:38:35 +08:00
});
}
2013-01-19 20:03:57 +08:00
public static EmbeddedResource FindEmbeddedResource(ModuleDefMD module, TypeDef decrypterType, Action<MethodDef> fixMethod) {
2012-01-18 14:38:35 +08:00
foreach (var method in decrypterType.Methods) {
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(method, "System.String", "()"))
2012-01-18 14:38:35 +08:00
continue;
if (!method.IsStatic)
continue;
fixMethod(method);
2013-01-19 20:03:57 +08:00
var resource = FindEmbeddedResource1(module, method) ?? FindEmbeddedResource2(module, method);
2012-06-12 16:37:51 +08:00
if (resource != null)
return resource;
2012-01-18 14:38:35 +08:00
}
return null;
}
2013-01-19 20:03:57 +08:00
static EmbeddedResource FindEmbeddedResource1(ModuleDefMD module, MethodDef method) {
foreach (var s in DotNetUtils.GetCodeStrings(method)) {
var resource = DotNetUtils.GetResource(module, s) as EmbeddedResource;
2012-06-12 16:37:51 +08:00
if (resource != null)
return resource;
}
return null;
}
2013-01-19 20:03:57 +08:00
static EmbeddedResource FindEmbeddedResource2(ModuleDefMD module, MethodDef method) {
var strings = new List<string>(DotNetUtils.GetCodeStrings(method));
2012-06-12 16:37:51 +08:00
if (strings.Count != 1)
return null;
var encryptedString = strings[0];
int xorKey;
2013-01-19 20:03:57 +08:00
if (!GetXorKey2(method, out xorKey))
2012-06-12 16:37:51 +08:00
return null;
var sb = new StringBuilder(encryptedString.Length);
foreach (var c in encryptedString)
sb.Append((char)(c ^ xorKey));
2013-01-19 20:03:57 +08:00
return DotNetUtils.GetResource(module, sb.ToString()) as EmbeddedResource;
2012-06-12 16:37:51 +08:00
}
2013-01-19 20:03:57 +08:00
static bool GetXorKey2(MethodDef method, out int xorKey) {
2012-06-12 16:37:51 +08:00
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 2; i++) {
var ldelem = instrs[i];
if (ldelem.OpCode.Code != Code.Ldelem_U2)
continue;
var ldci4 = instrs[i + 1];
2012-11-08 14:06:46 +08:00
if (!ldci4.IsLdcI4())
2012-06-12 16:37:51 +08:00
continue;
if (instrs[i + 2].OpCode.Code != Code.Xor)
continue;
2012-11-08 14:06:46 +08:00
xorKey = ldci4.GetLdcI4Value();
2012-06-12 16:37:51 +08:00
return true;
}
xorKey = 0;
return false;
}
2013-01-19 20:03:57 +08:00
public static bool FindRegisterMethod(TypeDef type, out MethodDef regMethod, out MethodDef handler) {
2012-01-18 14:38:35 +08:00
foreach (var method in type.Methods) {
if (!method.IsStatic || method.Body == null)
continue;
if (method.Body.ExceptionHandlers.Count != 1)
continue;
foreach (var instr in method.Body.Instructions) {
if (instr.OpCode.Code != Code.Ldftn)
continue;
2012-11-08 14:06:46 +08:00
var handlerRef = instr.Operand as IMethod;
2012-01-18 14:38:35 +08:00
if (handlerRef == null)
continue;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(handlerRef, "System.Reflection.Assembly", "(System.Object,System.ResolveEventArgs)"))
2012-01-18 14:38:35 +08:00
continue;
2012-11-08 14:06:46 +08:00
if (!new SigComparer().Equals(type, handlerRef.DeclaringType))
2012-01-18 14:38:35 +08:00
continue;
2013-01-19 20:03:57 +08:00
handler = DotNetUtils.GetMethod(type, handlerRef);
2012-01-18 14:38:35 +08:00
if (handler == null)
continue;
if (handler.Body == null || handler.Body.ExceptionHandlers.Count != 1)
continue;
regMethod = method;
return true;
}
}
regMethod = null;
handler = null;
return false;
}
}
}