5136siegfried/vps-security-toolkit

GitHub: 5136siegfried/vps-security-toolkit

一个模块化的 Linux VPS 安全审计工具包,自动执行全面安全检查并生成带全局评分的 HTML 报告,同时支持作为可复用的 CI workflow 集成到 GitHub Actions 中。

Stars: 0 | Forks: 0

# vps-security-toolkit
**一个用于 VPS 基础设施的模块化 DevSecOps 审计工具包。** 生成包含全局安全分数的独立 HTML 报告。 您可以手动运行、计划运行,或将其作为可重用的 CI workflow 从任何 GitHub 项目中调用。 [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE) [![Shell: Bash](https://img.shields.io/badge/Shell-Bash-green.svg)](run.sh) [![CI: GitHub Actions](https://img.shields.io/badge/CI-GitHub_Actions-black.svg)](.github/workflows) [English](#english) · [Français](#français)
## English ### 功能介绍 对 Linux VPS 执行全面的安全审计,并生成一份单一、自包含的 HTML 报告 —— 无外部依赖,无 SaaS,数据不会离开您的基础设施(除非您配置了 webhook/邮件通知)。 报告包含一个基于所有检查项计算出的 **全局分数 /100**,带有颜色编码的各个部分、可折叠面板,以及针对每个发现问题的直接修复命令。 ### 覆盖范围 | 领域 | 检查内容 | |---|---| | **System** | CPU、RAM、磁盘、inodes、swap、uptime、kernel、top 进程 | | **Network** | 开放端口、防火墙规则 (ufw/iptables/nftables)、到公网 IP 的出站连接、ARP 表、路由 | | **SSH 与访问** | 暴力破解尝试、top 攻击者 IP、活动会话、`sshd_config` 与 CIS 基准对比(9 个参数) | | **用户与权限** | 具有活动 shell 的账户、UID 为 0 的非 root 用户、sudo/wheel 成员、授权的 SSH 密钥、sudoers、crontabs | | **TLS 证书** | Let's Encrypt 证书的过期检查,包含剩余天数和颜色警报 | | **Kernel 加固** | 根据推荐值评估的 11 个 sysctl 安全参数 | | **服务** | Systemd 失败单元、运行中的服务、开机启用的服务 | | **Docker** | 容器状态/健康/重启情况、特权模式、悬空镜像、日志驱动、stdout 日志错误检测、Trivy 镜像扫描 | | **Nginx** | 访问日志解析 —— 注入/遍历模式、已知的扫描器 User-Agents、4xx/5xx 比率、top 攻击者 IP、top 端点 | | **完整性** | Lynis CIS 加固指数、rkhunter、chkrootkit、debsums、AIDE | | **CVE** | Trivy 系统扫描(仅限 HIGH/CRITICAL) | | **机密信息** | 检测配置文件(`.env`、`.yaml`、`.conf` 等)中的凭据模式 —— 输出中的值将被掩码处理 | | **最佳实践** | 涵盖 6 个领域的 20 项检查 —— 见下表 | **最佳实践检查 (04-bestpractices.sh)** | 领域 | 检查内容 | |---|---| | 安全工具 | fail2ban(已安装 + 活跃 + jails)、lynis、rkhunter(db 日期)、chkrootkit、debsums、aide(+ db 已初始化)、trivy | | 系统 | unattended-upgrades(已安装 + 已启用)、NTP 同步 (timedatectl/chrony)、已配置 swap、journald 持久化存储、logrotate nginx | | SSH | 定义了 AllowUsers/AllowGroups、非标准端口 | | Docker | json-file 日志驱动、userns-remap、没有 HEALTHCHECK 的容器、docker-bench-security | | 备份 | borg、restic、rclone 或 cron 中的 rsync | | 网络 | 没有全局地址的活跃 IPv6 | ### 架构 ``` vps-security-toolkit/ │ ├── run.sh # Single entry point — sources everything, builds HTML │ ├── lib/ │ ├── html.sh # Shared HTML rendering helpers (section, table, badge, alert...) │ ├── score.sh # Global score engine — score_penalty "" │ └── notify.sh # Webhook (Discord/Slack) + mail dispatch │ ├── collectors/ │ ├── 00-system.sh # CPU, RAM, disk, processes, packages │ ├── 01-network.sh # Ports, firewall, connections, ARP, routes │ ├── 02-auth.sh # SSH, users, privileges, SUID/SGID │ ├── 03-integrity.sh # Lynis, rootkits, debsums, AIDE, Trivy, secrets, certs, services │ ├── 04-bestpractices.sh # 20 best practice checks with remediation commands │ ├── 05-docker.sh # Container state, image scan, stdout log analysis │ └── 06-nginx-attacks.sh # Nginx access log attack detection │ ├── Dockerfile # All-in-one image: lynis, trivy, rkhunter, chkrootkit, debsums, aide ├── docker-compose.yml ├── .env.example │ └── .github/workflows/ ├── audit.yml # Reusable workflow (workflow_call) — callable from any repo ├── scheduled.yml # Weekly cron └── on-demand.yml # Manual dispatch with host as input ``` ### 快速开始 **前置条件:** Linux 主机、bash、root 权限。可选但建议安装:`lynis`、`trivy`、`rkhunter`、`chkrootkit`、`debsums`、`aide`、`fail2ban` —— 缺失的工具会在“最佳实践”部分报告为警告,而不是错误。 ``` git clone https://github.com/5136siegfried/vps-security-toolkit.git cd vps-security-toolkit cp .env.example .env # edit at minimum NGINX_LOG_PATHS chmod +x run.sh lib/*.sh collectors/*.sh sudo ./run.sh --output /tmp/audit.html ``` 从远程 VPS 获取报告: ``` scp root@your-vps:/tmp/audit.html ./audit.html && open audit.html ``` ### Nginx 日志配置 该工具包使用**显式的按主机路径配置**(没有硬编码的约定)。在您的 `.env` 中设置 `NGINX_LOG_PATHS`,或者在运行前将其导出: ``` # 单一站点 NGINX_LOG_PATHS=/var/log/nginx/mysite.access.log # 多站点 — 以逗号分隔 NGINX_LOG_PATHS=/var/log/nginx/site1.access.log,/var/log/nginx/site2.access.log,/var/log/nginx/api.access.log ``` 回退机制:如果未设置 `NGINX_LOG_PATHS`,收集器将自动扫描 `/var/log/nginx/*access*.log`。 ### 配置参考 | 变量 | 默认值 | 用途 | |---|---|---| | `NGINX_LOG_PATHS` | auto | Nginx 访问日志路径,以逗号分隔 | | `NGINX_LOG_LINES` | `5000` | 每个日志文件分析的行数 | | `DOCKER_LOG_LINES` | `500` | 每个容器读取的行数 | | `NOTIFY_SCORE_THRESHOLD` | `70` | 触发通知的分数下限 | | `NOTIFY_WEBHOOK_URL` | — | Discord 或 Slack webhook URL | | `NOTIFY_WEBHOOK_TYPE` | `discord` | `discord` 或 `slack` | | `NOTIFY_MAIL_TO` | — | 邮件接收者(需要 `mail`/`sendmail`) | | `NOTIFY_MAIL_FROM` | auto | 发件人地址 | ### 可重用的 CI workflow 该工具包在多项目环境中的核心价值在于:**一个 workflow 定义,可从任何 repository 调用**。 ``` # 在任何其他 GitHub repo 中 — .github/workflows/security.yml name: VPS Security Audit on: schedule: - cron: "0 6 * * 1" # every Monday at 06:00 UTC workflow_dispatch: {} jobs: audit: uses: 5136siegfried/vps-security-toolkit/.github/workflows/audit.yml@main with: target_host: ${{ vars.VPS_HOST }} nginx_log_paths: "/var/log/nginx/mysite.access.log" score_threshold: "70" secrets: inherit ``` **必需的 secrets**(在调用仓库的 Settings → Secrets 中设置一次): | Secret | 必填 | 用途 | |---|---|---| | `VPS_SSH_KEY` | ✅ | 目标主机的 SSH 私钥 | | `NOTIFY_WEBHOOK_URL` | 可选 | Discord/Slack 通知 | | `NOTIFY_MAIL_TO` | 可选 | 电子邮件通知 | 该 workflow 会通过 SSH 连接到目标主机,复制工具包,运行审计,以 GitHub Actions artifact 的形式获取 HTML 报告(保留 90 天),并在完成后自动清理。 ### 通知 只有当分数低于设定的阈值时才会触发通知 —— 在干净的运行中不会产生噪音。 **Discord/Slack webhook:** ``` NOTIFY_WEBHOOK_URL=https://discord.com/api/webhooks/... NOTIFY_WEBHOOK_TYPE=discord # or slack NOTIFY_SCORE_THRESHOLD=70 ``` **邮件:** ``` NOTIFY_MAIL_TO=admin@example.com # 需要 host 上提供 mail 或 sendmail ``` ### 已知限制 - **这不是实时的 IDS。** 这是一个时间点审计,而不是持续的入侵检测。它不能替代 fail2ban、OSSEC 或 SIEM。 - **Nginx 格式假设。** 攻击收集器期望标准的 combined 日志格式。自定义日志格式需要调整 `collectors/06-nginx-attacks.sh`。 - **Docker 日志仅限 stdout。** 没有聚合或长期保留。对于大容量或生产级的日志分析,建议在此工具包之外使用专用的 pipeline(如 Loki、ELK)。 - **全面覆盖需要 root 权限。** 没有 root 权限,rkhunter、debsums、AIDE、机密扫描和 SUID 检查将受到降级限制,并在报告中标记。 - **Trivy rootfs 扫描速度很慢**(在繁忙的 VPS 上约需 5-10 分钟)。已应用 `timeout` 包装器;在大型文件系统上扫描可能不完整。 ## Français ### 功能说明 Lance un audit de sécurité complet sur un VPS Linux et produit un rapport HTML autonome — aucune dépendance externe, aucun SaaS, aucune donnée quittant l'infrastructure (sauf notification webhook/mail si configurée). Le rapport inclut un **score global /100** calculé sur l'ensemble des checks, avec des sections colorées, des panneaux repliables, et les commandes de remédiation directement dans chaque alerte. ### 快速使用 ``` git clone https://github.com/5136siegfried/vps-security-toolkit.git cd vps-security-toolkit cp .env.example .env sudo ./run.sh --output /tmp/audit.html ``` Récupérer le rapport depuis un VPS distant : ``` scp root@mon-vps:/tmp/audit.html . && open audit.html ``` ### 配置 Nginx (按 host) Le toolkit ne suppose aucune convention de chemin fixe. Définir `NGINX_LOG_PATHS` explicitement : ``` NGINX_LOG_PATHS=/var/log/nginx/site1.access.log,/var/log/nginx/site2.access.log ``` Si non défini : fallback automatique sur `/var/log/nginx/*access*.log`. ### 可复用的 CI workflow ``` # 在任何其他 GitHub repo 中 jobs: audit: uses: 5136siegfried/vps-security-toolkit/.github/workflows/audit.yml@main with: target_host: ${{ vars.VPS_HOST }} nginx_log_paths: "/var/log/nginx/monsite.access.log" secrets: inherit ``` Le workflow se connecte en SSH sur la cible, copie le toolkit, lance l'audit, rapatrie le rapport en artifact CI (conservé 90 jours) et nettoie après lui. ### Contribuer Voir [CONTRIBUTING.md](CONTRIBUTING.md) pour les règles de robustesse bash (`set -eu`, patterns `grep -c`, `find /`, SIGPIPE) et le processus de PR. Licence MIT.
Built for 5136.fr · No telemetry · No SaaS · Your data stays on your infrastructure
标签:Bash, DevSecOps, GitHub Actions, VPS, 上游代理, 关系图谱, 基线检查, 应用安全, 网络调试, 自动化, 自动笔记, 请求拦截