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

122 lines
3.4 KiB
C#
Raw Normal View History

2012-06-12 16:37:51 +08:00
/*
2014-03-12 05:15:43 +08:00
Copyright (C) 2011-2014 de4dot@gmail.com
2012-06-12 16:37:51 +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 ICSharpCode.SharpZipLib.Zip.Compression;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2012-06-12 16:37:51 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.Babel_NET {
class InflaterCreator {
2013-01-19 20:03:57 +08:00
public static Inflater Create(MethodDef method, bool noHeader) {
return Create(FindInflaterType(method), noHeader);
2012-06-12 16:37:51 +08:00
}
2013-01-19 20:03:57 +08:00
public static Inflater Create(TypeDef inflaterType, bool noHeader) {
2012-06-12 16:37:51 +08:00
if (inflaterType == null)
2013-01-19 20:03:57 +08:00
return CreateNormal(noHeader);
var initHeaderMethod = FindInitHeaderMethod(inflaterType);
2012-06-12 16:37:51 +08:00
if (initHeaderMethod == null)
2013-01-19 20:03:57 +08:00
return CreateNormal(noHeader, "Could not find inflater init header method");
var magic = GetMagic(initHeaderMethod);
2012-06-12 16:37:51 +08:00
if (!magic.HasValue)
2013-01-19 20:03:57 +08:00
return CreateNormal(noHeader);
2012-06-12 16:37:51 +08:00
return new BabelInflater(noHeader, magic.Value);
}
2013-01-19 20:03:57 +08:00
static Inflater CreateNormal(bool noHeader) {
return CreateNormal(noHeader, null);
2012-07-07 13:11:32 +08:00
}
2013-01-19 20:03:57 +08:00
static Inflater CreateNormal(bool noHeader, string errorMessage) {
2012-06-12 16:37:51 +08:00
if (errorMessage != null)
Logger.w("{0}", errorMessage);
2012-06-12 16:37:51 +08:00
return new Inflater(noHeader);
}
2013-01-19 20:03:57 +08:00
static TypeDef FindInflaterType(MethodDef method) {
2012-06-12 16:37:51 +08:00
if (method == null || method.Body == null)
return null;
foreach (var instr in method.Body.Instructions) {
if (instr.OpCode.Code != Code.Call)
continue;
var calledMethod = instr.Operand as MethodDef;
2012-06-12 16:37:51 +08:00
if (calledMethod == null || !calledMethod.IsStatic)
continue;
var type = calledMethod.DeclaringType;
foreach (var nested in type.NestedTypes) {
2013-01-19 20:03:57 +08:00
if (DeobUtils.HasInteger(nested.FindMethod(".ctor"), 0x8001))
2012-06-12 16:37:51 +08:00
return type;
}
}
return null;
}
2013-01-19 20:03:57 +08:00
static MethodDef FindInitHeaderMethod(TypeDef inflaterType) {
2012-06-12 16:37:51 +08:00
foreach (var nested in inflaterType.NestedTypes) {
2013-01-19 20:03:57 +08:00
var method = FindInitHeaderMethod2(nested);
2012-06-12 16:37:51 +08:00
if (method != null)
return method;
}
return null;
}
2013-01-19 20:03:57 +08:00
static MethodDef FindInitHeaderMethod2(TypeDef nested) {
2012-06-12 16:37:51 +08:00
foreach (var method in nested.Methods) {
if (method.IsStatic || method.Body == null)
continue;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(method, "System.Boolean", "()"))
2012-06-12 16:37:51 +08:00
continue;
return method;
}
return null;
}
2013-01-19 20:03:57 +08:00
static int? GetMagic(MethodDef method) {
2012-06-12 16:37:51 +08:00
if (method == null || method.Body == null)
return null;
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 3; i++) {
var ldci4_1 = instrs[i];
2012-11-08 14:06:46 +08:00
if (!ldci4_1.IsLdcI4() || ldci4_1.GetLdcI4Value() != 16)
2012-06-12 16:37:51 +08:00
continue;
var callvirt = instrs[i + 1];
if (callvirt.OpCode.Code != Code.Callvirt)
continue;
var ldci4_2 = instrs[i + 2];
2012-11-08 14:06:46 +08:00
if (!ldci4_2.IsLdcI4())
2012-06-12 16:37:51 +08:00
continue;
if (instrs[i + 3].OpCode.Code != Code.Xor)
continue;
2012-11-08 14:06:46 +08:00
return ldci4_2.GetLdcI4Value();
2012-06-12 16:37:51 +08:00
}
return null;
}
}
}