AOSP源码定制-内核驱动编写

AOSP源码定制-内核驱动编写

Posted by hmoytx on April 22, 2024

AOSP源码定制-内核驱动编写

介绍

有时候为了分析一些壳的检测,需要在内核层面对读写相关的操作进行监控,每次去修改对应的内核源码编译重刷过于耗时耗力,这里就来尝试编写一个内核驱动,载入后监控读写。

前提

  1. 已经同步对应版本的内核源码并编译,这里不赘述。
  2. 真机测试并root。

开启配置

比起直接改源码,编译模块载入模块,不需要反复修改源码并刷入内核,相比较用frida等框架更不容易检测(就是因为frida检测才用这个)。
先为编译配置开启内核可加载、卸载等选项:

CONFIG_MODULES=Y
CONFIG_STRICT_MEMORY_RWX=N / CONFIG_DEBUG_RODATA=N
CONFIG_DEVMEM=Y
CONFIG_DEVKMEM=Y
CONFIG_KALLSYMS=Y
CONFIG_KALLSYMS_ALL=Y
CONFIG_HAVE_KPROBES=Y
CONFIG_HAVE_KRETPROBES=Y
CONFIG_HAVE_FUNCTION_TRACER=Y
CONFIG_HAVE_FUNCTION_GRAPH_TRACER=Y
CONFIG_TRACING=Y
CONFIG_FTRACE=Y

在内核源码目录下执行命令(前面编译过一次,会有导入过系统变量):

make menuconfig

然后出现一个图像化的配置页面。
1
通过”/”,打开搜索页面,查找上面对应的配置所在位置,以CONFIG_DEVKMEM为例,可以看到会给出定义路径。
2
去对应路径找到这个目录,drivers/char/Kconfig。
3
找到定义的位置,改成y即可。按照配置改好,重新编译内核,后面就可以开始编写驱动模块了

编译第一个内核驱动

这里我们编译一个内核模块有两种模式,一种是直接编译进内核,另一种是编译成单独的ko文件通过insmod,rmmod命令来加载与卸载。这里我们讲的是单独编译成ko文件。
在内核目录下创建一个modules目录,用于存放编写各类驱动模块。
先来写个helloworld模块进行测试。
4
简单加个代码测试,驱动代码编写有格式规范:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>
static int __init hello_init(void){
   printk(KERN_ALERT "Hello World!\n");
   return 0;
}
static void __exit hello_exit(void){
   printk(KERN_ALERT "Bye\n");
}
module_init(hello_init);
module_exit(hello_exit);

编写Makefile:

# 设置内核源码编译的输出目录
KERNEL_OUT=/home/fukuyama/sourceCode/msm/out
# 设置arm64交叉编译链工具路径
TOOLCHAIN=/home/fukuyama/sourceCode/Android8/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin/aarch64-linux-android-
# 设置arm32交叉编译链工具路径
TOOLCHAIN32=/home/fukuyama/sourceCode/Android8/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-
# 设置模块
obj-m := helloworld.o
# 编译命令配置 
all:
	make ARCH=arm64 CROSS_COMPILE_ARM32=$(TOOLCHAIN32)  CROSS_COMPILE=$(TOOLCHAIN) -C $(KERNEL_OUT) M=$(shell pwd)  modules
# 清理编译命令
clean:
	make -C $(KERNEL_OUT) M=$(shell pwd) cleancd 

直接make编译: 5
编译完后,adb 推送到 data/local/tmp目录,然后insmod执行模块查看内核日志输出即可:
6
可以看到已经有输出了,卸载模块也有输出,证明模块已经生效了。

编写监控模块

比如要监控open和read,我们需要获取到sys_call_table的基址。

echo 0 > /proc/sys/kernel/kptr_restrict 
cat /proc/kallsyms

7
然后编写代码,增加了uid大于10000筛选:

#include "linux/kernel.h"
#include "linux/init.h"
#include "linux/module.h"
#include "linux/moduleparam.h"
#include "asm/unistd.h"
#include "linux/slab.h"
#include "linux/sched.h"
#include "linux/uaccess.h"
#include <linux/syscalls.h>


void ** sys_call_table64 = (void**)0xffffffc001000000;

