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. ipv4固定ip地址

    1.vi /etc/sysconfig/network-scripts/ifcfg-enp7s0f0    ##在后面添加ip和域名解析IPADDR="192.168.130.34" ...

  2. jumpserver部署1.0版本

    A. jumpserver概述 跳板机概述: 跳板机就是一台服务器,开发或运维人员在维护过程中首先要统一登录到这台服务器,然后再登录到目标设备进行维护和操作: 跳板机缺点:没有实现对运维人员操作行为的 ...

  3. java数据结构1--数组、排序和Arrays工具类

    数组:Array 数组的定义 数组的内存结构 数组定义常见问题 数组常见操作 Java参数传递问题--值传递 二维数组 1.数组概念 同一种类型数据的集合,可以是基本数据类型,也可以是引用数据类型. ...

  4. 约瑟夫环(CVTE、网易2014.3.16笔试题offerP228)

    题目:0,1,2,……,n-1这n个数字排成一个圆圈,从数字0开始每次从这个圆圈里删除第m个数字.求出这个圆圈里剩下的最后一个数字. 法一:用环形链表模拟圆圈的经典算法(时间复杂度O(nm),空间复杂 ...

  5. org.hibernate.NonUniqueObjectException 原因及解决办法

    问题 使用hibernate更新对象时,出现如下错误: org.hibernate.NonUniqueObjectException: a different object with the same ...

  6. 算法——得到数据流中前K大的数

    用优先队列 public PriorityQueue<Integer> kthLargest(int k, int[]a) { PriorityQueue<Integer> q ...

  7. 集合比较器报错java.lang.IllegalArgumentException: Comparison method violates its general contract!

    Collections.sort(listMonthlyUsage, new Comparator<MonthlyUsageDto>() { //按照元素从小到大排序 @Override ...

  8. express 设置 cookie 以及 httpOnly

    权限控制基本 cookie 在 express 中引入 cookie-parser const express = require('express') const cookieParser = re ...

  9. EF另一个 SqlParameterCollection 中已包含 SqlParameter。

    代码: SqlParameter[] commandParameters = new SqlParameter[]{ new SqlParameter("@CultID",filt ...

  10. Spotlight_on_windows 安装和监控

    一.下载 1.下载并安装Spotlight_on_windows 2.填写注册码 Authorization Key:295713336449229168750 Site Message:Bergel ...