frida hook Windows程序

前言

最近在看frida的使用,就阅读了一下官方文档在此记录一下。此篇内容:https://frida.re/docs/functions/

内容讲的是用frida hook C语言编写的控制台程序,我用的C编译器Dev C++: https://wwx.lanzoui.com/iIEQlmddw7c

第一个例子:hello.exe

exe代码,将这个代码命名为hello.c用dev打开编译一下会生成hello.exe。打开让他一直运行,并记录打印的f函数地址

#include <stdio.h>
#include <unistd.h>

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

int main(int argc, char * argv[])
{
  int i = 0;
  printf ("f() is at %p\n", f);
  while (1)
  {
    f (i++);
    sleep (1);
  }
}
hook 获取函数参数

也可以获取函数返回值,onEnter下面加个onLeave就行。将下面的代码命名为hooktest1.py

from __future__ import print_function
import frida
import sys

#device = frida.get_device_manager().enumerate_devices()
local = frida.get_local_device()
# a = local.get_process('hello.exe')
# print(a)
session = local.attach("hello.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()

接着命令行运行python hooktest1.py f函数地址,f函数地址为hello.exe打印的函数地址。你会发现函数参数会被打印的终端,说明hook成功了。
在这里插入图片描述

hook修改参数

modifytest1.py

import frida
import sys

session = frida.attach("hello.exe")
script = session.create_script("""
Interceptor.attach(ptr("%s"), {
    onEnter: function(args) {
        args[0] = ptr("1337");
    }
});
""" % int(sys.argv[1], 16))
script.load()
sys.stdin.read()

接着命令行运行python modifytest1.py f函数地址,你会发现hello.exe打印的内容变成了1337,不在的递增了。当你Ctrl+C停止py脚本的时候,打印的内容又是原来的内容。
在这里插入图片描述

hook调用函数

calltest1.py

import frida
import sys

session = frida.attach("hello.exe")
script = session.create_script("""
var f = new NativeFunction(ptr("%s"), 'void', ['int']);
f(1911);
f(1911);
f(1911);
""" % int(sys.argv[1], 16))
script.load()

接着命令行运行python calltest1.py f函数地址,你会发现hello.exe多打印了三个1911。很明显NativeFunction这个对象就是调用其他程序的函数,第一个参数为函数地址,第二个参数为函数返回值,第三个参数为函数参数列表。
在这里插入图片描述

第二个例子:hi.exe

c语言代码,和第一个例子的唯一区别只是f函数的参数变成了字符串

#include <stdio.h>
#include <unistd.h>

int f(const char * s)
{
  printf ("String: %s\n", s);
  return 0;
}

int main(int argc, char * argv[])
{
  const char * s = "Testing!";

  printf ("f() is at %p\n", f);
  printf ("s is at %p\n", s);

  while (1)
  {
    f (s);
    sleep (1);
  }
}
hook修改字符串
from __future__ import print_function
import frida
import sys

session = frida.attach("hi.exe")
script = session.create_script("""
//创建字符串对象
var st = Memory.allocUtf8String("TESTMEPLZ!");
var f = new NativeFunction(ptr("%s"), 'int', ['pointer']);
//调用函数
f(st);
""" % int(sys.argv[1], 16))
def on_message(message, data):
    print(message)
script.on('message', on_message)
script.load()

和上面一样的运行,一样的效果,只是需要在js代码中自定义字符串对象。

其他

官网还有个例子是hook修改socket连接端口,不过给的C代码是运行在Linux系统的。我也不懂C,就无法测试了。

Frida是一个强大的动态分析工具,可以用于对应用程序进行hookFrida提供了多种hook方式,包括在应用程序启动前注入代码、通过USB连接和远程连接等。通过在应用程序启动前注入代码,可以在应用程序启动时即实现hook的效果。可以使用frida.get_usb_device()方法连接待调试的USB设备,并使用frida.get_device()方法指定调试的设备。此外,也可以通过远程连接方式进行hook,使用frida -U -f 包名 -l xxx.js --no-pause指令进行注入。以上是使用Frida进行hook的一些基本操作方式。需要根据具体的场景和需求选择合适的方法和参数进行操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Frida hook零基础教程](https://blog.csdn.net/cyjmosthandsome/article/details/120906998)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [python之frida安卓逆向之hook大法好](https://blog.csdn.net/weixin_51111267/article/details/125109497)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值