2011-10-17 06:22:22 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-04-30 04:22:43 +08:00
|
|
|
|
using System;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet.Emit;
|
2011-10-17 06:22:22 +08:00
|
|
|
|
|
|
|
|
|
namespace de4dot.blocks.cflow {
|
2012-06-12 03:19:36 +08:00
|
|
|
|
class BlockCflowDeobfuscator : BlockDeobfuscator, IBranchHandler {
|
2011-10-17 06:22:22 +08:00
|
|
|
|
Block block;
|
2012-06-12 03:19:36 +08:00
|
|
|
|
InstructionEmulator instructionEmulator;
|
|
|
|
|
BranchEmulator branchEmulator;
|
|
|
|
|
|
|
|
|
|
public BlockCflowDeobfuscator() {
|
|
|
|
|
instructionEmulator = new InstructionEmulator();
|
|
|
|
|
branchEmulator = new BranchEmulator(instructionEmulator, this);
|
|
|
|
|
}
|
2011-10-17 06:22:22 +08:00
|
|
|
|
|
2012-04-30 04:22:43 +08:00
|
|
|
|
protected override bool deobfuscate(Block block) {
|
2011-10-17 06:22:22 +08:00
|
|
|
|
this.block = block;
|
2012-11-01 01:52:50 +08:00
|
|
|
|
if (!block.LastInstr.isConditionalBranch() && block.LastInstr.OpCode.Code != Code.Switch)
|
2012-04-30 04:22:43 +08:00
|
|
|
|
return false;
|
2012-01-27 08:02:17 +08:00
|
|
|
|
instructionEmulator.init(blocks);
|
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;
|
|
|
|
|
instructionEmulator.emulate(instr);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-04-30 04:22:43 +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
|
|
|
|
}
|
|
|
|
|
|
2012-06-12 03:19:36 +08:00
|
|
|
|
return branchEmulator.emulate(block.LastInstr.Instruction);
|
2011-10-18 14:17:21 +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++)
|
2012-12-16 07:03:56 +08:00
|
|
|
|
block.insert(block.Instructions.Count - 1, OpCodes.Pop.ToInstruction());
|
2011-10-17 06:22:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-06-12 03:19:36 +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
|
|
|
|
|
2012-06-12 03:19:36 +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;
|
|
|
|
|
|
|
|
|
|
popPushedArgs(1);
|
2011-10-19 05:31:50 +08:00
|
|
|
|
block.replaceSwitchWithBranch(target);
|
2011-10-18 14:17:21 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2011-10-17 06:22:22 +08:00
|
|
|
|
}
|
|
|
|
|
}
|