2012-12-11 07:23:16 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2012-12-11 07:23:16 +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.Collections.Generic;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2012-12-11 07:23:16 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.Eazfuscator_NET {
|
|
|
|
|
interface IDynocodeGenerator {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
IEnumerable<int> GetValues(int input);
|
2012-12-11 07:23:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Something added in EF 3.5 which they call Dynocode. The string decrypter can now
|
|
|
|
|
// call some iterator classes that will return some integers that it will use to
|
|
|
|
|
// XOR some key.
|
|
|
|
|
class Dynocode {
|
|
|
|
|
ISimpleDeobfuscator simpleDeobfuscator;
|
|
|
|
|
Dictionary<TypeDef, IDynocodeGenerator> typeToDCGen = new Dictionary<TypeDef, IDynocodeGenerator>();
|
|
|
|
|
|
|
|
|
|
class DCGen1 : IDynocodeGenerator {
|
|
|
|
|
public int magic1;
|
|
|
|
|
public int magic2;
|
|
|
|
|
public int magic3;
|
|
|
|
|
public int magic4;
|
|
|
|
|
public int magic5;
|
|
|
|
|
public int magic6;
|
|
|
|
|
public int magic7;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public IEnumerable<int> GetValues(int input) {
|
2012-12-11 07:23:16 +08:00
|
|
|
|
yield return magic1;
|
|
|
|
|
yield return magic2;
|
|
|
|
|
yield return input ^ magic3;
|
|
|
|
|
yield return magic4;
|
|
|
|
|
yield return magic5;
|
|
|
|
|
yield return magic6;
|
|
|
|
|
yield return input ^ magic7;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DCGen2 : IDynocodeGenerator {
|
|
|
|
|
public int magic1;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public IEnumerable<int> GetValues(int input) {
|
2012-12-11 07:23:16 +08:00
|
|
|
|
int x = 0;
|
|
|
|
|
int y = 1;
|
|
|
|
|
while (true) {
|
|
|
|
|
yield return y;
|
|
|
|
|
if (--input == 0)
|
|
|
|
|
break;
|
|
|
|
|
int tmp = y;
|
|
|
|
|
y = (x + y + input) ^ magic1;
|
|
|
|
|
x = tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DCGen3 : IDynocodeGenerator {
|
|
|
|
|
public int magic1;
|
|
|
|
|
public int magic2;
|
|
|
|
|
public DCGen2 dc2;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public IEnumerable<int> GetValues(int input) {
|
2012-12-11 07:23:16 +08:00
|
|
|
|
int i = 7;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var val in dc2.GetValues(input)) {
|
2012-12-11 07:23:16 +08:00
|
|
|
|
int x = val ^ input;
|
|
|
|
|
if ((x % 4) == 0)
|
|
|
|
|
x ^= magic1;
|
|
|
|
|
if ((x % 16) == 0)
|
|
|
|
|
x ^= magic2;
|
|
|
|
|
yield return x;
|
|
|
|
|
if (--i == 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<TypeDef> Types {
|
2012-12-11 19:36:59 +08:00
|
|
|
|
get {
|
|
|
|
|
foreach (var type in typeToDCGen.Keys)
|
|
|
|
|
yield return type.DeclaringType;
|
|
|
|
|
}
|
2012-12-11 07:23:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Dynocode(ISimpleDeobfuscator simpleDeobfuscator) {
|
|
|
|
|
this.simpleDeobfuscator = simpleDeobfuscator;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public IDynocodeGenerator GetDynocodeGenerator(TypeDef type) {
|
2012-12-11 07:23:16 +08:00
|
|
|
|
if (type == null)
|
|
|
|
|
return null;
|
|
|
|
|
var dt = type.DeclaringType;
|
|
|
|
|
if (dt == null)
|
|
|
|
|
return null;
|
|
|
|
|
IDynocodeGenerator dcGen;
|
|
|
|
|
if (typeToDCGen.TryGetValue(type, out dcGen))
|
|
|
|
|
return dcGen;
|
|
|
|
|
|
|
|
|
|
if (dt.NestedTypes.Count == 1)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
dcGen = GetDCGen1(type);
|
2012-12-11 07:23:16 +08:00
|
|
|
|
else if (dt.NestedTypes.Count == 2)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
dcGen = GetDCGen3(type);
|
2012-12-11 07:23:16 +08:00
|
|
|
|
|
|
|
|
|
typeToDCGen[type] = dcGen;
|
|
|
|
|
|
|
|
|
|
return dcGen;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
DCGen1 GetDCGen1(TypeDef type) {
|
|
|
|
|
var method = GetMoveNext(type);
|
2012-12-11 07:23:16 +08:00
|
|
|
|
if (method == null)
|
|
|
|
|
return null;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
simpleDeobfuscator.Deobfuscate(method);
|
|
|
|
|
var swLabels = GetSwitchLabels(method);
|
2012-12-11 07:23:16 +08:00
|
|
|
|
if (swLabels == null || swLabels.Count < 7)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var dcGen = new DCGen1();
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!GetMagicDC1(method, swLabels[0], out dcGen.magic1))
|
2012-12-11 07:23:16 +08:00
|
|
|
|
return null;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!GetMagicDC1(method, swLabels[1], out dcGen.magic2))
|
2012-12-11 07:23:16 +08:00
|
|
|
|
return null;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!GetMagicXorDC1(method, swLabels[2], out dcGen.magic3))
|
2012-12-11 07:23:16 +08:00
|
|
|
|
return null;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!GetMagicDC1(method, swLabels[3], out dcGen.magic4))
|
2012-12-11 07:23:16 +08:00
|
|
|
|
return null;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!GetMagicDC1(method, swLabels[4], out dcGen.magic5))
|
2012-12-11 07:23:16 +08:00
|
|
|
|
return null;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!GetMagicDC1(method, swLabels[5], out dcGen.magic6))
|
2012-12-11 07:23:16 +08:00
|
|
|
|
return null;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!GetMagicXorDC1(method, swLabels[6], out dcGen.magic7))
|
2012-12-11 07:23:16 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return dcGen;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static IList<Instruction> GetSwitchLabels(MethodDef method) {
|
2012-12-11 07:23:16 +08:00
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
|
|
|
|
if (instr.OpCode.Code != Code.Switch)
|
|
|
|
|
continue;
|
|
|
|
|
return instr.Operand as IList<Instruction>;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static bool GetMagicDC1(MethodDef method, Instruction target, out int magic) {
|
2012-12-11 07:23:16 +08:00
|
|
|
|
magic = 0;
|
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
int index = instrs.IndexOf(target);
|
|
|
|
|
if (index < 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
for (int i = index; i < instrs.Count - 3; i++) {
|
|
|
|
|
var instr = instrs[i];
|
|
|
|
|
if (instr.OpCode.FlowControl != FlowControl.Next)
|
|
|
|
|
return false;
|
|
|
|
|
if (instr.OpCode.Code != Code.Stfld)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 1].OpCode.Code != Code.Ldarg_0)
|
|
|
|
|
continue;
|
|
|
|
|
var ldci4 = instrs[i + 2];
|
|
|
|
|
if (!ldci4.IsLdcI4())
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 3].OpCode.Code != Code.Stfld)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
magic = ldci4.GetLdcI4Value();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static bool GetMagicXorDC1(MethodDef method, Instruction target, out int magic) {
|
2012-12-11 07:23:16 +08:00
|
|
|
|
magic = 0;
|
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
int index = instrs.IndexOf(target);
|
|
|
|
|
if (index < 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
for (int i = index; i < instrs.Count - 2; i++) {
|
|
|
|
|
var instr = instrs[i];
|
|
|
|
|
if (instr.OpCode.FlowControl != FlowControl.Next)
|
|
|
|
|
return false;
|
|
|
|
|
if (!instr.IsLdcI4())
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 1].OpCode.Code != Code.Xor)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 2].OpCode.Code != Code.Stfld)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
magic = instr.GetLdcI4Value();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
DCGen3 GetDCGen3(TypeDef type) {
|
|
|
|
|
var method = GetMoveNext(type);
|
2012-12-11 07:23:16 +08:00
|
|
|
|
if (method == null)
|
|
|
|
|
return null;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
simpleDeobfuscator.Deobfuscate(method);
|
2012-12-11 07:23:16 +08:00
|
|
|
|
|
|
|
|
|
var dcGen = new DCGen3();
|
|
|
|
|
int index = 0;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!GetMagicDC3(method, ref index, out dcGen.magic1))
|
2012-12-11 07:23:16 +08:00
|
|
|
|
return null;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!GetMagicDC3(method, ref index, out dcGen.magic2))
|
2012-12-11 07:23:16 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var dt = type.DeclaringType;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
dcGen.dc2 = GetDCGen2(dt.NestedTypes[0] == type ? dt.NestedTypes[1] : dt.NestedTypes[0]);
|
2012-12-11 07:23:16 +08:00
|
|
|
|
|
|
|
|
|
return dcGen;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static bool GetMagicDC3(MethodDef method, ref int index, out int magic) {
|
2012-12-11 07:23:16 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = index; i < instrs.Count - 2; i++) {
|
|
|
|
|
var ldci4 = instrs[i];
|
|
|
|
|
if (!ldci4.IsLdcI4())
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 1].OpCode.Code != Code.Xor)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 2].OpCode.Code != Code.Stfld)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
index = i + 3;
|
|
|
|
|
magic = ldci4.GetLdcI4Value();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
magic = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
DCGen2 GetDCGen2(TypeDef type) {
|
|
|
|
|
var method = GetMoveNext(type);
|
2012-12-11 07:23:16 +08:00
|
|
|
|
if (method == null)
|
|
|
|
|
return null;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
simpleDeobfuscator.Deobfuscate(method);
|
2012-12-11 07:23:16 +08:00
|
|
|
|
|
|
|
|
|
var dcGen = new DCGen2();
|
|
|
|
|
int index = 0;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!GetMagicDC3(method, ref index, out dcGen.magic1))
|
2012-12-11 07:23:16 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return dcGen;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static MethodDef GetMoveNext(TypeDef type) {
|
2012-12-11 07:23:16 +08:00
|
|
|
|
foreach (var m in type.Methods) {
|
|
|
|
|
if (!m.IsVirtual)
|
|
|
|
|
continue;
|
|
|
|
|
foreach (var mo in m.Overrides) {
|
|
|
|
|
if (mo.MethodDeclaration.FullName == "System.Boolean System.Collections.IEnumerator::MoveNext()")
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foreach (var m in type.Methods) {
|
|
|
|
|
if (!m.IsVirtual)
|
|
|
|
|
continue;
|
|
|
|
|
if (m.Name != "MoveNext")
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(m, "System.Boolean", "()"))
|
2012-12-11 07:23:16 +08:00
|
|
|
|
continue;
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|