拦截 open调用 (进程白名单,文件白名单)

本文介绍了一个Linux系统调用拦截器,它拦截`open`调用,实现进程白名单和文件白名单。代码示例展示了如何通过`LD_PRELOAD`加载自定义的`open`函数,限制非白名单进程访问特定文件,并记录访问日志。文中提到了代码优化建议,如使用哈希表存储白名单,减少内存分配和提高效率。
摘要由CSDN通过智能技术生成

拦截 open

gcc -shared -fPIC -o libintercept.so intercept.c -ldl
LD_PRELOAD=./libintercept.so ./processA
在Linux中,我们可以使用LD_PRELOAD环境变量来拦截并替换系统调用,如open函数。这是一个简单的示例,展示了如何创建一个新的open函数来替换系统的open函数:
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>

// 定义原始的open函数
static int (*original_open)(const char*, int, ...);

// 我们的新open函数
int open(const char *pathname, int flags, ...) {
   
    printf("Intercepted open call for %s\n", pathname);

    // 获取原始的open函数
    original_open = dlsym(RTLD_NEXT, "open");

    // 调用原始的open函数
    return original_open(pathname, flags);
}

~~~

你可以将这段代码保存为一个.c文件,然后使用以下命令编译为共享库:

gcc -shared -fPIC -o libintercept.so intercept.c -ldl


然后,你可以使用LD_PRELOAD环境变量来运行你的程序,如下所示:

```bash
LD_PRELOAD=./libintercept.so your_program

这样,每当你的程序调用open函数时,都会调用我们的新open函数,而不是系统的open函数。

请注意,这只是一个基本的示例,你可能需要根据你的具体需求来修改这个新的open函数。此外,使用LD_PRELOAD拦截系统调用应该谨慎进行,因为它可能会影响到其他库的行为。


第一个需求

第一个需求
1.白名单进程 保存在 进程白名单文件 , 受保护文件的文件名保存在 文件白名单文件 中,
2.白名单 进程 能访问 所有文件(受保护文件和非保护文件)
3.对所有进程访问文件的 过程进行日志记录.(进程名,文件名,访问时间,成功与否)

在这里插入图片描述

文件结构

.
├── Files
│   ├── t1
│   ├── t2
│   ├── t3
│   ├── t4
│   ├── t5
│   ├── t6
│   ├── t7
│   ├── t8
│   └── t9
├── WHITELIST
│   ├── file_whitelist
│   ├── file_whitelist_backup
│   └── process_whitelist
├── intercept.c
├── libintercept.so
├── log.txt
├── p1
└── p1.c
进程白名单

白名单文件的路径名是/home/xxx/Test/WHITELIST/process_whitelist

/home/xxx/Test/p1
/home/xxx/Test/p2
/home/xxx/Test/p3
/home/xxx/Test/p4
/home/xxx/Test/p5
文件白名单

非白名单文件的路径名是/home/xxx/Test/WHITELIST/file_whitelist

/home/xxx/Test/Files/t1
/home/xxx/Test/Files/t2
/home/xxx/Test/Files/t3
/home/xxx/Test/Files/t4
/home/xxx/Test/Files/t5

测试代码

#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main() {
   
    DIR *dir;
    struct dirent *entry;
    int fd;

    // 打开目录
    dir = opendir("/home/xxx/Test/Files");
    if (dir == NULL) {
   
        perror("无法打开目录");
        return 1;
    }

    // 遍历目录中的每个文件
    while ((entry = readdir(dir)) != NULL) {
   
        // 检查文件名是否以"t"开头
        if (strncmp(entry->d_name, "t", 1) == 0) {
   
            char filepath[1024];

            // 构造文件路径
            snprintf(filepath, sizeof(filepath), "/home/xxx/Test/Files/%s", entry->d_name);

            // 使用open函数打开文件
            fd = open(filepath, O_RDWR);
            if (fd == -1) {
   
                perror("无法打开文件");
                continue;
            }

            printf("打开文件: %s\n", filepath);

            // 写入文件
            const char *message = "我访问了这个文件\n";
            if (write(fd, message, strlen(message)) == -1) {
   
                perror("无法写入文件");
            }

            // 关闭文件
            if (close(fd) == -1) {
   
                perror("无法关闭文件");
            }
        }
    }

    // 关闭目录
    if (closedir(dir) == -1) {
   
        perror("无法关闭目录");
    }

    return 0;
}

第一个版本

gcc -shared -fPIC -o libintercept.so intercept.c -ldl

当然,这是修改后的完整代码:

#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>

// 定义原始的open函数
static int (*original_open)(const char*, int, ...);

// 根据进程ID获取进程名
char *get_process_name(pid_t pid) {
   
    char path[256
  • 21
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
拦截器可以用来实现IP白名单的功能,具体步骤如下: 1. 创建一个拦截器类,实现HandlerInterceptor接口,并在preHandle方法中进行IP白名单校验。代码示例: ```java public class IPWhiteListInterceptor implements HandlerInterceptor { private List<String> whiteList = Arrays.asList("127.0.0.1", "192.168.0.1"); // IP白名单列表 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String ip = request.getRemoteAddr(); // 获取客户端IP地址 if (whiteList.contains(ip)) { // 如果客户端IP在白名单列表中,则放行 return true; } else { // 否则返回403错误 response.sendError(HttpServletResponse.SC_FORBIDDEN, "IP地址不在白名单中"); return false; } } } ``` 2. 在WebMvcConfigurer的addInterceptors方法中注册拦截器,并指定拦截规则。代码示例: ```java @Configuration public class IPWhiteListWebMvcConfiguration implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new IPWhiteListInterceptor()) .addPathPatterns("/**"); // 拦截所有请求 } } ``` 3. 在需要进行IP白名单校验的Controller方法上添加自定义注解,并在拦截器中获取注解配置信息进行校验。代码示例: ```java @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface IPWhiteList { String[] value() default {}; // IP白名单列表 } public class IPWhiteListInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; IPWhiteList annotation = handlerMethod.getMethodAnnotation(IPWhiteList.class); if (annotation != null) { // 如果方法上有IPWhiteList注解,则进行IP白名单校验 String[] whiteList = annotation.value(); String ip = request.getRemoteAddr(); if (Arrays.asList(whiteList).contains(ip)) { return true; } else { response.sendError(HttpServletResponse.SC_FORBIDDEN, "IP地址不在白名单中"); return false; } } } return true; } } @RestController public class UserController { @GetMapping("/user") @IPWhiteList({"127.0.0.1", "192.168.0.1"}) // 添加IPWhiteList注解 public String getUser() { return "user"; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丁金金

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值