Git Product home page Git Product logo

monohooker's Introduction

MonoHooker

本代码的功能是运行时修改C#函数

特点:

  • 运行时直接修改内存中的 jit 代码,不会修改 UnityEditor.dll 等文件,避免让别人修改文件的尴尬。
  • 不影响调试。
  • 同时支持 .net 2.x 与 .net 4.x。
  • 目前测试支持 unity4.7.2, unity5.x, unity 2017 与 unity 2018。
  • 使用很方便,在C#内定义签名与原始方法相同的两个方法然后注册一下就能用了。
  • 目前已支持 Android Mono, Windows Mono/IL2CPP

预览

image

原理

  • MethodInfo.MethodHandle.GetFunctionPointer().ToPointer() 指向了 jit 后的 native 代码,因此修改此地址的代码即可以修改功能。
  • 通过一系列跳转就可以巧妙的替换原函数实现,同时也保留调用原函数的功能。
  • 本代码的实现与下面 reference 的实现略有不同,比其使用更少的字节,Hook 成功率更高,具体实现可以看代码。

使用方法

    [DidReloadScripts] // 最好脚本加载完毕就 hook
    static void InstallHook()
    {
        if(_hooker == null)
        {
            Type type = Type.GetType("UnityEditor.LogEntries,UnityEditor.dll");
            // 找到需要 Hook 的方法
            MethodInfo miTarget = type.GetMethod("Clear", BindingFlags.Static | BindingFlags.Public);

            type = typeof(PinnedLog);

            // 找到被替换成的新方法
            MethodInfo miReplacement = type.GetMethod("NewClearLog", BindingFlags.Static | BindingFlags.NonPublic);

            // 这个方法是用来调用原始方法的
            MethodInfo miProxy = type.GetMethod("ProxyClearLog", BindingFlags.Static | BindingFlags.NonPublic);

            // 创建一个 Hooker 并 Install 就OK啦, 之后无论哪个代码再调用原始方法都会重定向到
            //  我们写的方法ヾ(゚∀゚ゞ)
            _hooker = new MethodHooker(miTarget, miReplacement, miProxy);
            _hooker.Install();
        }
    }

存在问题

  • 运行时目前只支持 android jit 以及 windows IL2CPP, android il2cpp 需要增加一个 so 来调用 mprotect 方法,现在不准备搞。

reference

monohooker's People

Contributors

misaka-mikoto-tech avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.