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()