clashhub-net/vps-security-guide

GitHub: clashhub-net/vps-security-guide

一份中英双语 VPS 安全加固指南,涵盖 SSH 硬化、防火墙、防暴力破解、DDoS 防护及入侵检测,并提供一键加固脚本。

Stars: 0 | Forks: 0

# VPS 安全防护指南 [![License](https://img.shields.io/badge/license-CC%20BY--NC--SA%204.0-blue.svg)](LICENSE) [![GitHub stars](https://img.shields.io/github/stars/clashhub-net/vps-security-guide.svg?style=flat-square)](https://github.com/clashhub-net/vps-security-guide/stargazers) **中文** | **[English](README_EN.md)** ## 目录 - [为什么需要安全防护?](#为什么需要安全防护) - [SSH 安全加固](#ssh-安全加固) - [防火墙配置](#防火墙配置) - [_fail2ban_ 防暴力破解](#fail2ban-防暴力破解) - [DDoS 基础防护](#ddos-基础防护) - [安全脚本集合](#安全脚本集合) - [入侵检测](#入侵检测) - [常见问题](#常见问题) ## 为什么需要安全防护? ### VPS 常见安全威胁 | 威胁类型 | 危害程度 | 发生频率 | |----------|----------|----------| | **SSH 暴力破解** | ⭐⭐⭐⭐⭐ | 每天数十次 | | **DDoS 攻击** | ⭐⭐⭐⭐⭐ | 偶发 | | **恶意扫描** | ⭐⭐⭐ | 持续 | | **漏洞利用** | ⭐⭐⭐⭐ | 偶发 | | **账户破解** | ⭐⭐⭐⭐ | 较少 | ### 安全防护层级 第一层:SSH 加固 第二层:防火墙 第三层:入侵检测 第四层:fail2ban 第五层:DDoS 防护 ## SSH 安全加固 ### 1. 修改 SSH 默认端口 # 修改 SSH 配置文件 vim /etc/ssh/sshd_config # 找到 Port 22,修改为其他端口 Port 2222 # 重启 SSH 服务 systemctl restart sshd ### 2. 禁用 root 登录 # 创建新用户 adduser admin # 赋予 sudo 权限 usermod -aG sudo admin # 修改 SSH 配置 vim /etc/ssh/sshd_config # 修改以下配置 PermitRootLogin no PasswordAuthentication no PubkeyAuthentication yes # 重启 SSH systemctl restart sshd ### 3. SSH 密钥登录 # 本地生成密钥对 ssh-keygen -t ed25519 -C "your_email@example.com" # 上传公钥到服务器 ssh-copy-id -i ~/.ssh/id_ed25519.pub admin@your_vps_ip # 测试登录 ssh -i ~/.ssh/id_ed25519 admin@your_vps_ip -p 2222 ### 4. 一键 SSH 加固脚本 #!/bin/bash # SSH 安全加固脚本 # 使用方法: bash ssh_hardening.sh echo "=== SSH 安全加固脚本 ===" # 备份原配置 cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak # 修改 SSH 端口 read -p "请输入新端口 (建议 10000-65535): " NEW_PORT sed -i "s/^#Port 22/Port $NEW_PORT/" /etc/ssh/sshd_config sed -i "s/^Port 22/Port $NEW_PORT/" /etc/ssh/sshd_config # 禁用 root 登录 sed -i "s/^#PermitRootLogin yes/PermitRootLogin no/" /etc/ssh/sshd_config sed -i "s/^PermitRootLogin yes/PermitRootLogin no/" /etc/ssh/sshd_config # 禁用密码登录 sed -i "s/^#PasswordAuthentication yes/PasswordAuthentication no/" /etc/ssh/sshd_config sed -i "s/^PasswordAuthentication yes/PasswordAuthentication no/" /etc/ssh/sshd_config # 限制空密码登录 sed -i "s/^#PermitEmptyPasswords no/PermitEmptyPasswords no/" /etc/ssh/sshd_config sed -i "s/^PermitEmptyPasswords yes/PermitEmptyPasswords no/" /etc/ssh/sshd_config # 重启 SSH systemctl restart sshd echo "=== SSH 加固完成 ===" echo "新端口: $NEW_PORT" echo "请使用密钥登录测试后再退出!" ## 防火墙配置 ### 1. 安装 UFW (Ubuntu/Debian) # 安装 UFW apt update && apt install ufw -y # 设置默认策略 ufw default deny incoming ufw default allow outgoing # 开放 SSH 端口 ufw allow 2222/tcp # 开放常用端口 ufw allow 80/tcp # HTTP ufw allow 443/tcp # HTTPS ufw allow 443/udp # QUIC # 启用防火墙 ufw enable # 查看状态 ufw status verbose ### 2. 安装 firewalld (CentOS) # 安装 firewalld yum install firewalld -y # 启动服务 systemctl start firewalld systemctl enable firewalld # 开放端口 firewall-cmd --permanent --add-port=2222/tcp firewall-cmd --permanent --add-port=80/tcp firewall-cmd --permanent --add-port=443/tcp # 重载配置 firewall-cmd --reload # 查看状态 firewall-cmd --list-all ### 3. 一键防火墙脚本 #!/bin/bash # 防火墙配置脚本 # 检测系统 if [ -f /etc/debian_version ]; then SYSTEM="debian" elif [ -f /etc/redhat-release ]; then SYSTEM="centos" else echo "不支持的系统" exit 1 fi echo "检测到系统: $SYSTEM" if [ "$SYSTEM" = "debian" ]; then apt update && apt install ufw -y # 默认策略 ufw default deny incoming ufw default allow outgoing # 开放端口 ufw allow 2222/tcp comment 'SSH' ufw allow 80/tcp comment 'HTTP' ufw allow 443/tcp comment 'HTTPS' ufw enable echo "UFW 防火墙已配置完成" elif [ "$SYSTEM" = "centos" ]; then yum install firewalld -y systemctl start firewalld firewall-cmd --permanent --add-port=2222/tcp firewall-cmd --permanent --add-port=80/tcp firewall-cmd --permanent --add-port=443/tcp firewall-cmd --reload echo "Firewalld 防火墙已配置完成" fi ## fail2ban 防暴力破解 ### 1. 安装 fail2ban # Ubuntu/Debian apt update && apt install fail2ban -y # CentOS yum install epel-release -y yum install fail2ban -y # 启动服务 systemctl start fail2ban systemctl enable fail2ban ### 2. 配置 fail2ban # 复制默认配置 cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local # 编辑配置 vim /etc/fail2ban/jail.local # 修改以下参数 [DEFAULT] bantime = 3600 # 封禁时间 1 小时 findtime = 600 # 10 分钟内 maxretry = 3 # 最大重试次数 [sshd] enabled = true port = 2222 # 你的 SSH 端口 # 重启服务 systemctl restart fail2ban ### 3. 查看 fail2ban 状态 # 查看被封禁的 IP fail2ban-client banned # 查看 SSH 监狱状态 fail2ban-client status sshd # 解封 IP fail2ban-client set sshd unbanip 192.168.1.100 ## DDoS 基础防护 ### 1. 内核参数优化 # 编辑 sysctl.conf vim /etc/sysctl.conf # 添加以下内容 # 防止 SYN 洪水攻击 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_syn_retries = 2 net.ipv4.tcp_synack_retries = 2 # 限制连接数 net.ipv4.ip_local_port_range = 2000 65000 net.ipv4.tcp_max_tw_buckets = 2000 # 限制 ICMP 流量 net.ipv4.icmp_echo_ignore_broadcasts = 1 # 应用配置 sysctl -p ### 2. 使用 UFW 限制连接数 # 限制每个 IP 的并发连接数 ufw limit from 192.168.1.0/24 to any port 443 proto tcp # 限制 SSH 连接频率 ufw limit 2222/tcp ## 安全脚本集合 ### 1. 综合安全检查脚本 #!/bin/bash # VPS 安全检查脚本 echo "======================================" echo " VPS 安全检查报告" echo "======================================" echo "" # 检查 SSH 配置 echo "[1] SSH 配置检查" echo "-----------------------------" grep "^Port" /etc/ssh/sshd_config || echo "使用默认端口 22 (建议修改)" grep "^PermitRootLogin" /etc/ssh/sshd_config || echo "未禁用 root 登录 (建议禁用)" grep "^PasswordAuthentication" /etc/ssh/sshd_config || echo "未禁用密码登录 (建议禁用)" echo "" # 检查防火墙 echo "[2] 防火墙状态检查" echo "-----------------------------" if command -v ufw &> /dev/null; then ufw status | head -5 elif command -v firewall-cmd &> /dev/null; then firewall-cmd --list-all else echo "未检测到防火墙 (建议安装)" fi echo "" # 检查 fail2ban echo "[3] fail2ban 检查" echo "-----------------------------" if command -v fail2ban-client &> /dev/null; then fail2ban-client status sshd 2>/dev/null || echo "fail2ban 未运行" else echo "未安装 fail2ban (建议安装)" fi echo "" # 检查登录日志 echo "[4] 最近 SSH 登录记录" echo "-----------------------------" last -10 echo "" # 检查可疑进程 echo "[5] 高CPU/内存进程" echo "-----------------------------" ps aux --sort=-%cpu | head -6 echo "" echo "======================================" echo " 安全检查完成" echo "======================================" ### 2. 一键安全加固脚本 #!/bin/bash # VPS 一键安全加固脚本 echo "======================================" echo " VPS 安全加固脚本" echo "======================================" # 1. 更新系统 echo "[1/6] 更新系统..." apt update && apt upgrade -y # 2. 安装必要工具 echo "[2/6] 安装安全工具..." apt install -y ufw fail2ban curl wget vim # 3. 配置 SSH echo "[3/6] 配置 SSH..." read -p "请输入新 SSH 端口: " SSH_PORT sed -i "s/^#Port 22/Port $SSH_PORT/" /etc/ssh/sshd_config sed -i "s/^Port 22/Port $SSH_PORT/" /etc/ssh/sshd_config sed -i "s/^#PermitRootLogin yes/PermitRootLogin no/" /etc/ssh/sshd_config sed -i "s/^#PasswordAuthentication yes/PasswordAuthentication no/" /etc/ssh/sshd_config # 4. 配置防火墙 echo "[4/6] 配置防火墙..." ufw default deny incoming ufw default allow outgoing ufw allow $SSH_PORT/tcp ufw allow 80/tcp ufw allow 443/tcp ufw --force enable # 5. 配置 fail2ban echo "[5/6] 配置 fail2ban..." cat > /etc/fail2ban/jail.local <> /etc/sysctl.conf < VPSVIP | ClashVIP | ClashHub

标签:DDoS防护, Fail2ban, GitHub Advanced Security, IP 地址批量处理, IT运维, Linux运维, Socks5代理, SSH, SSH密钥登录, VPS, 云服务器, 内存分配, 动态API解析, 子域名变形, 子域名枚举, 安全加固, 安全指南, 安全脚本, 安全配置, 应用安全, 漏洞防护, 系统安全, 网络安全, 网络防护, 防暴力破解, 防火墙, 隐私保护