zoukankan      html  css  js  c++  java
  • 使用cecil 完毕 code injection

     

    1. 安装Mono.Cecil

     

    2. 创建一个測试项目:

     

    加入測试方法:

     

    这种方法的返回值会被动态重写.

     
     public class Class1
        {
           public static string Test()
           {
               return "ok";
           }
        }


     

     

    3. code Injection 代码:

     

    var path = @"XXinDebugClassLibrary1.dll";
     
               var assembly = AssemblyDefinition.ReadAssembly
                    (path);
               var type =assembly.MainModule.GetType("ClassLibrary1.Class1");
               var foundMethod = type.GetMethods().First(m => m.Name =="Test");
    ////清空当前方法指令
               foundMethod.Body.Instructions.Clear();
    ////获得当前IL的指令执行器
               var worker = foundMethod.Body.GetILProcessor();
    ////改动返回值
               Instruction ins1 = worker.Create(OpCodes.Ldstr, "will be changed onnext time run");
               Instruction ins2 = worker.Create(OpCodes.Ret);
     
               worker.Append(ins1);
               worker.Append(ins2);
    ////保存DLL文件
               assembly.Write(path);
     
    ////下次调用就会看到(由于新保存的DLL下次才干载入到)
               Console.WriteLine(Class1.Test());
               Console.Read();

    4. 执行查看结果


  • 相关阅读:
    【简】题解 AWSL090429 【市场】
    【简】题解 AWSL090429 【噪音】
    差分约束
    凸包模板
    杂模板
    后缀数组刷题
    Trie刷题
    字符串模板
    网络流建模专题
    组合数模板
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6957545.html
Copyright © 2011-2022 走看看