SSH 暴力破解防御指南
只要服务器暴露在公网上,SSH 端口扫描和暴力破解几乎必然发生。 防御核心原则:纵深防御——单一手段可能被绕过,多层叠加后攻击成本极高。
防御优先级
| 优先级 | 手段 | 效果 |
|---|---|---|
| 必须做 | ① 公钥认证,禁用密码登录 | 密码暴力破解彻底失效 |
| ② fail2ban 自动封禁 | 多次失败自动拉黑 | |
| 强烈推荐 | ③ 修改默认端口(22→其他) | 减少 90%+ 扫描流量 |
| ④ 限制来源 IP | 非授权来源无法连接 | |
| 高安全 | ⑤ 双因素认证(2FA) | 私钥泄露也无法登录 |
| ⑥ OSSEC 入侵检测 | 发现异常行为主动告警 | |
| 进阶加固 | ⑦ SSH 连接限制 | 降低暴力破解成功率 |
| ⑧ SSH 蜜罐(Cowrie) | 监控分析攻击行为 |
① 公钥认证(最彻底)
# 本机生成密钥对
ssh-keygen -t ed25519 -C "[email protected]"
# 上传公钥到服务器
ssh-copy-id -i ~/.ssh/id_ed25519.pub admin@<server-ip>
# 在服务器上禁用密码登录
# /etc/ssh/sshd_config
PubkeyAuthentication yes
PasswordAuthentication no
PermitEmptyPasswords no
ChallengeResponseAuthentication no
sudo systemctl restart sshd
⚠️ 退出前必须新开终端测试公钥登录是否正常。
② fail2ban
# 安装
yum install -y epel-release fail2ban # CentOS
# apt install -y fail2ban # Ubuntu
# 配置 /etc/fail2ban/jail.local
# [DEFAULT] bantime=3600 findtime=600 maxretry=5
# [sshd] enabled=true maxretry=3
systemctl enable --now fail2ban
# 管理
fail2ban-client status sshd # 查看封禁状态
fail2ban-client set sshd unbanip <ip> # 解封
iptables -L f2b-sshd -n # 查看规则
③ 修改端口 + 限制来源
# /etc/ssh/sshd_config: Port 2222
# 防火墙开放 + 重启 SSH
# 限制来源 IP(运维 IP 白名单)
iptables -A INPUT -p tcp -s <运维IP> --dport 2222 -j ACCEPT
④ 双因素认证(2FA)
# 安装
yum install -y google-authenticator
# 为用户配置
google-authenticator # 交互设置,扫描二维码
# /etc/pam.d/sshd 添加
auth required pam_google_authenticator.so nullok
# /etc/ssh/sshd_config
ChallengeResponseAuthentication yes
systemctl restart sshd
⑥ 入侵检测
yum install -y ossec-hids # 主机入侵检测系统
# 常用排查命令
cat ~/.ssh/authorized_keys # 检查后门公钥
crontab -l # 检查计划任务后门
ss -tunapl | grep ESTABLISHED # 检查异常连接
ps aux | grep -iE "miner|xmrig" # 检查挖矿进程
⑦ SSH 连接限制
通过限制并发连接数和超时时间,降低暴力破解的成功率。
# /etc/ssh/sshd_config
# 限制最大连接数
MaxSessions 3
MaxStartups 3:30:10
# 设置连接超时
ClientAliveInterval 300
ClientAliveCountMax 2
# 限制登录时间
LoginGraceTime 30
# 限制用户
AllowUsers admin operator
DenyUsers root guest
sudo systemctl restart sshd
参数说明:
MaxSessions— 单个连接的最大会话数MaxStartups— 并发未认证连接的最大数量(start:rate:full 格式,超过 start 后按 rate 概率拒绝,达到 full 全部拒绝)ClientAliveInterval— 服务器向客户端发送心跳的间隔(秒),超时断开LoginGraceTime— 用户登录的最长时间(秒),超时断开AllowUsers/DenyUsers— 白名单/黑名单用户控制
⑧ SSH 蜜罐监控(Cowrie)
高级防护手段,用于监控和分析攻击行为。收集攻击者使用的命令和工具,提升防御能力。
# 克隆项目
git clone https://github.com/cowrie/cowrie.git
cd cowrie
# 安装依赖
pip install -r requirements.txt
# 配置蜜罐
cp etc/cowrie.cfg.dist etc/cowrie.cfg
# 修改 listen_endpoints = tcp:2222:interface=0.0.0.0
实时攻击监控脚本
#!/bin/bash
# 实时监控 SSH 登录尝试
tail -f /var/log/auth.log | while read line; do
if echo "$line" | grep -q "Failed password"; then
ip=$(echo "$line" | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')
echo "$(date): 检测到暴力破解尝试,来源IP: $ip" >> /var/log/ssh_attacks.log
# 发送告警(可接入钉钉、企业微信等)
curl -X POST "https://your-webhook-url" \
-d "{'text': 'SSH攻击告警: $ip 正在尝试暴力破解'}"
fi
done
SSH 终极安全配置模板
综合以上所有方法的生产环境 SSH 配置基准:
# /etc/ssh/sshd_config 最佳实践配置
# 基础安全
Port 2022
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
# 认证配置
PubkeyAuthentication yes
PasswordAuthentication no
PermitEmptyPasswords no
PermitRootLogin no
AuthenticationMethods publickey
# 连接限制
MaxAuthTries 3
MaxSessions 3
MaxStartups 3:30:10
LoginGraceTime 30
ClientAliveInterval 300
ClientAliveCountMax 2
# 用户限制
AllowUsers admin operator
DenyUsers root guest nobody
# 其他安全选项
X11Forwarding no
AllowTcpForwarding no
GatewayPorts no
PermitTunnel no
Banner /etc/ssh/banner.txt
每日巡检脚本要点
- fail2ban 是否运行?
systemctl is-active fail2ban - 最近登录失败次数?
grep "Failed password" /var/log/secure | wc -l - 是否有陌生 IP 成功登录?
grep "Accepted" /var/log/secure过滤已知 IP - sshd_config 是否被篡改?md5sum 比对
- authorized_keys 是否有新增?公钥数量比对
应急响应流程
| 阶段 | 操作 |
|---|---|
| 确认(5min) | last 查登录记录 → 查 authorized_keys 后门 → 查 bash_history |
| 止血(15min) | pkill -kill -t <tty> 断连接 → iptables -I INPUT -s <ip> -j DROP → 必要时停 SSH |
| 排查 | 检查 cron/systemd/authorized_keys 后门、挖矿进程、横向移动 |
| 修复 | 严重入侵重装系统,从备份恢复,重新加固 |
| 复盘 | 输出事件报告,更新加固方案,补充监控告警 |
关联页面
| 页面 | 关联点 |
|---|---|
| network-troubleshooting-order | iptables / firewalld 防火墙规则配置 |
| linux-kernel-tuning-production | 安全相关内核参数 |
| server-performance-four-dimensions | 网络连接监控(ss/netstat)与异常检测 |
| server-security-hardening-checklist | 新机器上线安全加固清单(20 项全面基线)——SSH 加固是其中核心环节 |
| linux-user-management-guide | Linux 用户管理避坑指南(密码策略/pam_tally2/fail2ban/登录审计等深度覆盖) |
| anti-brute-force-script | SSH 防暴力破解自动化脚本(iptables 封禁/白名单/通知/监控)——fail2ban 轻量替代方案 |
| journalctl-log-tracking-guide | journalctl 实时检测 SSH 攻击 |
| ssh-connection-debugging-guide | SSH 连接调试完全指南(ssh -vvv 输出解读 / 服务端日志联查 / 典型问题排查) |