关于“CentOS被尝试登陆”问题

来自Wikioe
Eijux讨论 | 贡献2022年4月29日 (五) 20:43的版本 (创建页面,内容为“category:Linux == 关于 == 【时间:2022/04/29 19:34:40】 使用 XShell 登录 CentOS 时,注意到提示在过去的一个月,服务器被尝试登录了将近十万次!!! 提示内容如下: : <syntaxhighlight lang="bash" highlight="12-13"> Xshell 7 (Build 0056) Copyright (c) 2020 NetSarang Computer, Inc. All rights reserved. Type `help' to learn how to use Xshell prompt. [C:\~]$ Connecting to **.**.***.**:**... Connection estab…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索


关于

【时间:2022/04/29 19:34:40】

使用 XShell 登录 CentOS 时,注意到提示在过去的一个月,服务器被尝试登录了将近十万次!!!


提示内容如下:

Xshell 7 (Build 0056)
Copyright (c) 2020 NetSarang Computer, Inc. All rights reserved.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ 

Connecting to **.**.***.**:**...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last failed login: Fri Apr 29 09:58:48 UTC 2022 from 49.248.77.234 on ssh:notty
There were 98858 failed login attempts since the last successful login.
Last login: Mon Mar 14 15:09:10 2022 from ***.**.**.**
[root@newjersey ~]#
最近一次尝试登录的是“49.248.77.234”(印度—马哈拉施特拉邦—浦那)

解决方案

There were xxx failed login attempts since the last successful login.
很明显:系统正在遭受密码暴力破解的风险。

虽然设置的密码没有撞库的风险,但是,暴力破解的一次次尝试登录着实给服务器使用带来了影响。

通过查找资料,了解了几种解决方案:

  1. 使用 SSH 密钥登录,并禁止密码登录。【建议】
  2. 修改 SSH 端口
    • 虽然这比传统的安全加固方式有一定难度,但它可以再一定程度上抵御一些简单的密码暴力破解脚本。
    • 但是高级的攻击方式会扫描系统所有开放端口,并很方便地找到修改后的 SSHD 端口。
  3. 手动禁用登陆失败的 IP 地址
  4. 安装并启用 fail2ban
    • fail2ban 可对使用 SSH 多次失败登录的自动检测,以便阻止重复尝试。


使用 SSH 密钥登录

修改 SSH 端口

默认情况下,SSH 侦听端口 22。

更改默认 SSH 端口会通过降低自动攻击的风险为您的服务器增加额外的安全层。

步骤:

  1. 防火墙开启端口:
    1. 如果使用 firewalld
      firewall-cmd --permanent --zone=public --add-port=5522/tcp
      firewall-cmd --reload
      
      • CentOS 还需要调整 SELinux 规则:
        semanage port -a -t ssh_port_t -p tcp 5522
        
    2. 如果使用 iptables
      iptables -A INPUT -p tcp --dport 5522 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
      
    3. 如果使用 UFW(Ubuntu 的默认防火墙配置工具):
      ufw allow 5522/tcp
      
    • 在 Linux 中,1024 以下的端口号是为知名服务保留的,只能由 root 绑定:
      虽然可以为 SSH 服务使用 1-1024 范围内的端口,以避免以后出现端口分配问题,但建议选择 1024 以上的端口。(如上:5522)
  2. 配置 SSH:
    [root@newjersey ~]# cat /etc/ssh/sshd_config
    #	$OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
    
    # This is the sshd server system-wide configuration file.  See
    # sshd_config(5) for more information.
    
    # This sshd was compiled with PATH=/usr/local/bin:/usr/bin
    
    # The strategy used for options in the default sshd_config shipped with
    # OpenSSH is to specify options with their default value where
    # possible, but leave them commented.  Uncommented options override the
    # default value.
    
    # If you want to change the port on a SELinux system, you have to tell
    # SELinux about this change.
    # semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
    #
    #Port 22
    Port 5522
    
    ...
    
    • 修改 SSH 配置文件时要格外小心,配置错误可能导致 SSH 服务无法启动。
  3. 重启 SSH 服务:
    systemctl restart sshd
    
    • 在 CentOS 中,服务为“sshd”;在其他系统中,服务为“ssh”。
  4. 验证 SSH 守护程序是否正在侦听 5522:
    [root@newjersey ~]# ss -an | grep 5522
    tcp   LISTEN      0        128            0.0.0.0:5522           0.0.0.0:*
    tcp   ESTAB       0        0      192.168.121.108:5522     192.168.121.1:57638
    tcp   LISTEN      0        128               [::]:5522              [::]:*
    
    • 要指定端口,请调用“ssh”后跟“-p <port_number>”选项的命令:
      ssh -p 5522 username@remote_host_or_ip
      

手动禁用登陆失败的 IP 地址

步骤:

  1. 查看登陆失败的 IP 地址:
    [root@newjersey ~]# grep "Failed password for invalid user" /var/log/secure | awk '{print $13}' | sort | uniq -c | sort -nr
        335 64.94.87.153
        223 45.9.20.25
        198 163.239.13.33
        171 167.172.46.113
        171 159.65.27.49
        135 92.255.85.135
        118 49.81.17.200
        118 114.228.91.16
        115 92.255.85.237
        111 179.43.154.185
         76 103.150.55.85
         60 142.93.133.12
         50 43.154.89.207
         50 213.81.201.121
         49 52.163.248.162
         49 50.116.3.96
         49 43.156.248.251
    [root@newjersey ~]#
    
    太他妈多了……
  2. 将 IP 加入服务器访问限制名单:
    [root@newjersey ~]# cat /var/log/secure |  grep "Failed password for invalid user" | awk '{print $13}' | sort | uniq -c | sort -n | tail -10 |awk '{print "sshd:"$2":deny"}' >> /etc/hosts.allow
    
    [root@newjersey ~]# tail -20 /etc/hosts.allow
    #
    # hosts.allow   This file contains access rules which are used to
    #               allow or deny connections to network services that
    #               either use the tcp_wrappers library or that have been
    #               started through a tcp_wrappers-enabled xinetd.
    #
    #               See 'man 5 hosts_options' and 'man 5 hosts_access'
    #               for information on rule syntax.
    #               See 'man tcpd' for information on tcp_wrappers
    #
    sshd:43.156.248.251:deny
    sshd:50.116.3.96:deny
    sshd:52.163.248.162:deny
    sshd:213.81.201.121:deny
    sshd:43.154.89.207:deny
    sshd:142.93.133.12:deny
    sshd:103.150.55.85:deny
    sshd:179.43.154.185:deny
    sshd:92.255.85.237:deny
    sshd:114.228.91.16:deny
    sshd:49.81.17.200:deny
    sshd:92.255.85.135:deny
    sshd:159.65.27.49:deny
    sshd:167.172.46.113:deny
    sshd:163.239.13.33:deny
    sshd:45.9.20.25:deny
    sshd:64.94.87.153:deny
    

安装并启用 fail2ban

注意事项:
fail2ban 内置在 Plesk/cPanel 中的,它会配置它使用 iptables 而不是 firewalld。
如果使用 Plesk/cPanel,请改用本指南启用它

步骤:

  1. 在 CentOS 7/8 上使用 firewalld 安装和配置 fail2ban:
    dnf install epel-release
    dnf install fail2ban
    echo '
    [sshd] 
    enabled = true
    ' > /etc/fail2ban/jail.local
    systemctl restart fail2ban
    
    • dnf:新一代的 RPM 软件包管理器。
  2. fail2ban 使用:
    1. 查看 fail2ban 状态:(可以使用具有不同详细程度的各种选项)
      systemctl status fail2ban
      fail2ban-client status
      fail2ban-client status sshd
      
    2. 如果IP被禁止,通过下面的方式取消禁止 IP:
      fail2ban-client unban 192.168.56.1