升級OpenSSH7.9
背景
企業資訊保安管理日趨完善,部分軟體專案須經過系統定級、備案,通過第三方等級保護測評,出具《資訊保安等級保護測評報告》,才能符合驗收條件。
就是一般所說的“入網安評”。
測評一定會發現漏洞和隱患,需要軟體廠商或者企業運維團隊針對已發現的問題清單逐一整改和加固。
一般來說,第三方安全廠商通過漏洞掃描和入侵探測,出具詳細的問題清單,每條清單也會有相應的整改措施或者加固建議,然後,我們就照著清單,逐條解決。
都是些套路,本沒啥驚喜——昨天卻因為OpenSSH搞得一頭汗。
清單上有一項:
危險等級:中危
漏洞名稱:OpenSSH 使用者列舉漏洞(CVE-2018-15919)
詳細描述:OpenSSH 7.8及之前版本,auth-gss2.c
檔案存在安全漏洞。遠端攻擊者可利用該漏洞檢測其指定的使用者是否存在。
解決辦法:
廠商升級:新版本OpenSSH-7.9已經修復這個安全問題,請到廠商的主頁下載。
連結:http://www.openssh.com/ http://www.openssh.com/portable.html
目標伺服器是CentOS7.4,起初我以為萬能的RPM包再加上yum install --downloadonly --downloaddir=<directory> <package>
和yum localinstall <package>
就能輕鬆搞定……但是……
最新的RPM是7.4p1,不符合要求
[root@localhost ~]# cat /etc/centos-release CentOS Linux release 7.4.1708 (Core) [root@localhost ~]# yum info openssh Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.cqu.edu.cn * updates: mirrors.aliyun.com Available Packages Name: openssh Arch: x86_64 Version: 7.4p1
目標機器與網際網路隔離,如果RPM包不可用,只能用原始碼編譯,如果編譯遇到檔案缺失等問題,會比較繁瑣。
多方尋找7.9p1+CentOS7的RPM資源,未果,只能編譯了。
嘗試原始碼編譯
# 解除安裝7.4 yum uninstall openssh # 安裝編譯所需的headers和libraries yum install zlib-devel yum install openssl-devel yum install pam-devel # 下載原始碼包 wget https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-7.9p1.tar.gz # 解壓 tar zxvf openssh-7.9p1.tar.gz # 切目錄 cd openssh-7.9p1 # 配置 ./configure --prefix=/usr --sysconfdir=/etc/ssh --with-pam # 編譯 make # 安裝 make install # 拷貝ssh-copy-id install -v -m755 contrib/ssh-copy-id /usr/bin # 拷貝幫助檔案 install -v -m644 contrib/ssh-copy-id.1 /usr/share/man/man1 # 建立文件目錄 install -v -m755 -d /usr/share/doc/openssh-7.9p1 # 拷貝文件 install -v -m644 INSTALL LICENCE OVERVIEW README* /usr/share/doc/openssh-7.9p1 # 允許root登入(7.9改變了PermitRootLogin的預設值) echo "PermitRootLogin yes" >> /etc/ssh/sshd_config # 使用PAM echo "UserPAM yes" >> /etc/ssh/sshd_config # 註冊服務 cp -p contrib/redhat/sshd.init /etc/init.d/sshd chmod +x /etc/init.d/sshd # 加入sshd到chkconfig管理 chkconfig --add sshd # 設定開機啟動 chkconfig sshd on # 檢查啟動項 chkconfig --list sshd # 驗證版本資訊 ssh -V # 重啟ssh服務 service sshd restart
如果一切順利,客戶機能夠通過ssh
登入伺服器。
可能遇到的錯誤和解決方法
在安裝過程中我遇到過多個不同的錯誤,在總結這篇短文時,才發現那些都是彎路。
ssh
連線時出錯:error Could not get shadow information for <user>
服務啟動成功,使用者密碼也都對,就是無法建立連線,可能是UsePAM
和SELinux
的問題。
客戶端登入時,即便輸入了正確的密碼,仍然提示:
[user@localhost~]# ssh [email protected] [email protected]'s password: Permission denied, please try again.
檢視服務端日誌(/var/log/messages
),發現:
error: Could not get shadow information for <user> Failed password for <user> from <ip> port <port> ssh2
這可能是因為UsePAM
沒有啟用,檢查/etc/ssh/sshd_config
:
# 檢查UsePAM,確認是否啟用了PAM # UseAPM no UsePAM yes
修改配置後,重啟sshd
服務後,服務恢復。如果編譯時缺了--with-pam
引數,UsePAM yes
會讓服務報錯。
如果不想修改PAM
選項,也可以關閉Selinux
:
# 臨時關閉 setenforce 0 # 永久關閉 vi /etc/selinux/config # 然後將 SELINUX=enforcing 修改為 SELINUX=disabled
UsePAM yes
時,無論啟用或禁用selinux
,都不會引發Could not get shadow information
錯誤。
sshd
啟動報錯:Bad SSH2 cipher spec '...'
可能是升級了openssl
但sshd_config
配置未更新,或者配置有錯,造成不一致。
可以查詢當前被支援的加密方法:
[user@localhost ~]# ssh -Q cipher 3des-cbc aes128-cbc aes192-cbc aes256-cbc [email protected] aes128-ctr aes192-ctr aes256-ctr [email protected] [email protected] [email protected]
也可以用paste -s -d,
直接將查詢結果串接並寫入配置檔案:
echo 'Ciphers' `ssh -Q cipher | paste -d, -s` >> /etc/ssh/sshd_config
sshd
啟動報錯:Bad SSH2 mac spec '...'
查詢被支援的訊息摘要演算法:
[user@localhost ~]# ssh -Q mac hmac-sha1 hmac-sha1-96 hmac-sha2-256 hmac-sha2-512 hmac-md5 hmac-md5-96 [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]
也可以用paste -s -d,
直接將查詢結果串接並寫入配置檔案:
echo 'MACs' `ssh -Q mac | paste -d, -s` >> /etc/ssh/sshd_config
sshd
啟動報錯:Bad SSH2 KexAlgorithms '...'
查詢支援的演算法:
[user@localhost ~]# ssh -Q kex diffie-hellman-group1-sha1 diffie-hellman-group14-sha1 diffie-hellman-group14-sha256 diffie-hellman-group16-sha512 diffie-hellman-group18-sha512 diffie-hellman-group-exchange-sha1 diffie-hellman-group-exchange-sha256 ecdh-sha2-nistp256 ecdh-sha2-nistp384 ecdh-sha2-nistp521 curve25519-sha256 [email protected]
也可以用paste -s -d,
直接將查詢結果串接並寫入配置檔案:
echo 'KexAlgorithms' `ssh -Q kex | paste -d, -s` >> /etc/ssh/sshd_config
技巧
- 遇到錯誤,服務端日誌必須是首選檢查點。
-
可藉助
sshd -d
啟用debug
模式來排查問題。 -
可以用
ssh -vvv
以便更快找到問題。