aayishaashraf/DNS_COVERT_CHANNEL_DETECTION_using_ML

GitHub: aayishaashraf/DNS_COVERT_CHANNEL_DETECTION_using_ML

该系统是一个 AI 驱动的 DNS 安全检测平台,通过多层分析架构实时识别和拦截恶意域名以防御基于 DNS 的各类网络攻击。

Stars: 0 | Forks: 0

# DNS Shield AI - 高级恶意域名检测系统 [![Python](https://img.shields.io/badge/Python-3.11-blue.svg)](https://www.python.org/) [![TensorFlow](https://img.shields.io/badge/TensorFlow-2.15.0-orange.svg)](https://www.tensorflow.org/) [![Docker](https://img.shields.io/badge/Docker-Ready-blue.svg)](https://www.docker.com/) **生产就绪、AI 驱动的 DNS 威胁检测系统,具备多层防御、实时监控和自动化事件响应功能。** 学生:Aayisha Ashraf | 西伦敦大学 ## 目录 - [概述](#overview) - [核心功能](#key-features) - [系统架构](#system-architecture) - [DNS 解析器集成](#dns-resolver-integration) - [组件](#components) - [安装](#installation) - [配置](#configuration) - [使用说明](#usage) - [监控](#monitoring) - [API 文档](#api-documentation) - [检测机制](#detection-mechanisms) - [性能](#performance) - [故障排除](#troubleshooting) ## 概述 **DNS Shield** 是一个基于机器学习的高级 DNS 威胁检测系统,可实时识别并拦截恶意域名。该系统采用复杂的**三层架构**,结合启发式分析、深度学习 (LSTM) 和外部威胁情报 (VirusTotal),实现了 **95.93% 的检测准确率**,同时保持了高吞吐量(**103,000+ 查询/秒**)。 ### 核心功能 - **实时检测 8 种基于 DNS 的攻击** - **自动对恶意域名进行 Sinkhole** 以防止访问 - **集成 VirusTotal** 进行 70 多家安全供应商验证 - **针对安全事件发送专业的邮件告警** - **通过 Prometheus 和 Grafana 提供全面监控** - **为每个检测决策生成 XAI 解释** ### 应用场景 - **企业网络保护** - 保护企业 DNS 基础设施 - **安全研究** - 分析 DNS 攻击模式和行为 - **教育目的** - 演示基于机器学习的网络安全解决方案 - **SOC 运营** - 自动化威胁检测和事件响应 ## 核心功能 ### **高级 AI 检测** - **三层架构**:快速启发式过滤 → 深度 LSTM 分析 → VirusTotal 验证 - **96.93% 准确率**:基于真实世界威胁数据集验证 - **8 种攻击类型**:DGA、Typosquatting、DNS Tunneling、Fast Flux、C2、IBHH、Homograph、Structured DGA - **Zero-Day 检测**:通过集成 VirusTotal 捕获未知威胁 ### **生产就绪** - **Docker 容器化**:易于部署和水平扩展 - **高性能**:143,000+ 查询/秒吞吐量 - **自动 Sinkhole**:立即缓解威胁 - **取证审计日志**:完整的事件历史记录 - **API 认证**:使用 API 密钥进行安全的访问控制 ### **全面监控** - **Prometheus 指标**:实时性能和威胁跟踪 - **Grafana 仪表盘**:可视化监控和告警 - **邮件通知**:支持 HTML/文本格式的专业事件告警 - **XAI 解释**:可解释的检测决策 ### **外部集成** - **VirusTotal API**:多供应商威胁情报(70+ 安全引擎) - **SMTP 邮件告警**:支持 Gmail、Outlook、Yahoo - **RESTful API**:易于与 SIEM 和其他安全工具集成 ## 系统架构 ``` ┌─────────────────────────────────────────────────────────────────┐ │ DNS SHIELD COMPLETE SYSTEM │ └─────────────────────────────────────────────────────────────────┘ ┌──────────────────┐ │ User/Client │ │ DNS Query │ └────────┬─────────┘ │ ┌──────────────▼──────────────┐ │ DNS Resolver (Optional) │ │ • Intercepts queries │ │ • Forwards to DNS Shield │ │ • Returns result to client │ └──────────────┬──────────────┘ │ ┌──────────────▼──────────────┐ │ Flask API Server :5000 │ │ • Authentication check │ │ • Request routing │ │ • Response formatting │ └──────────────┬──────────────┘ │ ┌────────────────────────┼────────────────────────┐ │ │ │ ▼ ▼ ▼ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ Whitelist │ │ Layer 1: │ │ VirusTotal │ │ Check │ │ Heuristics │ │ Intelligence │ │ • Trusted │ │ • Entropy │ │ • 70+ vendors│ │ domains │ │ • DGA │ │ • Real-time │ └──────┬───────┘ │ • FastFlux │ │ updates │ │ │ • Tunneling │ └──────┬───────┘ │ │ • Typosquat │ │ │ └──────┬───────┘ │ │ │ │ │ ▼ │ │ ┌──────────────┐ │ │ │ Layer 2: │ │ │ │ LSTM Model │ │ │ │ • Deep │ │ │ │ Learning │ │ │ │ • 15 Feat. │ │ │ │ • Neural │ │ │ └──────┬───────┘ │ │ │ │ └───────────────────────┼───────────────────────┘ │ ┌──────▼──────┐ │ Decision │ │ Engine │ └──────┬──────┘ │ ┌──────────────────────┼──────────────────────┐ │ │ │ ▼ ▼ ▼ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ Sinkhole │ │ Forensic │ │ Email │ │ Database │ │ Audit Log │ │ Alerts │ │ • Block │ │ • History │ │ • SMTP │ │ list │ │ • Incidents │ │ • HTML/Text │ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ │ │ │ └─────────────────────┼─────────────────────┘ │ ┌──────▼──────┐ │ Prometheus │ │ Metrics │ │ :9090 │ └──────┬──────┘ │ ┌──────▼──────┐ │ Grafana │ │ Dashboards │ │ :3000 │ └─────────────┘ ``` ## DNS 解析器集成 ### 概述 DNS Shield 可与 DNS 解析器(如 BIND、Unbound、PowerDNS)集成,为整个网络提供**实时保护**。解析器拦截 DNS 查询,并将其转发给 DNS Shield 进行分析,然后再将结果返回给客户端。 ### 集成选项 #### **选项 1:独立模式(当前)** ``` User → DNS Shield API → Analysis → Response ``` - 用于测试和开发的直接 API 查询 - 用于手动分析的 Web 界面 - Python/脚本集成 #### **选项 2:DNS 解析器集成(生产环境)** ``` User → DNS Resolver → DNS Shield API → Decision → Resolver → User ``` - 透明保护(用户无感知) - 自动拦截恶意域名 - 全网覆盖 ### DNS 解析器设置示例 #### **使用 BIND9:** ``` # /etc/bind/named.conf.options options { // Forward queries to DNS Shield for analysis forwarders { 127.0.0.1 port 5000; # DNS Shield }; // Response Policy Zone (RPZ) for sinkholing response-policy { zone "rpz.local"; }; }; # 为 sinkholed 域配置 RPZ zone zone "rpz.local" { type master; file "/etc/bind/rpz.local.zone"; allow-query { none; }; }; ``` #### **使用 Unbound:** ``` # /etc/unbound/unbound.conf server: # Module for Python integration module-config: "python validator iterator" python: # DNS Shield integration script python-script: "/etc/unbound/dns_shield_check.py" ``` **dns_shield_check.py:** ``` import requests def check_domain(domain): response = requests.post( 'http://localhost:5000/predict', json={'domain': domain}, headers={'X-API-Key': 'dns-shield-admin-2024'} ) result = response.json() return result['malicious'] def init(id, cfg): return True def deinit(id): return True def inform_super(id, qstate, superqstate, qdata): return True def operate(id, event, qstate, qdata): if event == MODULE_EVENT_NEW: domain = qstate.qinfo.qname_str if check_domain(domain): # Sinkhole malicious domain qstate.return_msg.rep.flags |= 0x80 # Set AA flag return True return True ``` ### 带有解析器的数据流 1. **用户 DNS 查询** → `example.com` 2. **DNS 解析器** → 接收查询 3. **转发至 DNS Shield** → POST /predict 4. **DNS Shield 分析**: - 白名单检查 - 第一层(启发式) - 第二层(需要时的 LSTM) - VirusTotal 验证 5. **决策**: - **恶意** → Sinkhole 至 0.0.0.0,记录事件,发送告警 - **良性** → 转发至上游 DNS,返回 IP 6. **响应给用户** → IP 地址或 Sinkhole ### 性能考量 | 指标 | 无解析器 | 带解析器 | |--------|------------------|---------------| | **延迟** | N/A | 每次查询 +5-10ms | | **吞吐量** | 103K QPS | 50-80K QPS(解析器开销) | | **覆盖范围** | 手动查询 | 所有网络 DNS 流量 | | **透明度** | 对用户可见 | 透明 | ### 部署建议 **小型办公室(< 100 名用户):** - 单个 DNS Shield 实例 - 同一台服务器上的 DNS 解析器 - 共享缓存以提高性能 **中型企业(< 1000 名用户):** - 2-3 个 DNS Shield 实例(负载均衡) - 独立的 DNS 解析器集群 - 对良性域名设置高 TTL **大型企业(> 1000 名用户):** - 5 个以上的 DNS Shield 实例(自动扩容) - 专用的 DNS 解析器基础设施 - 多区域部署 - 用于 VirusTotal 缓存的 CDN ### 配置示例 ``` # 用于 resolver 集成的 DNS Shield 配置 ENABLE_RESOLVER_MODE = True RESOLVER_CACHE_TTL = 3600 # Cache benign lookups for 1 hour SINKHOLE_IP = "0.0.0.0" # IP for malicious domains SINKHOLE_TTL = 300 # Short TTL for sinkhole (5 min) ``` ## 🔧 组件 ### 核心组件 #### 1. **Flask API 服务器** (new_app.py) - **用途**:用于域名分析的 RESTful API - **端口**:5000 - **端点**: - `POST /predict` - 域名分析 - `GET /stats` - 系统统计信息 - `GET /metrics` - Prometheus 指标 - `GET /security/audit-view` - 取证日志 - **特性**:API 认证、速率限制、CORS、XAI 解释 #### 2. **LSTM 神经网络** - **模型文件**:`dns_covert_detector_final.h5` - **框架**:TensorFlow/Keras 2.15.0 - **架构**: - 输入:15 个 DNS 特征 - LSTM 层:128 个单元 - Dense 层:64 → 32 → 1 个单元 - 激活函数:Sigmoid - **训练数据**:50,000+ 个域名(良性 + 恶意) - **准确率**:独立运行 99.86%,结合 VirusTotal 达 99.93% #### 3. **特征提取器** (src/dns_feature_extractor.py) - **提取 15 个特征**:长度、熵、字符分布、数字比例、子域名计数等 - **处理时间**:每个域名 <1ms - **特征**:使用保存的缩放器进行标准化 (dns_scaler_final.pkl) #### 4. **威胁检测器** (src/threat_detector.py) - **启发式规则**:DGA 模式、Typosquatting、Fast Flux、DNS Tunneling、IBHH - **速度**:<1ms 检测 - **覆盖率**:约 80% 的查询(快速路径) ### 监控组件 #### 5. **Prometheus**(端口 9090) - **抓取间隔**:15 秒 - **指标**:查询、检测、延迟、攻击类型、层级使用情况 - **存储**:持久化存储卷 (prometheus-data) #### 6. **Grafana**(端口 3000) - **凭据**:admin/admin - **仪表盘**:预配置 (grafana-dashboard.json) - **面板**:查询时间线、攻击分布、性能指标 ### 外部集成 #### 7. **VirusTotal API** - **供应商**:70+ 安全引擎(Norton、McAfee、Kaspersky 等) - **速率限制**:4 次请求/分钟(免费版) - **缓存**:1 小时 TTL - **用途**:验证、Zero-Day 检测、降低误报率 #### 8. **SMTP 邮件告警** - **协议**:带 STARTTLS 的 SMTP - **服务商**:Gmail(应用密码)、Outlook、Yahoo、自定义 SMTP - **格式**:纯文本 (ASCII) + HTML(样式化) - **内容**:事件详情、采取的操作、建议、链接 ### 数据存储 #### 9. **Sinkhole 数据库**(内存中) - **结构**:`{domain: {timestamp, threat_type, confidence}}` - **用途**:跟踪被拦截的域名,防止重复 #### 10. **取证审计日志**(内存中) - **字段**:时间戳、域名、预测结果、层级、置信度、事件 ID - **导出**:支持 JSON/CSV 格式以供分析 ## 安装 ### 前置条件 - Docker & Docker Compose (v20.10+) - Python 3.11(如果不使用 Docker 运行) - 至少 8GB 内存,建议 2 个 CPU 核心 - 网络连接(用于 VirusTotal) ### 快速开始(Docker) ``` # 1. Clone repository git clone cd DNS_Covert_Channel_Detection # 2. 验证模型是否存在 ls models/ # dns_covert_detector_final.h5, dns_scaler_final.pkl, final_features.txt # 3. Build 并启动 docker-compose up -d --build # 4. 验证 docker ps # Should show dns-shield, dns-shield-prometheus, dns-shield-grafana # 5. 测试 curl -X POST http://localhost:5000/predict \ -H "Content-Type: application/json" \ -d '{"domain": "google.com"}' ``` ### 访问入口 - **Web UI**:http://localhost:5000 - **Prometheus**:http://localhost:9090 - **Grafana**:http://localhost:3000 (admin/admin) ## 配置 ### DNS Shield 设置 (new_app.py) ``` # 邮件告警 ENABLE_EMAIL_ALERTS = False # Set True to enable SMTP_SERVER = "smtp.gmail.com" SMTP_USER = "your-email@gmail.com" SMTP_PASSWORD = "app-password" # Gmail App Password ALERT_RECIPIENT = "security@example.com" # VirusTotal ENABLE_VIRUSTOTAL = True VIRUSTOTAL_API_KEY = "your-api-key" VT_CACHE_TTL = 3600 # API 认证 REQUIRE_AUTH = False VALID_API_KEYS = {'dns-shield-admin-2024', 'test-key-12345'} # Thresholds LOW_ENTROPY_THRESHOLD = 2.8 HIGH_ENTROPY_THRESHOLD = 4.3 LSTM_THRESHOLD = 0.35 # Whitelist WHITELIST_DOMAINS = {'google.com', 'youtube.com', 'facebook.com'} ``` ## 联系方式 **Aayisha Ashraf** 院校:西伦敦大学 **最后更新**:2026 年 3 月 29 日 | **版本**:1.0.0 | **状态**:✅ 生产就绪
🛡️ DNS Shield - 使用 AI 保护网络 🛡️
标签:AMSI绕过, DNS安全, Docker, IP 地址批量处理, Python, 威胁检测, 安全防御评估, 无后门, 深度学习, 自定义请求头, 请求拦截, 逆向工具