re | frida | hook windows进程

frida | hook windows进程

参考官方文档:https://frida.re/docs/functions/
frida就是动态插桩技术啦

先写个这样子的C程序然后跑起来

#include<stdio.h>
#include<Windows.h>

void output(int n){
	printf("Number: %d\n", n);
}

int main(){
	int i = 0;
	printf("func at %p\n", output);
	while(1){
		output(i++);
		Sleep(1000);
	}
	return 0;
}

跑起来以后用frida去hook就好啦:

from __future__ import print_function    # 这里__future__的目的是引入新版本特性
import frida
import sys

session = frida.attach('1.exe')

#local = frida.get_local_device()
#session = local.attach("1.exe")

script = session.create_script('''
Interceptor.attach(ptr("%s"),{
	onEnter: function(args){
		send(args[0].toInt32());
	}
});
''' % int(sys.argv[1], 16))

def on_message(message, data):
	print(message)
	
script.on('message', on_message)
script.load()
sys.stdin.read()

具体的细节看官方文档就好了。

补充 2022/1/25

重新把上面的例子改写了一下,增加了一点注释(上面c语言的代码也改了一点)
增加了修改参数和调用native函数的代码
其实主要的部分就是写js
外面的框架就是frida server

from __future__ import print_function
import frida
import sys


hook_func_addr = 0x40154B               # 函数的地址
call_func_addr = 0x401530

session = frida.attach('1.exe')    # 附加的进程名

#local = frida.get_local_device()
#session = local.attach("1.exe")

script = session.create_script('''

// 调用函数测试
let func_haha = new NativeFunction(ptr("%s"), 'void', [])
func_haha()       // 调用native函数
func_haha()
func_haha()

// hook
Interceptor.attach(ptr("%s"),{
    onEnter: function(args){
        send(args[0].toInt32())          // 向frida server发送一个msg, 内容是参数1
        args[0] = ptr('1111')             // 对参数进行修改
        console.log('修改参数为1111')
    }
});

''' % (call_func_addr, hook_func_addr))


def on_message(message, data):          # 收到message时候的回调函数
    print(message)


script.on('message', on_message)
script.load()
sys.stdin.read()

posted @   Mz1  阅读(2290)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示