Android 11在libc层拦截I/O调用

需要在Android 11的Libc层拦截系统调用。之前在网络上找到一篇博文,说的是修改下述汇编文件:(以read调用为例)

bionic/libc/arch-arm/syscalls/read.S

博文地址:

https://blog.csdn.net/m0_37340681/article/details/88577679

经实践,上述方法在Android 10版本是适用的。估计在Android 10以前的版本也适用。

但是,Android 11没有bionic/libc/arch-arm/syscalls/ 文件夹,也没有read.S等汇编文件,需要我们探索新的方法。

查看SYSCALLS.TXT

打开bionic/libc/SYSCALLS.txt,查看里面的注释,里面有这么一句:

This file is used to automatically generate bionic’s system call stubs.


This file is processed by a python script named gensyscalls.py, run via genrules in Android.bp.

大意是:编译时,会根据这个SYSCALLS.txt来自动生成系统调用的.S文件。

也就是说,在Android 11 中,bionic/libc/arch-arm/syscalls/这个文件夹的汇编文件是自动生成的,且编译时才会生成。

下面以close调用为例说明拦截方法。

1. 修改SYSCALLS.TXT

在SYSCALLS.TXT中找到:

>  int         __close:close(int)  all

修改为:

>  int         ____close:close(int)  all

即加两个下划线。随后编译生成的系统调用函数名便会变为____close(int)

2. 编写__close.cpp

  1. 在bionic/libc/bionic中新建__close.cpp文件

  2. 在__close.cpp中添加如下代码:

#include <unistd.h>
#include <sys/__close.h>

//真正的系统调用
extern "C" int ____close(int);

//我们拦截并修改的系统调用
int __close(int f){
    /* 此处添加你的代码 */
    /* ........................... */
    return ____close(f);
}

这样原来调用__close()的程序会进入到我们的__close()函数中。

3. 编写__close.h

  1. 在bionic/libc/include/sys中新建__close.h文件
  2. 编写__close.h文件:

#include <linux/unistd.h>
#include <sys/cdefs.h>
#include <sys/stat.h>

__BEGIN_DECLS

extern int __close(int);

__END_DECLS

4. 修改编译脚本

  1. 打开文件bionic/libc/Android.bp
  2. 搜索"bionic/open.cpp",来到它附近。这个位置有一堆cpp。
  3. 在附近一行插入如下(注意加逗号)
"bionic/__close.cpp",

5. 编译并刷入

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值