de4dot-cex/de4dot.code/deobfuscators/Eazfuscator_NET/DecrypterType.cs

372 lines
9.9 KiB
C#
Raw Normal View History

2012-02-27 19:55:37 +08:00
/*
2014-03-12 05:15:43 +08:00
Copyright (C) 2011-2014 de4dot@gmail.com
2012-02-27 19:55:37 +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;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2012-02-27 19:55:37 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.Eazfuscator_NET {
class DecrypterType {
2012-11-18 08:09:07 +08:00
ModuleDefMD module;
2012-04-10 09:52:18 +08:00
ISimpleDeobfuscator simpleDeobfuscator;
TypeDef type;
MethodDef int64Method;
2012-02-27 19:55:37 +08:00
bool initialized;
ulong l1;
2012-03-06 15:17:55 +08:00
int i1, i2, i3;
2012-02-27 19:55:37 +08:00
int m1_i1, m2_i1, m2_i2, m3_i1;
MethodDef[] efConstMethods;
2012-02-27 19:55:37 +08:00
2012-12-11 04:42:49 +08:00
public MethodDef Int64Method {
get { return int64Method; }
}
public TypeDef Type {
2012-02-27 19:55:37 +08:00
get { return type; }
set {
if (type == null)
type = value;
else if (type != value)
throw new ApplicationException("Found another one");
}
}
public bool Detected {
get { return type != null; }
}
2012-11-18 08:09:07 +08:00
public DecrypterType(ModuleDefMD module, ISimpleDeobfuscator simpleDeobfuscator) {
2012-03-23 17:13:59 +08:00
this.module = module;
2012-04-10 09:52:18 +08:00
this.simpleDeobfuscator = simpleDeobfuscator;
2012-03-23 17:13:59 +08:00
}
2013-01-19 20:03:57 +08:00
public bool Initialize() {
2012-02-27 19:55:37 +08:00
if (initialized)
return true;
2013-01-19 20:03:57 +08:00
int64Method = FindInt64Method();
2012-02-27 19:55:37 +08:00
if (int64Method == null)
return false;
2013-01-19 20:03:57 +08:00
if (!InitializeEfConstMethods())
2012-03-23 17:13:59 +08:00
return false;
2013-01-19 20:03:57 +08:00
if (!FindInt1And2())
2012-02-27 19:55:37 +08:00
return false;
2013-01-19 20:03:57 +08:00
if (!FindInt3())
2012-02-27 19:55:37 +08:00
return false;
2013-01-19 20:03:57 +08:00
if (!FindMethodInts())
2012-02-27 19:55:37 +08:00
return false;
initialized = true;
return true;
}
2013-01-19 20:03:57 +08:00
bool InitializeEfConstMethods() {
2012-03-23 17:13:59 +08:00
if (type == null)
return false;
efConstMethods = new MethodDef[6];
2012-03-23 17:13:59 +08:00
2013-01-19 20:03:57 +08:00
efConstMethods[0] = FindEfConstMethodCall(int64Method);
efConstMethods[5] = FindEfConstMethodCall(efConstMethods[0]);
efConstMethods[4] = FindEfConstMethodCall(efConstMethods[5]);
var calls = FindEfConstMethodCalls(efConstMethods[4]);
2012-04-10 09:52:18 +08:00
if (calls.Count != 2)
return false;
2013-01-19 20:03:57 +08:00
if (GetNumberOfTypeofs(calls[0]) == 3) {
2012-04-10 09:52:18 +08:00
efConstMethods[2] = calls[0];
efConstMethods[1] = calls[1];
}
else {
efConstMethods[2] = calls[0];
efConstMethods[1] = calls[1];
}
2013-01-19 20:03:57 +08:00
efConstMethods[3] = FindEfConstMethodCall(efConstMethods[1]);
2012-03-23 17:13:59 +08:00
2012-04-10 09:52:18 +08:00
foreach (var m in efConstMethods) {
if (m == null)
2012-03-23 17:13:59 +08:00
return false;
}
return true;
}
2013-01-19 20:03:57 +08:00
static int GetNumberOfTypeofs(MethodDef method) {
2012-04-10 09:52:18 +08:00
if (method == null)
return 0;
int count = 0;
foreach (var instr in method.Body.Instructions) {
if (instr.OpCode.Code == Code.Ldtoken)
count++;
}
return count;
}
2013-01-19 20:03:57 +08:00
MethodDef FindEfConstMethodCall(MethodDef method) {
var list = FindEfConstMethodCalls(method);
2012-04-10 09:52:18 +08:00
if (list == null || list.Count != 1)
return null;
return list[0];
}
2013-01-19 20:03:57 +08:00
List<MethodDef> FindEfConstMethodCalls(MethodDef method) {
2012-04-10 09:52:18 +08:00
if (method == null)
return null;
var list = new List<MethodDef>();
2012-04-10 09:52:18 +08:00
foreach (var instr in method.Body.Instructions) {
if (instr.OpCode.Code != Code.Call)
continue;
var calledMethod = instr.Operand as MethodDef;
2012-04-10 09:52:18 +08:00
if (calledMethod == null || !calledMethod.IsStatic || calledMethod.Body == null)
2012-03-23 17:13:59 +08:00
continue;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(calledMethod, "System.Int32", "()"))
2012-04-10 09:52:18 +08:00
continue;
if (type.NestedTypes.IndexOf(calledMethod.DeclaringType) < 0)
2012-03-23 17:13:59 +08:00
continue;
2012-04-10 09:52:18 +08:00
list.Add(calledMethod);
2012-03-23 17:13:59 +08:00
}
2012-04-10 09:52:18 +08:00
return list;
2012-03-23 17:13:59 +08:00
}
2013-01-19 20:03:57 +08:00
MethodDef FindInt64Method() {
2012-02-27 19:55:37 +08:00
if (type == null)
return null;
foreach (var method in type.Methods) {
if (!method.IsStatic || method.Body == null || method.HasGenericParameters)
continue;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(method, "System.Int64", "()"))
2012-02-27 19:55:37 +08:00
continue;
2013-01-19 20:03:57 +08:00
if (!FindInt64(method))
2012-02-27 19:55:37 +08:00
continue;
return method;
}
return null;
}
2013-01-19 20:03:57 +08:00
bool FindInt64(MethodDef method) {
2012-02-27 19:55:37 +08:00
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 1; i++) {
var ldci8 = instrs[i];
if (ldci8.OpCode.Code != Code.Ldc_I8)
continue;
if (instrs[i + 1].OpCode.Code != Code.Xor)
continue;
l1 = (ulong)(long)ldci8.Operand;
return true;
}
return false;
}
2013-01-19 20:03:57 +08:00
bool FindInt1And2() {
var consts = GetConstants(efConstMethods[2]);
2012-02-27 19:55:37 +08:00
if (consts.Count != 2)
return false;
2012-03-06 15:17:55 +08:00
i1 = consts[0];
i2 = consts[1];
2012-02-27 19:55:37 +08:00
return true;
}
2013-01-19 20:03:57 +08:00
bool FindInt3() {
var consts = GetConstants(efConstMethods[5]);
2012-02-27 19:55:37 +08:00
if (consts.Count != 1)
return false;
2012-03-06 15:17:55 +08:00
i3 = consts[0];
2012-02-27 19:55:37 +08:00
return true;
}
2013-01-19 20:03:57 +08:00
bool FindMethodInts() {
2012-02-27 19:55:37 +08:00
foreach (var nestedType in type.NestedTypes) {
2013-01-19 20:03:57 +08:00
var methods = GetBinaryIntMethods(nestedType);
2012-02-27 19:55:37 +08:00
if (methods.Count < 3)
continue;
2012-04-10 09:52:18 +08:00
foreach (var m in methods)
2013-01-19 20:03:57 +08:00
simpleDeobfuscator.Deobfuscate(m);
if (!FindMethod1Int(methods))
2012-02-27 19:55:37 +08:00
continue;
2013-01-19 20:03:57 +08:00
if (!FindMethod2Int(methods))
2012-02-27 19:55:37 +08:00
continue;
2013-01-19 20:03:57 +08:00
if (!FindMethod3Int(methods))
2012-02-27 19:55:37 +08:00
continue;
return true;
}
return false;
}
2013-01-19 20:03:57 +08:00
static List<MethodDef> GetBinaryIntMethods(TypeDef type) {
var list = new List<MethodDef>();
2012-02-27 19:55:37 +08:00
foreach (var method in type.Methods) {
if (!method.IsStatic || method.Body == null)
continue;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(method, "System.Int32", "(System.Int32,System.Int32)"))
2012-02-27 19:55:37 +08:00
continue;
list.Add(method);
}
return list;
}
2013-01-19 20:03:57 +08:00
bool FindMethod1Int(IEnumerable<MethodDef> methods) {
2012-02-27 19:55:37 +08:00
foreach (var method in methods) {
2013-01-19 20:03:57 +08:00
if (CountInstructions(method, Code.Ldarg_0) != 1)
2012-02-27 19:55:37 +08:00
continue;
2013-01-19 20:03:57 +08:00
var constants = GetConstants(method);
2012-02-27 19:55:37 +08:00
if (constants.Count != 1)
continue;
m1_i1 = constants[0];
return true;
}
return false;
}
2013-01-19 20:03:57 +08:00
bool FindMethod2Int(IEnumerable<MethodDef> methods) {
2012-02-27 19:55:37 +08:00
foreach (var method in methods) {
2013-01-19 20:03:57 +08:00
var constants = GetConstants(method);
2012-02-27 19:55:37 +08:00
if (constants.Count != 2)
continue;
m2_i1 = constants[0];
m2_i2 = constants[1];
return true;
}
return false;
}
2013-01-19 20:03:57 +08:00
bool FindMethod3Int(IEnumerable<MethodDef> methods) {
2012-02-27 19:55:37 +08:00
foreach (var method in methods) {
2013-01-19 20:03:57 +08:00
if (CountInstructions(method, Code.Ldarg_0) != 2)
2012-02-27 19:55:37 +08:00
continue;
2013-01-19 20:03:57 +08:00
var constants = GetConstants(method);
2012-02-27 19:55:37 +08:00
if (constants.Count != 1)
continue;
m3_i1 = constants[0];
return true;
}
return false;
}
2013-01-19 20:03:57 +08:00
static int CountInstructions(MethodDef method, Code code) {
2012-02-27 19:55:37 +08:00
int count = 0;
foreach (var instr in method.Body.Instructions) {
if (instr.OpCode.Code == code)
count++;
}
return count;
}
2013-01-19 20:03:57 +08:00
static List<int> GetConstants(MethodDef method) {
2012-02-27 19:55:37 +08:00
var list = new List<int>();
if (method == null)
return list;
int index = 0;
var instrs = method.Body.Instructions;
2012-04-30 07:26:10 +08:00
var constantsReader = new EfConstantsReader(method);
2012-02-27 19:55:37 +08:00
while (true) {
int val;
2013-01-19 20:03:57 +08:00
if (!constantsReader.GetNextInt32(ref index, out val))
2012-02-27 19:55:37 +08:00
break;
2012-04-10 09:52:18 +08:00
if (index < instrs.Count && instrs[index].OpCode.Code != Code.Ret)
2012-02-27 19:55:37 +08:00
list.Add(val);
}
return list;
}
2013-01-19 20:03:57 +08:00
int BinOp1(int a, int b) {
2012-02-27 19:55:37 +08:00
return a ^ (b - m1_i1);
}
2013-01-19 20:03:57 +08:00
int BinOp2(int a, int b) {
2012-02-27 19:55:37 +08:00
return (a - m2_i1) ^ (b + m2_i2);
}
2013-01-19 20:03:57 +08:00
int BinOp3(int a, int b) {
2012-02-27 19:55:37 +08:00
return a ^ (b - m3_i1) ^ (a - b);
}
2013-01-19 20:03:57 +08:00
int ConstMethod1() {
return BinOp3(BinOp2(efConstMethods[1].DeclaringType.MDToken.ToInt32(), BinOp3(efConstMethods[0].DeclaringType.MDToken.ToInt32(), efConstMethods[4].DeclaringType.MDToken.ToInt32())), ConstMethod6());
2012-02-27 19:55:37 +08:00
}
2013-01-19 20:03:57 +08:00
int ConstMethod2() {
return BinOp1(efConstMethods[2].DeclaringType.MDToken.ToInt32(), efConstMethods[3].DeclaringType.MDToken.ToInt32() ^ BinOp2(efConstMethods[1].DeclaringType.MDToken.ToInt32(), BinOp3(efConstMethods[5].DeclaringType.MDToken.ToInt32(), ConstMethod4())));
2012-02-27 19:55:37 +08:00
}
2013-01-19 20:03:57 +08:00
int ConstMethod3() {
return BinOp3(BinOp1(ConstMethod2() ^ i1, efConstMethods[3].DeclaringType.MDToken.ToInt32()), BinOp2(efConstMethods[0].DeclaringType.MDToken.ToInt32() ^ efConstMethods[5].DeclaringType.MDToken.ToInt32(), i2));
2012-02-27 19:55:37 +08:00
}
2013-01-19 20:03:57 +08:00
int ConstMethod4() {
return BinOp3(efConstMethods[3].DeclaringType.MDToken.ToInt32(), BinOp1(efConstMethods[0].DeclaringType.MDToken.ToInt32(), BinOp2(efConstMethods[1].DeclaringType.MDToken.ToInt32(), BinOp3(efConstMethods[2].DeclaringType.MDToken.ToInt32(), BinOp1(efConstMethods[4].DeclaringType.MDToken.ToInt32(), efConstMethods[5].DeclaringType.MDToken.ToInt32())))));
2012-02-27 19:55:37 +08:00
}
2013-01-19 20:03:57 +08:00
int ConstMethod5() {
return BinOp2(BinOp2(ConstMethod3(), BinOp1(efConstMethods[4].DeclaringType.MDToken.ToInt32(), ConstMethod2())), efConstMethods[5].DeclaringType.MDToken.ToInt32());
2012-02-27 19:55:37 +08:00
}
2013-01-19 20:03:57 +08:00
int ConstMethod6() {
return BinOp1(efConstMethods[5].DeclaringType.MDToken.ToInt32(), BinOp3(BinOp2(efConstMethods[4].DeclaringType.MDToken.ToInt32(), efConstMethods[0].DeclaringType.MDToken.ToInt32()), BinOp3(efConstMethods[2].DeclaringType.MDToken.ToInt32() ^ i3, ConstMethod5())));
2012-02-27 19:55:37 +08:00
}
2013-01-19 20:03:57 +08:00
public ulong GetMagic() {
2012-02-27 19:55:37 +08:00
if (type == null)
throw new ApplicationException("Can't calculate magic since type isn't initialized");
var bytes = new List<byte>();
if (module.Assembly != null) {
2012-11-18 08:09:07 +08:00
if (!PublicKeyBase.IsNullOrEmpty2(module.Assembly.PublicKey))
bytes.AddRange(module.Assembly.PublicKeyToken.Data);
bytes.AddRange(Encoding.Unicode.GetBytes(module.Assembly.Name.String));
2012-02-27 19:55:37 +08:00
}
2013-01-19 20:03:57 +08:00
int cm1 = ConstMethod1();
bytes.Add((byte)(type.MDToken.ToInt32() >> 24));
2012-02-27 19:55:37 +08:00
bytes.Add((byte)(cm1 >> 16));
bytes.Add((byte)(type.MDToken.ToInt32() >> 8));
2012-02-27 19:55:37 +08:00
bytes.Add((byte)cm1);
bytes.Add((byte)(type.MDToken.ToInt32() >> 16));
2012-02-27 19:55:37 +08:00
bytes.Add((byte)(cm1 >> 8));
bytes.Add((byte)type.MDToken.ToInt32());
2012-02-27 19:55:37 +08:00
bytes.Add((byte)(cm1 >> 24));
ulong magic = 0;
foreach (var b in bytes) {
magic += b;
magic += magic << 20;
magic ^= magic >> 12;
}
magic += magic << 6;
magic ^= magic >> 22;
magic += magic << 30;
return magic ^ l1;
}
}
}