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
/*
Copyright (C) 2011-2012 de4dot@gmail.com
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 dot10.DotNet;
using dot10.DotNet.Emit;
2012-01-18 14:38:35 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.Babel_NET {
static class BabelUtils {
2012-11-08 14:06:46 +08:00
public static EmbeddedResource findEmbeddedResource(ModuleDefMD module, TypeDef decrypterType) {
2012-01-18 14:38:35 +08:00
return findEmbeddedResource(module, decrypterType, (method) => { });
}
2012-11-08 14:06:46 +08:00
public static EmbeddedResource findEmbeddedResource(ModuleDefMD module, TypeDef decrypterType, ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob) {
2012-01-18 14:38:35 +08:00
return findEmbeddedResource(module, decrypterType, (method) => {
simpleDeobfuscator.deobfuscate(method);
simpleDeobfuscator.decryptStrings(method, deob);
});
}
2012-11-08 14:06:46 +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) {
if (!DotNetUtils.isMethod(method, "System.String", "()"))
continue;
if (!method.IsStatic)
continue;
fixMethod(method);
2012-06-12 16:37:51 +08:00
var resource = findEmbeddedResource1(module, method) ?? findEmbeddedResource2(module, method);
if (resource != null)
return resource;
2012-01-18 14:38:35 +08:00
}
return null;
}
2012-11-08 14:06:46 +08:00
static EmbeddedResource findEmbeddedResource1(ModuleDefMD module, MethodDef method) {
2012-06-12 16:37:51 +08:00
foreach (var s in DotNetUtils.getCodeStrings(method)) {
var resource = DotNetUtils.getResource(module, s) as EmbeddedResource;
if (resource != null)
return resource;
}
return null;
}
2012-11-08 14:06:46 +08:00
static EmbeddedResource findEmbeddedResource2(ModuleDefMD module, MethodDef method) {
2012-06-12 16:37:51 +08:00
var strings = new List<string>(DotNetUtils.getCodeStrings(method));
if (strings.Count != 1)
return null;
var encryptedString = strings[0];
int xorKey;
if (!getXorKey2(method, out xorKey))
return null;
var sb = new StringBuilder(encryptedString.Length);
foreach (var c in encryptedString)
sb.Append((char)(c ^ xorKey));
return DotNetUtils.getResource(module, sb.ToString()) as EmbeddedResource;
}
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;
}
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;
if (!DotNetUtils.isMethod(handlerRef, "System.Reflection.Assembly", "(System.Object,System.ResolveEventArgs)"))
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;
handler = DotNetUtils.getMethod(type, handlerRef);
if (handler == null)
continue;
if (handler.Body == null || handler.Body.ExceptionHandlers.Count != 1)
continue;
regMethod = method;
return true;
}
}
regMethod = null;
handler = null;
return false;
}
}
}