gpt4 book ai didi

c - Linux 内核 : System call hooking example

转载 作者:IT王子 更新时间:2023-10-29 00:44:08 28 4
gpt4 key购买 nike

我正在尝试编写一些简单的测试代码作为 Hook 系统调用表的演示。

“sys_call_table”在2.6中不再导出,所以我只是从System.map文件中抓取地址,我可以看到它是正确的(在我找到的地址翻内存,我可以看到指向系统调用的指针)。

但是,当我尝试修改此表时,内核给出“糟糕”消息“无法处理虚拟地址 c061e4f4 处的内核分页请求”,并且机器重新启动。

这是运行 2.6.18-164.10.1.el5 的 CentOS 5.4。有某种保护措施还是我只是有一个错误?我知道它随 SELinux 一起提供,我已经尝试将它设置为宽容模式,但这并没有什么不同

这是我的代码:

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/unistd.h>

void **sys_call_table;

asmlinkage int (*original_call) (const char*, int, int);

asmlinkage int our_sys_open(const char* file, int flags, int mode)
{
printk("A file was opened\n");
return original_call(file, flags, mode);
}

int init_module()
{
// sys_call_table address in System.map
sys_call_table = (void*)0xc061e4e0;
original_call = sys_call_table[__NR_open];

// Hook: Crashes here
sys_call_table[__NR_open] = our_sys_open;
}

void cleanup_module()
{
// Restore the original call
sys_call_table[__NR_open] = original_call;
}

最佳答案

我终于自己找到了答案。

http://www.linuxforums.org/forum/linux-kernel/133982-cannot-modify-sys_call_table.html

内核在某些时候被更改,因此系统调用表是只读的。

密码朋克:

Even if it is late but the Solution may interest others too: In the entry.S file you will find: Code:

.section .rodata,"a"
#include "syscall_table_32.S"

sys_call_table -> ReadOnly You have to compile the Kernel new if you want to "hack" around with sys_call_table...

该链接还有一个将内存更改为可写的示例。

nasekomoe:

Hi everybody. Thanks for replies. I solved the problem long ago by modifying access to memory pages. I have implemented two functions that do it for my upper level code:

#include <asm/cacheflush.h>
#ifdef KERN_2_6_24
#include <asm/semaphore.h>
int set_page_rw(long unsigned int _addr)
{
struct page *pg;
pgprot_t prot;
pg = virt_to_page(_addr);
prot.pgprot = VM_READ | VM_WRITE;
return change_page_attr(pg, 1, prot);
}

int set_page_ro(long unsigned int _addr)
{
struct page *pg;
pgprot_t prot;
pg = virt_to_page(_addr);
prot.pgprot = VM_READ;
return change_page_attr(pg, 1, prot);
}

#else
#include <linux/semaphore.h>
int set_page_rw(long unsigned int _addr)
{
return set_memory_rw(_addr, 1);
}

int set_page_ro(long unsigned int _addr)
{
return set_memory_ro(_addr, 1);
}

#endif // KERN_2_6_24

这是对我有用的原始代码的修改版本。

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/unistd.h>
#include <asm/semaphore.h>
#include <asm/cacheflush.h>

void **sys_call_table;

asmlinkage int (*original_call) (const char*, int, int);

asmlinkage int our_sys_open(const char* file, int flags, int mode)
{
printk("A file was opened\n");
return original_call(file, flags, mode);
}

int set_page_rw(long unsigned int _addr)
{
struct page *pg;
pgprot_t prot;
pg = virt_to_page(_addr);
prot.pgprot = VM_READ | VM_WRITE;
return change_page_attr(pg, 1, prot);
}

int init_module()
{
// sys_call_table address in System.map
sys_call_table = (void*)0xc061e4e0;
original_call = sys_call_table[__NR_open];

set_page_rw(sys_call_table);
sys_call_table[__NR_open] = our_sys_open;
}

void cleanup_module()
{
// Restore the original call
sys_call_table[__NR_open] = original_call;
}

关于c - Linux 内核 : System call hooking example,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14430475/

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