痛心:實驗室伺服器被挖礦怎麼辦?

痛心:實驗室伺服器被挖礦怎麼辦?

實驗室的伺服器有比較高的配置,安裝了多塊顯示卡,平時實驗室的同學主要通過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
```

可以看到有大量嘗試登錄伺服器的記錄,做好伺服器的安全防護,刻不容緩。

參考資料

  1. SSH簡介及兩種遠程登錄的方法
  2. SSH之hosts.allow和hosts.deny文件
  3. Linux限制ssh登錄次數,防止暴力破解
  4. firewall-cmd命令詳解
  5. linux journalctl 命令