2012-06-12 16:37:51 +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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using ICSharpCode.SharpZipLib.Zip.Compression;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
using dot10.DotNet;
|
|
|
|
|
using dot10.DotNet.Emit;
|
2012-06-12 16:37:51 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.Babel_NET {
|
|
|
|
|
class InflaterCreator {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public static Inflater create(MethodDef method, bool noHeader) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
return create(findInflaterType(method), noHeader);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public static Inflater create(TypeDef inflaterType, bool noHeader) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
if (inflaterType == null)
|
|
|
|
|
return createNormal(noHeader);
|
|
|
|
|
var initHeaderMethod = findInitHeaderMethod(inflaterType);
|
|
|
|
|
if (initHeaderMethod == null)
|
|
|
|
|
return createNormal(noHeader, "Could not find inflater init header method");
|
|
|
|
|
var magic = getMagic(initHeaderMethod);
|
|
|
|
|
if (!magic.HasValue)
|
|
|
|
|
return createNormal(noHeader);
|
|
|
|
|
return new BabelInflater(noHeader, magic.Value);
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-07 13:11:32 +08:00
|
|
|
|
static Inflater createNormal(bool noHeader) {
|
|
|
|
|
return createNormal(noHeader, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Inflater createNormal(bool noHeader, string errorMessage) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
if (errorMessage != null)
|
|
|
|
|
Log.w("{0}", errorMessage);
|
|
|
|
|
return new Inflater(noHeader);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +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;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
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) {
|
|
|
|
|
if (DeobUtils.hasInteger(DotNetUtils.getMethod(nested, ".ctor"), 0x8001))
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
static MethodDef findInitHeaderMethod(TypeDef inflaterType) {
|
2012-06-12 16:37:51 +08:00
|
|
|
|
foreach (var nested in inflaterType.NestedTypes) {
|
|
|
|
|
var method = findInitHeaderMethod2(nested);
|
|
|
|
|
if (method != null)
|
|
|
|
|
return method;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +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;
|
|
|
|
|
if (!DotNetUtils.isMethod(method, "System.Boolean", "()"))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return method;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +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];
|
|
|
|
|
if (!DotNetUtils.isLdcI4(ldci4_1) || DotNetUtils.getLdcI4Value(ldci4_1) != 16)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var callvirt = instrs[i + 1];
|
|
|
|
|
if (callvirt.OpCode.Code != Code.Callvirt)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var ldci4_2 = instrs[i + 2];
|
|
|
|
|
if (!DotNetUtils.isLdcI4(ldci4_2))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (instrs[i + 3].OpCode.Code != Code.Xor)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return DotNetUtils.getLdcI4Value(ldci4_2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|