Gitlovess/linux-hardening-lab

GitHub: Gitlovess/linux-hardening-lab

基于 CIS Benchmark Level 2 标准的 Ubuntu 22.04 服务器安全加固实验,整合 auditd 取证审计、UFW 防火墙、fail2ban 入侵防御和 SSH 密钥认证构建深度防御架构。

Stars: 0 | Forks: 0

# 🐧 Linux 服务器安全加固实验 使用 **CIS Benchmark Level 2**、**auditd** 取证日志、**UFW** 防火墙、**fail2ban** 自动化入侵防御以及 **SSH 密钥认证**,对生产级 Ubuntu 22.04 LTS 服务器进行安全加固 —— 模拟 SOC 环境下的真实基础设施安全。 ## 🎯 为什么这很重要 监控服务器是 SOC 实验室中最关键的资产 —— 如果攻击者攻陷了它,他们就控制了你整个可见性层。与端点加固不同,**基础设施加固**保护的是其他一切所依赖的系统。 **深度防御:日志记录 → 防火墙 → 入侵防御 → 加固访问** ## 🧪 实验架构 ``` ┌──────────────────────────────────────────────────┐ │ Ubuntu Server 22.04 LTS │ │ │ │ ┌─────────────────────────────────────────┐ │ │ │ Layer 1: auditd (Forensic Logging) │ │ │ │ - Monitors /etc/sudoers │ │ │ │ - Tracks /etc/passwd, /etc/shadow │ │ │ │ - Logs system time changes │ │ │ │ - Session login/logout tracking │ │ │ └─────────────────────────────────────────┘ │ │ ┌─────────────────────────────────────────┐ │ │ │ Layer 2: UFW Firewall │ │ │ │ - Default DENY all incoming │ │ │ │ - Allow SSH (Port 22) only │ │ │ │ - Drop all routed traffic │ │ │ └─────────────────────────────────────────┘ │ │ ┌─────────────────────────────────────────┐ │ │ │ Layer 3: fail2ban │ │ │ │ - Monitors auth.log │ │ │ │ - Bans IPs after 5 failed attempts │ │ │ │ - Injects blocks directly into UFW │ │ │ └─────────────────────────────────────────┘ │ │ ┌─────────────────────────────────────────┐ │ │ │ Layer 4: SSH Hardening │ │ │ │ - ED25519 key-based auth only │ │ │ │ - Root login disabled │ │ │ │ - Password auth completely off │ │ │ │ - 45-second idle timeout │ │ │ └─────────────────────────────────────────┘ │ └──────────────────────────────────────────────────┘ ``` ## 🔧 工具与标准 | 工具 | 用途 | |------|---------| | **CIS Benchmark Level 2** | Ubuntu Linux 安全加固指南 | | **auditd** | 内核级取证审计日志 | | **UFW** | 基于主机的防火墙(默认拒绝姿态) | | **fail2ban** | 自动化暴力破解 IP 封禁 | | **sshd_config** | SSH 守护进程加固 | | **ausearch** | auditd 日志查询与分析工具 | ## 📋 加固步骤 ### 1. auditd — 取证日志 ``` sudo apt install auditd sudo systemctl enable auditd sudo systemctl start auditd # 创建 CIS 规则文件 sudo nano /etc/audit/rules.d/cis-hardening.rules # 应用规则 sudo augenrules --load ``` | 规则类别 | 监控文件 | 用途 | |--------------|-----------------|---------| | 权限提升 | `/etc/sudoers` | 检测 sudo 滥用 | | 系统时间 | `adjtimex`, `settimeofday` | 检测日志篡改 | | 身份变更 | `/etc/passwd`, `/etc/shadow` | 检测凭证修改 | | 网络配置 | `/etc/hosts`, `/etc/network/` | 检测网络操纵 | | 会话跟踪 | `/var/log/lastlog` | 跟踪登录/注销事件 | **验证:** ``` sudo useradd testuser sudo ausearch -k identity # 预期:包含 key=identity 的 forensic 日志条目 ``` ### 2. UFW — 基于主机的防火墙 ``` sudo ufw default deny incoming sudo ufw default deny routed sudo ufw default allow outgoing sudo ufw allow 22/tcp sudo ufw enable sudo ufw status verbose ``` **验证:** ``` telnet localhost 80 # Should timeout (blocked) telnet localhost 22 # Should connect (allowed) ``` ### 3. fail2ban — 自动化入侵防御 ``` sudo apt install fail2ban sudo systemctl enable fail2ban ``` **jail.local 配置**(见 `configs/jail.local`): ``` [sshd] enabled = true port = ssh filter = sshd logpath = /var/log/auth.log maxretry = 5 bantime = 600 findtime = 600 banaction = ufw ``` **验证 — 模拟 5 次失败登录:** ``` for i in {1..5}; do echo "$(date) sshd[$$]: Failed password for root from 10.0.0.99 port 22 ssh2" | sudo tee -a /var/log/auth.log done sudo fail2ban-client status sshd # 预期:10.0.0.99 被列为 banned ``` ### 4. SSH 加固 **生成 ED25519 密钥对(Windows 主机):** ``` ssh-keygen -t ed25519 -C "lab-key" ``` **将公钥传输到服务器:** ``` ssh-copy-id -i ~/.ssh/id_ed25519.pub username@ ``` **sshd_config 设置**(见 `configs/sshd_config`): | 设置项 | 值 | 用途 | |---------|-------|---------| | `PermitRootLogin` | `no` | 阻止 root SSH 访问 | | `PasswordAuthentication` | `no` | 强制仅使用密钥认证 | | `PermitEmptyPasswords` | `no` | 阻止空密码 | | `MaxAuthTries` | `4` | 限制暴力破解尝试 | | `ClientAliveInterval` | `45` | 断开空闲会话 | ``` sudo systemctl restart sshd ``` **验证:** ``` ssh -o PreferredAuthentications=password username@ # 预期:Permission denied (publickey) ``` ## ✅ 验证总结 | 测试 | 预期结果 | |------|-----------------| | `useradd` 后执行 `ausearch -k identity` | 包含 identity key 的取证日志 | | `telnet localhost 80` | 连接超时 | | `telnet localhost 22` | 立即连接 | | 5 次失败登录 → fail2ban | IP 被 UFW 自动封禁 | | SSH 密码尝试 | `Permission denied (publickey)` | ## 📁 仓库结构 ``` linux-hardening-lab/ ├── README.md ├── configs/ │ ├── cis-hardening.rules # auditd CIS ruleset │ ├── jail.local # fail2ban SSH jail config │ └── sshd_config # SSH daemon hardening settings ├── docs/ │ └── cis-benchmark-notes.md # CIS Level 2 controls reference └── screenshots/ └── .gitkeep ``` ## 🎯 展示的技能 - Ubuntu 22.04 LTS 上的 CIS Benchmark Level 2 - auditd 部署与自定义规则编写 - 使用 `ausearch` 进行取证日志分析 - UFW 默认拒绝防火墙配置 - 结合 UFW 集成的 fail2ban jail 配置 - 自动化暴力破解检测与 IP 封禁 - ED25519 SSH 密钥对生成与部署 - SSH 守护进程加固(`sshd_config`) - 深度防御分层安全架构 ## 📌 参考 - [CIS Benchmarks](https://www.cisecurity.org/cis-benchmarks) - [Ubuntu 安全文档](https://ubuntu.com/security) - [auditd Rules — Neo23x0](https://github.com/Neo23x0/auditd) - [fail2ban 文档](https://github.com/fail2ban/fail2ban) ## 🔗 SOC 家庭实验室系列 | 实验 | 项目 | 状态 | |-----|---------|--------| | 实验 1 | [NSM Stack — TShark + Zeek + Suricata IDS](https://github.com/Gitlovess/nsm-lab) | ✅ 完成 | | 实验 2 | [pfSense 防火墙 + Suricata IPS](https://github.com/Gitlovess/pfSense-Suricata-IPS-lab) | ✅ 完成 | | 实验 3 | [Windows 10 端点加固](https://github.com/Gitlovess/Windows-10-Endpoint-Hardening) | ✅ 完成 | | 实验 4 | Linux 服务器安全加固(本仓库) | ✅ 完成 |
标签:Linux服务器, 入侵防御, 子域名枚举, 安全基线加固, 系统安全, 系统运维