C# Hook 方法
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 方法的更多相关文章
- 如何正确的hook方法objc_msgSend · jmpews
如何正确的hook方法objc_msgSend 前言 如果希望对 Objective-C 的方法调用进行 log, 一个很好的解决方法就是 hook 方法 objc_msgSend, 当然想到的就是利 ...
- 一种简单的hook方法--LD_PRELOAD变量
LD_PRELOAD这个变量允许你定义在程序运行时优先加载的动态链接库,从而在程序运行时的动态链接 下面程序的看一个例子-getuid.c //getuid.c #include<stdio.h ...
- Django之钩子Hook方法
局部钩子: 在Fom类中定义 clean_字段名() 方法,就能够实现对特定字段进行校验.(校验函数正常必须返回当前字段值) def clean_name(self): pass n ...
- 【安全性测试】利用反编译查看对应activity的方法采用hook技术绑定劫持_入门
本次主要为了研究手机端的安全性而写的一篇文章,在基于自己对手机安全性的研究下,想到了这些工具之间的结合,当然这也算是第一次对手机安全研究勇敢地踏出一步,也不知道是否成功,还是准备撞南墙撞到底吧! 使用 ...
- Android Hook 借助Xposed
主要就是使用到了Xposed中的两个比较重要的方法,handleLoadPackage获取包加载时候的回调并拿到其对应的classLoader:findAndHookMethod对指定类的方法进行Ho ...
- iOS开发中乱用hook可能导致灾难
今天有同事遇到问题,他重写viewDidAppear:方法,但是,代码并没有执行到.后来我发现,是另个一同事用了黑魔法搞的鬼,而且他本人并不知道这么做会产生影响.(本文中所有黑魔法指Swizzle) ...
- thinkphp的钩子的两种配置和两种调用方法
thinkphp的钩子行为类是一个比较难以理解的问题,网上有很多写thinkphp钩子类的文章,我也是根据网上的文章来设置thinkphp的钩子行为的,但根据这些网上的文章,我在设置的过程中,尝试了十 ...
- Android Hook Dexposed原理小析
dexposed是阿里巴巴在xposed框架上面开发的hotpatch一套框架 当然hotpatch的方式有很多,这里先介绍下dexposed原理 Demo中有个test函数, 在调用hook之前正常 ...
- [转载] Android.Hook框架xposed开发篇
本文转载自: http://www.52pojie.cn/thread-396793-1-1.html 原帖:http://drops.wooyun.org/tips/7488 作者:瘦蛟舞 官方教程 ...
随机推荐
- SIP协议 会话发起协议(一)
会话发起协议(SIP)是VoIP技术中最常用的协议之一.它是一种应用层协议,与其他应用层协议协同工作,通过Internet控制多媒体通信会话. SIP - 概述 以下是有关SIP的几点注意事项 - S ...
- 007-Zabbix Server 自带模板监控MySQL数据库
监控数据库分为三种: 1.Zabbix Server 自带模板监控无密码MySQL数据库 2.Zabbix Server 自带模板监控有密码MySQL数据库 3.Zabbix Server 自带模板监 ...
- FastDFS 集群
FastDFS 集群 克隆虚拟机 VMware修改mac 修改 ip地址 rm -f /etc/udev/rules.d/70-persistent-net.rules reboot Tracker集 ...
- 前后台入门系统搭建详解(springboot+angularjs)
1 . 搭建boot启动框架,这一步骤什么都不用添加,搭建完后框架如下: 2.因为是前后台项目,所以一般是需要有前台页面的,需要在后端maven依赖中加入web依赖包 spring-boot-star ...
- squid之------ACL控制
一.ACL概念 Squid提供了强大的代理控制机制,通过合理设置ACL(Access Control List,访问控制列表)并进行限制,可以针对源地址.目标地址.访问的URL路径.访问的时间等各 ...
- 【洛谷P2447】外星千足虫
题目大意:给定一个 M 个含 N 个未知数的异或方程组,保证有解,若存在唯一解,给出至少需要几个方程才能得出唯一解,若不存在,直接输出不存在. 题解:异或方程组也满足类似初等行变换的操作,只不过所有的 ...
- BeanPostProcessor(转)
BeanPostProcessor简介 BeanPostProcessor是Spring IOC容器给我们提供的一个扩展接口.接口声明如下: public interface BeanPostProc ...
- C#任务调度——LimitedConcurrencyLevelTaskScheduler
这是参考大佬分享的代码写的有问题请提出指正,谢谢. using Serilog; using System; using System.Collections.Generic; using Syste ...
- 2019JAVA最新课程-React从入门到实战(新)
1.准备工作 可以在yunp.top网站看webpack,node/npm,cnpm的相关使用视频教程 react有两种使用方式,一是在现有网站中添加:二是创建一个全新的 官网创建全新一个react ...
- SpringApplication.run 做了哪些事?
SpringApplication.run一共做了两件事,分别是 创建SpringApplication对象 利用创建好的SpringApplication对象,调用run方法论 结论: 面试官: 我 ...