吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 273|回复: 8
收起左侧

[求助] 求助各位大佬,关于fridaNativeFunction后和Interceptor.attach的问题

[复制链接]
pass流逝 发表于 2024-1-18 15:21
想请教下各位大佬一个问题
就是假设在在目标程序上有个A函数,
那么我是用frIDA的NativeFunction方法把A函数本地化后,
同时我又使用了Interceptor.attach来拦截A函数的参数,并且实现了一些功能逻辑

那么就会出现一个问题,如果是是目标程序调用的A函数,
他会走我的拦截函数并且按我代码实现必要逻辑,
但是如果是我主动调用的A函数,他不会去执行我的拦截函数

我知道有的大佬说为啥不用Interceptor.replace来实现,但是一样是主动调用的时候不会走这里面的逻辑,

可能也有大佬说,你可以在主动调用A后,然后在去实现拦截里面的逻辑,但是有些逻辑存在执行优先级的问题,或者是说触发时机的问题

所以请教下各位大佬有没有好的办法,最好可以给一个例子的,用的JS

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

wasm2023 发表于 2024-1-24 10:37
楼主,问题解决了吗,最好能提供下apk啥的
 楼主| pass流逝 发表于 2024-1-26 17:31
wasm2023 发表于 2024-1-24 10:37
楼主,问题解决了吗,最好能提供下apk啥的

额,并没有解决,我是hook的x86的程序,不是安卓的,但是我在他官方的文档中看到这样一段api的说明
new NativeFunction(address, returnType, argTypes[, options]): just like the previous constructor, but where the fourth argument, options, is an object that may contain one or more of the following keys:

abi: same enum as above.
scheduling: scheduling behavior as a string. Supported values are:
cooperative: Allow other threads to execute JavaScript code while calling the native function, i.e. let go of the lock before the call, and re-acquire it afterwards. This is the default behavior.
exclusive: Do not allow other threads to execute JavaScript code while calling the native function, i.e. keep holding the JavaScript lock. This is faster but may result in deadlocks.
exceptions: exception behavior as a string. Supported values are:
steal: If the called function generates a native exception, e.g. by dereferencing an invalid pointer, Frida will unwind the stack and steal the exception, turning it into a JavaScript exception that can be handled. This may leave the application in an undefined state, but is useful to avoid crashing the process while experimenting. This is the default behavior.
propagate: Let the application deal with any native exceptions that occur during the function call. (Or, the handler installed through Process.setExceptionHandler().)
traps: code traps to be enabled, as a string. Supported values are:
default: Interceptor.attach() callbacks will be called if any hooks are triggered by a function call.
all: In addition to Interceptor callbacks, Stalker may also be temporarily reactivated for the duration of each function call. This is useful for e.g. measuring code coverage while guiding a fuzzer, implementing “step into” in a debugger, etc. Note that this is also possible when using the Java and ObjC APIs, as method wrappers also provide a clone(options) API to create a new method wrapper with custom NativeFunction options.
这里说了NativeFunction时候,给他指定上{traps:default} 就可以了,实际上我测试后也是无效的,这个api是在12.8开始就支持了的,然后还有一个很抽象的问题,就是我一个朋友,他用replace来实现,他那边可以走hook,我这边不行,代码一样,我也搞不懂为啥。
wasm2023 发表于 2024-1-26 18:17
貌似与电脑,frida版本有关,实际是可以走hook的
 楼主| pass流逝 发表于 2024-1-26 19:52
wasm2023 发表于 2024-1-26 18:17
貌似与电脑,frida版本有关,实际是可以走hook的

额,但是很抽象,这种问题不只是我一个人有,所以我也不知道为什么,版本我现在用的16.11的 15的也用过一样不走,
wasm2023 发表于 2024-1-26 20:21
gpt4的参考说明:当使用 Frida 的 Interceptor.attach 或 Interceptor.replace 时,只有当目标程序自然地(即非通过 Frida 主动调用)调用该函数时,拦截逻辑才会被执行。这是因为 Frida 的拦截器是在目标程序的执行流中激活的。当你通过 Frida 的 NativeFunction 主动调用同一函数时,这种调用绕过了拦截逻辑,因为它直接发生在 Frida 的上下文中,而不是目标程序的正常执行流中。
要解决这个问题,你可以创建一个包装函数,这个包装函数既包含拦截逻辑,又能调用原始函数。这样,无论是目标程序还是你通过 Frida 主动调用该函数,都会执行相同的逻辑。
例如,假设有一个名为 A 的函数,你可以这样做:
使用 Interceptor.attach 在 A 函数上设置拦截器。
创建一个包装函数 wrappedA,它包含拦截逻辑并调用 A。
当需要主动调用 A 函数时,调用 wrappedA 而不是直接调用 A。
以下是一个示例代码片段:
javascriptCopy code
// 原始的 A 函数地址
var aFuncAddr = ptr("0x123456"); // 替换为实际地址

// 使用 NativeFunction 主动调用 A
var aFunc = new NativeFunction(aFuncAddr, 'void', ['int']);

// 拦截 A 函数
Interceptor.attach(aFuncAddr, {
    onEnter: function(args) {
        // 拦截逻辑
        console.log("A 函数被调用,参数: " + args[0]);
    },
    onLeave: function(retval) {
        // 可能的返回处理
    }
});

// 包装函数,包含拦截逻辑并调用 A
function wrappedA(arg) {
    // 这里复制拦截器中的逻辑
    console.log("wrappedA 被调用,参数: " + arg);

    // 调用原始的 A 函数
    aFunc(arg);
}

// 现在,无论是主动调用还是目标程序调用,都会执行相同的拦截逻辑
wrappedA(10); // 主动调用
这种方法确保了不论是通过正常流程调用 A 还是通过 Frida 主动调用 wrappedA,都会执行相同的逻辑。这样,你就可以在不改变原有逻辑和优先级的情况下,实现你想要的功能。
 楼主| pass流逝 发表于 2024-1-27 17:19
wasm2023 发表于 2024-1-26 20:21
gpt4的参考说明:当使用 Frida 的 Interceptor.attach 或 Interceptor.replace 时,只有当目标程序自然地( ...

这样的确是一个解决办法,就是这样太麻烦了,哈哈哈哈,用frida就是贪图方便,这种方法解决小函数什么的,或者是拦截逻辑简单的,可以,但是稍微负责一点就特别麻烦了,所以,我也是没办法,逼得用我的蹩脚c++撸了一个。不过谢谢大佬帮我解答
wasm2023 发表于 2024-1-27 23:11
pass流逝 发表于 2024-1-27 17:19
这样的确是一个解决办法,就是这样太麻烦了,哈哈哈哈,用frida就是贪图方便,这种方法解决小函数什么的 ...

大佬,记得看下私信呦
 楼主| pass流逝 发表于 2024-1-28 14:36
wasm2023 发表于 2024-1-27 23:11
大佬,记得看下私信呦

好嘞好嘞
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-12 00:03

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表