Crane.MethodHook 1.0.6

dotnet add package Crane.MethodHook --version 1.0.6
NuGet\Install-Package Crane.MethodHook -Version 1.0.6
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Crane.MethodHook" Version="1.0.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Crane.MethodHook --version 1.0.6
#r "nuget: Crane.MethodHook, 1.0.6"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Crane.MethodHook as a Cake Addin
#addin nuget:?package=Crane.MethodHook&version=1.0.6

// Install Crane.MethodHook as a Cake Tool
#tool nuget:?package=Crane.MethodHook&version=1.0.6

介绍

此库尝试通过汇编JMP指令实现.net中方法钩子。
This library implement .net method hooking by using native JMP directive.

调用方法

如果要在控制台中显示hook细节,请调用这个方法

Crane.MethodHook.MethodHookManager.Instance.ShowHookDetails(true);

添加MethodHook示例,可以添加多个MethodHook

var sourceMethod = typeof(string).GetMethod("Compare", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(string), typeof(string) }, null);
var targetMethod = typeof(Program).GetMethod(nameof(NewCompare), BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(string), typeof(string) }, null);
Crane.MethodHook.MethodHookManager.Instance.AddHook(new MethodHook(sourceMethod, targetMethod));

要让所有的Hook生效,就必须开启Hook

Crane.MethodHook.MethodHookManager.Instance.StartHook();

Hook开启后,可以随时关闭Hook

Crane.MethodHook.MethodHookManager.Instance.StopHook();

获取当前方法所绑定的MethodHook,然后可以使用InvokeOriginal来调用Hook前的原始方法。
下面这个例子string.CompareHook生效后,在新方法中希望调用原始的string.Compare方法,就可以这样:

public static int NewCompare(string a, string b)
{
    var methodHook = Crane.MethodHook.MethodHookManager.Instance.GetHook(System.Reflection.MethodBase.GetCurrentMethod());
    return -1 * methodHook.InvokeOriginal<int>(null,a,b); 
}

特别说明

这个库Hook时原方法和目标方法不验证签名,意思是对象的实例方法也可以Hook到一个静态方法,私有方法也可以Hook到公共方法,很无脑。
实现方法是在定义时将静态方法的第一个参数设置为实例对象,其他参数在后面依次添加。如下示例:

var sourceMethod = typeof(Person).GetMethod("ShowPersonAge", BindingFlags.Public | BindingFlags.Instance, null, new Type[] { }, null);
var targetMethod = typeof(Program).GetMethod(nameof(ShowPersonAgeNew), BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(Person) }, null);
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public void ShowPersonAge()
    {
        Console.WriteLine(Name + " is " + Age.ToString() + " years old.");
    }
}

public static void ShowPersonAgeNew(Person a)
{
    if (a.Name == "John")
    {
        Console.WriteLine(a.Name + " is " + a.Age.ToString() + " years old.");
    }
    else
    {
        Console.WriteLine(a.Name + " is 999 years old.");
    }
            
}

另外,测试发现值类型的实例方法HOOK后可能识别不到值实例参数,这个暂无有效方案。
如果有技术上的问题,请联系我扣扣252502568,长久交流。

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Framework net40 is compatible.  net403 was computed.  net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.6 111 4/2/2024

最新版支持 .Net 4x/6/8,如有技术疑问加我扣扣252502568详细交流。