使用frida进行硬件断点

[复制链接]
查看1028 | 回复0 | 2025-4-25 16:45:39 | 显示全部楼层 |阅读模式

立即注册,突破游客限制,与技术大牛交流切磋,畅享海量资源,让你的技术之路不再孤单!

您需要 登录 才可以下载或查看,没有账号?立即注册

×
在frida 16.5.0 中实现了硬件断点
在Frida 16.5.0 Released的发布中,frida添加了硬件断点的实现,其说明如下:

Some of you may have found yourself in a situation where a piece of data in memory looks interesting, and you want to locate the code responsible for it.
You may have tried Frida’s MemoryAccessMonitor API, but found the page granularity hard to work with.
To address this, hsorbo and I implementing support for hardware breakpoints and watchpoints. The long story short is that thread objects returned by Process.enumerateThreads() now have setHardwareBreakpoint(), setHardwareWatchpoint(), and corresponding methods to unset them at a later point. These are then combined with Process.setExceptionHandler() where you call the unset method and return true to signal that the exception was handled, and execution should be resumed.

翻译:

你们中的一些人可能遇到过这样的情况:内存中的一段数据看起来很有趣,而你想要找到生成它的代码。你可能尝试过使用Frida的MemoryAccessMonitor API,但发现其页面粒度难以操作。

为了解决这个问题,hsorbo和我正在实现对硬件断点和监视点的支持。简单来说,现在由Process.enumerateThreads()返回的线程对象有了setHardwareBreakpoint()、setHardwareWatchpoint()以及相应的取消设置方法,这些方法可以在稍后使用。然后,这些功能与Process.setExceptionHandler()结合使用,在其中调用取消方法并返回true,以表明异常已被处理,程序应该继续执行。

相关示例也在发布的链接上有实现。
其过程如下:
  • 使用`Process.enumerateMallocRanges()`扫描堆内存中的“50”,打出子弹后,匹配扫描结果中的“49”,确定子弹存储地址对子弹存储地址下硬件断点,当有汇编对子弹存储的地址rwx时则会抛出异常,打印该处汇编的地址




  1. thread.setHardwareWatchpoint(index, address, size, conditions);
  2. // `index`:硬件寄存器的索引(如0-3,受架构限制)
  3. // `address`:内存地址(需用`ptr()`转换,如`ptr('0x1fbf5191884')`)
  4. // `size`:监控的字节长度(如1、2、4、8字节)
  5. // `conditions`:触发条件,如`'w'`表示写操作,`'r'`读操作,`'x'`执行操作
复制代码
示例:
  1. function installWatchpoint(address, size, conditions) {
  2.     const thread = Process.enumerateThreads()[0];// 选择第一个线程
  3.     // 如果断点命中则会抛出异常,从而禁用硬件断点
  4.     Process.setExceptionHandler(e => {
  5.         console.log(`=== Handler got ${e.type} at ${e.context.pc}`);
  6.         // 检查当前线程是否为目标线程 且 异常类型是断点和单步执行
  7.         if (Process.getCurrentThreadId() === thread.id && ['breakpoint', 'single-step'].includes(e.type)) {
  8.             thread.unsetHardwareWatchpoint(0); // 卸载索引0的监视点
  9.             console.log('Watchpoint disabled');
  10.             return true;
  11.         }
  12.         return false;
  13.     });
  14.      
  15.     thread.setHardwareWatchpoint(0, address, size, conditions);// 硬件寄存器[0]
  16.     console.log('Watchpoint ready');
  17. }
  18. // 调用示例:监控地址0x1fbf5191884的4字节写操作
  19. installWatchpoint(ptr('0x1fbf5191884'), 4, 'w');
复制代码





这个人还没有签名
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

图文推荐
大神求
大神求
6 天前
热门排行