一、實驗背景
發現 SSH 存在許多安全漏洞,原因是 CentOS 7.9 使用了一個比較舊的 OpenSSH 版本 v6.6.1,而這些漏洞在新版的 OpenSSH 中均已被修復,所以出於安全考慮,需要升級。yum 倉庫中並沒有最新版的 OpenSSH,我們需要自己從官方下載最新的opeenSSh源碼包編譯制作 rpm 安裝包。因為客戶服務器不能連外網,所以還需要將其做成離線升級包。
二、實驗環境
操作系統: CentOS7.9 Mininal
serverA 192.168.41.140 模擬開發機,能聯網,用於制作離線升級包
serverB 10.202.205.22 模擬客戶服務器,不能聯網,openSSH相關包及其依賴版本較低
三、實驗預期
在severA上完成openSSH相關編譯及依賴下載,寫成一鍵升級腳本,拖到serverB上完成openSSH的升級。
當前最新openSSh源碼包版本為 openssh-8.6p1.tar.gz
四、實驗操作
在serverA上操作
## 1.安裝工具、依賴包
yum -y install wget epel-release
yum -y install rpm-build gcc make
yum -y install openssl openssl-devel krb5-devel pam-devel libX11-devel xmkmf libXt-devel
## (2種方法試驗安裝工具、依賴包)
yum -y install rpm-build openssh openssl openssl-devel zlib zlib-devel pam pam-devel tcp_wrappers tcp_wrappers-devel gcc gcc-c++ make automake autoconf libtool
## 2.下載源碼包,創建打包目錄
cd /root
wget https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-8.6p1.tar.gz
mkdir -p /root/rpmbuild/{SOURCES,SPECS}
mv openssh-8.6p1.tar.gz /root/rpmbuild/SOURCES/
cd /root/rpmbuild/SOURCES/ && tar xf openssh-8.6p1.tar.gz && cd openssh-8.6p1
## 3.對openssh.spec的12和15行做下更改,刪除103行 BuildRequires: openssl-devel < 1.1
vi /root/rpmbuild/SOURCES/openssh-8.6p1/contrib/redhat/openssh.spec
... ...
103 BuildRequires: openssl-devel < 1.1
... ...
276 %if %{build6x}
277 install -m644 contrib/redhat/sshd.pam.old $RPM_BUILD_ROOT/etc/pam.d/sshd
278 %else
279 install -m644 contrib/redhat/sshd.pam $RPM_BUILD_ROOT/etc/pam.d/sshd
280 %endif
... ...
## 4.根據openssh.spec文件中279行對源碼中的sshd.pam做下修改,修改如下:
cat /root/rpmbuild/SOURCES/openssh-8.6p1/contrib/redhat/sshd.pam
#%PAM-1.0
auth required pam_sepermit.so
auth include password-auth
account required pam_nologin.so
account include password-auth
password include password-auth
# pam_selinux.so close should be the first session rule
session required pam_selinux.so close
session required pam_loginuid.so
# pam_selinux.so open should only be followed by sessions to be executed in the user context
session required pam_selinux.so open env_params
session optional pam_keyinit.so force revoke
session include password-auth
## 5.對sshd.conf文件提前做下更改,添加如下配置
vi /root/rpmbuild/SOURCES/openssh-8.6p1/sshd_config
PermitRootLogin yes
PubkeyAuthentication yes
PasswordAuthentication yes
## 6.裝包后權限處理(備注:數字0無效 1有效)
sed -i -e "s/%define no_x11_askpass 0/%define no_x11_askpass 1/g" /root/rpmbuild/SOURCES/openssh-8.6p1/contrib/redhat/openssh.spec
sed -i -e "s/%define no_gnome_askpass 0/%define no_gnome_askpass 1/g" /root/rpmbuild/SOURCES/openssh-8.6p1/contrib/redhat/openssh.spec
sed -i '/BuildRequires: openssl-devel < 1.1/d' /root/rpmbuild/SOURCES/openssh-8.6p1/contrib/redhat/openssh.spec
## 7.復制openssh.spec到SPECS打包目錄下
cd /root/rpmbuild/SOURCES/openssh-8.6p1/contrib/redhat/
cp -a openssh.spec /root/rpmbuild/SPECS/
## 8.處理掉下載的源碼包,將處理后的源碼包重新打包
cd /root/rpmbuild/SOURCES/
rm -f openssh-8.6p1.tar.gz
tar zcf openssh-8.6p1.tar.gz -C /root/rpmbuild/SOURCES/ openssh-8.6p1
rm -rf openssh-8.6p1
## 9.開始打包
cd /root/rpmbuild/SPECS
rpmbuild -bb openssh.spec
ll /root/rpmbuild/RPMS/x86_64/
###################################### 打包完畢 ############################################
我打包好的rpm大家可以直接下載使用:
https://pan.baidu.com/s/1iZ2erjTJbAUVeKSjkfSWGg 密碼:i26h
五、升級實驗
在serverA上進行升級
## 1.升級測試
cd /root/rpmbuild/RPMS/x86_64/
rpm -Uvh openssh-*
ssh -V
## 2.查看升級后sshd文件有沒有被替換(如果沒有sshd_config.rpmnew文件,跳過2、3步驟)
ll /etc/ssh/sshd_config*
-rw------- 1 root root 3891 May 9 2020 /etc/ssh/sshd_config
-rw------- 1 root root 3149 Mar 1 21:45 /etc/ssh/sshd_config.rpmnew
## 2.1進行手動替換
mv /etc/ssh/sshd_config{,-bak}
mv /etc/ssh/sshd_config.rpmnew /etc/ssh/sshd_config
## 3.替換完查看新配置文件
egrep -v "^$|^#" /etc/ssh/sshd_config
PermitRootLogin yes
PubkeyAuthentication yes
PasswordAuthentication yes
## 4.重啟ssh服務,重啟失敗查看失敗信息(多半原因是/etc/ssh/ssh_host_*_key權限問題,設置600權限)
service sshd restart
chmod 600 /etc/ssh/ssh_host_*_key
## 5.重啟ssh服務
service sshd restart
## 6.通過xshell連接,彈出一個錯誤對話框,提示“服務器發送了一個意外的數據包。received:3,expected:20”的錯誤信息。在/etc/ssh/sshd_config最后增加以下一行,然后重啟服務
KexAlgorithms curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group14-sha1
## 7.設置開機啟動
chkconfig sshd on
## 8.查看狀態
service sshd status
## 9.如果新開終端連接的時,root密碼報錯,並且已經根據上面后續操作,那可能就是SElinux的問題,我們進行臨時禁用,即可正常登錄.
setenforce 0
## 10.然后修改/etc/selinux/config文件,進行永久禁用SElinux即可。
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
###################################### 升級完畢 ############################################
六、引用資料、感謝各位大佬
后邊跟上 https://www.cnblogs.com/hbgs/p/14466259.html
HunterMichaelG https://blog.csdn.net/michaelwoshi/article/details/108154328