ait-aecid/rootkit-detection-ebpf-time-trace

GitHub: ait-aecid/rootkit-detection-ebpf-time-trace

利用 eBPF 探针监测内核函数执行时间,通过统计学方法识别因 Rootkit 劫持而产生的时序异常,从而检测文件隐藏行为。

Stars: 28 | Forks: 3

# 使用 eBPF 时间追踪进行 Rootkit 检测 本代码库包含用于收集内核函数时间测量数据的代码,这些函数在被 Rootkit 隐藏文件时会被篡改;此外还包含一种半监督检测方法,用于分析内核函数执行时间的变化。本实现依赖于开源 Rootkit [CARAXES](https://github.com/ait-aecid/caraxes),它封装了 `filldir` 函数以篡改文件枚举的结果,例如在执行 `ls` 命令时。我们使用 eBPF 探针从 getdents 系统调用中的多个函数(包括 `filldir`)获取时间测量数据。在检测方面,我们应用了一个基于统计检验的简单机器学习模型。有关数据收集和异常检测机制的详细说明,请参阅以下出版物。如果您使用了本代码库中提供的任何资源,请引用以下出版物: * Landauer, M., Alton, L., Lindorfer, M., Skopik, F., Wurzenberger, M., & Hotwagner, W. (2025). Trace of the Times: Rootkit Detection through Temporal Anomalies in Kernel Activity. Under Review. ## Rootkit 和 eBPF 探测 以下步骤设置了 Rootkit 并解释了如何从内核函数中收集时间测量数据。如果您只对异常检测感兴趣并希望使用我们的公开数据集,可以跳过本节。 ### 设置 Rootkit 和探测功能已在 Linux 内核 5.15-6.11 和 Python 3.10 上进行了测试。要运行这些工具,请下载本代码库并安装以下运行 Rootkit 和探测机制所需的依赖项。 ``` ubuntu@ubuntu:~$ git clone https://github.com/ait-aecid/rootkit-detection-ebpf-time-trace.git ubuntu@ubuntu:~$ cd rootkit-detection-ebpf-time-trace ubuntu@ubuntu:~/rootkit-detection-ebpf-time-trace$ sudo apt update ubuntu@ubuntu:~/rootkit-detection-ebpf-time-trace$ sudo apt install python3-bpfcc make gcc flex bison python3-pip linux-headers-$(uname -r) ubuntu@ubuntu:~/rootkit-detection-ebpf-time-trace$ pip install -r requirements.txt ``` 某些场景需要额外的资源。具体来说,`ls-basic` 场景需要编译 `ls-basic` 脚本,系统负载场景需要安装 `stress-ng`。如果您不想使用这些场景,可以跳过以下依赖项。 ``` ubuntu@ubuntu:~/rootkit-detection-ebpf-time-trace$ gcc -o ls-basic ls-basic.c ubuntu@ubuntu:~/rootkit-detection-ebpf-time-trace$ sudo apt install stress-ng ``` 以下命令是强制性的,因为 Rootkit 默认情况下会篡改 `getdents` 系统调用,然而目前仅支持对 `filldir` 的篡改进行探测。下载 Rootkit,将本代码库中提供的 `hooks.h` 文件替换进去(这确保了 Hook 的是 `filldir` 而不是 `getdents`),然后编译 Rootkit。 ``` ubuntu@ubuntu:~/rootkit-detection-ebpf-time-trace$ cd .. ubuntu@ubuntu:~$ git clone https://github.com/ait-aecid/caraxes.git ubuntu@ubuntu:~$ cd caraxes/ ubuntu@ubuntu:~/caraxes$ cp ../rootkit-detection-ebpf-time-trace/hooks.h . ubuntu@ubuntu:~/caraxes$ sudo make ``` 如果您在安装 Rootkit 时遇到问题,或者想测试它是否按预期工作,请查看 [CARAXES](https://github.com/ait-aecid/caraxes) GitHub 页面上的 ReadMe。 之后,返回本代码库并打开 `linux.py`,编辑变量 `KERNEL_OBJECT_PATH`,使其指向您刚刚克隆的 caraxes 文件夹;默认路径是 `"/home/ubuntu/caraxes/"`。 ``` ubuntu@ubuntu:~/caraxes$ cd ../rootkit-detection-ebpf-time-trace ubuntu@ubuntu:~/rootkit-detection-ebpf-time-trace$ vim linux.py ``` ### 内核函数执行时间的测量 现在您已准备好运行探测机制,该机制将自动向内核注入探针、启动 Rootkit、将时间测量数据存储到文件中,并停止 Rootkit。为了触发系统调用,脚本会创建一个包含待隐藏文件的目录,并在轮询探针的同时执行 100 次 `ls`(可以使用 `-i` 标志修改)。该脚本允许收集带有 Rootkit(`--rootkit` 标志)、不带 Rootkit(`--normal` 标志)或两者皆有的测量数据,并支持多种场景。使用以下命令运行默认场景: ``` ubuntu@ubuntu:~/rootkit-detection-ebpf-time-trace$ sudo python3 probing.py --normal --rootkit compiling eBPF probes... probes compiled! attached bpf probes: iterate_dir-enter iterate_dir-return dcache_readdir-enter dcache_readdir-return filldir64-enter filldir64-return verify_dirent_name-enter verify_dirent_name-return touch_atime-enter touch_atime-return Running experiment with ls for 100 times. Iteration 0... detection_PID: 70133 Iteration 1... detection_PID: 70134 ... Iteration 99... detection_PID: 70338 polled 40 times! done with the "rootkit version" Experiment finished, saving output. Saved data to events/events_2025-01-17T09:59:52.183801_rootkit.json.gz 412K events/events_2025-01-17T09:59:52.183801_rootkit.json.gz ``` 测量数据存储在 `events` 目录中。使用 `python3 probing.py -h` 查看帮助页面,了解允许在其他场景(例如,使用 `ls-basic` 代替 `ls` 或模拟系统负载)下设置数据收集并为不同的运行分配名称(`--description`)的其他参数。查看 `repeat_seq.sh` 以获取一些参数化命令;实际上,我们使用该脚本收集了我们的公开数据集。请注意,我们只考虑了 getdents 系统调用中可用的部分内核函数。要指定探测机制应将探针附加到哪些函数,请打开 `probing.py` 并在文件开头的 `probe_points` 列表中添加或删除函数名称。 ## 异常检测 运行异常检测算法只需要安装 Python 依赖项。如果您在上一步中尚未完成此操作,请运行以下命令以使用 pip 安装依赖。 ``` ubuntu@ubuntu:~$ git clone https://github.com/ait-aecid/rootkit-detection-ebpf-time-trace.git ubuntu@ubuntu:~$ cd rootkit-detection-ebpf-time-trace ubuntu@ubuntu:~/rootkit-detection-ebpf-time-trace$ pip install -r requirements.txt ``` 然后,下载并解压我们在 [Zenodo](https://zenodo.org/records/14679675) 上提供的数据集。如果您在上一步中生成了自己的数据并希望使用它,请跳过此步骤。 ``` ubuntu@ubuntu:~/rootkit-detection-ebpf-time-trace$ wget https://zenodo.org/records/14679675/files/events.zip ubuntu@ubuntu:~/rootkit-detection-ebpf-time-trace$ unzip events.zip ``` 现在您可以按如下方式运行异常检测。指定包含测量数据的目录(`-d`)、用于训练的正常数据的比例(`-t`)、操作模式(`-m`)和分组函数(`-g`)。脚本将从指定目录加载所有文件,将其拆分为训练和测试数据(在输出中汇总),计算并打印检测指标,并绘制混淆矩阵。 ``` ubuntu@ubuntu:~/rootkit-detection-ebpf-time-trace$ python3 evaluate.py -d events -t 0.333 -m offline -g fun 100%|█████████████████████████████████████████████| 1250/1250 [02:47<00:00, 7.45it/s] Processed all files from events Normal batches: 750 Normal batches for training: 250 default: 50 file_count: 50 system_load: 50 ls_basic: 50 filename_length: 50 Normal batches for testing: 500 default: 100 file_count: 100 system_load: 100 ls_basic: 100 filename_length: 100 Anomalous batches: 500 default: 100 file_count: 100 system_load: 100 ls_basic: 100 filename_length: 100 Results (Run 1) Threshold=3.5111917342151415e-16 Time=0.0027740001678466797 TP=499 FP=9 TN=491 FN=1 TPR=R=0.998 FPR=0.018 TNR=0.982 P=0.9822834645669292 F1=0.9900793650793651 ACC=0.99 MCC=0.9801254640896192 Confusion Matrix (Run 1) Predicted default file_count system_load ls_basic filename_length Pos Neg Pos Neg Pos Neg Pos Neg Pos Neg 100 0 100 0 100 0 100 0 100 0 Pos - Actual default 3 97 100 0 99 1 100 0 2 98 Neg - Actual default 100 0 100 0 100 0 100 0 100 0 Pos - Actual file_count 100 0 0 100 100 0 100 0 100 0 Neg - Actual file_count 100 0 100 0 100 0 100 0 100 0 Pos - Actual system_load 100 0 100 0 4 96 100 0 100 0 Neg - Actual system_load 100 0 100 0 100 0 99 1 100 0 Pos - Actual ls_basic 100 0 100 0 100 0 2 98 100 0 Neg - Actual ls_basic 99 1 100 0 100 0 100 0 100 0 Pos - Actual filename_length 3 97 100 0 99 1 100 0 0 100 Neg - Actual filename_length ``` 使用 `python3 evaluate.py -h` 查看手册,以了解更多关于该脚本可用参数的信息。此外,请查看 `demo.sh`,以了解我们在论文评估中使用的参数化命令。 # 引用 如果您使用了本代码库中提供的任何资源,请引用以下出版物: * Landauer, M., Alton, L., Lindorfer, M., Skopik, F., Wurzenberger, M., & Hotwagner, W. (2025). Trace of the Times: Rootkit Detection through Temporal Anomalies in Kernel Activity. Under Review.
标签:0day挖掘, Apex, CARAXES, Docker镜像, filldir钩子, getdents, Linux内核, Linux安全模块, SQLite数据库, 内核安全, 内核探针, 内核监控, 半监督学习, 安全渗透, 异常检测, 性能分析, 文件隐藏检测, 时间侧信道, 时间序列分析, 机器学习, 系统调用劫持, 统计测试, 网络安全, 逆向工具, 隐私保护