de4dot-cex/de4dot.code/deobfuscators/DeepSea/ArrayBlockDeobfuscator.cs

228 lines
5.6 KiB
C#
Raw Normal View History

2012-04-30 07:29:05 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2012-04-30 07:29:05 +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;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2012-04-30 07:29:05 +08:00
using de4dot.blocks;
using de4dot.blocks.cflow;
namespace de4dot.code.deobfuscators.DeepSea {
class ArrayBlockDeobfuscator : BlockDeobfuscator {
2012-07-17 00:04:20 +08:00
ArrayBlockState arrayBlockState;
2012-11-09 07:21:45 +08:00
Dictionary<Local, ArrayBlockState.FieldInfo> localToInfo = new Dictionary<Local, ArrayBlockState.FieldInfo>();
2012-05-02 16:48:44 +08:00
DsConstantsReader constantsReader;
2012-04-30 07:29:05 +08:00
2012-07-17 00:04:20 +08:00
public ArrayBlockDeobfuscator(ArrayBlockState arrayBlockState) {
this.arrayBlockState = arrayBlockState;
2012-04-30 07:29:05 +08:00
}
2013-01-19 20:03:57 +08:00
public override void DeobfuscateBegin(Blocks blocks) {
base.DeobfuscateBegin(blocks);
InitLocalToInfo();
2012-04-30 07:29:05 +08:00
}
2013-01-19 20:03:57 +08:00
void InitLocalToInfo() {
2012-04-30 07:29:05 +08:00
localToInfo.Clear();
2013-01-19 20:03:57 +08:00
foreach (var block in blocks.MethodBlocks.GetAllBlocks()) {
2012-04-30 07:29:05 +08:00
var instrs = block.Instructions;
for (int i = 0; i < instrs.Count - 1; i++) {
var ldsfld = instrs[i];
if (ldsfld.OpCode.Code != Code.Ldsfld)
continue;
var stloc = instrs[i + 1];
2013-01-19 20:03:57 +08:00
if (!stloc.IsStloc())
2012-04-30 07:29:05 +08:00
continue;
2013-01-19 20:03:57 +08:00
var info = arrayBlockState.GetFieldInfo((IField)ldsfld.Operand);
2012-04-30 07:29:05 +08:00
if (info == null)
continue;
2012-11-09 07:21:45 +08:00
var local = stloc.Instruction.GetLocal(blocks.Locals);
2012-04-30 07:29:05 +08:00
if (local == null)
continue;
localToInfo[local] = info;
}
}
}
2013-01-19 20:03:57 +08:00
protected override bool Deobfuscate(Block block) {
2013-04-30 18:15:07 +08:00
bool modified = false;
2012-04-30 07:29:05 +08:00
constantsReader = null;
var instrs = block.Instructions;
for (int i = 0; i < instrs.Count; i++) {
2013-01-19 20:03:57 +08:00
bool ch = Deobfuscate1(block, i);
2012-04-30 07:29:05 +08:00
if (ch) {
2013-04-30 18:15:07 +08:00
modified = true;
2012-04-30 07:29:05 +08:00
continue;
}
2013-01-19 20:03:57 +08:00
ch = Deobfuscate2(block, i);
2012-04-30 07:29:05 +08:00
if (ch) {
2013-04-30 18:15:07 +08:00
modified = true;
2012-04-30 07:29:05 +08:00
continue;
}
2013-01-19 20:03:57 +08:00
ch = Deobfuscate3(block, i);
2012-04-30 07:29:05 +08:00
if (ch) {
2013-04-30 18:15:07 +08:00
modified = true;
2012-04-30 07:29:05 +08:00
continue;
}
}
2013-04-30 18:15:07 +08:00
return modified;
2012-04-30 07:29:05 +08:00
}
2012-12-14 16:18:14 +08:00
static bool IsLdelem(ArrayBlockState.FieldInfo info, Code code) {
switch (info.elementType) {
case ElementType.Boolean:
case ElementType.I1:
case ElementType.U1:
return code == Code.Ldelem_I1 || code == Code.Ldelem_U1;
case ElementType.Char:
case ElementType.I2:
case ElementType.U2:
return code == Code.Ldelem_I2 || code == Code.Ldelem_U2;
case ElementType.I4:
case ElementType.U4:
return code == Code.Ldelem_I4 || code == Code.Ldelem_U4;
default:
return false;
}
}
static bool IsStelem(ArrayBlockState.FieldInfo info, Code code) {
switch (info.elementType) {
case ElementType.Boolean:
case ElementType.I1:
case ElementType.U1:
return code == Code.Stelem_I1;
case ElementType.Char:
case ElementType.I2:
case ElementType.U2:
return code == Code.Stelem_I2;
case ElementType.I4:
case ElementType.U4:
return code == Code.Stelem_I4;
default:
return false;
}
}
2013-01-19 20:03:57 +08:00
bool Deobfuscate1(Block block, int i) {
2012-04-30 07:29:05 +08:00
var instrs = block.Instructions;
if (i >= instrs.Count - 2)
return false;
var ldloc = instrs[i];
2013-01-19 20:03:57 +08:00
if (!ldloc.IsLdloc())
2012-04-30 07:29:05 +08:00
return false;
2012-11-09 07:21:45 +08:00
var local = ldloc.Instruction.GetLocal(blocks.Locals);
2012-04-30 07:29:05 +08:00
if (local == null)
return false;
2012-07-17 00:04:20 +08:00
ArrayBlockState.FieldInfo info;
2012-04-30 07:29:05 +08:00
if (!localToInfo.TryGetValue(local, out info))
return false;
var ldci4 = instrs[i + 1];
2013-01-19 20:03:57 +08:00
if (!ldci4.IsLdcI4())
2012-04-30 07:29:05 +08:00
return false;
var ldelem = instrs[i + 2];
2012-12-14 16:18:14 +08:00
if (!IsLdelem(info, ldelem.OpCode.Code))
2012-04-30 07:29:05 +08:00
return false;
2013-01-19 20:03:57 +08:00
block.Remove(i, 3 - 1);
instrs[i] = new Instr(Instruction.CreateLdcI4((int)info.ReadArrayElement(ldci4.GetLdcI4Value())));
2012-04-30 07:29:05 +08:00
return true;
}
2013-01-19 20:03:57 +08:00
bool Deobfuscate2(Block block, int i) {
2012-04-30 07:29:05 +08:00
var instrs = block.Instructions;
if (i >= instrs.Count - 2)
return false;
var ldsfld = instrs[i];
if (ldsfld.OpCode.Code != Code.Ldsfld)
return false;
2013-01-19 20:03:57 +08:00
var info = arrayBlockState.GetFieldInfo(ldsfld.Operand as IField);
2012-04-30 07:29:05 +08:00
if (info == null)
return false;
var ldci4 = instrs[i + 1];
2013-01-19 20:03:57 +08:00
if (!ldci4.IsLdcI4())
2012-04-30 07:29:05 +08:00
return false;
var ldelem = instrs[i + 2];
2012-12-14 16:18:14 +08:00
if (!IsLdelem(info, ldelem.OpCode.Code))
2012-04-30 07:29:05 +08:00
return false;
2013-01-19 20:03:57 +08:00
block.Remove(i, 3 - 1);
instrs[i] = new Instr(Instruction.CreateLdcI4((int)info.ReadArrayElement(ldci4.GetLdcI4Value())));
2012-04-30 07:29:05 +08:00
return true;
}
2013-01-19 20:03:57 +08:00
bool Deobfuscate3(Block block, int i) {
2012-04-30 07:29:05 +08:00
var instrs = block.Instructions;
2012-05-02 16:48:44 +08:00
if (i + 1 >= instrs.Count)
2012-04-30 07:29:05 +08:00
return false;
int start = i;
var ldsfld = instrs[i];
if (ldsfld.OpCode.Code != Code.Ldsfld)
return false;
2013-01-19 20:03:57 +08:00
var info = arrayBlockState.GetFieldInfo(ldsfld.Operand as IField);
2012-04-30 07:29:05 +08:00
if (info == null)
return false;
2013-01-19 20:03:57 +08:00
if (!instrs[i + 1].IsLdcI4())
2012-05-02 16:48:44 +08:00
return false;
2013-01-19 20:03:57 +08:00
var constants = GetConstantsReader(block);
2012-04-30 07:29:05 +08:00
int value;
2012-05-02 16:48:44 +08:00
i += 2;
2013-01-19 20:03:57 +08:00
if (!constants.GetInt32(ref i, out value))
2012-04-30 07:29:05 +08:00
return false;
if (i >= instrs.Count)
return false;
var stelem = instrs[i];
2012-12-14 16:18:14 +08:00
if (!IsStelem(info, stelem.OpCode.Code))
2012-04-30 07:29:05 +08:00
return false;
2013-01-19 20:03:57 +08:00
block.Remove(start, i - start + 1);
2012-04-30 07:29:05 +08:00
return true;
}
2013-01-19 20:03:57 +08:00
DsConstantsReader GetConstantsReader(Block block) {
2012-04-30 07:29:05 +08:00
if (constantsReader != null)
return constantsReader;
2012-05-02 16:48:44 +08:00
return constantsReader = new DsConstantsReader(block.Instructions);
2012-04-30 07:29:05 +08:00
}
}
}