├── CONTRIBUTTING.md ├── LICENSE ├── README.md ├── docker ├── Nginx │ ├── Dockerfile │ └── nginx.conf ├── api.md ├── architecture.md ├── deployment.md ├── docker-compose.yml ├── snort │ ├── Dockerfile │ └── snort.conf ├── spark │ ├── Dockerfile │ └── spark.conf └── usage.md ├── docs ├── architecture.md ├── deployment.md └── usage.md ├── examples ├── snort_rules │ └── local.rules └── spark_config │ └── spark-defaults.conf ├── screenshots └── real-time-monitoring.png ├── scripts ├── deploy.sh └── setup.sh ├── src ├── spark_app │ ├── main.py │ └── requirements.txt └── web_server │ ├── app.py │ ├── requirements.txt │ └── templates │ └── index.html └── tests ├── snort_test.py └── spark_tests.py /CONTRIBUTTING.md: -------------------------------------------------------------------------------- 1 | # 贡献指南 2 | 3 | 感谢您有兴趣为 DistributedNIDS-Framework 贡献代码!请按照以下步骤进行: 4 | 5 | ## 提交问题 6 | 7 | 如果您发现了 bug 或有新功能的建议,请在 [Issues](https://github.com/yourusername/DistributedSnort-Framework/issues) 中提交。 8 | 9 | ## 贡献代码 10 | 11 | 1. Fork 本仓库 12 | 2. 创建您的分支 (`git checkout -b feature/新功能`) 13 | 3. 提交您的更改 (`git commit -m '添加新功能'`) 14 | 4. 推送到分支 (`git push origin feature/新功能`) 15 | 5. 创建一个 Pull Request 16 | 17 | ## 代码风格 18 | 19 | 请遵循 PEP 8 代码风格指南。 20 | 21 | ## 测试 22 | 23 | 在提交 Pull Request 之前,请确保所有测试通过。 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | © 2024 Your Name 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy... -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ![DistributedSnort-Framework](https://img.shields.io/badge/DistributedSnort-Framework-blue) ![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg) ![GitHub stars](https://img.shields.io/github/stars/yourusername/DistributedSnort-Framework) ![GitHub forks](https://img.shields.io/github/forks/yourusername/DistributedSnort-Framework) 2 | 3 | ## 🚀 项目简介 4 | 5 | **DistributedNIDS-Framework** 是一个开源的分布式入侵检测系统 (IDS),结合了强大的 [Snort](https://snort.org/) 入侵检测引擎和 [Apache Spark](https://spark.apache.org/) 的分布式数据处理能力。通过容器化部署,利用 [Docker](https://www.docker.com/) 和 [Nginx](https://www.nginx.com/) 实现高效的负载均衡和可扩展性,提供实时监控与报警功能,并配备直观的 Web 管理界面,帮助用户轻松管理和监控网络安全。 6 | 7 | ## 🔍 功能特性 8 | 9 | - **分布式流量分析与入侵检测**:利用 Apache Spark 的分布式计算能力,实时处理和分析来自多个 Snort 节点的网络流量数据。 10 | - **容器化部署**:通过 Docker 容器化各个组件,简化部署流程,确保环境一致性。 11 | - **高效负载均衡**:使用 Nginx 作为反向代理,智能分配流量,保证 Web 服务的高可用性。 12 | - **实时监控与报警**:实时检测网络威胁,并通过多种方式(如邮件、短信)发送报警通知。 13 | - **可视化管理界面**:直观的 Web 界面,方便用户查看检测结果、管理规则和配置报警设置。 14 | - **可扩展性强**:模块化设计,支持根据需求轻松扩展处理能力和功能。 15 | 16 | ## 🛠 技术栈 17 | 18 | - **Snort**:入侵检测系统 19 | - **Apache Spark**:分布式数据处理 20 | - **Docker**:容器化部署 21 | - **Nginx**:负载均衡 22 | - **Flask**:后端 Web 服务 23 | - **React**:前端管理界面 24 | 25 | ## 📷 截图 26 | 27 | ### 实时监控界面 28 | ![实时监控](screenshots/real-time-monitoring.png)) 29 | 30 | ## 📦 安装与部署 31 | 32 | ### 环境要求 33 | 34 | - **操作系统**:Linux / macOS / Windows (Docker Desktop) 35 | - **Docker**:>= 20.10 36 | - **Docker Compose**:>= 1.29 37 | - **硬件**:至少 8 核 CPU,16GB 内存 38 | 39 | ### 安装步骤 40 | 41 | 1. **克隆仓库** 42 | 43 | ```bash 44 | git clone https://github.com/yourusername/DistributedSnort-Framework.git 45 | cd DistributedSnort-Framework 46 | ``` 47 | 48 | 2. **构建 Docker 镜像** 49 | 50 | ```bash 51 | docker-compose build 52 | ``` 53 | 54 | 3. **启动所有服务** 55 | 56 | ```bash 57 | docker-compose up -d 58 | ``` 59 | 60 | 4. **检查服务状态** 61 | 62 | ```bash 63 | docker-compose ps 64 | ``` 65 | 66 | 5. **访问 Web 界面** 67 | 68 | 打开浏览器,访问 [http://localhost:8080](http://localhost:8080) 69 | 70 | ### 配置说明 71 | 72 | - **Snort 配置**:编辑 `docker/snort/snort.conf` 以配置 Snort 规则和参数。 73 | - **Spark 配置**:编辑 `docker/spark/spark.conf` 以调整 Spark 集群设置。 74 | - **Nginx 配置**:编辑 `docker/nginx/nginx.conf` 以设置负载均衡策略。 75 | - **Web 服务器配置**:编辑 `src/web_server/app.py` 以配置后端服务参数。 76 | 77 | ### 常见问题 78 | 79 | #### 无法启动 Snort 容器 80 | 81 | - 检查 `snort.conf` 配置文件是否正确,确保规则文件路径正确。 82 | - 查看 Snort 容器日志: 83 | 84 | ```bash 85 | docker logs snort 86 | ``` 87 | 88 | #### Spark 集群未响应 89 | 90 | - 检查 Spark 容器日志,确保所有节点正常启动并互相通信。 91 | 92 | ```bash 93 | docker logs spark-master 94 | docker logs spark-worker 95 | ``` 96 | 97 | ## 📝 使用指南 98 | 99 | ### 访问 Web 界面 100 | 101 | 在浏览器中打开 [http://localhost:8080](http://localhost:8080),即可访问管理界面。 102 | 103 | ### 登录 104 | 105 | 默认用户名和密码: 106 | 107 | - **用户名**: `admin` 108 | - **密码**: `password` 109 | 110 | **请登录后立即更改密码以确保安全。** 111 | 112 | ### 实时监控 113 | 114 | 在“实时监控”页面,您可以查看当前检测到的入侵尝试和网络流量统计。 115 | 116 | ### 管理规则 117 | 118 | 在“规则管理”页面,您可以添加、删除或修改 Snort 规则。 119 | 120 | ### 报警设置 121 | 122 | 在“报警设置”页面,配置报警通知方式,如邮件或短信。 123 | 124 | ### 查看日志 125 | 126 | 在“日志”页面,查看系统运行日志和历史检测记录。 127 | 128 | ## 📚 文档 129 | 130 | - [架构设计](docs/architecture.md) 131 | - [部署指南](docs/deployment.md) 132 | - [使用指南](docs/usage.md) 133 | - [API 文档](docs/api.md) 134 | 135 | ## 🧩 贡献 136 | 137 | 我们欢迎社区的贡献!请按照以下步骤进行: 138 | 139 | 1. Fork 本仓库 140 | 2. 创建您的分支 (`git checkout -b feature/新功能`) 141 | 3. 提交您的更改 (`git commit -m '添加新功能'`) 142 | 4. 推送到分支 (`git push origin feature/新功能`) 143 | 5. 创建一个 Pull Request 144 | 145 | 请参阅 [贡献指南](CONTRIBUTING.md) 了解更多信息。 146 | 147 | ## 🧪 测试 148 | 149 | 在提交 Pull Request 之前,请确保所有测试通过。查看 [测试文档](tests/) 了解更多。 150 | 151 | ## 📝 许可证 152 | 153 | 本项目采用 [MIT 许可证](LICENSE) 154 | 155 | --- 156 | 157 | -------------------------------------------------------------------------------- /docker/Nginx/Dockerfile: -------------------------------------------------------------------------------- 1 | # 使用官方 Nginx 镜像 2 | FROM nginx:latest 3 | 4 | # 复制 Nginx 配置文件 5 | COPY nginx.conf /etc/nginx/nginx.conf 6 | 7 | # 设置工作目录 8 | WORKDIR /etc/nginx 9 | 10 | # 启动 Nginx 11 | CMD ["nginx", "-g", "daemon off;"] 12 | -------------------------------------------------------------------------------- /docker/Nginx/nginx.conf: -------------------------------------------------------------------------------- 1 | events {} 2 | 3 | http { 4 | upstream web_servers { 5 | server web_server1:8080; 6 | server web_server2:8080; 7 | } 8 | 9 | server { 10 | listen 80; 11 | 12 | location / { 13 | proxy_pass http://web_servers; 14 | proxy_set_header Host $host; 15 | proxy_set_header X-Real-IP $remote_addr; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /docker/api.md: -------------------------------------------------------------------------------- 1 | # API 文档 2 | 3 | ## 获取检测结果 4 | 5 | **Endpoint:** `/api/results` 6 | 7 | **Method:** GET 8 | 9 | **Response:** 10 | 11 | ```json 12 | { 13 | "status": "success", 14 | "data": [ 15 | { 16 | "id": 1, 17 | "timestamp": "2024-12-06T12:00:00Z", 18 | "source_ip": "192.168.1.10", 19 | "destination_ip": "10.0.0.5", 20 | "alert": "Possible SQL Injection" 21 | }, 22 | ... 23 | ] 24 | } 25 | ``` 26 | 27 | ## 添加新规则 28 | 29 | **Endpoint:** `/api/rules` 30 | 31 | **Method:** POST 32 | 33 | **Request Body:** 34 | 35 | ```json 36 | { 37 | "rule": "alert tcp any any -> any 80 (msg:\"SQL Injection Attempt\"; content:\"SELECT\"; nocase; sid:1000001;)" 38 | } 39 | ``` 40 | 41 | **Response:** 42 | 43 | ```json 44 | { 45 | "status": "success", 46 | "message": "Rule added successfully." 47 | } 48 | ``` 49 | 50 | ## 删除规则 51 | 52 | **Endpoint:** `/api/rules/{id}` 53 | 54 | **Method:** DELETE 55 | 56 | **Response:** 57 | 58 | ```json 59 | { 60 | "status": "success", 61 | "message": "Rule deleted successfully." 62 | } 63 | ``` 64 | 65 | -------------------------------------------------------------------------------- /docker/architecture.md: -------------------------------------------------------------------------------- 1 | # 架构设计 2 | 3 | DistributedSnort-Framework 采用分布式架构,主要组件包括: 4 | 5 | - **Snort 节点**: 部署在容器中,负责实时流量分析与入侵检测。 6 | - **Apache Spark**: 作为分布式处理引擎,负责处理来自 Snort 节点的数据。 7 | - **Nginx**: 作为反向代理和负载均衡器,将请求分发到后端 Web 服务器。 8 | - **Web 服务器**: 提供可视化管理界面,实时展示检测结果和报警信息。 9 | 10 | ## 数据流 11 | 12 | 1. 网络流量被 Snort 节点捕获并进行初步分析。 13 | 2. 分析结果通过 Spark 进行聚合和深度处理。 14 | 3. 处理后的数据通过 Nginx 分发到 Web 服务器。 15 | 4. 用户通过 Web 界面查看和管理入侵检测结果。 16 | 17 | ## 组件关系图 18 | 19 | [网络流量] → [Snort 节点] → [Spark 集群] → [Nginx] → [Web 服务器] -------------------------------------------------------------------------------- /docker/deployment.md: -------------------------------------------------------------------------------- 1 | # 部署指南 2 | 3 | ## 环境要求 4 | 5 | - Docker >= 20.10 6 | - Docker Compose >= 1.29 7 | - 服务器至少 8 核 CPU,16GB 内存 8 | 9 | ## 安装步骤 10 | 11 | 1. 克隆仓库 12 | 13 | ```bash 14 | git clone https://github.com/yourusername/DistributedSnort-Framework.git 15 | cd DistributedSnort-Framework 16 | ``` 17 | 18 | 2. 构建Docker镜像 19 | 20 | ```bash 21 | docker-compose build 22 | ``` 23 | 24 | 3. 启动所有服务 25 | 26 | ```bash 27 | docker-compose up -d 28 | ``` 29 | 30 | 4. 检查服务状态 31 | 32 | ```bash 33 | docker-compose ps 34 | ``` 35 | 36 | ## 安装步骤 37 | 38 | **Snort 配置**: 编辑 `docker/snort/snort.conf` 以配置 Snort 规则和参数。 39 | 40 | **Spark 配置**: 编辑 `docker/spark/spark.conf` 以调整 Spark 集群设置。 41 | 42 | **Nginx 配置**: 编辑 `docker/nginx/nginx.conf` 以设置负载均衡策略。 43 | 44 | **Web 服务器配置**: 编辑 `src/web_server/app.py` 以配置后端服务参数。 45 | 46 | ## 常见问题 47 | 48 | 无法启动 Snort 容器 49 | 50 | - 请检查 `snort.conf` 配置文件是否正确,确保规则文件路径正确。 51 | 52 | Spark 集群未响应 53 | 54 | - 检查 Spark 容器日志,确保所有节点正常启动并互相通信。 -------------------------------------------------------------------------------- /docker/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.8' 2 | 3 | services: 4 | snort: 5 | build: ./docker/snort 6 | container_name: snort 7 | networks: 8 | - snort_net 9 | volumes: 10 | - ./docker/snort/snort.conf:/etc/snort/snort.conf 11 | - ./examples/snort_rules:/etc/snort/rules 12 | ports: 13 | - "5555:5555" 14 | 15 | spark-master: 16 | build: ./docker/spark 17 | container_name: spark-master 18 | networks: 19 | - snort_net 20 | ports: 21 | - "7077:7077" 22 | - "8080:8080" 23 | 24 | spark-worker: 25 | build: ./docker/spark 26 | container_name: spark-worker 27 | networks: 28 | - snort_net 29 | depends_on: 30 | - spark-master 31 | environment: 32 | - SPARK_MASTER_URL=spark://spark-master:7077 33 | ports: 34 | - "8081:8081" 35 | 36 | nginx: 37 | build: ./docker/nginx 38 | container_name: nginx 39 | networks: 40 | - snort_net 41 | ports: 42 | - "80:80" 43 | 44 | web_server1: 45 | build: ./src/web_server 46 | container_name: web_server1 47 | networks: 48 | - snort_net 49 | ports: 50 | - "8080:8080" 51 | 52 | web_server2: 53 | build: ./src/web_server 54 | container_name: web_server2 55 | networks: 56 | - snort_net 57 | ports: 58 | - "8081:8080" 59 | 60 | networks: 61 | snort_net: 62 | driver: bridge 63 | -------------------------------------------------------------------------------- /docker/snort/Dockerfile: -------------------------------------------------------------------------------- 1 | # 使用官方 Snort 镜像作为基础 2 | FROM ubuntu:20.04 3 | 4 | # 安装必要的软件包 5 | RUN apt-get update && \ 6 | apt-get install -y snort && \ 7 | apt-get clean 8 | 9 | # 复制 Snort 配置文件 10 | COPY snort.conf /etc/snort/snort.conf 11 | 12 | # 设置工作目录 13 | WORKDIR /etc/snort 14 | 15 | # 启动 Snort 16 | CMD ["snort", "-c", "/etc/snort/snort.conf", "-i", "eth0"] -------------------------------------------------------------------------------- /docker/snort/snort.conf: -------------------------------------------------------------------------------- 1 | # Snort configuration file 2 | 3 | var HOME_NET any 4 | var EXTERNAL_NET any 5 | 6 | # Include rules 7 | include /etc/snort/rules/local.rules 8 | 9 | # Output options 10 | output alert_fast: stdout -------------------------------------------------------------------------------- /docker/spark/Dockerfile: -------------------------------------------------------------------------------- 1 | # 使用官方 Spark 镜像 2 | FROM bitnami/spark:latest 3 | 4 | # 复制 Spark 配置文件 5 | COPY spark.conf /opt/bitnami/spark/conf/ 6 | 7 | # 设置工作目录 8 | WORKDIR /opt/bitnami/spark 9 | 10 | # 启动 Spark 11 | CMD ["spark-class", "org.apache.spark.deploy.master.Master"] 12 | -------------------------------------------------------------------------------- /docker/spark/spark.conf: -------------------------------------------------------------------------------- 1 | # Spark configuration 2 | 3 | spark.master spark://spark-master:7077 4 | spark.executor.instances 2 5 | spark.executor.memory 2g 6 | spark.driver.memory 1g 7 | -------------------------------------------------------------------------------- /docker/usage.md: -------------------------------------------------------------------------------- 1 | # 使用指南 2 | 3 | ## 访问 Web 界面 4 | 5 | 在浏览器中打开 [http://localhost:8080](http://localhost:8080),即可访问管理界面。 6 | 7 | ## 登录 8 | 9 | 默认用户名和密码: 10 | 11 | - 用户名: admin 12 | - 密码: password 13 | 14 | 请登录后立即更改密码以确保安全。 15 | 16 | ## 实时监控 17 | 18 | 在“实时监控”页面,您可以查看当前检测到的入侵尝试和网络流量统计。 19 | 20 | ## 管理规则 21 | 22 | 在“规则管理”页面,您可以添加、删除或修改 Snort 规则。 23 | 24 | ## 报警设置 25 | 26 | 在“报警设置”页面,配置报警通知方式,如邮件或短信。 27 | 28 | ## 查看日志 29 | 30 | 在“日志”页面,查看系统运行日志和历史检测记录。 -------------------------------------------------------------------------------- /docs/architecture.md: -------------------------------------------------------------------------------- 1 | # 架构设计 2 | 3 | DistributedSnort-Framework 采用分布式架构,主要组件包括: 4 | 5 | - **Snort 节点**: 部署在容器中,负责实时流量分析与入侵检测。 6 | - **Apache Spark**: 作为分布式处理引擎,负责处理来自 Snort 节点的数据。 7 | - **Nginx**: 作为反向代理和负载均衡器,将请求分发到后端 Web 服务器。 8 | - **Web 服务器**: 提供可视化管理界面,实时展示检测结果和报警信息。 9 | 10 | ## 数据流 11 | 12 | 1. 网络流量被 Snort 节点捕获并进行初步分析。 13 | 2. 分析结果通过 Spark 进行聚合和深度处理。 14 | 3. 处理后的数据通过 Nginx 分发到 Web 服务器。 15 | 4. 用户通过 Web 界面查看和管理入侵检测结果。 16 | 17 | ## 组件关系图 18 | 19 | [网络流量] → [Snort 节点] → [Spark 集群] → [Nginx] → [Web 服务器] -------------------------------------------------------------------------------- /docs/deployment.md: -------------------------------------------------------------------------------- 1 | # 部署指南 2 | 3 | ## 环境要求 4 | 5 | - Docker >= 20.10 6 | - Docker Compose >= 1.29 7 | - 服务器至少 8 核 CPU,16GB 内存 8 | 9 | ## 安装步骤 10 | 11 | 1. 克隆仓库 12 | 13 | ```bash 14 | git clone https://github.com/yourusername/DistributedSnort-Framework.git 15 | cd DistributedSnort-Framework 16 | ``` 17 | 18 | 2. 构建Docker镜像 19 | 20 | ```bash 21 | docker-compose build 22 | ``` 23 | 24 | 3. 启动所有服务 25 | 26 | ```bash 27 | docker-compose up -d 28 | ``` 29 | 30 | 4. 检查服务状态 31 | 32 | ```bash 33 | docker-compose ps 34 | ``` 35 | 36 | ## 安装步骤 37 | 38 | **Snort 配置**: 编辑 `docker/snort/snort.conf` 以配置 Snort 规则和参数。 39 | 40 | **Spark 配置**: 编辑 `docker/spark/spark.conf` 以调整 Spark 集群设置。 41 | 42 | **Nginx 配置**: 编辑 `docker/nginx/nginx.conf` 以设置负载均衡策略。 43 | 44 | **Web 服务器配置**: 编辑 `src/web_server/app.py` 以配置后端服务参数。 45 | 46 | ## 常见问题 47 | 48 | 无法启动 Snort 容器 49 | 50 | - 请检查 `snort.conf` 配置文件是否正确,确保规则文件路径正确。 51 | 52 | Spark 集群未响应 53 | 54 | - 检查 Spark 容器日志,确保所有节点正常启动并互相通信。 -------------------------------------------------------------------------------- /docs/usage.md: -------------------------------------------------------------------------------- 1 | # 使用指南 2 | 3 | ## 访问 Web 界面 4 | 5 | 在浏览器中打开 [http://localhost:8080](http://localhost:8080),即可访问管理界面。 6 | 7 | ## 登录 8 | 9 | 默认用户名和密码: 10 | 11 | - 用户名: admin 12 | - 密码: password 13 | 14 | 请登录后立即更改密码以确保安全。 15 | 16 | ## 实时监控 17 | 18 | 在“实时监控”页面,您可以查看当前检测到的入侵尝试和网络流量统计。 19 | 20 | ## 管理规则 21 | 22 | 在“规则管理”页面,您可以添加、删除或修改 Snort 规则。 23 | 24 | ## 报警设置 25 | 26 | 在“报警设置”页面,配置报警通知方式,如邮件或短信。 27 | 28 | ## 查看日志 29 | 30 | 在“日志”页面,查看系统运行日志和历史检测记录。 -------------------------------------------------------------------------------- /examples/snort_rules/local.rules: -------------------------------------------------------------------------------- 1 | # 示例规则:检测 SQL 注入 2 | alert tcp any any -> any 80 (msg:"SQL Injection Attempt"; content:"SELECT"; nocase; sid:1000001;) 3 | -------------------------------------------------------------------------------- /examples/spark_config/spark-defaults.conf: -------------------------------------------------------------------------------- 1 | spark.master spark://spark-master:7077 2 | spark.executor.memory 2g 3 | spark.driver.memory 1g 4 | -------------------------------------------------------------------------------- /screenshots/real-time-monitoring.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuyang-sudo/DistributedNIDS-Framework/a0e7396aa346857dad47e6e093b689c3b3ec6a93/screenshots/real-time-monitoring.png -------------------------------------------------------------------------------- /scripts/deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # 构建 Docker 镜像 4 | docker-compose build 5 | 6 | # 启动所有服务 7 | docker-compose up -d 8 | 9 | echo "部署完成。访问 http://localhost:8080 查看。" 10 | -------------------------------------------------------------------------------- /scripts/setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # 更新包列表 4 | sudo apt-get update 5 | 6 | # 安装 Docker 7 | sudo apt-get install -y docker.io 8 | 9 | # 安装 Docker Compose 10 | sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose 11 | sudo chmod +x /usr/local/bin/docker-compose 12 | 13 | # 启动 Docker 服务 14 | sudo systemctl start docker 15 | sudo systemctl enable docker 16 | 17 | echo "安装完成。" 18 | -------------------------------------------------------------------------------- /src/spark_app/main.py: -------------------------------------------------------------------------------- 1 | from pyspark import SparkContext, SparkConf 2 | from pyspark.streaming import StreamingContext 3 | 4 | def process_stream(rdd): 5 | if not rdd.isEmpty(): 6 | alerts = rdd.collect() 7 | for alert in alerts: 8 | print(f"Alert: {alert}") 9 | 10 | if __name__ == "__main__": 11 | conf = SparkConf().setAppName("SnortAlertProcessor") 12 | sc = SparkContext(conf=conf) 13 | ssc = StreamingContext(sc, 5) # 5秒批处理 14 | 15 | lines = ssc.socketTextStream("snort", 5555) 16 | lines.foreachRDD(process_stream) 17 | 18 | ssc.start() 19 | ssc.awaitTermination() 20 | -------------------------------------------------------------------------------- /src/spark_app/requirements.txt: -------------------------------------------------------------------------------- 1 | pyspark==3.3.1 2 | -------------------------------------------------------------------------------- /src/web_server/app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, jsonify, render_template, request 2 | import os 3 | 4 | app = Flask(__name__) 5 | 6 | @app.route('/') 7 | def index(): 8 | return render_template('index.html') 9 | 10 | @app.route('/api/results', methods=['GET']) 11 | def get_results(): 12 | # 示例数据,实际应从数据库或 Spark 获取 13 | results = [ 14 | { 15 | "id": 1, 16 | "timestamp": "2024-12-06T12:00:00Z", 17 | "source_ip": "192.168.1.10", 18 | "destination_ip": "10.0.0.5", 19 | "alert": "Possible SQL Injection" 20 | }, 21 | # 更多结果 22 | ] 23 | return jsonify({"status": "success", "data": results}) 24 | 25 | @app.route('/api/rules', methods=['POST']) 26 | def add_rule(): 27 | rule = request.json.get('rule') 28 | # 将规则添加到 Snort 配置 29 | with open('/etc/snort/rules/local.rules', 'a') as f: 30 | f.write(f"{rule}\n") 31 | return jsonify({"status": "success", "message": "Rule added successfully."}), 201 32 | 33 | if __name__ == '__main__': 34 | app.run(host='0.0.0.0', port=8080) 35 | -------------------------------------------------------------------------------- /src/web_server/requirements.txt: -------------------------------------------------------------------------------- 1 | Flask==2.1.1 2 | -------------------------------------------------------------------------------- /src/web_server/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | DistributedSnort-Framework 5 | 6 | 7 | 8 | 9 |
10 |

