XavierGit109/DNS-Sentinel
GitHub: XavierGit109/DNS-Sentinel
基于 systemd-resolved 的轻量级 Linux DNS 隧道检测守护进程,通过熵分析和多维信号识别可疑 DNS 查询模式。
Stars: 0 | Forks: 0
# DNS Sentinel
用于 Linux 的 DNS 隧道检测守护进程。通过 `resolvectl` 监控 DNS 查询,并利用熵分析、标签长度、子域名深度、查询突发和 TXT 记录比例来标记可疑模式。
**版本:** 1.0.0
## 系统要求
- Python 3.10 或更新版本
- systemd-resolved(提供 `resolvectl`)
- 无需外部 Python 依赖
### 验证前置条件
```
python3 --version # must be 3.10+
command -v resolvectl # must exist
systemctl is-active systemd-resolved # must be "active"
```
## 安装
```
cd dns-sentinel-v1.0.0
sudo bash install.sh
```
此操作将会:
- 将应用程序文件复制到 `/opt/dns-sentinel/`
- 将配置文件安装到 `/etc/dns-sentinel/`
- 在 `~/Desktop/Tunnel/` 创建日志目录
- 安装并配置 systemd 服务 `dns-sentinel.service`
## 快速入门
```
# 1. 安装
sudo bash install.sh
# 2. 启用并启动
sudo systemctl enable --now dns-sentinel
# 3. 验证其正在运行
sudo systemctl status dns-sentinel
# 4. 查看实时警报
sudo journalctl -u dns-sentinel -f
```
## 工作原理
DNS Sentinel 从 `resolvectl monitor` 捕获 DNS 查询日志,并根据多个检测信号对每个查询进行分析:
| 信号 | 检测内容 | 默认阈值 |
|--------|----------------|-------------------|
| Shannon 熵 | 编码隧道载荷典型的高熵标签 | > 3.8 |
| 标签长度 | 异常长的单个标签 | > 52 个字符 |
| 子域名深度 | 嵌套层级极深的子域名 | > 5 个层级 |
| FQDN 长度 | 异常长的完整域名 | > 150 个字符 |
| 查询突发 | 来自单一来源的密集查询 | > 100 次查询 / 60 秒 |
| TXT 记录比例 | 占比过高的 TXT 查询(常见于隧道技术) | 占总查询的 > 60% |
当查询触发一个或多个信号时,警报将以每日文件的形式记录到 `~/Desktop/Tunnel/` 中,并同时记录到 systemd 日志。
## 配置
### 检测阈值
编辑 `/etc/dns-sentinel/dns-sentinel.env`:
```
# Shannon entropy 阈值(默认:3.8)
DNS_SENTINEL_ENTROPY=3.8
# 单标签长度阈值(默认:52)
DNS_SENTINEL_LABEL_LEN=52
# 子域名深度阈值(默认:5)
DNS_SENTINEL_DEPTH=5
# 总 FQDN 长度阈值(默认:150)
DNS_SENTINEL_FQDN_LEN=150
# 每个时间窗口内触发突发警报的查询数(默认:100)
DNS_SENTINEL_BURST_COUNT=100
# 突发检测时间窗口(以秒为单位,默认:60)
DNS_SENTINEL_BURST_WINDOW=60
# TXT 查询比率阈值(默认:0.6)
DNS_SENTINEL_TXT_RATIO=0.6
# 应用 TXT 比率前的最少查询数(默认:20)
DNS_SENTINEL_TXT_MIN=20
# 警报去重时间窗口(以秒为单位,默认:60)
DNS_SENTINEL_DEDUP=60
# 覆盖日志目录(默认:~/Desktop/Tunnel)
#DNS_SENTINEL_LOG_DIR=/var/log/dns-sentinel
```
编辑后,重启服务:
```
sudo systemctl restart dns-sentinel
```
### 白名单
编辑 `/etc/dns-sentinel/allowlist.txt` 以添加受信任的域名(每行一个)。匹配的域名将完全跳过分析。
重新加载白名单而无需重启:
```
sudo systemctl reload dns-sentinel
```
## 服务管理
```
# 启动 / 停止 / 重启
sudo systemctl start dns-sentinel
sudo systemctl stop dns-sentinel
sudo systemctl restart dns-sentinel
# 开机启动
sudo systemctl enable dns-sentinel
# 检查状态
sudo systemctl status dns-sentinel
# 查看日志
sudo journalctl -u dns-sentinel -f
# 重载 allowlist(无需重启)
sudo systemctl reload dns-sentinel
```
## Systemd 安全强化
服务单元包含安全强化配置:
- `NoNewPrivileges=yes`
- `ProtectSystem=strict`
- `ProtectHome=read-only`
- `ProtectKernelTunables=yes`
- `ProtectKernelModules=yes`
- `PrivateTmp=yes`
- 内置看门狗(30 秒心跳)
- 故障时自动重启并具备速率限制
## 文件布局
| 路径 | 用途 |
|------|---------|
| `/opt/dns-sentinel/` | 应用程序文件 |
| `/etc/dns-sentinel/dns-sentinel.env` | 检测阈值 |
| `/etc/dns-sentinel/allowlist.txt` | 受信任的域名 |
| `~/Desktop/Tunnel/` | 每日警报日志文件 |
## 模块化使用
DNS Sentinel 可以嵌入到其他 Python 项目中:
```
from config import Config
from analyzer import Analyzer
config = Config.from_env()
config.on_alert.append(lambda alert: print(alert))
```
## 运行测试
```
python3 test_analyzer.py -v
```
## 卸载
```
sudo bash uninstall.sh
```
## 许可证
专有软件 — 详情请参见 [LICENSE](LICENSE)。
本软件供个人和教育用途免费使用。商业用途、再分发或集成到商业产品中需要付费许可证。
有关商业许可的咨询:**xavier101alpha@proton.me**
标签:AMSI绕过, DNS安全, DNS查询分析, DNS监控, DNS隧道检测, Linux系统服务, meg, Python, resolvectl, systemd, TXT记录比例, 信息安全, 威胁检测, 子域名深度检测, 守护进程, 安全防护, 异常检测, 无后门, 查询突发检测, 系统安全运维, 网络安全, 逆向工具, 隐私保护, 香农熵