private void InjectCallHook(MethodReference methodRef, bool haveReturnValue) { try { foreach (var typeDef in m_ModuleDefinition.Types) { if (ScriptProcessor.DontInjectTypes.Contains(typeDef.FullName)) { continue; } if (!ScriptProcessor.CheckType(typeDef, methodRef)) { continue; } foreach (var methodDef in typeDef.Methods) { var fullName = methodDef.FullName; if (fullName == methodRef.FullName) { continue; } if (ScriptProcessor.DontInjectMethods.Contains(fullName)) { continue; } if (!ScriptProcessor.CheckMethod(methodDef, methodRef)) { continue; } var body = methodDef.Body; body.InitLocals = true; var ilProcessor = body.GetILProcessor(); var insertPoint = body.Instructions[0]; if (insertPoint.OpCode == OpCodes.Nop) { insertPoint = body.Instructions[1]; } else { ilProcessor.InsertBefore(insertPoint, ilProcessor.Create(OpCodes.Nop)); } body.Variables.Add(new VariableDefinition(m_ObjectArrayTypeRef)); int arrLocalIndex = body.Variables.Count - 1; int ct = methodDef.Parameters.Count; AddCallHook(arrLocalIndex, ct, methodDef, methodRef, haveReturnValue, insertPoint, ilProcessor); } } } catch (Exception ex) { ScriptProcessor.ErrorTxts.Add(string.Format("InjectPrologue({0}), exception:{1}\n{2}", methodRef.FullName, ex.Message, ex.StackTrace)); } }
private void execScript_Click(object sender, EventArgs e) { if (assemblyList.Items.Count <= 0) { return; } string path = exportDir.Text.Trim(); if (path.Length <= 0) { MessageBox.Show("请先选择一个输出目录!"); return; } bool useSymbols = symbolFileComboBox.SelectedIndex == 1; OpenFileDialog ofd = new OpenFileDialog(); ofd.DefaultExt = "scp"; ofd.Filter = "脚本文件|*.scp||"; ofd.CheckPathExists = true; ofd.CheckFileExists = true; ofd.Multiselect = false; ofd.Title = "请指定脚本文件"; if (DialogResult.OK == ofd.ShowDialog()) { string file = ofd.FileName; List <string> files = new List <string>(); foreach (string s in assemblyList.Items) { files.Add(s); } ScriptProcessor.Start(files, path, useSymbols, file); } }
private void MainForm_Load(object sender, EventArgs e) { resultCtrl.Text = @""; symbolFileComboBox.SelectedIndex = 0; ScriptProcessor.Init(); }
private void InjectMemoryLog(MethodReference methodRef, bool haveReturnValue) { try { foreach (var typeDef in m_ModuleDefinition.Types) { if (ScriptProcessor.DontInjectTypes.Contains(typeDef.FullName)) { continue; } if (!ScriptProcessor.CheckType(typeDef, methodRef)) { continue; } foreach (var methodDef in typeDef.Methods) { var fullName = methodDef.FullName; if (fullName == methodRef.FullName) { continue; } if (ScriptProcessor.DontInjectMethods.Contains(fullName)) { continue; } if (!ScriptProcessor.CheckMethod(methodDef, methodRef)) { continue; } var body = methodDef.Body; if (HaveNew(methodDef)) { string tag = CalcTag(methodDef); var ilProcessor = body.GetILProcessor(); var insertPoint = body.Instructions[0]; if (insertPoint.OpCode == OpCodes.Nop) { insertPoint = body.Instructions[1]; } else { ilProcessor.InsertBefore(insertPoint, ilProcessor.Create(OpCodes.Nop)); } ilProcessor.InsertBefore(insertPoint, ilProcessor.Create(OpCodes.Ldc_I4_0)); ilProcessor.InsertBefore(insertPoint, ilProcessor.Create(OpCodes.Ldstr, tag)); ilProcessor.InsertBefore(insertPoint, ilProcessor.Create(OpCodes.Call, methodRef)); if (haveReturnValue) { ilProcessor.InsertBefore(insertPoint, ilProcessor.Create(OpCodes.Pop)); } //退出前再调用一次 insertPoint = FindRet(body, null); while (null != insertPoint) { insertPoint.OpCode = OpCodes.Nop; insertPoint.Operand = null; var newRet = ilProcessor.Create(OpCodes.Ret); ilProcessor.InsertAfter(insertPoint, newRet); insertPoint = newRet; ilProcessor.InsertBefore(insertPoint, ilProcessor.Create(OpCodes.Ldc_I4_1)); ilProcessor.InsertBefore(insertPoint, ilProcessor.Create(OpCodes.Ldstr, tag)); ilProcessor.InsertBefore(insertPoint, ilProcessor.Create(OpCodes.Call, methodRef)); if (haveReturnValue) { ilProcessor.InsertBefore(insertPoint, ilProcessor.Create(OpCodes.Pop)); } insertPoint = FindRet(body, insertPoint); } } } } } catch (Exception ex) { ScriptProcessor.ErrorTxts.Add(string.Format("InjectPrologue({0}), exception:{1}\n{2}", methodRef.FullName, ex.Message, ex.StackTrace)); } }
static void Main(string[] args) { if (args.Length > 0) { string outputDir = string.Empty; bool useSymbols = false; List <string> files = new List <string>(); string scpFile = "modify.scp"; for (int i = 0; i < args.Length; ++i) { if (0 == string.Compare(args[i], "-symbols", true)) { useSymbols = true; } else if (0 == string.Compare(args[i], "-out", true)) { if (i < args.Length - 1) { string arg = args[i + 1]; if (!arg.StartsWith("-")) { outputDir = arg; ++i; } } } else if (0 == string.Compare(args[i], "-scp", true)) { if (i < args.Length - 1) { string arg = args[i + 1]; if (!arg.StartsWith("-")) { string file = arg; if (!File.Exists(file)) { Console.WriteLine("file path not found ! {0}", file); } else { scpFile = file; } ++i; } } } else if (0 == string.Compare(args[i], "-src", true)) { if (i < args.Length - 1) { string arg = args[i + 1]; if (!arg.StartsWith("-")) { string file = arg; if (!File.Exists(file)) { Console.WriteLine("file path not found ! {0}", file); } else { files.Add(file); } ++i; } } } else { string file = args[i]; if (!File.Exists(file)) { Console.WriteLine("file path not found ! {0}", file); } else { files.Add(file); } } } if (files.Count > 0) { if (string.IsNullOrEmpty(outputDir)) { string srcDir = Path.GetDirectoryName(files[0]); outputDir = Path.GetDirectoryName(srcDir); } ScriptProcessor.Init(); ScriptProcessor.Start(files, outputDir, useSymbols, scpFile); } Environment.Exit(0); } else { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); s_MainForm = new MainForm(); Application.Run(s_MainForm); } }