任务:
查找所有呼叫功能
public static void WriteString(int index0, string s, int index1)
{
Console.WriteLine(s);
}
在 SomeCnsl.exe 并将参数 's' 括在函数 ChangeString 中
public static string ChangeText(string text)
{
return text + "new";
}
例:
original: WriteString(0,"hello",1);
wrap: WriteString(0,ChangeText("hello"),1);
为了解决这个问题,我使用Mono.Cecil。我的解决方案如下所示:
private static AssemblyDefinition MainAssembly;
static void Main(string[] args)
{
MainAssembly = AssemblyDefinition.ReadAssembly("SomeCnsl.exe");
var changeTextMethod = typeof(SomeCnsl.Program).GetMethod("ChangeText");
var changeTextMethodRef = MainAssembly.MainModule.Import(changeTextMethod);
var mainMethod = MainAssembly.Modules.SelectMany(mod => ModuleDefinitionRocks.GetAllTypes(mod))
.SelectMany(t => t.Methods)
.Where(method => null != method.Body);
foreach (var body in mainMethod.Select(m => m.Body))
{
var processor = body.GetILProcessor();
var instructions = body.Instructions.Where(instr => instr.OpCode == OpCodes.Call && instr.ToString().Contains("WriteString")).ToList();
foreach (var instr in instructions)
{
var stringEndArg = GetStringArgument(instr);
var writeInstruction = processor.Create(OpCodes.Call, changeTextMethodRef);
processor.InsertAfter(stringEndArg, writeInstruction);
}
}
SavePatchedAssembly();
}
为了找到字符串参数,我创建了递归方法GetStringArgument:
public static Instruction GetStringArgument(Instruction callDrawString)
{
if (callDrawString.Previous.OpCode == OpCodes.Ldstr || callDrawString.Previous.OpCode == OpCodes.Ldarg_1 ||
(callDrawString.Previous.OpCode == OpCodes.Call && callDrawString.Previous.ToString().Contains("System.String::")) ||
(callDrawString.Previous.OpCode == OpCodes.Callvirt && callDrawString.Previous.ToString().Contains("System.String::")) ||
(callDrawString.Previous.OpCode == OpCodes.Callvirt && callDrawString.Previous.ToString().Contains("Generic.List`1<System.String>::get_Item")) ||
(callDrawString.Previous.OpCode == OpCodes.Callvirt && callDrawString.Previous.ToString().Contains("Generic.Dictionary`2<") && callDrawString.Previous.ToString().Contains("System.String>::get_Item")) ||
((callDrawString.Previous.Operand as ParameterReference) != null && (callDrawString.Previous.Operand as ParameterReference).ParameterType.FullName == typeof(string).FullName) ||
((callDrawString.Previous.Operand as FieldReference) != null && (callDrawString.Previous.Operand as FieldReference).FieldType.FullName == typeof(string).FullName) ||
((callDrawString.Previous.Operand as PropertyReference) != null && (callDrawString.Previous.Operand as PropertyReference).PropertyType.FullName == typeof(string).FullName))
{
return callDrawString.Previous;
}
else
{
return GetStringArgument(callDrawString.Previous);
}
}
这是工作。直到在 WriteString 的参数中放置一些字符串,如下所示:
static Dictionary<string, int> listParam = new Dictionary<string, int> { { "first", 1 }, { "second", 2 }, { "third", 3 } };
static int index = 2;
static string indexString = "second";
static void Main(string[] args)
{
while(true)
{
Thread.Sleep(1000);
WriteString(index, indexString, listParam[indexString]);
}
}
ILCode WriteString call:
IL_0022: ldsfld int32 SomeCnsl.Program::index
IL_0027: ldsfld string SomeCnsl.Program::indexString
IL_002c: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string, int32> SomeCnsl.Program::listParam
IL_0031: ldsfld string SomeCnsl.Program::indexString
IL_0036: callvirt instance !1/*int32*/ class [mscorlib]System.Collections.Generic.Dictionary`2<string, int32>::get_Item(!0/*string*/)
IL_003b: call void SomeCnsl.Program::WriteString(string, int32, int32)
IL_0040: nop
所以,我的问题是:
我可以更精确地定义函数 WriteText 中第二个参数中的所有 IL 命令吗?如果我能,那又如何?
它不是对你的问题的回答,但它可以帮助你以另一种方式做到这一点。你可以使用 Roslyn 根据需要重写代码。
可以通过继承CSharpSyntaxRewriting
然后重写相关方法来实现。之后,您将收到一个新的语法树,其中包含修改后的代码,然后您可以编译它并将其保存到磁盘。以这里为例。