【Android安全】frida-gum教程

frida-gum教程

frida-gum概述

frida-gum是基于inline-hook实现的
提供的功能:代码跟踪(Stalker)、内存访问监控(MemoryAccessMonitor)、符号查找、栈回溯实现、内存扫描、动态代码生成和重定位

inline Hook 原理

. 备份原指令,重写成为跳转指令

. 跳转到我们新的代码空间,把之前备份的指令执行一下。然后执行我们自己的逻辑(打印参数之类的)

. 跳回原程序空间,继续往下跑

例如,针对SVC系统调用指令:
在这里插入图片描述

参考:
https://bbs.kanxue.com/thread-268086.htm

Interceptor

Interceptor 是对 inline-hook 的封装,一般对native层的hook就直接使用Interceptor
例如

// 按地址读参数和返回值
Java.perform(function(){
    var Offset=0xD543C4;
    Interceptor.attach(Module.findBaseAddress(SoName).add(ptr(Offset)),{
        onEnter: function(args) {
            send("addr-" + Offset + " hooked ");

            // 参数1
            send("arg0 " + " - x0: " +  this.context.x0);
            var arg0_contnt = Memory.readU64(this.context.x0);
            // send("arg0_contnt " +  parseInt(arg0_contnt,16));
            send(Memory.readByteArray(ptr(arg0_contnt),128));
            // send(Memory.readByteArray(ptr(Memory.readU64(this.context.x0)),128));//ptr('0xEC644071'): 定义一个指针,指针地址为0xEC644071

            send("arg1 " + " - x1: " +  this.context.x1);
            var arg1_contnt = Memory.readU64(this.context.x1);
            send(Memory.readByteArray(ptr(arg1_contnt),128));            

            send("arg2 " + " - x2: " +  this.context.x2);
            var arg2_contnt = Memory.readU64(this.context.x2);
            send(Memory.readByteArray(ptr(arg2_contnt),128));             

        },
        onLeave: function(retval){
            console.log(Offset +" finished \\n");
            send("arg2 " + " - x2: " +  this.context.x2);
        }
    });
});

Stalker 潜行者

可以跟踪指定线程,规定特定event发生时的行为

基本使用:

Interceptor.attach(addr, {
       onEnter: function (args) {
           this.args0 = args[0];
           this.tid = Process.getCurrentThreadId();//获取线程ID
           //跟随
           Stalker.follow(this.tid, {
               events: {//事件
                   call: true,//调用
                   ret: false,//返回
                   exec: true,//执行
                   block: false,//块
                   compile: false//编译
               },
               //接收时间
               onReceive(events){
                   for (const [index,value] of Stalker.parse(events)) {
                       console.log(index,value);
                   }
               }
           });
       }, onLeave: function (retval) {
           Stalker.unfollow(this.tid);
       }
   });

MemoryAccessMonitor 内存读写监视器

MemoryAccessMonitor可以监控 对目标内存区间的访问,在目标内存区间有读写行为时,用户指定的回调函数会被触发

本质上是将目标内存页设置为不可读写,这样在发生读写行为时会触发事先注册好的中断处理函数,其中会调用到用户使用 gum_memory_access_monitor_new 注册的回调方法中。

比如,找到了一个字符串,但是找不到该字符串的Xref(也就是读写该字符串的地方)
在这里插入图片描述
这个时候可以用frida监听该内存区域(0x581799)上的读写,来判断哪些函数使用了该字符串:

MemoryAccessMonitor.enable({
    base:libjni.base.add(0x581799),
    size:12
},{
    onAccess: function(details) {
        console.log("0x" + (details.from - libjni.base).toString(16))
    }
})

这种方法会一次修改一个内存页,并且触发一次就失效了


参考:
https://bbs.kanxue.com/thread-273450.htm
https://blog.csdn.net/weixin_56039202/article/details/127041865

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Frida是一款免费的,基于Python和JavaScript来实现的,面向开发人员、逆向工程师和安全研究人员的动态检测工具包。 Frida拥有一套全面的测试套件,不但调试效率极高,而且在广泛的使用中经历了多年严格的测试。 尤其是,移动应用安全测试和服务巨头NowSecure对齐钟爱有加,在NowSecure内部,安全人员通过Frida这个工具套装,已经完成对大量的移动应用程序大规模深度的安全分析测试。目前依然在该公司的安全测试中扮演重要的角色。 基于Python和JavaScript的Frida,天生就是跨平台的动态调试工具,不但可以运行在Windows、Linux、macOS之上,而且还可以调试Windows应用程序、Linux应用程序,macOS、iOS、Andriod和QNX等几乎全平台的应用程序。可以说,一旦掌握Frida这套工具,就可以在全平台,对全平台的应用程序进行动态调试和分析。 Frida使用极其方便,在使用过程中,只需将你编写的JavaScript脚本通过Frida自身的工具注入到目标进程中,就可以HOOK任何功能,其中包括但不限于监视加密API或跟踪应用程序关键代码等。在使用过程中,无需知道被“研究”程序的源代码。 尤其是可以一边编辑JavaScript脚本,一边运行JavaScript脚本的功能对于调试分析来说极为友好。只需“保存”正在编辑的JavaScript脚本,就立即就能看到该脚本执行的结果,全称无需其它人工介入,也无需重新启动被“研究”的应用程序,极大地简化了分析流程,同时也极大地提高了工作效率。因此,得到了众多安全分析人士的青睐。 本课程从最基本的调试环境搭建开始,基于经典的Windows“扫雷”游戏的动态调试分析,编码等,循序渐进演示Firda在分析调试Windows应用程序中基本使用方法和技巧。拥有这些知识储备之后,在加上官方的参考文档,你就可以轻松地将这些知识“迁移”至分析和调试其他平台的应用程序。 课程资料,请看第一课中github链接。
Frida-Gum is a powerful instrumentation framework for dynamic binary analysis and reverse engineering. It provides a high-level API for writing custom code to interact with native code running on different platforms. To use Gum to find a C++ class method function using C++ code, you can start by loading your target binary into memory using the `gum_process_attach()` function. Once attached, you can use `gum_module_find_export_by_name()` function to find the address of the C++ class method function inside the target binary. Here is an example code snippet that demonstrates how to use Gum to find a C++ class method function: ```c++ #include <gum/gum.h> class MyClass { public: void myMethod(int arg1, int arg2) { // implementation of myMethod } }; int main() { gum_init_embedded(); GumAddress myMethodAddress = 0; GumAddress targetAddress = gum_module_find_base_address("mybinary"); if (targetAddress != 0) { GumExportDetails exportDetails; if (gum_module_find_export_by_name("mybinary", "_ZN7MyClass8myMethodEii", &exportDetails)) { myMethodAddress = exportDetails.address; } } // myMethodAddress now contains the address of MyClass::myMethod() gum_deinit_embedded(); return 0; } ``` In this example, we defined a simple C++ class `MyClass` with a method `myMethod()`. We then attached to the target binary using `gum_process_attach()`, and used `gum_module_find_export_by_name()` to find the address of the `MyClass::myMethod()` function inside the target binary. The function signature `_ZN7MyClass8myMethodEii` corresponds to the mangled name of the `MyClass::myMethod()` function in C++. Note that this is just a basic example, and you will need to adapt the code to your specific use case. Also, keep in mind that using Gum to interact with native code requires a good understanding of the target binary and its environment.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值