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

292 lines
8.8 KiB
C#
Raw Normal View History

2012-01-11 11:38:02 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2012-01-11 11:38:02 +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-01-22 02:55:37 +08:00
2012-01-11 11:38:02 +08:00
namespace de4dot.blocks.cflow {
public abstract class MethodCallInlinerBase : IBlocksDeobfuscator {
2012-01-11 11:38:02 +08:00
// We can't catch all infinite loops, so inline methods at most this many times
const int MAX_ITERATIONS = 10;
protected Blocks blocks;
protected Block block;
int iteration;
AccessChecker accessChecker;
2012-01-11 11:38:02 +08:00
2013-04-30 18:15:07 +08:00
public bool ExecuteIfNotModified { get; set; }
2013-01-19 20:03:57 +08:00
public void DeobfuscateBegin(Blocks blocks) {
2012-01-11 11:38:02 +08:00
this.blocks = blocks;
iteration = 0;
2012-01-11 11:38:02 +08:00
}
2013-01-19 20:03:57 +08:00
public bool Deobfuscate(List<Block> allBlocks) {
2012-01-11 11:38:02 +08:00
if (iteration++ >= MAX_ITERATIONS)
return false;
2013-04-30 18:15:07 +08:00
bool modified = false;
foreach (var block in allBlocks) {
this.block = block;
2013-04-30 18:15:07 +08:00
modified |= DeobfuscateInternal();
}
2013-04-30 18:15:07 +08:00
return modified;
2012-01-11 11:38:02 +08:00
}
2013-01-19 20:03:57 +08:00
protected abstract bool DeobfuscateInternal();
2012-01-22 02:55:37 +08:00
2012-05-02 16:45:18 +08:00
protected class InstructionPatcher {
readonly int patchIndex;
public readonly int afterIndex;
public readonly Instruction lastInstr;
readonly Instr clonedInstr;
public InstructionPatcher(int patchIndex, int afterIndex, Instruction lastInstr) {
this.patchIndex = patchIndex;
this.afterIndex = afterIndex;
this.lastInstr = lastInstr;
2012-11-01 01:52:50 +08:00
this.clonedInstr = new Instr(lastInstr.Clone());
2012-05-02 16:45:18 +08:00
}
2013-01-19 20:03:57 +08:00
public void Patch(Block block) {
2012-05-02 16:45:18 +08:00
block.Instructions[patchIndex] = clonedInstr;
}
}
2013-01-19 20:03:57 +08:00
protected bool InlineLoadMethod(int patchIndex, MethodDef methodToInline, Instruction loadInstr, int instrIndex) {
if (!IsReturn(methodToInline, instrIndex))
2012-01-22 02:55:37 +08:00
return false;
2013-01-19 20:03:57 +08:00
int methodArgsCount = DotNetUtils.GetArgsCount(methodToInline);
2012-01-22 02:55:37 +08:00
for (int i = 0; i < methodArgsCount; i++)
2013-01-19 20:03:57 +08:00
block.Insert(patchIndex++, OpCodes.Pop.ToInstruction());
2012-01-22 02:55:37 +08:00
2012-11-01 01:52:50 +08:00
block.Instructions[patchIndex] = new Instr(loadInstr.Clone());
2012-01-22 02:55:37 +08:00
return true;
}
2013-01-19 20:03:57 +08:00
protected bool InlineOtherMethod(int patchIndex, MethodDef methodToInline, Instruction instr, int instrIndex) {
return InlineOtherMethod(patchIndex, methodToInline, instr, instrIndex, 0);
2012-07-07 13:11:32 +08:00
}
2013-01-19 20:03:57 +08:00
protected bool InlineOtherMethod(int patchIndex, MethodDef methodToInline, Instruction instr, int instrIndex, int popLastArgs) {
return PatchMethod(methodToInline, TryInlineOtherMethod(patchIndex, methodToInline, instr, instrIndex, popLastArgs));
2012-05-02 16:45:18 +08:00
}
2013-01-19 20:03:57 +08:00
protected bool PatchMethod(MethodDef methodToInline, InstructionPatcher patcher) {
2012-05-02 16:45:18 +08:00
if (patcher == null)
return false;
2013-01-19 20:03:57 +08:00
if (!IsReturn(methodToInline, patcher.afterIndex))
2012-05-02 16:45:18 +08:00
return false;
2013-01-19 20:03:57 +08:00
patcher.Patch(block);
2012-05-02 16:45:18 +08:00
return true;
}
2013-01-19 20:03:57 +08:00
protected InstructionPatcher TryInlineOtherMethod(int patchIndex, MethodDef methodToInline, Instruction instr, int instrIndex) {
return TryInlineOtherMethod(patchIndex, methodToInline, instr, instrIndex, 0);
2012-07-07 13:11:32 +08:00
}
2013-01-19 20:03:57 +08:00
protected virtual Instruction OnAfterLoadArg(MethodDef methodToInline, Instruction instr, ref int instrIndex) {
2012-12-14 19:39:06 +08:00
return instr;
}
2013-01-19 20:03:57 +08:00
protected InstructionPatcher TryInlineOtherMethod(int patchIndex, MethodDef methodToInline, Instruction instr, int instrIndex, int popLastArgs) {
2012-01-22 02:55:37 +08:00
int loadIndex = 0;
2013-01-19 20:03:57 +08:00
int methodArgsCount = DotNetUtils.GetArgsCount(methodToInline);
2012-01-22 02:55:37 +08:00
bool foundLdarga = false;
while (instr != null && loadIndex < methodArgsCount) {
bool isLdarg = true;
switch (instr.OpCode.Code) {
case Code.Ldarg:
case Code.Ldarg_S:
case Code.Ldarg_0:
case Code.Ldarg_1:
case Code.Ldarg_2:
case Code.Ldarg_3:
break;
case Code.Ldarga:
case Code.Ldarga_S:
foundLdarga = true;
break;
default:
isLdarg = false;
break;
}
if (!isLdarg)
break;
2012-11-01 01:52:50 +08:00
if (instr.GetParameterIndex() != loadIndex)
2012-05-02 16:45:18 +08:00
return null;
2012-01-22 02:55:37 +08:00
loadIndex++;
2013-01-19 20:03:57 +08:00
instr = DotNetUtils.GetInstruction(methodToInline.Body.Instructions, ref instrIndex);
instr = OnAfterLoadArg(methodToInline, instr, ref instrIndex);
2012-01-22 02:55:37 +08:00
}
if (instr == null || loadIndex != methodArgsCount - popLastArgs)
2012-05-02 16:45:18 +08:00
return null;
2012-01-22 02:55:37 +08:00
2013-09-28 20:54:00 +08:00
switch (instr.OpCode.Code) {
case Code.Call:
case Code.Callvirt:
2012-01-22 02:55:37 +08:00
if (foundLdarga)
2012-05-02 16:45:18 +08:00
return null;
2012-01-22 02:55:37 +08:00
var callInstr = instr;
2012-11-01 01:52:50 +08:00
var calledMethod = callInstr.Operand as IMethod;
2012-01-22 02:55:37 +08:00
if (calledMethod == null)
2012-05-02 16:45:18 +08:00
return null;
2012-01-22 02:55:37 +08:00
2013-01-19 20:03:57 +08:00
if (!IsCompatibleType(-1, calledMethod.MethodSig.RetType, methodToInline.MethodSig.RetType))
2012-05-02 16:45:18 +08:00
return null;
2012-01-22 02:55:37 +08:00
2013-01-19 20:03:57 +08:00
if (!CheckSameMethods(calledMethod, methodToInline, popLastArgs))
2012-05-02 16:45:18 +08:00
return null;
2012-01-22 02:55:37 +08:00
2013-01-19 20:03:57 +08:00
if (!HasAccessTo(instr.Operand))
return null;
2012-05-02 16:45:18 +08:00
return new InstructionPatcher(patchIndex, instrIndex, callInstr);
2013-09-28 20:54:00 +08:00
case Code.Newobj:
2012-01-22 02:55:37 +08:00
if (foundLdarga)
2012-05-02 16:45:18 +08:00
return null;
2012-01-22 02:55:37 +08:00
var newobjInstr = instr;
2012-11-01 01:52:50 +08:00
var ctor = newobjInstr.Operand as IMethod;
2012-01-22 02:55:37 +08:00
if (ctor == null)
2012-05-02 16:45:18 +08:00
return null;
2012-01-22 02:55:37 +08:00
2013-01-19 20:03:57 +08:00
if (!IsCompatibleType(-1, ctor.DeclaringType, methodToInline.MethodSig.RetType))
2012-05-02 16:45:18 +08:00
return null;
2012-01-22 02:55:37 +08:00
2012-11-01 01:52:50 +08:00
var methodArgs = methodToInline.Parameters;
2013-01-19 20:03:57 +08:00
var calledMethodArgs = DotNetUtils.GetArgs(ctor);
2012-11-18 10:02:12 +08:00
if (methodArgs.Count + 1 - popLastArgs != calledMethodArgs.Count)
2012-05-02 16:45:18 +08:00
return null;
2012-01-22 02:55:37 +08:00
for (int i = 1; i < calledMethodArgs.Count; i++) {
2013-01-19 20:03:57 +08:00
if (!IsCompatibleType(i, calledMethodArgs[i], methodArgs[i - 1].Type))
2012-05-02 16:45:18 +08:00
return null;
2012-01-22 02:55:37 +08:00
}
2013-01-19 20:03:57 +08:00
if (!HasAccessTo(instr.Operand))
return null;
2012-05-02 16:45:18 +08:00
return new InstructionPatcher(patchIndex, instrIndex, newobjInstr);
2013-09-28 20:54:00 +08:00
case Code.Ldfld:
case Code.Ldflda:
case Code.Ldftn:
case Code.Ldvirtftn:
case Code.Ldlen:
case Code.Initobj:
case Code.Isinst:
case Code.Castclass:
case Code.Newarr:
case Code.Ldtoken:
case Code.Unbox_Any:
2012-01-22 02:55:37 +08:00
var ldInstr = instr;
if (methodArgsCount != 1)
2012-05-02 16:45:18 +08:00
return null;
2013-09-28 20:54:00 +08:00
if (instr.OpCode.OperandType != OperandType.InlineNone && !HasAccessTo(instr.Operand))
return null;
2012-05-02 16:45:18 +08:00
return new InstructionPatcher(patchIndex, instrIndex, ldInstr);
2012-01-22 02:55:37 +08:00
2013-09-28 20:54:00 +08:00
default:
return null;
}
2012-05-02 16:45:18 +08:00
}
2013-01-19 20:03:57 +08:00
bool HasAccessTo(object operand) {
if (operand == null)
return false;
accessChecker.UserType = blocks.Method.DeclaringType;
2013-01-19 20:03:57 +08:00
return accessChecker.CanAccess(operand) ?? GetDefaultAccessResult();
}
2013-01-19 20:03:57 +08:00
protected virtual bool GetDefaultAccessResult() {
return true;
}
2013-01-19 20:03:57 +08:00
protected virtual bool IsReturn(MethodDef methodToInline, int instrIndex) {
var instr = DotNetUtils.GetInstruction(methodToInline.Body.Instructions, ref instrIndex);
2012-05-02 16:45:18 +08:00
return instr != null && instr.OpCode.Code == Code.Ret;
2012-01-22 02:55:37 +08:00
}
2013-01-19 20:03:57 +08:00
protected bool CheckSameMethods(IMethod method, MethodDef methodToInline) {
return CheckSameMethods(method, methodToInline, 0);
2012-07-07 13:11:32 +08:00
}
2013-01-19 20:03:57 +08:00
protected bool CheckSameMethods(IMethod method, MethodDef methodToInline, int ignoreLastMethodToInlineArgs) {
2012-11-01 01:52:50 +08:00
var methodToInlineArgs = methodToInline.Parameters;
2013-01-19 20:03:57 +08:00
var methodArgs = DotNetUtils.GetArgs(method);
2012-11-01 23:42:02 +08:00
bool hasImplicitThis = method.MethodSig.ImplicitThis;
2012-11-02 14:36:02 +08:00
if (methodToInlineArgs.Count - ignoreLastMethodToInlineArgs != methodArgs.Count)
return false;
for (int i = 0; i < methodArgs.Count; i++) {
var methodArg = methodArgs[i];
2013-01-19 20:03:57 +08:00
var methodToInlineArg = GetArgType(methodToInline, methodToInlineArgs[i].Type);
if (!IsCompatibleType(i, methodArg, methodToInlineArg)) {
2012-11-01 01:52:50 +08:00
if (i != 0 || !hasImplicitThis)
return false;
if (!isCompatibleValueThisPtr(methodArg, methodToInlineArg))
return false;
}
}
return true;
}
2013-01-19 20:03:57 +08:00
static TypeSig GetArgType(MethodDef method, TypeSig arg) {
2012-12-14 19:39:06 +08:00
if (arg.GetElementType() != ElementType.MVar)
return arg;
var mvar = (GenericMVar)arg;
foreach (var gp in method.GenericParameters) {
if (gp.Number != mvar.Number)
continue;
foreach (var gpc in gp.GenericParamConstraints)
return gpc.Constraint.ToTypeSig();
}
return arg;
}
2013-01-19 20:03:57 +08:00
protected virtual bool IsCompatibleType(int paramIndex, IType origType, IType newType) {
2012-11-01 01:52:50 +08:00
return new SigComparer().Equals(origType, newType);
2012-01-22 02:55:37 +08:00
}
2012-01-25 19:19:45 +08:00
2012-11-01 01:52:50 +08:00
static bool isCompatibleValueThisPtr(IType origType, IType newType) {
var newByRef = newType as ByRefSig;
2012-01-25 19:19:45 +08:00
if (newByRef == null)
return false;
2013-01-19 20:03:57 +08:00
if (!IsValueType(newByRef.Next) || !IsValueType(origType))
2012-01-25 19:19:45 +08:00
return false;
2012-11-01 01:52:50 +08:00
return new SigComparer().Equals(origType, newByRef.Next);
}
2013-01-19 20:03:57 +08:00
protected static bool IsValueType(IType type) {
if (type == null)
return false;
var ts = type as TypeSig;
if (ts == null)
return type.IsValueType;
return ts.IsValueType && ts.ElementType != ElementType.Void;
2012-01-25 19:19:45 +08:00
}
2012-01-11 11:38:02 +08:00
}
}