Crane.MethodHook
2.0.3
dotnet add package Crane.MethodHook --version 2.0.3
NuGet\Install-Package Crane.MethodHook -Version 2.0.3
<PackageReference Include="Crane.MethodHook" Version="2.0.3" />
<PackageVersion Include="Crane.MethodHook" Version="2.0.3" />
<PackageReference Include="Crane.MethodHook" />
paket add Crane.MethodHook --version 2.0.3
#r "nuget: Crane.MethodHook, 2.0.3"
#:package Crane.MethodHook@2.0.3
#addin nuget:?package=Crane.MethodHook&version=2.0.3
#tool nuget:?package=Crane.MethodHook&version=2.0.3
介绍
此库尝试通过汇编JMP指令实现.net中方法钩子。
This library implement .net method hooking by using native JMP directive.
调用方法
添加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();
当前MethodHookManager设计为静态类型,碰到同一共享方法在多AppDomain进行Hook时,请确保在AppDomain切换时只保持一个AppDomain中进行Hook,其他已经Hook了的域需要进行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到公共方法,很无脑。
如果原方法是实例方法,那么HOOK方法的第一个参数就设置为该实例。对于其他情况,HOOK方法的第一个参数就设置为null。
其他参数在后面依次添加。如下示例:
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.");
}
}
从V2.0.0版开始,增加对更多虚方法、匿名对象方法、运算符和泛型方法的HOOK支持,使用.Net Standard 2.0编译,支持.NET 4.7+/6/8/10,与.net跨平台架构保持一致。 如果有技术上的问题,请联系我扣扣252502568,长久交流。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Crane.MethodHook:
| Package | Downloads |
|---|---|
|
CJ.Plug.Models
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
V2.0.3版本增加了对匿名类型对象方法的HOOK支持。