#define SURPRESS_WARNING __attribute__((unused))
#define LL unsigned long long

// int mm_uid = 10067;
// module_param(mm_uid, int, 0664);


SURPRESS_WARNING int getCurrentPid(void)
{
   int pid = get_current()->pid;
   return pid;
}

SURPRESS_WARNING LL isUserPid(void)
{
   const struct cred * m_cred = current_cred();
   kuid_t uid = m_cred->uid;
   int m_uid = uid.val;
   if(m_uid >10000)
   {
      return true;
   }
   return false;
}

SURPRESS_WARNING asmlinkage LL (*old_openat64)(int dirfd, const char __user* pathname, int flags, umode_t modex);
SURPRESS_WARNING LL new_openat64(int dirfd, const char __user* pathname, int flags, umode_t modex)
{
    const struct cred * m_cred = current_cred();
   kuid_t uid = m_cred->uid;
   int m_uid = uid.val;
   LL ret = -1;
   ret = old_openat64(dirfd, pathname, flags, modex);
   if(isUserPid())
   {
      char bufname[256] = {0};
      strncpy_from_user(bufname, pathname, 255);
      if(strstr("/sdcard/trace.txt",bufname)){

      }else{
          printk("myLog::openat64 pathname:[%s] ret:[%llu] current->pid:[%d] current->uid:[%d]\n", bufname,ret , getCurrentPid(),m_uid);
      }

      
   }
   
   return ret;
}


SURPRESS_WARNING asmlinkage LL (*old_read)(unsigned int fd, char __user *buf, size_t count);
SURPRESS_WARNING LL new_read(unsigned int fd, char __user *buf, size_t count)
{
    const struct cred * m_cred = current_cred();
   kuid_t uid = m_cred->uid;
   int m_uid = uid.val;
   LL ret = -1;
   
   ret = old_read(fd, buf, count);
   if(isUserPid())
   {
      char bufname[256] = {0};
      strncpy_from_user(bufname, buf, 24);
        printk("myLog::read fd:[%d] context:[%s] current->pid:[%d] current->uid:[%d]\n", fd,bufname, getCurrentPid(),m_uid);

      
   }
   return ret;
}



SURPRESS_WARNING int hook_init(void){
   printk("myLog::hook init success\n");
   if(sys_call_table64){
      old_openat64 = (void*)(sys_call_table64[__NR_openat]);
      printk("myLog::old_openat64 : %p\n", old_openat64);
      sys_call_table64[__NR_openat] = (void*)new_openat64;


      old_read = (void*)(sys_call_table64[__NR_read]);
      printk("myLog::old_read : %p\n", old_read);
      sys_call_table64[__NR_read] = (void*)new_read;

      printk("myLog::hook init end\n");
   }
   else{
      printk("mylog::fail to find sys_call_table\n");
   }
   return 0;
}


int __init myInit(void){
   printk("myLog::hooksyscall Loaded1\n");
   hook_init();
   return 0;
}

void __exit myExit(void){
   if(sys_call_table64){
      printk("myLog::cleanup start\n");
      sys_call_table64[__NR_openat] = (void*)old_openat64;
      sys_call_table64[__NR_read] = (void*)old_read;
      printk("myLog::cleanup finish\n");
   }
   printk("myLog::hooksyscall Quited\n");
}
module_init(myInit);
module_exit(myExit);

载入后查看效果:
8
已经有监控输出了。
为了对上面的监控更加细化,修改补充一部分uid的限制:

......

void ** sys_call_table64 = (void**)0xffffffc001000000;

#define SURPRESS_WARNING __attribute__((unused))
#define LL unsigned long long

int mm_uid = 10067;
module_param(mm_uid, int, 0664);

......

SURPRESS_WARNING LL isUserPid(void)
{
   const struct cred * m_cred = current_cred();
   kuid_t uid = m_cred->uid;
   int m_uid = uid.val;
   if(m_uid == mm_uid)
   {
      return true;
   }
   return false;
}

在启动模块的时候,增加启动参数,只打印对应uid的app读写监控。

insmod helloworld.ko  mm_uid=10067

演示略。
只是这个模块好像卸载的时候会死机重启。

总结

简单学习下内核驱动的编写与加载,简化内核监控,还可以补充定制用于绕过一些反调试。