de4dot-cex/de4dot.blocks/cflow/BlockCflowDeobfuscator.cs

80 lines
2.5 KiB
C#
Raw Permalink Normal View History

2011-10-17 06:22:22 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2011-10-17 06:22:22 +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 dnlib.DotNet.Emit;
2011-10-17 06:22:22 +08:00
namespace de4dot.blocks.cflow {
class BlockCflowDeobfuscator : BlockDeobfuscator, IBranchHandler {
2011-10-17 06:22:22 +08:00
Block block;
InstructionEmulator instructionEmulator;
BranchEmulator branchEmulator;
public BlockCflowDeobfuscator() {
instructionEmulator = new InstructionEmulator();
branchEmulator = new BranchEmulator(instructionEmulator, this);
}
2011-10-17 06:22:22 +08:00
2013-01-19 20:03:57 +08:00
protected override bool Deobfuscate(Block block) {
2011-10-17 06:22:22 +08:00
this.block = block;
2013-01-19 20:03:57 +08:00
if (!block.LastInstr.IsConditionalBranch() && block.LastInstr.OpCode.Code != Code.Switch)
return false;
instructionEmulator.Initialize(blocks, allBlocks[0] == block);
2011-10-17 06:22:22 +08:00
var instructions = block.Instructions;
if (instructions.Count == 0)
return false;
2011-10-27 21:57:33 +08:00
try {
for (int i = 0; i < instructions.Count - 1; i++) {
var instr = instructions[i].Instruction;
2013-01-19 20:03:57 +08:00
instructionEmulator.Emulate(instr);
2011-10-27 21:57:33 +08:00
}
}
catch (NullReferenceException) {
2011-10-27 21:57:33 +08:00
// Here if eg. invalid metadata token in a call instruction (operand is null)
return false;
2011-10-17 06:22:22 +08:00
}
2013-01-19 20:03:57 +08:00
return branchEmulator.Emulate(block.LastInstr.Instruction);
2011-10-18 14:17:21 +08:00
}
2013-01-19 20:03:57 +08:00
void PopPushedArgs(int stackArgs) {
2011-10-17 06:22:22 +08:00
// Pop the arguments to the bcc instruction. The dead code remover will get rid of the
// pop and any pushed arguments. Insert the pops just before the bcc instr.
for (int i = 0; i < stackArgs; i++)
2013-01-19 20:03:57 +08:00
block.Insert(block.Instructions.Count - 1, OpCodes.Pop.ToInstruction());
2011-10-17 06:22:22 +08:00
}
2013-01-19 20:03:57 +08:00
void IBranchHandler.HandleNormal(int stackArgs, bool isTaken) {
PopPushedArgs(stackArgs);
block.ReplaceBccWithBranch(isTaken);
2011-10-17 06:22:22 +08:00
}
2011-10-18 14:17:21 +08:00
2013-01-19 20:03:57 +08:00
bool IBranchHandler.HandleSwitch(Int32Value switchIndex) {
var target = CflowUtils.GetSwitchTarget(block.Targets, block.FallThrough, switchIndex);
2011-10-19 05:31:50 +08:00
if (target == null)
2011-10-18 14:17:21 +08:00
return false;
2013-01-19 20:03:57 +08:00
PopPushedArgs(1);
block.ReplaceSwitchWithBranch(target);
2011-10-18 14:17:21 +08:00
return true;
}
2011-10-17 06:22:22 +08:00
}
}