using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.CompilerServices; namespace MethodHookSample
{
class Program
{
static void Main(string[] args)
{
Target target = new Target();
var methodAdd = typeof(Target).GetMethod("Add", BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
var methodMinus = typeof(Target).GetMethod("Minus", BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
MethodHooker.HookMethod(methodAdd, methodMinus);
var result = target.Add(, );
Console.Write("5 + 4 = " + result);
Console.Read();
}
} public class Target
{
[MethodImpl(MethodImplOptions.NoInlining)]
public int Add(int a, int b)
{
return a + b;
} [MethodImpl(MethodImplOptions.NoInlining)]
public int Minus(int a, int b)
{
return a - b;
}
} public class MethodHooker
{
public static void HookMethod(MethodInfo sourceMethod, MethodInfo destinationMethod)
{
RuntimeHelpers.PrepareMethod(sourceMethod.MethodHandle);
RuntimeHelpers.PrepareMethod(destinationMethod.MethodHandle); if (sourceMethod.IsVirtual)
{
HookVirtualMethod(sourceMethod, destinationMethod);
return;
} unsafe
{
if (IntPtr.Size == )
{
int* inj = (int*)destinationMethod.MethodHandle.Value.ToPointer() + ;
int* tar = (int*)sourceMethod.MethodHandle.Value.ToPointer() + ;
#if DEBUG
Console.WriteLine("\nVersion x86 Debug\n"); byte* injInst = (byte*)*inj;
byte* tarInst = (byte*)*tar; int* injSrc = (int*)(injInst + );
int* tarSrc = (int*)(tarInst + ); *tarSrc = (((int)injInst + ) + *injSrc) - ((int)tarInst + );
#else
Console.WriteLine("\nVersion x86 Release\n");
*tar = *inj;
#endif
}
else
{
long* inj = (long*)destinationMethod.MethodHandle.Value.ToPointer() + ;
long* tar = (long*)sourceMethod.MethodHandle.Value.ToPointer() + ;
Console.WriteLine("\nVersion x64 Release\n");
*tar = *inj;
}
}
} static void HookVirtualMethod(MethodInfo sourceMethod, MethodInfo destinationMethod)
{
unsafe
{
UInt64* methodDesc = (UInt64*)(sourceMethod.MethodHandle.Value.ToPointer());
int index = (int)(((*methodDesc) >> ) & 0xFF);
if (IntPtr.Size == )
{
uint* classStart = (uint*)sourceMethod.DeclaringType.TypeHandle.Value.ToPointer();
classStart += ;
classStart = (uint*)*classStart;
uint* tar = classStart + index;
uint* inj = (uint*)destinationMethod.MethodHandle.Value.ToPointer() + ;
*tar = *inj;
}
else
{
ulong* classStart = (ulong*)sourceMethod.DeclaringType.TypeHandle.Value.ToPointer();
classStart += ;
classStart = (ulong*)*classStart;
ulong* tar = classStart + index;
ulong* inj = (ulong*)destinationMethod.MethodHandle.Value.ToPointer() + ;
*tar = *inj;
}
}
}
}
}

C# Hook 方法的更多相关文章

  1. 如何正确的hook方法objc_msgSend · jmpews

    如何正确的hook方法objc_msgSend 前言 如果希望对 Objective-C 的方法调用进行 log, 一个很好的解决方法就是 hook 方法 objc_msgSend, 当然想到的就是利 ...

  2. 一种简单的hook方法--LD_PRELOAD变量

    LD_PRELOAD这个变量允许你定义在程序运行时优先加载的动态链接库,从而在程序运行时的动态链接 下面程序的看一个例子-getuid.c //getuid.c #include<stdio.h ...

  3. Django之钩子Hook方法

    局部钩子: 在Fom类中定义 clean_字段名() 方法,就能够实现对特定字段进行校验.(校验函数正常必须返回当前字段值)  def clean_name(self): pass         n ...

  4. 【安全性测试】利用反编译查看对应activity的方法采用hook技术绑定劫持_入门

    本次主要为了研究手机端的安全性而写的一篇文章,在基于自己对手机安全性的研究下,想到了这些工具之间的结合,当然这也算是第一次对手机安全研究勇敢地踏出一步,也不知道是否成功,还是准备撞南墙撞到底吧! 使用 ...

  5. Android Hook 借助Xposed

    主要就是使用到了Xposed中的两个比较重要的方法,handleLoadPackage获取包加载时候的回调并拿到其对应的classLoader:findAndHookMethod对指定类的方法进行Ho ...

  6. iOS开发中乱用hook可能导致灾难

    今天有同事遇到问题,他重写viewDidAppear:方法,但是,代码并没有执行到.后来我发现,是另个一同事用了黑魔法搞的鬼,而且他本人并不知道这么做会产生影响.(本文中所有黑魔法指Swizzle) ...

  7. thinkphp的钩子的两种配置和两种调用方法

    thinkphp的钩子行为类是一个比较难以理解的问题,网上有很多写thinkphp钩子类的文章,我也是根据网上的文章来设置thinkphp的钩子行为的,但根据这些网上的文章,我在设置的过程中,尝试了十 ...

  8. Android Hook Dexposed原理小析

    dexposed是阿里巴巴在xposed框架上面开发的hotpatch一套框架 当然hotpatch的方式有很多,这里先介绍下dexposed原理 Demo中有个test函数, 在调用hook之前正常 ...

  9. [转载] Android.Hook框架xposed开发篇

    本文转载自: http://www.52pojie.cn/thread-396793-1-1.html 原帖:http://drops.wooyun.org/tips/7488 作者:瘦蛟舞 官方教程 ...

随机推荐

  1. Big Data(七)MapReduce计算框架(PPT截图)

    一.为什么叫MapReduce? Map是以一条记录为单位映射 Reduce是分组计算

  2. 18Bootstrap

    1 概念 一个前端开发的框架,Bootstrap,来自 Twitter,是目前很受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JavaScript 的,它简洁灵活,使得 Web 开发 ...

  3. multi gpu inference with tfserving or keras

    在tfserving上,目测只能在每个gpu上起docker    https://github.com/tensorflow/serving/issues/311#issuecomment-4801 ...

  4. MyEclipse使用教程:使用主题自定义工作台外观

    [MyEclipse CI 2019.4.0安装包下载] 您可以通过选择Window>Preferences>General>Appearance来使用主题自定义的工作台外观,可以从 ...

  5. python类库32[多进程同步Lock+Semaphore+Event]

    python类库32[多进程同步Lock+Semaphore+Event]   同步的方法基本与多线程相同. 1) Lock 当多个进程需要访问共享资源的时候,Lock可以用来避免访问的冲突. imp ...

  6. 安装c#服务

    https://www.cnblogs.com/zmztya/p/9577440.html 1.以管理员身份运行cmd 2.安装windows服务 cd C:\Windows\Microsoft.NE ...

  7. 【java工具类】下载文件

    FileUtil.java /** * 下载文件 * @param file; * @param response */ public static void downloadFile(File fi ...

  8. Markers

    immune pdf(file = paste0(outdir,"T_B_NK_feature.pdf")) VlnPlot(expr_1_4,features = c(" ...

  9. 神经网络内在逻辑:没打开的AI“黑匣子”

    转载自:http://www.elecfans.com/rengongzhineng/592200.html 伴随着大数据,AI在沉寂了多年之后,又迎来了新的高潮.在这场涉及大部分科学的革命中,人工神 ...

  10. GIL与event事件讲解

    一.GIL全局解释器锁 global interpreter lock 1.GIL是一个互斥锁:保证数据的安全(以牺牲效率来换取数据的安全),阻止同一个进程内多个线程同时执行(不能并行但是能够实现并发 ...