如何访问/proc文件系统的iiterate函数指针

问题描述 投票:0回答:1

我正在尝试创建一个简单的Linux rootkit,可用于隐藏进程。我选择尝试的方法是将指向“/proc”迭代函数的指针替换为指向自定义函数的指针,该自定义函数将隐藏我需要的进程。为此,我必须首先保存指向其原始迭代函数的指针,以便稍后可以替换它。可以通过访问“iterate”函数指针来访问“/proc”文件系统的迭代函数,该指针是“file_operations”结构的成员。

我尝试了以下两种方法来访问它,可以在代码段中看到label_1和label_2,每个方法都经过测试,另一个被注释掉。

static struct file *proc_filp;
static struct proc_dir_entry *test_proc;
static struct proc_dir_entry *proc_root;
static struct file_operations *fs_ops;

int (*proc_iterate) (struct file *, struct dir_context *);

static int __init module_init(void)
{
label_1:
    proc_filp = filp_open("/proc", O_RDONLY | O_DIRECTORY, 0);

    fs_ops = (struct file_operations *) proc_filp->f_op;
    printk(KERN_INFO "file_operations is %p", fs_ops);

    proc_iterate = fs_ops->iterate;
    printk(KERN_INFO "proc_iterate is %p", proc_iterate);

    filp_close(proc_filp, NULL);

label_2:
    test_proc = proc_create("test_proc", 0, NULL, &proc_fops);

    proc_root = test_proc->parent;
    printk(KERN_INFO "proc_root is %p", proc_root);

    fs_ops = (struct file_operations *) proc_root->proc_fops;
    printk(KERN_INFO "file_operations is %p", fs_ops);

    proc_iterate = fs_ops->iterate;
    printk(KERN_INFO "proc_iterate is %p", proc_iterate);

    remove_proc_entry("test_proc", NULL);

    return 0;
}

按照方法 1,我将“/proc”作为文件打开,然后按照文件指针访问它的“struct file_operations”(f_op),然后按照该指针尝试找到“iterate”。然而,虽然我能够成功访问“f_op”结构,但不知何故它的“迭代”似乎指向 NULL。以下 dmesg 日志显示了其输出。

[   47.707558] file_operations is 00000000b8d10f59
[   47.707564] proc_iterate is           (null)

按照方法 2,我创建一个新的 proc 目录条目,然后尝试访问它的父目录(应该指向“/proc”本身),然后尝试访问它的“struct file_operations”(proc_fops),然后尝试继续“迭代”。但是,使用这种方法我什至无法访问“/proc”目录,如“proc_root = test_proc->parent;”似乎返回NULL。这会导致后面的代码中出现“内核 NULL 指针取消引用”错误。以下 dmesg 日志显示了其输出。

[  212.078552] proc_root is           (null)
[  212.078567] BUG: unable to handle kernel NULL pointer dereference at 000000000000003

现在,我知道 Linux 内核中的情况已经发生了变化,我们不允许在不同的地址进行写入(例如将迭代指针更改为指向自定义函数),并且可以通过使这些页面可写来克服这个问题在内核中,但这将在我稍后尝试创建此 rootkit 时出现。目前我什至不知道如何读取原始的“迭代”指针。

所以,我有这些问题: [1] 下面的代码有问题吗?如何修复它? [2] 有没有其他方法可以访问 /proc 的迭代函数的指针?

在运行 linux 4.15.7 的 Arch Linux 上测试

linux linux-kernel kernel kernel-module rootkit
1个回答
0
投票

您是否还拥有该代码?我正在尝试做同样的事情,但我在编写正确的自定义函数时遇到困难。 有点卡在那里

© www.soinside.com 2019 - 2024. All rights reserved.