DistributedSnort-Framework

11 |

实时入侵检测系统

12 |
13 | 14 |
15 |
16 | 17 | 18 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /tests/snort_test.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | import subprocess 3 | 4 | class SnortTest(unittest.TestCase): 5 | def test_snort_conf(self): 6 | # 检查 Snort 配置文件是否存在 7 | result = subprocess.run(['docker', 'exec', 'snort', 'test', '-f', '/etc/snort/snort.conf'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) 8 | self.assertEqual(result.returncode, 0, "Snort 配置文件有误或不存在。") 9 | 10 | def test_snort_running(self): 11 | # 检查 Snort 容器是否在运行 12 | result = subprocess.run(['docker', 'ps', '--filter', 'name=snort', '--format', '{{.Status}}'], stdout=subprocess.PIPE) 13 | status = result.stdout.decode().strip() 14 | self.assertIn("Up", status, "Snort 容器未运行。") 15 | 16 | if __name__ == '__main__': 17 | unittest.main() 18 | -------------------------------------------------------------------------------- /tests/spark_tests.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from pyspark import SparkContext, SparkConf 3 | from src.spark_app.main import process_stream 4 | 5 | class SparkAppTest(unittest.TestCase): 6 | def setUp(self): 7 | conf = SparkConf().setAppName("TestApp") 8 | self.sc = SparkContext(conf=conf) 9 | self.rdd = self.sc.parallelize(["alert1", "alert2"]) 10 | 11 | def test_process_stream(self): 12 | # 捕获打印输出 13 | import io 14 | import sys 15 | captured_output = io.StringIO() 16 | sys.stdout = captured_output 17 | 18 | process_stream(self.rdd) 19 | 20 | sys.stdout = sys.__stdout__ 21 | output = captured_output.getvalue() 22 | self.assertIn("Alert: alert1", output) 23 | self.assertIn("Alert: alert2", output) 24 | 25 | def tearDown(self): 26 | self.sc.stop() 27 | 28 | if __name__ == '__main__': 29 | unittest.main() 30 | --------------------------------------------------------------------------------