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. laravel-admin 表单提交报错

    Method App\Admin\Controllers\GoodsSpecController::store does not exist. Method App\Admin\Controllers ...

  2. SecureCRT文件和文件夹显示不同颜色(像linux中那样效果)

    如何设置secureCRT使用的他可以像linux文件和文件夹显示不同的颜色呢 原先显示效果如下: 效果图 配置

  3. 22_1mybaits入门

    1.什么是框架? 它是我们软件开发中的一套解决方案,不同的框架解决的是不同的问题. 使用框架的好处: 框架封装了很多的细节,使开发者可以使用极简的方式实现功能.大大提高开发效率. 2.三层架构 表现层 ...

  4. jenkins 构建时显示git分支插件、显示构建分支插件

    参数化构建分支 1.安装插件:Git Parameter 2.找到我们在Jenkins中建立的工程,勾选“参数化构建过程”,并如下配置 3.在“源码管理”中如下配置 Jenkins构建完显示构建用户和 ...

  5. Bootstarp-table入门(1)

    https://blog.csdn.net/dlf123321/article/details/52231926?locationNum=11&fps=1

  6. selenium常用定位方式

    Selenium 是一个Web应用程序的自动化测试工具.使用javaScript内核语言编写,几乎支持所有能运行javaScript的浏览器(包括IE(7, 8, 9, 10, 11),Mozilla ...

  7. webpack命令:Module build failed(from ./node_modules/babel-loader/lib/index.js)/405/错误解决

    在项目中运行的时候出现报错,错误为Module build failed (from ./node_modules/babel-loader/lib/index.js) 解决方案: 控制台输入  np ...

  8. percona-toolkit 3.0.13 简单安装记录

    percona-toolkit 3.0.13 简单安装记录 环境:centos6.x mysql:8.0.17 yum -y install perl-DBIyum -y install perl-D ...

  9. DevOps之持续集成SonarQube代码质量扫描

    一.SonarQube介绍       SonarQube是一个用于代码质量检测管理的开放平台,可以集成不同的检测工具,代码分析工具,以及持续集成工具.SonarQube 并不是简单地把不同的代码检查 ...

  10. sqlserver 中批量删除\r\n 换行符

    从Excel中向sqlserver 中批量粘贴数据时 可能会粘贴进去换行符 \r\n  这时候在查询时候是看不见的 只有把该字段赋值到‘’中才能发现换行. 批量替换语句: update [表名]set ...