2012-01-09 08:25:25 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 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-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.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
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override void Initialize(List<Block> allBlocks) {
|
|
|
|
|
base.Initialize(allBlocks);
|
2012-11-01 01:52:50 +08:00
|
|
|
|
args = blocks.Method.Parameters;
|
2012-01-09 08:25:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
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 04:22:43 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
instructionEmulator.Initialize(blocks);
|
2012-04-30 04:22:43 +08:00
|
|
|
|
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:
|
2013-04-30 18:15:07 +08:00
|
|
|
|
modified |= 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:
|
2013-04-30 18:15:07 +08:00
|
|
|
|
modified |= 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:
|
2013-01-19 20:03:57 +08:00
|
|
|
|
instructionEmulator.MakeArgUnknown((Parameter)instr.Operand);
|
2012-04-30 04:22:43 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Ldloca:
|
|
|
|
|
case Code.Ldloca_S:
|
2013-01-19 20:03:57 +08:00
|
|
|
|
instructionEmulator.MakeLocalUnknown((Local)instr.Operand);
|
2012-04-30 04:22:43 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
instructionEmulator.Emulate(instr.Instruction);
|
2012-04-30 04:22:43 +08:00
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2013-04-30 18:15:07 +08:00
|
|
|
|
return modified;
|
2012-01-09 08:25:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool FixLoadInstruction(Block block, int index, Value value) {
|
|
|
|
|
if (value.IsInt32()) {
|
2012-01-09 08:25:25 +08:00
|
|
|
|
var intValue = (Int32Value)value;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!intValue.AllBitsValid())
|
2012-01-09 08:25:25 +08:00
|
|
|
|
return false;
|
2013-10-16 16:13:09 +08:00
|
|
|
|
block.Instructions[index] = new Instr(Instruction.CreateLdcI4(intValue.Value));
|
2012-01-09 08:25:25 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-01-19 20:03:57 +08:00
|
|
|
|
else if (value.IsInt64()) {
|
2012-01-09 08:25:25 +08:00
|
|
|
|
var intValue = (Int64Value)value;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!intValue.AllBitsValid())
|
2012-01-09 08:25:25 +08:00
|
|
|
|
return false;
|
2013-10-16 16:13:09 +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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|