痛心:實驗室服務器被黑挖礦怎么辦?
實驗室的服務器有比較高的配置,安裝了多塊顯卡,平時實驗室的同學主要通過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“中的為准。
-
禁止所有主機通過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
-
允許同一網段的主機連接服務器
# vi /etc/hosts.allow # 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嘗試登錄次數,防止暴力破解的功能。
-
編輯”/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
-
查看被鎖定的用戶,清除鎖定信息
# 查看被鎖定的用戶 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
```
可以看到有大量嘗試登錄服務器的記錄,做好服務器的安全防護,刻不容緩。