2012-05-03 14:01:35 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2012-05-03 14:01:35 +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 de4dot.blocks;
|
|
|
|
|
using de4dot.blocks.cflow;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2012-05-03 14:01:35 +08:00
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.DeepSea {
|
|
|
|
|
class CastDeobfuscator : IBlocksDeobfuscator {
|
|
|
|
|
Blocks blocks;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
Dictionary<Local, LocalInfo> localInfos = new Dictionary<Local, LocalInfo>();
|
2012-05-03 14:01:35 +08:00
|
|
|
|
|
|
|
|
|
class LocalInfo {
|
2012-11-09 07:21:45 +08:00
|
|
|
|
public readonly Local local;
|
|
|
|
|
ITypeDefOrRef type;
|
2012-05-03 14:01:35 +08:00
|
|
|
|
bool isValid;
|
|
|
|
|
|
2012-11-09 07:21:45 +08:00
|
|
|
|
public ITypeDefOrRef CastType {
|
2012-05-03 14:01:35 +08:00
|
|
|
|
get { return type; }
|
|
|
|
|
set {
|
|
|
|
|
if (!isValid)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (value == null) {
|
|
|
|
|
invalid();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (type != null && !new SigComparer().Equals(type, value)) {
|
2012-05-03 14:01:35 +08:00
|
|
|
|
invalid();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 07:21:45 +08:00
|
|
|
|
public LocalInfo(Local local) {
|
2012-05-03 14:01:35 +08:00
|
|
|
|
this.local = local;
|
|
|
|
|
this.isValid = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void invalid() {
|
|
|
|
|
isValid = false;
|
|
|
|
|
type = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString() {
|
|
|
|
|
if (type == null)
|
|
|
|
|
return string.Format("{0} - INVALID", local);
|
2012-11-02 22:57:11 +08:00
|
|
|
|
return string.Format("{0} - {1:X8} {2}", local, type.MDToken.ToInt32(), type.FullName);
|
2012-05-03 14:01:35 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool ExecuteOnNoChange {
|
|
|
|
|
get { return true; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void deobfuscateBegin(Blocks blocks) {
|
|
|
|
|
this.blocks = blocks;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool deobfuscate(List<Block> allBlocks) {
|
|
|
|
|
if (!init(allBlocks))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
bool changed = false;
|
|
|
|
|
|
|
|
|
|
var indexesToRemove = new List<int>();
|
|
|
|
|
foreach (var block in allBlocks) {
|
|
|
|
|
indexesToRemove.Clear();
|
|
|
|
|
var instrs = block.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 1; i++) {
|
|
|
|
|
var instr = instrs[i];
|
|
|
|
|
if (instr.OpCode.Code == Code.Ldloca || instr.OpCode.Code == Code.Ldloca_S) {
|
2012-11-09 07:21:45 +08:00
|
|
|
|
var local = instr.Operand as Local;
|
2012-05-03 14:01:35 +08:00
|
|
|
|
if (local == null)
|
|
|
|
|
continue;
|
|
|
|
|
localInfos[local].invalid();
|
|
|
|
|
}
|
|
|
|
|
else if (instr.isLdloc()) {
|
2012-11-09 07:21:45 +08:00
|
|
|
|
var local = instr.Instruction.GetLocal(blocks.Locals);
|
2012-05-03 14:01:35 +08:00
|
|
|
|
if (local == null)
|
|
|
|
|
continue;
|
|
|
|
|
var localInfo = localInfos[local];
|
|
|
|
|
var cast = instrs[i + 1];
|
|
|
|
|
if (localInfo.CastType == null)
|
|
|
|
|
continue;
|
|
|
|
|
if (!isCast(cast))
|
|
|
|
|
throw new ApplicationException("Not a cast instr");
|
|
|
|
|
|
|
|
|
|
indexesToRemove.Add(i + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (indexesToRemove.Count > 0) {
|
|
|
|
|
block.remove(indexesToRemove);
|
|
|
|
|
changed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var info in localInfos.Values) {
|
|
|
|
|
if (info.CastType == null)
|
|
|
|
|
continue;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
info.local.Type = info.CastType.ToTypeSig();
|
2012-05-03 14:01:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
|
foreach (var block in allBlocks) {
|
|
|
|
|
var instrs = block.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 1; i++) {
|
|
|
|
|
var instr = instrs[i];
|
|
|
|
|
int castIndex = i + 1;
|
|
|
|
|
if (instr.OpCode.Code == Code.Dup) {
|
|
|
|
|
if (i == 0)
|
|
|
|
|
continue;
|
|
|
|
|
castIndex = i;
|
|
|
|
|
instr = instrs[i - 1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (instr.isLdarg())
|
2012-11-09 07:21:45 +08:00
|
|
|
|
addCast(block, castIndex, i + 1, instr.Instruction.GetArgumentType(blocks.Method.MethodSig, blocks.Method.DeclaringType));
|
2012-05-03 14:01:35 +08:00
|
|
|
|
else if (instr.OpCode.Code == Code.Ldfld || instr.OpCode.Code == Code.Ldsfld) {
|
2012-11-09 07:21:45 +08:00
|
|
|
|
var field = instr.Operand as IField;
|
2012-05-03 14:01:35 +08:00
|
|
|
|
if (field == null)
|
|
|
|
|
continue;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
addCast(block, castIndex, i + 1, field.FieldSig.GetFieldType());
|
2012-05-03 14:01:35 +08:00
|
|
|
|
}
|
|
|
|
|
else if (instr.OpCode.Code == Code.Call || instr.OpCode.Code == Code.Callvirt) {
|
2012-11-09 07:21:45 +08:00
|
|
|
|
var calledMethod = instr.Operand as IMethod;
|
2012-05-03 14:01:35 +08:00
|
|
|
|
if (calledMethod == null || !DotNetUtils.hasReturnValue(calledMethod))
|
|
|
|
|
continue;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
addCast(block, castIndex, i + 1, calledMethod.MethodSig.GetRetType());
|
2012-05-03 14:01:35 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 07:21:45 +08:00
|
|
|
|
bool addCast(Block block, int castIndex, int index, TypeSig type) {
|
2012-05-03 14:01:35 +08:00
|
|
|
|
if (type == null)
|
|
|
|
|
return false;
|
|
|
|
|
if (castIndex >= block.Instructions.Count || index >= block.Instructions.Count)
|
|
|
|
|
return false;
|
|
|
|
|
var stloc = block.Instructions[index];
|
|
|
|
|
if (!stloc.isStloc())
|
|
|
|
|
return false;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
var local = stloc.Instruction.GetLocal(blocks.Locals);
|
2012-05-03 14:01:35 +08:00
|
|
|
|
if (local == null)
|
|
|
|
|
return false;
|
|
|
|
|
var localInfo = localInfos[local];
|
|
|
|
|
if (localInfo.CastType == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
2012-11-09 07:21:45 +08:00
|
|
|
|
if (!new SigComparer().Equals(localInfo.CastType, type))
|
2012-05-03 14:01:35 +08:00
|
|
|
|
block.insert(castIndex, new Instruction(OpCodes.Castclass, localInfo.CastType));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool init(List<Block> allBlocks) {
|
|
|
|
|
localInfos.Clear();
|
|
|
|
|
foreach (var local in blocks.Locals)
|
|
|
|
|
localInfos[local] = new LocalInfo(local);
|
|
|
|
|
if (localInfos.Count == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
foreach (var block in allBlocks) {
|
|
|
|
|
var instrs = block.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 1; i++) {
|
|
|
|
|
var ldloc = instrs[i];
|
|
|
|
|
if (!ldloc.isLdloc())
|
|
|
|
|
continue;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
var local = ldloc.Instruction.GetLocal(blocks.Locals);
|
2012-05-03 14:01:35 +08:00
|
|
|
|
if (local == null)
|
|
|
|
|
continue;
|
|
|
|
|
var localInfo = localInfos[local];
|
|
|
|
|
localInfo.CastType = getCastType(instrs[i + 1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool isCast(Instr instr) {
|
|
|
|
|
return instr.OpCode.Code == Code.Castclass || instr.OpCode.Code == Code.Isinst;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 07:21:45 +08:00
|
|
|
|
static ITypeDefOrRef getCastType(Instr instr) {
|
2012-05-03 14:01:35 +08:00
|
|
|
|
if (!isCast(instr))
|
|
|
|
|
return null;
|
2012-11-09 07:21:45 +08:00
|
|
|
|
return instr.Operand as ITypeDefOrRef;
|
2012-05-03 14:01:35 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|