挂钩系统调用ubuntu

时间:2019-11-02 15:11:43

标签: linux ubuntu hook system-calls

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

我的代码如下:

#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_call Anh地址系统调用表为ffffffff81e001e0

但是当我插入内核对象时,出现了这样的错误

BUG: unable to handle kernel paging request at ffffffff81e001f0

如何解决此错误???非常感谢

1 个答案:

答案 0 :(得分:0)

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

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

希望这对您有所帮助。