AOSP源碼定制-內核驅動編寫
介紹
有時候為了分析一些殼的檢測,需要在內核層面對讀寫相關的操作進行監控,每次去修改對應的內核源碼編譯重刷過于耗時耗力,這里就來嘗試編寫一個內核驅動,載入后監控讀寫。
前提
已經同步對應版本的內核源碼并編譯,這里不贅述。
真機測試并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_GRAPH_TRACER=Y
CONFIG_TRACING=Y
CONFIG_FTRACE=Y
在內核源碼目錄下執行命令(前面編譯過一次,會有導入過系統變量):
make menuconfig
然后出現一個圖像化的配置頁面。
通過"/",打開搜索頁面,查找上面對應的配置所在位置,以CONFIG_DEVKMEM為例,可以看到會給出定義路徑。
去對應路徑找到這個目錄,drivers/char/Kconfig。
找到定義的位置,改成y即可。按照配置改好,重新編譯內核,后面就可以開始編寫驅動模塊了
編譯第一個內核驅動
這里我們編譯一個內核模塊有兩種模式,一種是直接編譯進內核,另一種是編譯成單獨的ko文件通過insmod,rmmod命令來加載與卸載。這里我們講的是單獨編譯成ko文件。 在內核目錄下創建一個modules目錄,用于存放編寫各類驅動模塊。 先來寫個helloworld模塊進行測試。
簡單加個代碼測試,驅動代碼編寫有格式規范:
#include
#include
#include
static int __init hello_init(void){
printk(KERN_ALERT "Hello World! ");
return 0;
}
static void __exit hello_exit(void){
printk(KERN_ALERT "Bye ");
}
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編譯:
編譯完后,adb 推送到 data/local/tmp目錄,然后insmod執行模塊查看內核日志輸出即可:
可以看到已經有輸出了,卸載模塊也有輸出,證明模塊已經生效了。
編寫監控模塊
比如要監控open和read,我們需要獲取到syscalltable的基址。
echo 0 > /proc/sys/kernel/kptr_restrict
cat /proc/kallsyms
然后編寫代碼,增加了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
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] ", 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] ", fd,bufname, getCurrentPid(),m_uid);
}
return ret;
}
SURPRESS_WARNING int hook_init(void){
printk("myLog::hook init success ");
if(sys_call_table64){
old_openat64 = (void*)(sys_call_table64[__NR_openat]);
printk("myLog::old_openat64 : %p ", old_openat64);
sys_call_table64[__NR_openat] = (void*)new_openat64;
old_read = (void*)(sys_call_table64[__NR_read]);
printk("myLog::old_read : %p ", old_read);
sys_call_table64[__NR_read] = (void*)new_read;
printk("myLog::hook init end ");
}
else{
printk("mylog::fail to find sys_call_table ");
}
return 0;
}
int __init myInit(void){
printk("myLog::hooksyscall Loaded1 ");
hook_init();
return 0;
}
void __exit myExit(void){
if(sys_call_table64){
printk("myLog::cleanup start ");
sys_call_table64[__NR_openat] = (void*)old_openat64;
sys_call_table64[__NR_read] = (void*)old_read;
printk("myLog::cleanup finish ");
}
printk("myLog::hooksyscall Quited ");
}
module_init(myInit);
module_exit(myExit);
載入后查看效果:
已經有監控輸出了。 為了對上面的監控更加細化,修改補充一部分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
演示略。 只是這個模塊好像卸載的時候會死機重啟。
總結
簡單學習下內核驅動的編寫與加載,簡化內核監控,還可以補充定制用于繞過一些反調試。
-
Modules
+關注
關注
0文章
10瀏覽量
8012 -
AOSP
+關注
關注
0文章
16瀏覽量
6222 -
內核驅動
+關注
關注
0文章
5瀏覽量
2681
原文標題:AOSP源碼定制-內核驅動編寫
文章出處:【微信號:哆啦安全,微信公眾號:哆啦安全】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
評論