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
# 🛡️ 威胁缓解的脚本与自动化





## 📋 目录
- [概述](#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 | `