alexrepsec/Scripting-and-Automation-for-Threat-Mitigation

GitHub: alexrepsec/Scripting-and-Automation-for-Threat-Mitigation

该项目演示了一个基于Python和Bash构建的自动化威胁缓解管道,通过实时分析Apache日志检测Web攻击并利用iptables自动封锁恶意IP。

Stars: 0 | Forks: 0

# 🛡️ 威胁缓解的脚本与自动化 ![Python](https://img.shields.io/badge/Python-3.10+-blue?style=for-the-badge&logo=python&logoColor=white) ![Bash](https://img.shields.io/badge/Bash-Script-green?style=for-the-badge&logo=gnubash&logoColor=white) ![Ubuntu](https://img.shields.io/badge/Ubuntu-22.04-orange?style=for-the-badge&logo=ubuntu&logoColor=white) ![Kali](https://img.shields.io/badge/Kali-Linux-557C94?style=for-the-badge&logo=kalilinux&logoColor=white) ![Apache](https://img.shields.io/badge/Apache-2.4-red?style=for-the-badge&logo=apache&logoColor=white) ## 📋 目录 - [概述](#overview) - [实验环境](#lab-environment) - [攻击场景](#attack-scenario) - [项目结构](#project-structure) - [工作原理](#how-it-works) - [脚本](#scripts) - [实验指南](#lab-walkthrough) - [结果](#results) - [展示技能](#skills-demonstrated) ## 概述 本项目演示了一个使用 Python 和 Bash 构建的真实世界的**威胁缓解 pipeline**。一台运行 Ubuntu Server 22.04 的 Web 服务器受到 Kali Linux 机器使用 **Nikto**(一款 Web 漏洞扫描器)的攻击。自定义的 Python 监控脚本通过分析 Apache access log 实时检测攻击,使用 **iptables** 自动封锁攻击者 IP,并生成详细的 **HTML 事件报告**。 整个从检测到封锁的周期在 60 秒内完成,无需任何人工干预。 ## 实验环境 | 角色 | 操作系统 | IP 地址 | 工具 | |------|----|------------|------| | 🟢 防御者 / 受害者 | Ubuntu Server 22.04 | 192.168.253.131 | Apache2, Python3, iptables, UFW | | 🔴 攻击者 | Kali Linux | 192.168.253.130 | Nikto v2.5.0 | **虚拟化环境:** VMware Workstation — NAT 网络 **网络:** 两台机器位于同一个 NAT 子网中 (192.168.253.0/24) ``` ┌─────────────────────────────────────────────────────────┐ │ VMware NAT Network │ │ 192.168.253.0/24 │ │ │ │ ┌──────────────────┐ ┌──────────────────────┐ │ │ │ Kali Linux │ │ Ubuntu Server │ │ │ │ 192.168.253.130 │──────▶│ 192.168.253.131 │ │ │ │ │ Nikto │ │ │ │ │ [ATTACKER] │ scan │ Apache2 :80 │ │ │ └──────────────────┘ │ Python Monitor │ │ │ │ iptables │ │ │ │ [DEFENDER] │ │ │ └──────────────────────┘ │ └─────────────────────────────────────────────────────────┘ ``` ## 攻击场景 **威胁:** 使用 Nikto 进行 Web 侦察和漏洞扫描 **攻击向量:** HTTP — 端口 80 **检测到的技术:** 路径遍历、管理员面板枚举、尝试访问 `/etc/shadow`、CGI 探测、过时的服务器指纹识别 **从 Kali 发起的攻击命令:** ``` nikto -h http://192.168.253.131 ``` **Ubuntu 上的自动防御 pipeline:** ``` Apache access.log → Python Monitor → Pattern Match → Hit Counter → iptables DROP → HTML Report ``` ## 项目结构 ``` threat-mitigation/ │ ├── scripts/ │ ├── python/ │ │ └── web_threat_monitor.py # Core detection, blocking & reporting engine │ └── bash/ │ └── setup.sh # Environment setup script │ ├── assets/ │ ├── 01_apache_running.png │ ├── 02_monitor_running.png │ ├── 03_nikto_scan.png │ ├── 04_monitor_detecting.png │ ├── 05_iptables_blocked.png │ └── 06_incident_report.png │ ├── reports/ # Auto-generated HTML incident reports │ └── incident_report_YYYYMMDD_HHMMSS.html │ ├── logs/ │ ├── monitor.log # Runtime monitor log │ └── blocked_ips.json # Persistent blocked IP records │ └── README.md ``` ## 工作原理 ### 检测引擎 (`web_threat_monitor.py`) 监控脚本实时追踪 `/var/log/apache2/access.log`,并根据包含 **26 种攻击特征** 的库检查每个传入请求,涵盖: | 类别 | 特征 | |----------|-----------| | 扫描器指纹 | `nikto`, `curl`, `wget`, `python-requests` | | SQL 注入 | `select.*from`, `union.*select`, `insert.*into`, `drop.*table` | | XSS | `= 10 within 60 seconds: → iptables -I INPUT -s -j DROP → iptables -I OUTPUT -d -j DROP → Log incident to blocked_ips.json → Generate HTML incident report ``` ### 报告生成 每次封锁事件都会触发自动生成 HTML 事件报告,其中包含: - 会话统计(已封锁的 IP、事件、阈值配置) - 完整的事件日志表(时间戳、IP、命中数、原因、操作) - 活动 iptables 规则的实时快照 ## 脚本 ### `scripts/python/web_threat_monitor.py` 核心监控引擎。在防御者机器上持续运行。 **关键参数(可在脚本顶部配置):** ``` THRESHOLD = 10 # hits before blocking TIME_WINDOW = 60 # seconds to count hits within CHECK_INTERVAL = 2 # seconds between log reads LOG_FILE = "/var/log/apache2/access.log" ``` **运行:** ``` python3 scripts/python/web_threat_monitor.py ``` ### `scripts/bash/setup.sh` 在 Ubuntu Server 上自动完成完整的环境设置:安装 Apache2 + PHP、配置日志权限、为 iptables 设置 sudoers 规则,并创建项目目录结构。 **运行:** ``` chmod +x scripts/bash/setup.sh sudo bash scripts/bash/setup.sh ``` ## 实验指南 ### 第 1 步 — Apache 在 Ubuntu(防御者)上运行 ![Apache 运行中](https://static.pigsec.cn/wp-content/uploads/repos/cas/ee/ee770cfb63c257a66fd9eed01a6bf0545fa791e4098977d9737802a79e4068d8.png) Apache2 在 Ubuntu Server 22.04 上处于活动状态并运行,在端口 80 上提供目标 Web 应用程序。 ### 第 2 步 — 监控程序在 Ubuntu 上启动 ![监控程序运行中](https://static.pigsec.cn/wp-content/uploads/repos/cas/fc/fc6540acae1e0e087ae852bd18f1bb3c2cab3dc9d4b16492478c32303459b642.png) Python 监控程序初始化,开始追踪 Apache access log,并等待传入请求。阈值设定为在 60 秒内达到 10 次命中后自动封锁。 ### 第 3 步 — 从 Kali(攻击者)发起 Nikto 扫描 ![Nikto 扫描](https://static.pigsec.cn/wp-content/uploads/repos/cas/51/518a9beabc76e5b359a68c64e0883d312bc7cf0c4290344af282c48aa8b55273.png) Nikto v2.5.0 对目标执行全面的 Web 漏洞扫描。它探测过时的服务器版本、缺失的安全标头、暴露的管理面板以及已知的 CVE。该扫描在多个攻击向量上触发了 20 多次错误尝试。 ### 第 4 步 — 监控程序检测并封锁攻击 ![监控程序检测中](https://static.pigsec.cn/wp-content/uploads/repos/cas/05/057010fe9ca7281e7315d19ce4a12b2673eaf11fb9d9bffa0026a2864c29e5f3.png) 监控程序实时检测每个恶意请求。在 60 秒窗口内达到 10 次命中后,攻击者 IP `192.168.253.130` 将通过 iptables 自动封锁。封锁事件会触发立即生成报告。 ``` [WARNING] BLOCKED: 192.168.253.130 | Reason: Web attack: 10 hits within 60s | Hits: 10 [INFO] Report generated: incident_report_20260710_214536.html ``` ### 第 5 步 — 确认 iptables 规则 ![iptables 已封锁](https://static.pigsec.cn/wp-content/uploads/repos/cas/cd/cd03972ede54bda4bbc5b707bb187682666ea11607674c26ab26f5f0acf496fe.png) iptables INPUT 链确认已对攻击者 IP 应用了 DROP 规则,阻止了来自该主机的所有进一步通信。 ### 第 6 步 — HTML 事件报告 ![事件报告](https://static.pigsec.cn/wp-content/uploads/repos/cas/b8/b8fc0a194175f4001c256a04a9e0f6dc8b2d3e258551c1d0d3fa9217c3fd5799.png) 系统会自动生成包含完整攻击详情、会话统计和活动 iptables 规则实时快照的 HTML 事件报告。 ## 结果 | 指标 | 数值 | |--------|-------| | 攻击工具 | Nikto v2.5.0 | | 攻击者 IP | 192.168.253.130 | | 触发封锁的请求数 | 10 | | 检测时间 | < 5 秒 | | 封锁时间 | < 60 秒 | | 封锁方法 | iptables INPUT + OUTPUT DROP | | 报告生成 | 封锁事件触发时自动生成 | | 所需人工干预 | ❌ 无 | ## 展示技能 - **Python 脚本编写** — 实时日志解析、regex 模式匹配、subprocess 自动化 - **Linux 防火墙管理** — 通过 Python subprocess 注入 iptables 规则 - **日志分析** — 使用 tail-follow 实现监控 Apache access log - **威胁检测** — 具有速率限制的基于特征的检测引擎 - **事件报告** — 自动生成 HTML 报告 - **网络安全** — UFW 配置、NAT 实验环境设置 - **攻击工具** — Nikto Web 漏洞扫描器(攻击模拟) **alexrepsec** 网络安全爱好者 | 家庭实验室构建者 *本项目作为网络安全作品集的一部分而构建,旨在展示实用的 SOC 自动化及分析师技能。*
标签:Bash, PB级数据处理, Python, 主机安全, 威胁缓解, 安全运维, 应用安全, 无后门, 自动化响应, 逆向工具