2

痛心:实验室服务器被挖矿怎么办? - Wan-deuk

 2 years ago
source link: https://www.cnblogs.com/wan-deuk/p/16156437.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

痛心:实验室服务器被挖矿怎么办?

实验室的服务器有比较高的配置,安装了多块显卡,平时实验室的同学主要通过PuTTY、Xshell以及MobaXterm等工具远程访问服务器,上传代码跑实验。过去有段时间,服务器的显卡总是被挖矿程序占用,学校信息中心的老师告知实验室存在网络攻击行为,实验室因此被临时断网了数天,大家的科研学习都受到了影响。上述情况重复了好几次,大家逐渐意识到网络安全的重要性,开始探讨增强实验室服务器安全性的方案,致力于让服务器免受网络攻击。本文主要记录通过一系列配置提高服务器安全性的过程。

更改SSH配置

SSH服务的默认端口是22,是许多端口扫描工具预定义的端口列表中的一项。此外,系统中的常见用户,比如root、admin等,是网络黑客重点关照的对象。通过调整SSH的配置项,更改默认端口,禁止具有执行特权指令的用户远程登录,可以有效提高服务器的安全性。

  • 更改默认端口:22 -> xxxxx

    编辑SSH的配置文件”/etc/ssh/sshd_config“,更改默认端口为xxxx(1024~65535区间内的端口):

    # vi /etc/ssh/sshd_config
    
    Include /etc/ssh/sshd_config.d/*.conf
    
    Port xxxxx
    #AddressFamily any
    #ListenAddress 0.0.0.0
    #ListenAddress ::
    

    更改端口后,通过ssh命令登录服务器需要指定端口参数:ssh -p xxxxx username@hostname

  • 禁止root用户登录:”PermitRootLogin no“

    # vi /etc/ssh/sshd_config
    
    #LoginGraceTime 2m
    PermitRootLogin no 
    #StrictModes yes
    #MaxAuthTries 6
    #MaxSessions 10
    
  • 禁止其他用户登录:”DenyUsers root admin“

    # vi /etc/ssh/sshd_config
    
    # Example of overriding settings on a per-user basis
    #Match User anoncvs
    #       X11Forwarding no
    #       AllowTcpForwarding no
    #       PermitTTY no
    #       ForceCommand cvs server
    DenyUsers root admin 
    
  • 设置通过密码进行安全验证

    SSH提供了两种级别的验证方法:基于口令的安全验证和基于密钥的安全验证。其中,基于密钥的安全验证安全性更高,但由于操作起来比较麻烦(每个用户都要创建一对密钥,需要保管私钥文件),实验室没有采用这一方案。

限制IP访问

通过编辑”/etc/hosts.deny“和”/etc/hosts.allow“两个文件,可以实现限制访问系统的主机的功能(比如限制只能局域网内的主机访问系统)。其中,”/etc/hosts.allow“中的规则优先级更高,当两个文件中定义的规则冲突时,以”/etc/hosts.allow“中的为准。

  1. 禁止所有主机通过SSH连接服务器

    # vi /etc/hosts.deny
    
    # You may wish to enable this to ensure any programs that don't
    # validate looked up hostnames still leave understandable logs. In past
    # versions of Debian this has been the default.
    # ALL: PARANOID
    sshd:ALL
    
  2. 允许同一网段的主机连接服务器

    # If you're going to protect the portmapper use the name "rpcbind" for the
    # daemon name. See rpcbind(8) and rpc.mountd(8) for further information.
    #
    sshd:192.168.1.*:allow
    sshd:127.0.0.1:allow
    

设置Linux-PAM

PAM(Pluggable Authentication Module)是Linux提供的鉴权模块,通过编辑该模块的配置文件”/etc/pam.d/sshd“,可以实现限制SSH尝试登录次数,防止暴力破解的功能。

  1. 编辑”/etc/pam.d/sshd“

    设置SSH尝试登录失败3次后就锁定相应用户1小时(3600秒),用户被锁定期间,哪怕提供了正确的口令,也不能登录系统。注意配置的内容需要加到文件的起始位置。

    # vi /etc/pam.d/sshd
    
    # PAM configuration for the Secure Shell service
    auth required pam_tally2.so deny=3 unlock_time=3600 even_deny_root root_unlock_time=3600
    
  2. 查看被锁定的用户,清除锁定信息

    # 查看被锁定的用户
    pam_tally2 -u  
    
    Login           Failures Latest failure     From
    xxx                 6    04/17/22 16:25:47  192.168.1.xxx
    
    # 清除锁定信息
    pam_tally2 -r
    

开启防火墙

防火墙是另一项能够有效提高系统安全性的技术。实验室主要通过使用”firewall-cmd“命令来配置端口的开放和关闭,做好端口防护工作。由于网上的相关资料比较多,这里就不展开介绍了。

使用”journalctl“命令可以查看近期登录系统的日志记录:

```bash
journalctl -u sshd --since "2022-04-10" | grep password

Apr 12 15:47:32 Zjqgnx903p9xumgkh1cv2Zi sshd[8536]: Failed password for root from 137.184.224.xxx port 50748 ssh2
Apr 12 15:47:37 Zjqgnx903p9xumgkh1cv2Zi sshd[8538]: Failed password for root from 137.184.224.xxx port 58468 ssh2
Apr 12 15:47:44 Zjqgnx903p9xumgkh1cv2Zi sshd[8540]: Failed password for root from 137.184.224.xxx port 37956 ssh2
Apr 12 15:47:49 Zjqgnx903p9xumgkh1cv2Zi sshd[8542]: Failed password for root from 137.184.224.xxx port 45676 ssh2
Apr 12 15:47:55 Zjqgnx903p9xumgkh1cv2Zi sshd[8544]: Failed password for root from 137.184.224.xxx port 53396 ssh2
Apr 12 15:48:01 Zjqgnx903p9xumgkh1cv2Zi sshd[8546]: Failed password for root from 137.184.224.xxx port 32884 ssh2
...
Apr 15 08:36:28 Zjqgnx903p9xumgkh1cv2Zi sshd[13021]: Failed password for invalid user zx from 165.232.180.xxx port 48024 ssh2
Apr 15 08:36:33 Zjqgnx903p9xumgkh1cv2Zi sshd[13023]: Failed password for invalid user mvr from 165.232.180.xxx port 55306 ssh2
Apr 15 08:36:39 Zjqgnx903p9xumgkh1cv2Zi sshd[13025]: Failed password for invalid user ang from 165.232.180.xxx port 34356 ssh2
Apr 17 16:49:00 Zjqgnx903p9xumgkh1cv2Zi sshd[17028]: Accepted password for xxx from 221.10.55.xxx port 55762 ssh2
```

可以看到有大量尝试登录服务器的记录,做好服务器的安全防护,刻不容缓。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK