吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 975|回复: 6
收起左侧

[求助] Hook .Net JIT ?

[复制链接]
Sh4DoW321 发表于 2023-10-14 02:54
Hi friends
For example I have a compiled C# program that it has a Fucntion with two string arguments like below code :
static string MyFunc(string a, string b)
{
     return a + " *** " + b;
}
Now I am gonna write C++ program hook JIT and log MyFunc arguments value , I mean value of a and b variables
How can I do that ?
Thanks

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
为之奈何? + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

头像被屏蔽
Aen920 发表于 2023-10-14 09:14
提示: 作者被禁止或删除 内容自动屏蔽
周易 发表于 2023-10-14 11:38
本帖最后由 周易 于 2023-10-14 12:09 编辑

Hooking into the JIT compiler for .NET to monitor and log function arguments can be quite complex, as there is no straightforward way provided. Typically, you would need to use the profiling API (ICorProfilerInfo) provided by the CLR (Common Language Runtime), which allows you to monitor and manipulate .NET applications as they are running.

If you insist on hooking into the JIT compiler, you should have a deep understanding of not only the .NET CLR and its internal workings but also the target platform's architecture and machine code.

The Compiler Explorer can be very useful when working with such low-level aspects of programming.

using System;

class Program
{
    static string MyFunc(string a, string b)
    {
        return a + " *** " + b;
    }
}

Considering instruction set amd64 and .NET 7.0.105 on Linux. A call to jitted MyFunc may look like:

       mov      rdi, string_handle
       mov      rdi, gword ptr [rdi]
       mov      rsi, string_handle
       mov      rsi, gword ptr [rsi]
       call     [Program:MyFunc(System.String,System.String):System.String]

Here rdi and rsi are used to pass parameters.  This refers to the System V AMD64 ABI used by most Unix-like systems, where rdi and rsi are used for the first two arguments. On Windows, however, it would be rcx and rdx.

Also, it's very important to be aware of the impact of function inlining when dealing with JIT compilation. If MyFunc was inlined, you wouldn't see a call instruction for MyFunc in your assembly code.

In C#, you can prevent a method from being inlined by the JIT compiler using the MethodImpl attribute with the MethodImplOptions.NoInlining flag.

Here is a sample Frida script on Windows to hook into .NET Framework jit. You may manually add some code to check the methodInfo_ILCode and Interceptor.attach the entryAddress.

(function () {
    let p_LoadLibraryExW = Module.getExportByName("kernel32.dll", "LoadLibraryExW");
    Interceptor.attach(p_LoadLibraryExW, {
        onEnter: function (args) {
            this.lpLibFileName = args[0];
        },
        onLeave: function (retval) {
            if (retval === 0) {
                return;
            }
            let fileName = this.lpLibFileName.readUtf16String().split("\\").reverse()[0];
            if (fileName === "clrjit.dll") {
                let p_getJit = Module.getExportByName("clrjit.dll", "getJit");
                let getJit = new NativeFunction(p_getJit, "pointer", []);
                let cilJit = getJit();
                let cilJit_vftable = cilJit.add(0x0).readPointer();
                let p_compileMethod = cilJit_vftable.add(0x0).readPointer();
                Interceptor.attach(p_compileMethod, {
                    onEnter: function (args) {
                        this.self = args[0];
                        this.compHnd = args[1];
                        this.methodInfo = args[2];
                        this.flags = args[3];
                        this.entryAddress = args[4];
                        this.nativeSizeOfCode = args[5];
                    },
                    onLeave: function (retval) {
                        if (retval === 0) { //CORJIT_OK
                            let methodInfo_ftn = this.methodInfo.add(0x0).readPointer();
                            let methodInfo_scope = this.methodInfo.add(0x4).readPointer();
                            let methodInfo_ILCode = this.methodInfo.add(0x8).readPointer();
                            let methodInfo_ILCodeSize = this.methodInfo.add(0xc).readUInt();
                            console.log("ftn =", methodInfo_ftn);
                            console.log("scope =", methodInfo_scope);
                            console.log("entryAddress =", this.entryAddress.readPointer());
                            console.log("nativeSizeOfCode =", this.nativeSizeOfCode.readUInt());
                            console.log(methodInfo_ILCode.readByteArray(methodInfo_ILCodeSize));
                            console.log();
                        }
                    }
                });
            }
        }
    });
}());
苏紫方璇 发表于 2023-10-14 11:54
以前使用过一次这种hook,能用,但是不太好用,当时抄的这篇文章的代码,可以参考一下
https://blog.csdn.net/xfgryujk/article/details/79053312
 楼主| Sh4DoW321 发表于 2023-10-14 19:31
周易 发表于 2023-10-14 11:38
[md]Hooking into the JIT compiler for .NET to monitor and log function arguments can be quite comple ...

Thanks friend , but is it possible in C++ code ?
周易 发表于 2023-10-14 23:12
Sh4DoW321 发表于 2023-10-14 19:31
Thanks friend , but is it possible in C++ code ?

Yes it is possible to implement in C++. Frida has C API.

See https://frida.re/docs/c-api/.

 楼主| Sh4DoW321 发表于 2023-10-16 09:40
I mean write C++ code inside Visual Studio expect frida
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 18:45

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表