SSH 是遠端管理 Linux 伺服器的主要方式,其安全性至關重要。本文介紹如何透過停用 root 登入和密碼驗證,搭配金鑰交換和雙因素驗證,顯著提升 SSH 的安全性。同時,文章也探討如何設定和掃描 SSH 加密演算法,以符合 NIST 的 CNSA 標準,確保伺服器免受常見攻擊威脅。透過實作練習和詳細的步驟說明,讀者可以快速上手,強化自己的伺服器安全設定。
加強SSH安全:停用root登入與密碼驗證
強化SSH伺服器安全性
在面對日益增長的網路威脅時,強化SSH伺服器的安全性是保護伺服器免受攻擊的關鍵步驟。本文將探討如何透過停用root使用者登入和密碼驗證來提升SSH伺服器的安全性。
停用root使用者登入
預設情況下,許多Linux發行版允許root使用者透過SSH登入,這是一個巨大的安全風險。攻擊者可以利用暴力破解工具嘗試猜測root密碼,從而獲得伺服器的控制權。
設定步驟
- 編輯
/etc/ssh/sshd_config檔案。 - 找到
#PermitRootLogin yes或#PermitRootLogin prohibit-password行。 - 移除註解符號並將其設定為
PermitRootLogin no。 - 重新載入SSH服務以套用變更:
- 在CentOS/AlmaLinux上:
sudo systemctl reload sshd - 在Ubuntu上:
sudo systemctl reload ssh
- 在CentOS/AlmaLinux上:
停用密碼驗證
在設定好金鑰交換後,停用密碼驗證可以進一步提高安全性。這樣可以防止攻擊者使用暴力破解攻擊嘗試登入伺服器。
設定步驟
- 編輯
/etc/ssh/sshd_config檔案。 - 找到
#PasswordAuthentication yes行。 - 移除註解符號並將其設定為
PasswordAuthentication no。 - 重新載入SSH服務以套用變更。
實作練習:停用root登入和密碼驗證
練習步驟
- 在Ubuntu、CentOS或AlmaLinux 8伺服器虛擬機器上,編輯
sshd_config檔案。 - 將
#PasswordAuthentication yes修改為PasswordAuthentication no,並重新載入SSH服務。 - 將
#PermitRootLogin yes或#PermitRootLogin prohibit-password修改為PermitRootLogin no,並重新載入SSH服務。 - 從已設定金鑰對的客戶端嘗試登入伺服器虛擬機器。
- 從未設定金鑰對的客戶端嘗試登入伺服器虛擬機器(應該無法登入)。
啟用兩步驟驗證
兩步驟驗證提供了額外的安全層。透過安裝Google Authenticator PAM模組,可以要求使用者在登入時輸入一次性密碼。
設定步驟
- 在Ubuntu 22.04伺服器上安裝Google Authenticator PAM模組。
- 設定Google Authenticator以供本地登入和sudo使用。
在Ubuntu 22.04上設定雙因素驗證的實作
雙因素驗證(2FA)是增強系統安全性的重要手段。本將介紹如何在Ubuntu 22.04上設定雙因素驗證。
步驟1:安裝Google Authenticator應用程式
首先,在您的智慧手機上下載並安裝Google Authenticator應用程式。該應用程式可在Android和iOS的應用商店中找到。
步驟2:在Ubuntu上安裝libpam-google-authenticator套件
在Ubuntu虛擬機器上,執行以下命令安裝libpam-google-authenticator套件:
sudo apt install libpam-google-authenticator
步驟3:組態Google Authenticator
使用SSH遠端登入Ubuntu虛擬機器,並執行以下命令:
google-authenticator
此命令將生成一個QR碼,請使用您的智慧手機上的Google Authenticator應用程式掃描該QR碼。
內容解密:
google-authenticator命令用於初始化Google Authenticator組態。- 掃描QR碼後,您的智慧手機上將出現一個新的驗證碼。
步驟4:組態PAM以使用Google Authenticator
編輯 /etc/pam.d/common-auth 檔案,並在檔案頂部新增以下行:
auth required pam_google_authenticator.so
此組態將啟用雙因素驗證,用於本地登入和sudo操作。
內容解密:
auth required pam_google_authenticator.so表示需要Google Authenticator驗證。- 將此行新增到檔案頂部可確保在其他驗證方法之前進行雙因素驗證。
步驟5:測試雙因素驗證
登出並重新登入本地終端,系統將提示您輸入驗證碼。執行需要sudo許可權的命令時,也需要輸入驗證碼。
步驟6:組態SSH以使用雙因素驗證
編輯 /etc/ssh/sshd_config 檔案,將 KbdInteractiveAuthentication no 改為 KbdInteractiveAuthentication yes,並重新載入SSH組態:
sudo systemctl reload ssh
現在,透過SSH遠端登入Ubuntu虛擬機器時,系統將提示您輸入驗證碼。
步驟7:最佳化雙因素驗證組態
如果您的組織只需要對遠端SSH登入進行雙因素驗證,可以編輯 /etc/pam.d/sshd 檔案,並在 @include common-auth 行下方新增 auth required pam_google_authenticator.so。
內容解密:
- 將
auth required pam_google_authenticator.so新增到/etc/pam.d/sshd檔案中,可確保只有透過SSH遠端登入的使用者需要進行雙因素驗證。
將Google Authenticator與金鑰交換結合使用
要實作根據金鑰的身份驗證和Google Authenticator驗證,請在 /etc/ssh/sshd_config 檔案中新增以下行:
AuthenticationMethods publickey,keyboard-interactive:pam
重新載入SSH組態後,您將只能從設定了金鑰交換的機器登入,並且需要進行雙因素驗證。
內容解密:
AuthenticationMethods publickey,keyboard-interactive:pam表示需要金鑰身份驗證和Google Authenticator驗證。- 這種組態提供了三因素驗證,增強了系統安全性。
設定安全殼層(SSH)以使用雙重驗證及金鑰交換
實作實驗室:Ubuntu 22.04 上的雙重驗證設定
首先,安裝必要的 PAM 模組:
sudo apt install libpam-google-authenticator執行
google-authenticator命令以設定雙重驗證:google-authenticator按照提示完成設定,並掃描產生的 QR 碼以組態您的身份驗證器應用程式。
編輯
/etc/pam.d/sshd檔案,在檔案頂部找到@include common-auth行,並將其註解掉:#@include common-auth在
/etc/ssh/sshd_config檔案中,將ChallengeResponseAuthentication設定為yes:ChallengeResponseAuthentication yes重新載入或重啟 SSH 服務:
sudo systemctl reload sshd
內容解密:
- 安裝必要的 PAM 模組:使用
apt安裝libpam-google-authenticator,這是 Google Authenticator 的 PAM 模組。 - 執行
google-authenticator命令:該命令會建立一個新的金鑰並產生一個 QR 碼,用於在您的手機上設定身份驗證器應用程式。 - 編輯
/etc/pam.d/sshd檔案:註解掉@include common-auth行,以停用密碼登入。 - 設定
ChallengeResponseAuthentication:將其設定為yes以啟用挑戰-回應驗證機制,這是雙重驗證的必要條件。 - 重新載入或重啟 SSH 服務:使變更生效。
在 AlmaLinux 8 上設定雙重驗證
- 安裝 EPEL 儲存函式庫和 Google Authenticator PAM 模組:
sudo dnf install epel-release sudo dnf install google-authenticator qrencode-libs - 從主機機器的 GUI 終端機,使用 SSH 遠端登入到 AlmaLinux 8 虛擬機器,並執行
google-authenticator命令:google-authenticator -s ~/.ssh/google_authenticator - 編輯
/etc/pam.d/sshd檔案,並在檔案底部新增以下行:auth required pam_google_authenticator.so secret=/home/${USER}/.ssh/google_authenticator - 在
/etc/ssh/sshd_config檔案中,將ChallengeResponseAuthentication設定為yes,並重新載入 SSH 組態。
內容解密:
- 安裝 EPEL 儲存函式庫和 Google Authenticator PAM 模組:由於 AlmaLinux 預設儲存函式庫中沒有 Google Authenticator,因此需要啟用 EPEL 儲存函式庫。
google-authenticator命令:這次將google_authenticator檔案存放在.ssh目錄下,以避免 SELinux 的限制。- 編輯
/etc/pam.d/sshd和/etc/ssh/sshd_config檔案:新增必要的組態以啟用 Google Authenticator。
使用金鑰交換與雙重驗證
- 將主機機器的公鑰傳輸到 AlmaLinux 8 虛擬機器。
- 編輯
/etc/ssh/sshd_config檔案,將PasswordAuthentication設定為no,並新增以下行以啟用金鑰交換和雙重驗證:AuthenticationMethods publickey,password publickey,keyboard-interactive
內容解密:
- 傳輸公鑰:使用 SSH 金鑰交換機制,將公鑰從主機機器傳輸到 AlmaLinux 虛擬機器。
PasswordAuthentication設定為no:停用密碼登入,只允許金鑰交換。AuthenticationMethods設定:啟用金鑰交換和雙重驗證,實作三重驗證機制。
組態 SSH 使用強加密演算法
目前的 NIST 建議
根據目前的 CNSA Suite,建議使用更強的演算法和更長的金鑰。具體建議如下表所示:
| 演算法 | 用途 |
|---|---|
| RSA,3,072 位元或以上 | 金鑰建立和數位簽章 |
| Diffie-Hellman (DH),3,072 位元或以上 | 金鑰建立 |
| ECDH with NIST P-384 | 金鑰建立 |
| ECDSA with NIST P-384 | 數位簽章 |
| SHA-384 | 資料完整性 |
| AES-256 | 保密性 |
圖表翻譯:
此圖示呈現了 CNSA Suite 所建議的加密演算法及其用途,包括 RSA、Diffie-Hellman、ECDH、ECDSA、SHA-384 和 AES-256。這些演算法用於確保 SSH 連線的安全性和完整性。
瞭解 SSH 加密演算法
SSH 結合對稱和非對稱加密技術,類別似於 TLS。首先,SSH 使用者端使用公鑰方法與伺服器建立非對稱會話。然後,雙方協商並交換一個秘密金鑰,用於建立對稱會話。
圖表翻譯:
圖表翻譯: 此圖示展示了 SSH 使用者端和伺服器之間的加密連線建立過程,包括使用公鑰方法建立非對稱會話,以及協商秘密金鑰以建立對稱會話。
SSH 加密演算法的設定與掃描
要確保 SSH 連線的安全性,我們需要在伺服器端設定四類別加密演算法:Ciphers、HostKeyAlgorithms、KexAlgorithms 和 MAC。這些演算法負責加密客戶端和伺服器之間的資料交換、主機金鑰型別、對稱金鑰交換和訊息驗證碼。
檢視支援的加密演算法
首先,我們需要檢視 sshd_config 的 man 頁面,以瞭解支援的加密演算法。以下是以 CentOS 7 為例的內容:
支援的 Ciphers
- 3des-cbc
- aes128-cbc
- aes192-cbc
- aes256-cbc
- aes128-ctr
- aes192-ctr
- aes256-ctr
- aes128-gcm@openssh.com
- aes256-gcm@openssh.com
- arcfour
- arcfour128
- arcfour256
- blowfish-cbc
- cast128-cbc
- chacha20-poly1305@openssh.com
預設啟用的 Ciphers
- chacha20-poly1305@openssh.com
- aes128-ctr, aes192-ctr, aes256-ctr
- aes128-gcm@openssh.com, aes256-gcm@openssh.com
- aes128-cbc, aes192-cbc, aes256-cbc
- blowfish-cbc, cast128-cbc, 3des-cbc
HostKeyAlgorithms
- ecdsa-sha2-nistp256-cert-v01@openssh.com
- ecdsa-sha2-nistp384-cert-v01@openssh.com
- ecdsa-sha2-nistp521-cert-v01@openssh.com
- ssh-ed25519-cert-v01@openssh.com
- ssh-rsa-cert-v01@openssh.com
- ssh-dss-cert-v01@openssh.com
- ecdsa-sha2-nistp256, ecdsa-sha2-nistp384, ecdsa-sha2-nistp521
- ssh-ed25519, ssh-rsa, ssh-dss
KexAlgorithms
支援的演算法包括:
curve25519-sha256
curve25519-sha256@libssh.org
diffie-hellman-group1-sha1
diffie-hellman-group14-sha1
diffie-hellman-group-exchange-sha1
diffie-hellman-group-exchange-sha256
ecdh-sha2-nistp256
ecdh-sha2-nistp384
ecdh-sha2-nistp521
預設啟用的演算法包括:
curve25519-sha256, curve25519-sha256@libssh.org
ecdh-sha2-nistp256, ecdh-sha2-nistp384, ecdh-sha2-nistp521
diffie-hellman-group-exchange-sha256
diffie-hellman-group14-sha1
diffie-hellman-group1-sha1
MAC 演算法
預設啟用的 MAC 演算法包括:
umac-64-etm@openssh.com, umac-128-etm@openssh.com
hmac-sha2-256-etm@openssh.com, hmac-sha2-512-etm@openssh.com
hmac-sha1-etm@openssh.com
umac-64@openssh.com, umac-128@openssh.com
hmac-sha2-256, hmac-sha2-512, hmac-sha1
使用 ssh -Q 命令檢視支援的演算法
可以使用以下命令檢視支援的演算法:
ssh -Q cipher
ssh -Q key
ssh -Q kex
ssh -Q mac
掃描啟用的 SSH 演算法
可以使用以下兩種方法掃描 SSH 伺服器:
- 使用 SSHCheck 網站(https://sshcheck.com/)
- 使用 Nmap 指令碼引擎(需要安裝 nmap 套件)
使用 Nmap 掃描 SSH 伺服器
# 在 Ubuntu 上安裝 nmap
sudo apt update
sudo apt install nmap
# 在 CentOS 7 上安裝 nmap
sudo yum install nmap
# 在 AlmaLinux 8 或 9 上安裝 nmap
sudo dnf install nmap
# 使用 nmap 掃描 SSH 伺服器(需要替換 IP 地址)
nmap --script=ssh2-enum-algos.nse 192.168.0.14
# 如果防火牆阻擋了掃描,可以使用 -Pn 開關繞過防火牆檢測 Ping 請求包,繼續進行掃描。
nmap -Pn --script=ssh2-enum-algos.nse 192.168.0.14
# 將掃描結果儲存到文字檔案中。
nmap --script=ssh2-enum-algos.nse 192.168.0.14 -oN ubuntuscan.txt
# 開啟文字檔案檢視掃描結果。
練習實驗:使用 Nmap 掃描 SSH 伺服器
- 安裝 nmap 套件。
- 使用 nmap 和 ssh2-enum-algos.nse 指令碼掃描 SSH 伺服器。
- 將掃描結果儲存到文字檔案中。
- 開啟文字檔案檢視掃描結果,並與相關標準(如 NIST 的 CNSA 標準)進行比較。
- 使用 Shodan 網站(https://www.shodan.io)搜尋 Internet 導向的 SSH 伺服器,並觀察其啟用的演算法。
圖表翻譯:
此圖示呈現了SSH加密演算法設定的流程,包括設定Ciphers、HostKeyAlgorithms、KexAlgorithms和MAC等步驟。
@startuml
skinparam backgroundColor #FEFEFE
skinparam componentStyle rectangle
title SSH 安全強化停用Root登入與密碼驗證
package "安全架構" {
package "網路安全" {
component [防火牆] as firewall
component [WAF] as waf
component [DDoS 防護] as ddos
}
package "身份認證" {
component [OAuth 2.0] as oauth
component [JWT Token] as jwt
component [MFA] as mfa
}
package "資料安全" {
component [加密傳輸 TLS] as tls
component [資料加密] as encrypt
component [金鑰管理] as kms
}
package "監控審計" {
component [日誌收集] as log
component [威脅偵測] as threat
component [合規審計] as audit
}
}
firewall --> waf : 過濾流量
waf --> oauth : 驗證身份
oauth --> jwt : 簽發憑證
jwt --> tls : 加密傳輸
tls --> encrypt : 資料保護
log --> threat : 異常分析
threat --> audit : 報告生成
@enduml圖表翻譯: 此圖示詳細展示了設定SSH加密演算法的步驟順序,從開始到結束,涵蓋了所有必要的組態與檢查步驟。