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 @ 2021-10-11 18:53  Mz1  阅读(1818)  评论(0编辑  收藏  举报