ssh暴力破解解决ssh白名单及防火墙设置

从你的 journalctl -u sshd -f 日志来看,这些日志主要显示的是来自 外部 IP 对你的服务器 SSH 端口的连接尝试,并不属于你服务器自身的错误,而是一些典型的 暴力破解扫描行为(brute-force scan)探测行为。下面是每条日志的含义解释:


🔍 日志分析:

1. 连接尝试然后关闭

1
2
May 23 13:40:13 ... sshd[42391]: Connection from 165.154.168.234 port 53228 on 10.0.0.4 port 22
May 23 13:40:18 ... sshd[42391]: Connection closed by 165.154.168.234 port 53228 [preauth]
  • 说明:这个 IP 连接到了你的 SSH 端口,然后马上关闭连接了。
  • 状态:可能是扫描器探测,没有进行登录尝试。

2. 暴力破解尝试

1
2
3
4
May 23 13:40:52 ... sshd[42494]: Connection from 223.197.125.110 port 54040
May 23 13:40:53 ... pam_unix(sshd:auth): authentication failure ... user=root
May 23 13:40:54 ... sshd[42494]: Failed password for root from 223.197.125.110
May 23 13:40:56 ... sshd[42494]: Received disconnect ... Bye Bye [preauth]
  • 说明:这个 IP 尝试用 root 用户登录,密码验证失败。
  • 状态:这是典型的暴力破解攻击。

3. 连接后立刻断开

1
2
3
May 23 13:41:21 ... sshd[42588]: Connection from 193.32.162.185 ...
May 23 13:41:22 ... error: kex_exchange_identification: Connection closed by remote host
May 23 13:41:22 ... Connection closed by 193.32.162.185 ...
  • 说明:连接建立后,尚未完成密钥交换就被远程主机关闭。
  • 状态:也是扫描器或探测器的行为。

🚨 总结

这些日志表明你的服务器暴露在公网,正在被多个 IP 地址扫描或尝试暴力破解 SSH 登录。


✅ 建议的安全措施:

  1. 禁止 root 登录:

    1
    2
    3
    4
    5
    6
    # 编辑 SSH 配置
    vi /etc/ssh/sshd_config
    # 设置:
    PermitRootLogin no
    # 然后重启 SSH 服务
    systemctl restart sshd
  2. 开启防火墙限制 SSH IP:

    • ufw
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      # 1. 默认拒绝所有人访问 SSH(先清除现有规则)
      sudo ufw deny 22/tcp

      # 2. 允许指定 IP 访问 SSH
      sudo ufw allow from 203.0.113.10 to any port 22 proto tcp

      # 3. 启用 ufw(如果没启用过)
      sudo ufw enable

      # 4. 查看规则确认
      sudo ufw status numbered

    • iptables
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      # 1. 先清除之前可能的规则(仅SSH相关)
      iptables -D INPUT -p tcp --dport 22 -j ACCEPT || true

      # 2. 允许指定 IP 访问端口 22
      iptables -A INPUT -p tcp -s 203.0.113.10 --dport 22 -j ACCEPT

      # 3. 拒绝所有其他 IP 的 SSH 连接
      iptables -A INPUT -p tcp --dport 22 -j DROP

      # 4. 保存规则(CentOS 7)
      service iptables save

      # 或者 Ubuntu:
      iptables-save > /etc/iptables/rules.v4

    • 只允许特定 IP 登录 SSH(如果你有固定 IP):
      1
      2
      3
      firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="你的IP地址" port protocol="tcp" port="22" accept'
      firewall-cmd --permanent --remove-service=ssh
      firewall-cmd --reload

    或者开启root密码登录并限制 AllowUsers

    1
    2
    3
    4
    5
    6
    7
    8
    9
    开启root密码登录并限制 AllowUsers
    # 查看访问本机的公网地址使用
    curl ifconfig.me
    # 首先为 root 设置密码 后允许root用户登录
    vim /etc/ssh/sshd_config
    PermitRootLogin yes
    AllowUsers root@127.0.0.1/32 root@本机所在的外网网段/31 root@内网ip段

    systemctl restart ssh
  3. 使用非标准端口(变更默认22端口):

    1
    2
    3
    4
    # 修改配置
    vi /etc/ssh/sshd_config
    Port 22222 # 改为你自定义的端口
    systemctl restart sshd
  4. 使用 Fail2Ban 自动封锁暴力破解 IP:

    1
    2
    3
    yum install -y epel-release fail2ban
    yum install -y fail2ban
    systemctl enable --now fail2ban
  5. 使用跳板机或 VPN 隐藏 SSH 接口