Hook 如何在Android中使用Frida钩住fopen?

Hook 如何在Android中使用Frida钩住fopen?,hook,fopen,frida,Hook,Fopen,Frida,我有一个android应用程序,我在其中加载一个库,然后在某个时候读取一个文件。这是应用程序中正在使用的代码 FILE *fp = fopen(file_name, "r"); if (fp == NULL) { return res; } 现在我正试图使用钩子钩住fopen,以强制它返回null,但我似乎无法找到如何返回null 应用程序中包含的库称为libnative lib。因此我尝试挂接fopen时包含了以下frida代码 Module.enu

我有一个android应用程序,我在其中加载一个库,然后在某个时候读取一个文件。这是应用程序中正在使用的代码

    FILE *fp = fopen(file_name, "r");
    if (fp == NULL) {
        return res;
    }
现在我正试图使用钩子钩住
fopen
,以强制它返回null,但我似乎无法找到如何返回null

应用程序中包含的库称为
libnative lib。因此
我尝试挂接
fopen
时包含了以下frida代码

 Module.enumerateExports("libnative-lib.so", {                
      onMatch: function(e) {                            
          if(e.type == 'function') {
              if(e.name == "fopen") {
        console.log("Function recognized by name");
        Interceptor.attach(e.address, {       
        onEnter: function(args) {         
            console.log("Interceptor attached onEnter...");
            },                                
        onLeave: function(retval){        
            console.log("Interceptor attached onLeave...");
            }                                 
        });                                                                   
              }                   
          }                                             
      },                                                
      onComplete: function() {}                         
  }); 

您可以尝试调用
Module.findExportByName(null,“fopen”)
来获取
fopen
的地址,而不是枚举特定库的导出(null参数告诉frida查看所有加载库的导出),并使用与您相同的
拦截器
API。
这应该有点像这样:

Interceptor.attach(Module.findExportByName(null, "fopen"), {
    onEnter: function(args) {
        console.log("Interceptor attached onEnter...");
    },
    onLeave: function(args) {
        console.log("Interceptor attached onLeave...");
    }
}
你还没有明确说明你的代码是如何失败的,但为了确保你所说的库确实加载到了应用程序中,你可以列出所有加载的模块:

Process.enumerateModules()
    .forEach(function(m) {
        // You can print just the name by using m.name or the entire system path with m.path
        console.log(JSON.stringify(m));
    });
另一种方法是使用
Process.enumerateModules()
找到正确的模块,然后对得到的
module
对象调用
enumerateExports

这将确保您在正确的模块中搜索
fopen
,如果模块名称不完全是
libnative lib。因此

Process.enumerateModules()
    .filter(function(m){ return m["path"].toLowerCase().indexOf("libnative") != -1 ; })
    .forEach(function(mod) {
        console.log(JSON.stringify(mod));
        mod.enumerateExports().forEach(function (exp) {
            if (exp.name.indexOf("fopen") != -1) {
                console.log("fopen found!");
            }
        })
    });
嗯,如果这仍然不能解决您的问题,请在您的问题中发布一些附加信息。

fopen将调用open

我建议使用一个条件来声明它将打开您的特定文件&而不是替换/覆盖

Interceptor.attach(Module.findExportByName(null,“open”){
onEnter:函数(args){
this.file_name=Memory.readCString(ptr(args[0]);
},
onLeave:函数(retval){
if(“您的文件名”==this.file\u name)//从OneNet传递
retval.replace(0x0);//返回null
}
});
顺便说一句,如果启用标志
--启用jit
,则可以使用ECMAScript 6进行筛选

Module.enumerateExports(moduleName).filter(ex=>ex.name.includes('fopen')).forEach(ex=>{..})

您正在使用文件名
libnative lib。因此
作为模块名-您确定这是正确的模块名吗?最好枚举它们并从列表中选择正确的名称:使用
enumerateModules
确实,我验证了名为
libnative lib.so
的正确库已加载,并且它确实具有
fopen
。但是,当尝试使用您使用Interceptor.attach发布的第一段代码时,出现的错误是
未能加载脚本:gDBus。错误:re.frida.error.InvalidArgument:script(第12行):语法错误:解析错误
@kampias。这只是第一段代码中的一个小语法错误。在Interceptor.attach的第二个参数中关闭对象后,我忘记关闭对attach的调用。只需添加);我不知道我是怎么错过的。。。事实上,它现在起作用了,所以问题的目标如果实现了,我将其标记为已解决。但是,你知道为什么应用程序在使用第一个代码段挂接
fopen
后崩溃吗?如果不知道你想玩什么应用程序,或者至少知道你想在oneter和onLeave中执行的代码,我无法确定。问题的范围从任何事情到您的代码在程序上造成了太多的负载,从而导致程序崩溃(如果调用了许多fopen)到钩子检测机制(不太可能),再到您在回调中做了一些不好的事情。如果没有更多的信息很难说