2012-01-09 08:25:25 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
2012-01-09 08:25:25 +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-01-09 08:25:25 +08:00
|
|
|
|
using System.Collections.Generic;
|
2012-11-01 01:52:50 +08:00
|
|
|
|
using dot10.DotNet;
|
|
|
|
|
using dot10.DotNet.Emit;
|
2012-01-09 08:25:25 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.blocks.cflow {
|
|
|
|
|
// Very simple constants folder which is all that's needed at the moment
|
2012-04-30 04:22:43 +08:00
|
|
|
|
class ConstantsFolder : BlockDeobfuscator {
|
2012-01-09 08:25:25 +08:00
|
|
|
|
InstructionEmulator instructionEmulator = new InstructionEmulator();
|
2012-11-01 01:52:50 +08:00
|
|
|
|
IList<Parameter> args;
|
2012-01-09 08:25:25 +08:00
|
|
|
|
|
2012-04-30 04:22:43 +08:00
|
|
|
|
protected override void init(List<Block> allBlocks) {
|
|
|
|
|
base.init(allBlocks);
|
2012-11-01 01:52:50 +08:00
|
|
|
|
args = blocks.Method.Parameters;
|
2012-01-09 08:25:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-30 04:22:43 +08:00
|
|
|
|
protected override bool deobfuscate(Block block) {
|
2012-01-09 08:25:25 +08:00
|
|
|
|
bool changed = false;
|
2012-04-30 04:22:43 +08:00
|
|
|
|
|
|
|
|
|
instructionEmulator.init(blocks);
|
|
|
|
|
var instrs = block.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
|
|
|
|
var instr = instrs[i];
|
|
|
|
|
|
|
|
|
|
switch (instr.OpCode.Code) {
|
|
|
|
|
case Code.Ldarg:
|
|
|
|
|
case Code.Ldarg_0:
|
|
|
|
|
case Code.Ldarg_1:
|
|
|
|
|
case Code.Ldarg_2:
|
|
|
|
|
case Code.Ldarg_3:
|
|
|
|
|
case Code.Ldarg_S:
|
2012-11-01 01:52:50 +08:00
|
|
|
|
changed |= fixLoadInstruction(block, i, instructionEmulator.getArg(instr.Instruction.GetParameter(args)));
|
2012-04-30 04:22:43 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Ldloc:
|
|
|
|
|
case Code.Ldloc_0:
|
|
|
|
|
case Code.Ldloc_1:
|
|
|
|
|
case Code.Ldloc_2:
|
|
|
|
|
case Code.Ldloc_3:
|
|
|
|
|
case Code.Ldloc_S:
|
2012-11-01 01:52:50 +08:00
|
|
|
|
changed |= fixLoadInstruction(block, i, instructionEmulator.getLocal(instr.Instruction.GetLocal(blocks.Locals)));
|
2012-04-30 04:22:43 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Ldarga:
|
|
|
|
|
case Code.Ldarga_S:
|
2012-11-01 01:52:50 +08:00
|
|
|
|
instructionEmulator.makeArgUnknown((Parameter)instr.Operand);
|
2012-04-30 04:22:43 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Ldloca:
|
|
|
|
|
case Code.Ldloca_S:
|
2012-11-01 01:52:50 +08:00
|
|
|
|
instructionEmulator.makeLocalUnknown((Local)instr.Operand);
|
2012-04-30 04:22:43 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
instructionEmulator.emulate(instr.Instruction);
|
|
|
|
|
}
|
|
|
|
|
catch (NullReferenceException) {
|
|
|
|
|
// Here if eg. invalid metadata token in a call instruction (operand is null)
|
|
|
|
|
break;
|
2012-01-09 08:25:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-04-30 04:22:43 +08:00
|
|
|
|
|
2012-01-09 08:25:25 +08:00
|
|
|
|
return changed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool fixLoadInstruction(Block block, int index, Value value) {
|
|
|
|
|
if (value.isInt32()) {
|
|
|
|
|
var intValue = (Int32Value)value;
|
|
|
|
|
if (!intValue.allBitsValid())
|
|
|
|
|
return false;
|
2012-11-01 01:52:50 +08:00
|
|
|
|
block.Instructions[index] = new Instr(Instruction.CreateLdcI4(intValue.value));
|
2012-01-09 08:25:25 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (value.isInt64()) {
|
|
|
|
|
var intValue = (Int64Value)value;
|
|
|
|
|
if (!intValue.allBitsValid())
|
|
|
|
|
return false;
|
2012-12-16 07:03:56 +08:00
|
|
|
|
block.Instructions[index] = new Instr(OpCodes.Ldc_I8.ToInstruction(intValue.value));
|
2012-01-09 08:25:25 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|