SSH 暴力破解攻击的诊断与防御

815093faeecb8692b51dceec3d90e818

SSH 暴力破解攻击是针对 Linux 服务器的常见威胁之一,通过暴力尝试用户名和密码,攻击者试图获得服务器的访问权限。这种攻击不仅会对服务器安全构成威胁,还可能导致服务器性能下降。以下是 SSH 暴力破解攻击的 诊断与防御最佳实践


1. SSH 暴力破解攻击的表现与诊断

1.1 常见表现

  • 大量失败的登录尝试
    • 系统日志中出现大量 "Failed password" 或 "Invalid user" 消息。
  • 服务器负载升高
    • 攻击可能导致高 CPU 或网络带宽使用。
  • 意外账户锁定
    • 如果启用了登录失败锁定策略,正常用户可能因多次尝试被锁定。
  • 访问延迟
    • 服务器响应变慢,SSH 登录变得困难。

1.2 日志诊断方法

1.2.1 检查 SSH 登录日志

  • 查看登录失败记录:

    bash
     
    grep "Failed password" /var/log/auth.log   # Debian/Ubuntu
    grep "Failed password" /var/log/secure    # RHEL/CentOS
    • 示例输出:
       
       
      Sep 04 10:00:00 server sshd[1234]: Failed password for root from 192.168.1.100 port 56789 ssh2
  • 查看无效用户名尝试:

    bash
     
    grep "Invalid user" /var/log/auth.log

1.2.2 检查成功登录记录

  • 查看最近的登录记录:

    bash
     
    last -a
  • 检查异常的成功登录:

    bash
     
    grep "Accepted" /var/log/auth.log

1.2.3 检查登录失败的来源 IP

  • 统计失败登录 IP:
    bash
     
    grep "Failed password" /var/log/auth.log | awk '{print $11}' | sort | uniq -c | sort -nr
    • 示例输出:
      basic
       
      50 192.168.1.100
      30 203.0.113.50

1.2.4 实时监控登录日志

  • 使用 tail 实时查看日志:
    bash
     
    tail -f /var/log/auth.log

2. SSH 暴力破解防御措施

2.1 修改默认 SSH 配置

2.1.1 更改默认端口

  • 默认 SSH 端口为 22,攻击者通常直接扫描该端口。
  • 修改 /etc/ssh/sshd_config 文件:
    bash
     
    Port 2222
  • 重启 SSH 服务:
    bash
     
    sudo systemctl restart sshd

2.1.2 禁用 root 用户登录

  • 禁止直接使用 root 登录:
    bash
     
    PermitRootLogin no
  • 配置后,攻击者即使暴力破解也无法直接登录 root

2.1.3 限制登录用户

  • 允许特定用户登录:
    bash
     
    AllowUsers user1 user2

2.2 使用更强的认证方式

2.2.1 配置 SSH 密钥认证

  • 使用 SSH 密钥代替密码,提高安全性:
    • 生成 SSH 密钥对:
      bash
       
      ssh-keygen -t rsa -b 4096
    • 将公钥上传到服务器:
      bash
       
      ssh-copy-id user@server_ip
  • 禁用密码登录:
    修改 /etc/ssh/sshd_config 文件:
    bash
     
    PasswordAuthentication no
  • 重启 SSH 服务:
    bash
     
    sudo systemctl restart sshd

2.2.2 配置双因素认证(2FA)

  • 安装 Google Authenticator:
    bash
     
    sudo apt install libpam-google-authenticator
    google-authenticator
  • 配置 PAM 模块(/etc/pam.d/sshd):
    text
     
    auth required pam_google_authenticator.so
  • 修改 /etc/ssh/sshd_config
    bash
     
    ChallengeResponseAuthentication yes
  • 重启 SSH 服务。

2.3 使用防火墙限制访问

2.3.1 限制允许的 IP

  • 配置防火墙仅允许特定 IP 登录:
    • 使用 UFW(Ubuntu):
      bash
       
      sudo ufw allow from <trusted_ip> to any port 22
      sudo ufw enable
    • 使用 Firewalld(CentOS/RHEL):
      bash
       
      sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="<trusted_ip>" port port="22" protocol="tcp" accept'
      sudo firewall-cmd --reload

2.3.2 使用 Fail2Ban 防护

  • Fail2Ban 会自动检测多次失败登录并禁用攻击者 IP。

  • 安装并配置:

    bash
     
    sudo apt install fail2ban
  • 配置 /etc/fail2ban/jail.local

    text
     
    [sshd]
    enabled = true
    port = 2222
    logpath = /var/log/auth.log
    maxretry = 5
    bantime = 600
  • 启动服务:

    bash
     
    sudo systemctl enable fail2ban
    sudo systemctl start fail2ban
  • 查看被封禁的 IP:

    bash
     
    sudo fail2ban-client status sshd

2.4 日志监控与告警

2.4.1 配置日志告警

  • 使用工具(如 Logwatch)生成每日登录报告:
    bash
     
    sudo apt install logwatch
    sudo logwatch --detail High --mailto <your_email>

2.4.2 实时告警

  • 使用 swatch 实现实时监控和告警:
    • 安装 Swatch:
      bash
       
      sudo apt install swatch
    • 配置 Swatch 规则:
      bash
       
      echo "watchfor /Failed password/" > /etc/swatch.conf
      echo "exec /usr/bin/mail -s 'SSH Attack Alert' your_email@example.com" >> /etc/swatch.conf
    • 启动 Swatch:
      bash
       
      swatch --config-file=/etc/swatch.conf --tail-file=/var/log/auth.log

2.5 系统加固与最佳实践

  1. 定期更新系统与软件

    • 确保操作系统和 OpenSSH 版本为最新,修复已知漏洞:
      bash
       
      sudo apt update && sudo apt upgrade
  2. 启用 SELinux 或 AppArmor

    • 使用强制访问控制(SELinux/AppArmor)限制 SSH 服务的权限。
  3. 监控异常行为

    • 使用 ps, top, 或 netstat 监控异常的 SSH 连接:
      bash
       
      netstat -tnp | grep ':22'

3. 总结与防御策略

安全措施 操作方法
修改默认 SSH 配置 更改端口、禁用 root 登录、限制登录用户。
使用强认证机制 启用 SSH 密钥认证或双因素认证(2FA)。
配置防火墙 限制允许的 IP 地址,阻止未授权访问。
安装暴力破解防护工具 使用 Fail2Ban 检测并封禁攻击者 IP。
定期审查日志与配置告警 分析 SSH 日志,启用 Logwatch 或 Swatch 实时告警。
系统加固与更新 定期更新系统和 OpenSSH,启用 SELinux 或 AppArmor 进行保护。

通过以上防御措施,可以有效抵御 SSH 暴力破解攻击,确保服务器的安全性和可用性。

posted @ 2025-09-04 15:22  网硕互联  阅读(223)  评论(0)    收藏  举报