gpt4 book ai didi

linux - Hook 系统调用ubuntu

转载 作者:太空宇宙 更新时间:2023-11-04 11:43:07 24 4
gpt4 key购买 nike

我想尝试打开系统调用,在 Ubuntu 16.04 - 64 位 - 内核版本:4.15.0-45-generic 中编写

我的代码如下:

#include <asm/unistd.h>
#include <asm/cacheflush.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/syscalls.h>
#include <asm/pgtable_types.h>
#include <linux/highmem.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/moduleparam.h>
#include <linux/unistd.h>
#include <asm/cacheflush.h>
#define syscall __NR_open
MODULE_LICENSE("GPL");
MODULE_AUTHOR("LAMDUCANH _ CNTN");

/*MY sys_call_table address*/
//ffffffff81e001e0
void **system_call_table_addr;
/*my custom syscall that takes process name*/
asmlinkage int (*custom_syscall) (const char* path, int flag, mode_t mode);
/*hook*/
asmlinkage int hook(const char* path, int flag, mode_t mode) {
printk(KERN_INFO "Pname Syscall:HOOK! HOOK! HOOK! HOOK!...ROOOFFIIOO!");
printk(KERN_INFO "The process that opens the file is: %s\n", current->comm);
printk(KERN_INFO "The file name: %s\n", path);
return custom_syscall(path, flag, mode);
}

/*Make page writeable*/
int make_rw(unsigned long address){
unsigned int level;
pte_t *pte = lookup_address(address, &level);
if(pte->pte &~_PAGE_RW){
pte->pte |=_PAGE_RW;
}
return 0;
}
/* Make the page write protected */
int make_ro(unsigned long address){
unsigned int level;
pte_t *pte = lookup_address(address, &level);
pte->pte = pte->pte &~_PAGE_RW;
return 0;
}
static int __init entry_point(void){
printk(KERN_INFO "Module load successfully");
system_call_table_addr = (void*)0xffffffff81e001e0;
printk(KERN_INFO "Address system call open = %p", (void*) system_call_table_addr[syscall]);
custom_syscall = system_call_table_addr[syscall];
make_rw((unsigned long)system_call_table_addr);
system_call_table_addr[syscall] = hook;
return 0;
}
static void __exit exit_point(void){
printk(KERN_INFO "Module unload successfully");
system_call_table_addr[syscall] = custom_syscall;
make_ro((unsigned long)system_call_table_addr);
return;
}
module_init(entry_point);
module_exit(exit_point);

我通过这种方式找到我的地址系统调用表: sudo cat /boot/System.map-4.15.0-45-generic | grep sys_callanh地址系统调用表为ffffffff81e001e0

但是当我insmod内核对象时,我得到了这样的错误

BUG: unable to handle kernel paging request at ffffffff81e001f0

我该如何解决这个错误???非常感谢

最佳答案

我不确定此错误是否与地址系统调用表有关,但您可以尝试以下代码:

system_call_table_addr = (void*)kallsyms_lookup_name("sys_call_table");

希望对你有帮助。

关于linux - Hook 系统调用ubuntu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58672367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com