aimarketingflow/castiel-dns-defender

GitHub: aimarketingflow/castiel-dns-defender

一款跨平台的实时 DNS 攻击检测、防御与告警代理,通过原生防火墙拦截流量并提供超过25种基于DNS的威胁检测器。

Stars: 0 | Forks: 0

# Castiel **实时 DNS 攻击检测、防御与告警** 门户的守护者,关口注视者。 [![CI](https://static.pigsec.cn/wp-content/uploads/repos/cas/ad/ad5834178f7599af9fdda11629d49cae07f2997beec49821b2920eff5bfd50e7.svg)](https://github.com/aimarketingflow/castiel-dns-defender/actions/workflows/ci.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/castiel/dns)](https://goreportcard.com/report/github.com/castiel/dns) [![License: Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![Go Version](https://img.shields.io/badge/Go-1.23+-00ADD8.svg)](https://go.dev/) macOS · Linux · Windows · Raspberry Pi
## 概述 Castiel 是一个跨平台的 DNS 防御代理,它通过原生平台的防火墙重定向拦截所有 DNS 流量,并在将其转发到上游之前,让每个查询通过一个包含 25 多个检测器的 pipeline。它能实时检测并拦截基于 DNS 的攻击,包括 DGA 域名、DNS tunneling、fast-flux C2、water torture、cache poisoning、DNS rebinding、仿冒域名等。 ### 为什么选择 Castiel? - **25+ 种攻击检测器**,分为两个层级——从基础的 rate limiting 到 APT12 DNS 计算检测 - **真正的跨平台**——macOS 上的 PF,Linux 上的 nftables/iptables,Windows 上的 Windows 防火墙 - **单一静态二进制文件**——无运行时依赖,无需 Python,无需 Node.js - **防止 DoH 绕过**——拦截发往 18+ 个已知公共解析器的直接加密 DNS 连接 - **Prometheus 指标**——内置可观测性与 Grafana 仪表板 - **桌面告警**——三大平台上的原生通知 - **攻击模拟器**——内置 `attack-sim` 工具,用于测试所有检测机制 ## 快速开始 ### 从源码安装 ``` go install github.com/castiel/dns@latest ``` ### 通过 Homebrew 安装 ``` brew tap aimarketingflow/tap brew install --cask castiel ``` ### 从源码构建 ``` git clone https://github.com/aimarketingflow/castiel-dns-defender.git cd castiel-dns-defender go build -o castiel . sudo ./castiel -config config.yaml ``` ### macOS ``` sudo make install # Installs daemon, app, LaunchDaemon launchctl list | grep castiel # Check status ``` ### Linux / Raspberry Pi(即将推出) ``` make build-linux # Cross-compile (amd64 + arm64) sudo ./linux_build/install.sh # Install on target systemctl status castiel # Check status ``` ### Windows(即将推出) ``` make build-windows # Cross-compile .\windows_build\install.ps1 # Run as Admin sc.exe query Castiel # Check status ``` ## 特定平台的 DNS 拦截 | 平台 | 防火墙 | 服务管理器 | 通知 | |---|---|---|---| | **macOS** | 通过 `pfctl` 的 PF (Packet Filter) | LaunchDaemon (`launchctl`) | 通知中心 | | **Linux** | nftables(或 iptables 回退) | systemd | `notify-send` (libnotify) | | **Windows** | 系统 DNS + `netsh portproxy` + Windows 防火墙 | Windows 服务 (SCM) | Windows Toast | 所有平台还会拦截发往 18+ 个已知公共 DNS 解析器的直接 DoH/DoT 连接,以防止 DNS 流量通过加密 DNS 绕过 Castiel。 ## 检测 Pipeline ### 第一层 —— 核心防御 | # | 检测器 | 攻击 | 描述 | |---|----------|--------|-------------| | 1 | **Rate Limiting** | DDoS / Amplification | 每个 IP 的 token bucket + 全局 QPS 上限 | | 2 | **Zone Transfer 拦截** | 数据泄露 | 拦截 AXFR/IXFR 请求 | | 3 | **Blocklist 检查** | 恶意软件 / 钓鱼 | 威胁情报源 (URLhaus, OpenPhish, PhishTank) | | 4 | **Tunneling 检测** | DNS tunneling | 子域名标签的香农熵 | | 5 | **DGA 检测** | DGA 域名 | 熵 + 辅音比率 + n-gram 模型 | | 6 | **C2/Fast-Flux** | C2 beaconing | TTL 波动性 + IP 多样性跟踪 | | 7 | **NXDOMAIN 跟踪** | Water torture | 针对每个域名的 NXDOMAIN rate limiting,具有拦截模式 | | 8 | **EDNS0 检查** | SiphonDNS | ECS 欺骗、cookie 验证、未知选项检测 | | 9 | **Cache 查找** | — | 感知 TTL 的 LRU cache | | 10 | **Upstream 转发** | — | 明文 DNS 或 DoH (加密) | | 11 | **响应验证** | TUDOOR | Bailiwick 检查、ID/问题匹配、RCODE 验证 | | 12 | **分片防御** | IP 分片 | UDP payload 大小限制为 1232 字节 | | 13 | **DNSSEC 验证** | Cache poisoning | 拒绝虚假响应、降级检测 | | 14 | **Rebinding 防护** | DNS rebinding | 拦截解析为 RFC1918 IP 的公共 FQDN | | 15 | **DoH 绕过检测** | DoH 绕过 | 检测发往已知解析器的直接 DoH/DoT 连接 | ### 第二层 —— 高级防御 | # | 检测器 | 攻击 | 描述 | |---|----------|--------|-------------| | 16 | **增强型 Fast-Flux** | Fast-flux C2 | IP 轮换率、ASN 多样性、双重 flux NS 轮换 | | 17 | **字典 DGA** | Matsnu/Suppobox | 带有 150+ 词字典的动态规划单词拆分 | | 18 | **稀疏 DGA** | Ramdo/Ramnit/Virut | 每个客户端 IP 的 24 小时滚动 NXDOMAIN 比率 | | 19 | **CNAME 链验证** | CNAME 滥用 | 循环检测、悬空 CNAME、超限深度、跨 bailiwick | | 20 | **DNS 计算** | APT12 | IP 编码命令检测(连续八位组、编码数据) | | 21 | **低慢速数据外泄** | FrameworkPOS | 24h+ beaconing 分析、子域名多样性、数据量模式 | | 22 | **仿冒域名** | Typosquatting | Levenshtein 距离、同形字替换、连字符插入、TLD 交换 | ### 拦截覆盖范围 | 攻击类型 | 状态 | |---|---| | DNS 放大 DDoS | **已拦截** | | Water Torture / NXDOMAIN 泛洪 | **已拦截** | | DNS Cache Poisoning | **已拦截** (DNSSEC) | | DNS Tunneling / 数据外泄 | **已拦截** (熵值 + 低速慢速) | | DGA 域名(所有类型) | **已拦截** (启发式 + n-gram + 字典 + 稀疏) | | DNS Rebinding | **已拦截** (RFC1918 检查) | | Zone Transfer (AXFR) | **已拦截** (ACL) | | C2 Beaconing / Fast-Flux | **已拦截** (TTL + IP 轮换 + ASN) | | 中间人 (MITM) DNS | **已拦截** (DoH/DoT) | | DoH 绕过 | **已拦截** (所有平台上的防火墙规则) | | 仿冒 / Typosquatting | **已拦截** (Levenshtein + 同形字) | | CNAME 循环 / 悬空 | **已拦截** (链条验证) | | DNS 计算 (APT12) | **已拦截** (IP 模式分析) | | EDNS0 利用 | **已拦截** (选项检查 + 剥离) | | IP 分片 | **已拦截** (payload 大小上限) | | DNSSEC 降级 | **已检测** (先前有效 → 失败告警) | ## 配置 有关所有选项,请参见 `config.yaml`。关键部分: ``` # 特定于平台 pf: # macOS PF firewall redirect nft: # Linux nftables/iptables redirect dns_redirect: # Windows DNS redirect method # 检测 (所有平台) rate_limit: # Token bucket per-IP and global QPS tunneling_detection: # Shannon entropy threshold dga_detection: # Entropy + consonant ratio + n-gram dictionary_dga: # Dictionary word-splitting DGA sparse_dga: # 24h NXDOMAIN ratio per client cname_validation: # CNAME chain depth, loops, dangling dns_calculation: # APT12 IP encoding detection low_slow_exfil: # Beaconing + subdomain diversity lookalike_detection: # Levenshtein + homoglyph + TLD swap edns_inspection: # EDNS0 option inspection nxdomain_tracking: # Per-domain water torture defense dnssec_downgrade: # DNSSEC downgrade detection doh_bypass: # DoH bypass blocking response_validation: # Response packet validation # Infrastructure blocklists: # Threat intel feed URLs alerts: # JSONL logging, notifications, webhooks metrics: # Prometheus endpoint cache: # TTL-aware LRU cache ``` ## 指标 Castiel 在配置的 endpoint 暴露 Prometheus 指标: ``` castiel_total_queries_total castiel_blocked_queries_total{reason} castiel_cache_hits_total / cache_misses_total castiel_rate_limited_queries_total castiel_dga_alerts_total castiel_fastflux_alerts_total{reason} castiel_dictionary_dga_alerts_total{domain} castiel_sparse_dga_alerts_total{client_ip} castiel_cname_chain_alerts_total{type} castiel_dns_calculation_alerts_total{reason} castiel_low_slow_exfil_alerts_total{reason} castiel_lookalike_alerts_total{reason} castiel_edns_suspicious_total{type} castiel_nxdomain_water_torture_total{domain} castiel_dnssec_downgrade_alerts_total{domain} castiel_doh_bypass_alerts_total{resolver} castiel_response_validation_failures_total{field} ``` `deploy/grafana/` 中包含了一个预配置的 Grafana 仪表板。 ## 攻击模拟 内置的 `attack-sim` 工具可测试所有检测机制: ``` go run ./cmd/attack-sim/ --help ``` 模拟:DGA 域名、DNS tunneling、fast-flux、water torture、rebinding、仿冒域名、CNAME 循环、DNS 计算等。 ## DoH 紧急开关 各个平台上的紧急 DoH 控制: - **macOS/Linux**: `doh-killswitch.sh {toggle|off|on|status|stop|restore}` - **Windows**: `.\doh-killswitch.ps1 {toggle|off|on|status|stop|restore}` ## 项目结构 ``` Castiel/ ├── main.go # Entry point, daemon setup ├── main_darwin.go # macOS: PF firewall init ├── main_linux.go # Linux: nftables firewall init ├── main_windows.go # Windows: DNS redirect firewall init ├── config.yaml # Default configuration ├── go.mod # Go module (github.com/castiel/dns) ├── data/ │ ├── legitimate-domains.txt # N-gram model training corpus │ ├── root-trust-anchor.txt # DNSSEC trust anchor │ ├── custom_block.txt # Custom blocklist │ └── custom_allow.txt # Custom allowlist ├── internal/ │ ├── config/config.go # Config structs + validation │ ├── firewall/firewall.go # Cross-platform firewall interface │ ├── dnsproxy/proxy.go # DNS proxy server + detection pipeline │ ├── detectors/ │ │ ├── entropy.go # Shannon entropy tunneling detection │ │ ├── dga.go # DGA (heuristics + n-gram) │ │ ├── ngram.go # N-gram statistical model │ │ ├── ratelimit.go # Token bucket rate limiter │ │ ├── rebinding_c2.go # Rebinding + C2/fast-flux │ │ ├── edns.go # EDNS0 option inspection │ │ ├── nxndomain.go # Per-domain NXDOMAIN tracking │ │ ├── dnssec_downgrade.go # DNSSEC downgrade detection │ │ ├── response_validator.go # Response packet validation │ │ ├── doh_bypass.go # DoH bypass detection │ │ ├── fastflux.go # Enhanced fast-flux (rotation, ASN) │ │ ├── dictionary_dga.go # Dictionary DGA (word splitting) │ │ ├── sparse_dga.go # Sparse DGA (NXDOMAIN ratio) │ │ ├── cname_chain.go # CNAME chain validation │ │ ├── dns_calculation.go # DNS calculation (APT12) │ │ ├── lowslow_exfil.go # Low-and-slow exfiltration │ │ └── lookalike.go # Lookalike/typosquatting │ ├── blocklists/manager.go # Threat intel feed manager │ ├── cache/cache.go # TTL-aware LRU DNS cache │ ├── alerts/ # Alert routing + notifications │ ├── metrics/metrics.go # Prometheus metrics │ ├── pf/pf.go # macOS PF (//go:build darwin) │ ├── nft/nft.go # Linux nftables/iptables (//go:build linux) │ └── windivert/windivert.go # Windows DNS redirect (//go:build windows) ├── deploy/ # macOS deployment + Grafana ├── linux_build/ # Linux deployment (systemd) ├── windows_build/ # Windows deployment ├── macos-app/ # macOS SwiftUI menu bar app ├── cmd/attack-sim/ # DNS attack simulation tool ├── .github/workflows/ci.yml # CI/CD pipeline (macOS) ├── .goreleaser.yml # macOS release builds (Homebrew + GitHub Releases) └── Makefile # Build targets for all platforms ``` ## 构建 ``` # Native build (当前平台) make build # Cross-compile make build-linux # Linux amd64 + arm64 → linux_build/ make build-windows # Windows amd64 + arm64 → windows_build/ make build-cross # All platforms # Release builds (已 strip,已优化) make release # macOS daemon + SwiftUI app # 运行测试 go test ./... ``` ## 许可证 Apache License 2.0 —— 详情请参阅 [LICENSE](LICENSE)。 ## 致谢 - [miekg/dns](https://github.com/miekg/dns) —— Go DNS 库 - [Prometheus](https://prometheus.io/) —— 指标和监控 - 研究参考:SiphonDNS、TUDOOR、APT12 DNS Calculation、FrameworkPOS、Matsnu/Suppobox DGA、Ramdo/Ramnit DGA
标签:DNS安全, EVTX分析, Go, IP 地址批量处理, Ruby工具, 入侵防御, 日志审计, 系统代理, 网络安全, 自定义请求头, 隐私保护