以下是一个简单的示例代码,使用Linux的FUSE模块来实现hook系统:

#define FUSE_USE_VERSION 26

#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

static const char *root_dir = "/root/1";
static const char *forbidden_file = "forbidden.txt";

static int hook_getattr(const char *path, struct stat *stbuf)
{
    int res;

    // 判断是否在/root/1目录下
    if (strncmp(path, root_dir, strlen(root_dir)) != 0) {
        // 不是,不做拦截
        res = lstat(path, stbuf);
        if (res == -1) {
            return -errno;
        }
        return 0;
    }

    // 是,拦截并返回错误
    res = -ENOENT;
    if (strcmp(path + strlen(root_dir), "/") == 0 ||
        strcmp(path + strlen(root_dir), "/" forbidden_file) == 0) {
        // 请求根目录或禁止文件时返回错误
        return res;
    }

    // 其他请求都允许
    res = lstat(path, stbuf);
    if (res == -1) {
        return -errno;
    }
    return 0;
}

static int hook_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
                        off_t offset, struct fuse_file_info *fi)
{
    DIR *dp;
    struct dirent *de;

    (void) offset;
    (void) fi;

    // 判断是否在/root/1目录下
    if (strncmp(path, root_dir, strlen(root_dir)) != 0) {
        // 不是,不做拦截
        dp = opendir(path);
        if (dp == NULL) {
            return -errno;
        }

        while ((de = readdir(dp)) != NULL) {
            if (filler(buf, de->d_name, NULL, 0) != 0) {
                break;
            }
        }

        closedir(dp);
        return 0;
    }

    // 是,只返回根目录和禁止文件
    filler(buf, ".", NULL, 0);
    filler(buf, "..", NULL, 0);
    filler(buf, forbidden_file, NULL, 0);
    return 0;
}

static int hook_open(const char *path, struct fuse_file_info *fi)
{
    // 判断是否在/root/1目录下且请求了禁止文件
    if (strncmp(path, root_dir, strlen(root_dir)) == 0 &&
        strcmp(path + strlen(root_dir), "/" forbidden_file) == 0) {
        // 是,拦截并返回错误
        return -ENOENT;
    }

    // 其他请求都允许
    int fd = open(path, fi->flags);
    if (fd == -1) {
        return -errno;
    }

    close(fd);
    return 0;
}

static int hook_read(const char *path, char *buf, size_t size, off_t offset,
                     struct fuse_file_info *fi)
{
    // 判断是否在/root/1目录下且请求了禁止文件
    if (strncmp(path, root_dir, strlen(root_dir)) == 0 &&
        strcmp(path + strlen(root_dir), "/" forbidden_file) == 0) {
        // 是,拦截并返回错误
        return -ENOENT;
    }

    // 其他请求都允许
    int fd = open(path, fi->flags);
    if (fd == -1) {
        return -errno;
    }

    int res = pread(fd, buf, size, offset);
    if (res == -1) {
        res = -errno;
    }

    close(fd);
    return res;
}

static struct fuse_operations hook_oper = {
    .getattr    = hook_getattr,
    .readdir    = hook_readdir,
    .open       = hook_open,
    .read       = hook_read,
};

int main(int argc, char *argv[])
{
    return fuse_main(argc, argv, &hook_oper, NULL);
}

这里使用了FUSE模块来实现hook系统,具体的hook逻辑在hook_getattr()hook_readdir()hook_open()hook_read()函数中实现。

hook_getattr()函数用于拦截访问目录和文件的请求,判断是否在/root/1目录下,并且请求了禁止文件,如果是则返回错误,否则允许访问。

hook_readdir()函数用于拦截读取目录内容的请求,只返回/root/1目录下的根目录和禁止文件。

hook_open()函数用于拦截打开文件的请求,判断是否在/root/1目录下,并且请求了禁止文件,如果是则返回错误,否则允许打开文件。

hook_read()函数用于拦截读取文件内容的请求,判断是否在/root/1目录下,并且请求了禁止文件,如果是则返回错误,否则允许读取文件内容。

main()函数中调用fuse_main()函数启动FUSE模块,使用hook_oper结构体将hook函数传递给FUSE模块,从而实现hook系统

使用Linux是hook系统 当有进程访问root1目录下内容是拦截返回值提示 禁止操作 并给出代码示例

原文地址: https://www.cveoy.top/t/topic/fkaO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录