de4dot-cex/de4dot.code/deobfuscators/DeepSea/DsMethodCallInliner.cs

353 lines
10 KiB
C#
Raw Normal View History

2012-05-02 16:48:44 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2012-05-02 16:48:44 +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.Collections.Generic;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2012-05-02 16:48:44 +08:00
using de4dot.blocks;
using de4dot.blocks.cflow;
namespace de4dot.code.deobfuscators.DeepSea {
class DsMethodCallInliner : MethodCallInlinerBase {
InstructionEmulator instructionEmulator = new InstructionEmulator();
2012-11-09 07:21:45 +08:00
IList<Parameter> parameters;
Parameter arg1, arg2;
2012-05-02 16:48:44 +08:00
Value returnValue;
MethodDef methodToInline;
2012-05-02 19:51:07 +08:00
CachedCflowDeobfuscator cflowDeobfuscator;
public DsMethodCallInliner(CachedCflowDeobfuscator cflowDeobfuscator) {
this.cflowDeobfuscator = cflowDeobfuscator;
}
2012-05-02 16:48:44 +08:00
2013-01-19 20:03:57 +08:00
protected override bool DeobfuscateInternal() {
2013-04-30 18:15:07 +08:00
bool modified = false;
2012-05-02 16:48:44 +08:00
var instructions = block.Instructions;
for (int i = 0; i < instructions.Count; i++) {
var instr = instructions[i].Instruction;
if (instr.OpCode.Code == Code.Call)
2013-04-30 18:15:07 +08:00
modified |= InlineMethod(instr, i);
2012-05-02 16:48:44 +08:00
}
2013-04-30 18:15:07 +08:00
return modified;
2012-05-02 16:48:44 +08:00
}
2013-01-19 20:03:57 +08:00
bool InlineMethod(Instruction callInstr, int instrIndex) {
var method = callInstr.Operand as MethodDef;
2012-12-14 19:39:06 +08:00
if (method == null) {
var ms = callInstr.Operand as MethodSpec;
if (ms != null)
method = ms.Method as MethodDef;
if (method == null)
return false;
}
2013-01-19 20:03:57 +08:00
if (!CanInline(method))
2012-05-02 16:48:44 +08:00
return false;
if (instrIndex < 2)
return false;
var ldci4_1st = block.Instructions[instrIndex - 2];
var ldci4_2nd = block.Instructions[instrIndex - 1];
2013-01-19 20:03:57 +08:00
if (!ldci4_1st.IsLdcI4() || !ldci4_2nd.IsLdcI4())
2012-05-02 16:48:44 +08:00
return false;
2013-01-19 20:03:57 +08:00
if (!InlineMethod(method, instrIndex, ldci4_1st.GetLdcI4Value(), ldci4_2nd.GetLdcI4Value()))
2012-05-02 16:48:44 +08:00
return false;
return true;
}
2013-01-19 20:03:57 +08:00
protected override Instruction OnAfterLoadArg(MethodDef methodToInline, Instruction instr, ref int instrIndex) {
2012-12-14 19:39:06 +08:00
if (instr.OpCode.Code != Code.Box)
return instr;
if (methodToInline.MethodSig.GetGenParamCount() == 0)
return instr;
2013-01-19 20:03:57 +08:00
return DotNetUtils.GetInstruction(methodToInline.Body.Instructions, ref instrIndex);
2012-12-14 19:39:06 +08:00
}
2013-01-19 20:03:57 +08:00
bool InlineMethod(MethodDef methodToInline, int instrIndex, int const1, int const2) {
this.methodToInline = methodToInline = cflowDeobfuscator.Deobfuscate(methodToInline);
2012-05-02 16:48:44 +08:00
2012-11-09 07:21:45 +08:00
parameters = methodToInline.Parameters;
2012-05-02 16:48:44 +08:00
arg1 = parameters[parameters.Count - 2];
arg2 = parameters[parameters.Count - 1];
returnValue = null;
2013-01-19 20:03:57 +08:00
instructionEmulator.Initialize(methodToInline);
2012-05-02 16:48:44 +08:00
foreach (var arg in parameters) {
2012-11-09 07:21:45 +08:00
if (!arg.IsNormalMethodParameter)
continue;
if (arg.Type.ElementType >= ElementType.Boolean && arg.Type.ElementType <= ElementType.U4)
2013-01-19 20:03:57 +08:00
instructionEmulator.SetArg(arg, new Int32Value(0));
2012-05-02 16:48:44 +08:00
}
2013-01-19 20:03:57 +08:00
instructionEmulator.SetArg(arg1, new Int32Value(const1));
instructionEmulator.SetArg(arg2, new Int32Value(const2));
2012-05-02 16:48:44 +08:00
int index = 0;
2013-01-19 20:03:57 +08:00
if (!EmulateInstructions(ref index, false))
2012-05-02 16:48:44 +08:00
return false;
2013-01-19 20:03:57 +08:00
var patcher = TryInlineOtherMethod(instrIndex, methodToInline, methodToInline.Body.Instructions[index], index + 1, 2);
2012-05-02 16:48:44 +08:00
if (patcher == null)
return false;
2013-01-19 20:03:57 +08:00
if (!EmulateToReturn(patcher.afterIndex, patcher.lastInstr))
2012-05-02 16:48:44 +08:00
return false;
2013-01-19 20:03:57 +08:00
patcher.Patch(block);
block.Insert(instrIndex, OpCodes.Pop.ToInstruction());
block.Insert(instrIndex, OpCodes.Pop.ToInstruction());
2012-05-02 16:48:44 +08:00
return true;
}
2013-01-19 20:03:57 +08:00
bool EmulateInstructions(ref int index, bool allowUnknownArgs) {
2012-05-02 16:48:44 +08:00
Instruction instr;
var instrs = methodToInline.Body.Instructions;
int counter = 0;
var foundOpCodes = new Dictionary<Code, bool>();
bool checkInstrs = false;
while (true) {
if (counter++ >= 50)
return false;
if (index < 0 || index >= instrs.Count)
return false;
instr = instrs[index];
foundOpCodes[instr.OpCode.Code] = true;
switch (instr.OpCode.Code) {
case Code.Stloc:
case Code.Stloc_S:
case Code.Stloc_0:
case Code.Stloc_1:
case Code.Stloc_2:
case Code.Stloc_3:
case Code.Ldloc:
case Code.Ldloc_S:
case Code.Ldloc_0:
case Code.Ldloc_1:
case Code.Ldloc_2:
case Code.Ldloc_3:
case Code.Ldc_I4:
case Code.Ldc_I4_0:
case Code.Ldc_I4_1:
case Code.Ldc_I4_2:
case Code.Ldc_I4_3:
case Code.Ldc_I4_4:
case Code.Ldc_I4_5:
case Code.Ldc_I4_6:
case Code.Ldc_I4_7:
case Code.Ldc_I4_8:
case Code.Ldc_I4_M1:
case Code.Ldc_I4_S:
case Code.Add:
case Code.Sub:
case Code.Xor:
case Code.Or:
case Code.Nop:
case Code.Dup:
case Code.Mul:
case Code.Rem:
case Code.Div:
2013-01-19 20:03:57 +08:00
instructionEmulator.Emulate(instr);
2012-05-02 16:48:44 +08:00
index++;
break;
case Code.Ldarg:
case Code.Ldarg_S:
case Code.Ldarg_0:
case Code.Ldarg_1:
case Code.Ldarg_2:
case Code.Ldarg_3:
2012-11-09 07:21:45 +08:00
var arg = instr.GetParameter(parameters);
2012-05-02 16:48:44 +08:00
if (arg != arg1 && arg != arg2) {
if (!allowUnknownArgs)
goto done;
checkInstrs = true;
}
2013-01-19 20:03:57 +08:00
instructionEmulator.Emulate(instr);
2012-05-02 16:48:44 +08:00
index++;
break;
case Code.Call:
case Code.Callvirt:
case Code.Newobj:
goto done;
case Code.Switch:
2013-01-19 20:03:57 +08:00
var value = instructionEmulator.Pop() as Int32Value;
if (value == null || !value.AllBitsValid())
2012-05-02 16:48:44 +08:00
return false;
var targets = (Instruction[])instr.Operand;
if (value.Value >= 0 && value.Value < targets.Length)
index = instrs.IndexOf(targets[value.Value]);
2012-05-02 16:48:44 +08:00
else
index++;
break;
case Code.Br:
case Code.Br_S:
index = instrs.IndexOf((Instruction)instr.Operand);
break;
case Code.Brtrue:
case Code.Brtrue_S:
2013-01-19 20:03:57 +08:00
index = EmulateBrtrue(index);
2012-05-02 16:48:44 +08:00
break;
case Code.Brfalse:
case Code.Brfalse_S:
2013-01-19 20:03:57 +08:00
index = EmulateBrfalse(index);
2012-05-02 16:48:44 +08:00
break;
case Code.Isinst:
case Code.Castclass:
2013-01-19 20:03:57 +08:00
if (returnValue != null && instructionEmulator.Peek() == returnValue) {
2012-05-02 16:48:44 +08:00
// Do nothing
}
else
2013-01-19 20:03:57 +08:00
instructionEmulator.Emulate(instr);
2012-05-02 16:48:44 +08:00
index++;
break;
default:
if (instr.OpCode.OpCodeType != OpCodeType.Prefix)
goto done;
index++;
break;
}
}
done:
if (checkInstrs) {
if (!foundOpCodes.ContainsKey(Code.Ldc_I4_1))
return false;
if (!foundOpCodes.ContainsKey(Code.Ldc_I4_2))
return false;
if (!foundOpCodes.ContainsKey(Code.Add))
return false;
if (!foundOpCodes.ContainsKey(Code.Dup))
return false;
if (!foundOpCodes.ContainsKey(Code.Mul))
return false;
if (!foundOpCodes.ContainsKey(Code.Rem))
return false;
2012-05-02 19:51:07 +08:00
if (!foundOpCodes.ContainsKey(Code.Brtrue) && !foundOpCodes.ContainsKey(Code.Brtrue_S) &&
!foundOpCodes.ContainsKey(Code.Brfalse) && !foundOpCodes.ContainsKey(Code.Brfalse_S))
2012-05-02 16:48:44 +08:00
return false;
}
return true;
}
2013-01-19 20:03:57 +08:00
int EmulateBranch(int stackArgs, Bool3 cond, Instruction instrTrue, Instruction instrFalse) {
2012-05-02 16:48:44 +08:00
if (cond == Bool3.Unknown)
return -1;
Instruction instr = cond == Bool3.True ? instrTrue : instrFalse;
return methodToInline.Body.Instructions.IndexOf(instr);
}
2013-01-19 20:03:57 +08:00
int EmulateBrtrue(int instrIndex) {
var val1 = instructionEmulator.Pop();
2012-05-02 16:48:44 +08:00
var instr = methodToInline.Body.Instructions[instrIndex];
var instrTrue = (Instruction)instr.Operand;
var instrFalse = methodToInline.Body.Instructions[instrIndex + 1];
2013-01-19 20:03:57 +08:00
if (val1.IsInt32())
return EmulateBranch(1, Int32Value.CompareTrue((Int32Value)val1), instrTrue, instrFalse);
2012-05-02 16:48:44 +08:00
return -1;
}
2013-01-19 20:03:57 +08:00
int EmulateBrfalse(int instrIndex) {
var val1 = instructionEmulator.Pop();
2012-05-02 16:48:44 +08:00
var instr = methodToInline.Body.Instructions[instrIndex];
var instrTrue = (Instruction)instr.Operand;
var instrFalse = methodToInline.Body.Instructions[instrIndex + 1];
2013-01-19 20:03:57 +08:00
if (val1.IsInt32())
return EmulateBranch(1, Int32Value.CompareFalse((Int32Value)val1), instrTrue, instrFalse);
2012-05-02 16:48:44 +08:00
return -1;
}
2013-01-19 20:03:57 +08:00
bool EmulateToReturn(int index, Instruction lastInstr) {
2012-05-02 16:48:44 +08:00
int pushes, pops;
2012-11-09 07:21:45 +08:00
lastInstr.CalculateStackUsage(false, out pushes, out pops);
2013-01-19 20:03:57 +08:00
instructionEmulator.Pop(pops);
2012-05-02 16:48:44 +08:00
returnValue = null;
if (pushes != 0) {
returnValue = new UnknownValue();
2013-01-19 20:03:57 +08:00
instructionEmulator.SetProtected(returnValue);
instructionEmulator.Push(returnValue);
2012-05-02 16:48:44 +08:00
}
2013-01-19 20:03:57 +08:00
if (!EmulateInstructions(ref index, true))
2012-05-02 16:48:44 +08:00
return false;
if (index >= methodToInline.Body.Instructions.Count)
return false;
if (methodToInline.Body.Instructions[index].OpCode.Code != Code.Ret)
return false;
if (returnValue != null) {
2013-01-19 20:03:57 +08:00
if (instructionEmulator.Pop() != returnValue)
2012-05-02 16:48:44 +08:00
return false;
}
2013-01-19 20:03:57 +08:00
return instructionEmulator.StackSize() == 0;
2012-05-02 16:48:44 +08:00
}
2013-01-19 20:03:57 +08:00
public static bool CanInline(MethodDef method) {
2012-05-02 16:48:44 +08:00
if (method == null || method.Body == null)
return false;
2012-11-17 06:50:52 +08:00
if (method.Attributes != (MethodAttributes.Assembly | MethodAttributes.Static))
2012-05-02 16:48:44 +08:00
return false;
if (method.Body.ExceptionHandlers.Count > 0)
return false;
2012-11-09 07:21:45 +08:00
var parameters = method.MethodSig.GetParams();
2012-05-02 16:48:44 +08:00
int paramCount = parameters.Count;
if (paramCount < 2)
return false;
2012-12-14 19:39:06 +08:00
if (method.GenericParameters.Count > 0) {
foreach (var gp in method.GenericParameters) {
if (gp.GenericParamConstraints.Count == 0)
return false;
}
}
2012-05-02 16:48:44 +08:00
var param1 = parameters[paramCount - 1];
var param2 = parameters[paramCount - 2];
2013-01-19 20:03:57 +08:00
if (!IsIntType(param1.ElementType))
2012-05-03 00:43:04 +08:00
return false;
2013-01-19 20:03:57 +08:00
if (!IsIntType(param2.ElementType))
2012-05-03 00:43:04 +08:00
return false;
2012-05-02 16:48:44 +08:00
return true;
}
2013-01-19 20:03:57 +08:00
static bool IsIntType(ElementType etype) {
2012-05-02 16:48:44 +08:00
return etype == ElementType.Char || etype == ElementType.I2 || etype == ElementType.I4;
}
2013-01-19 20:03:57 +08:00
protected override bool IsReturn(MethodDef methodToInline, int instrIndex) {
2012-05-02 16:48:44 +08:00
int oldIndex = instrIndex;
2013-01-19 20:03:57 +08:00
if (base.IsReturn(methodToInline, oldIndex))
2012-05-02 16:48:44 +08:00
return true;
return false;
}
}
}