本文章所使用的的相關環境說明:
操作系統:CentOS Linux release 7.5.1804 (Core) (新環境)
第三方yum源:(可百度自行尋找並配置,如:清華大學源,163源,搜狐、阿里雲yum源等國內第三方yum源都可以)
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
通過升級SSH服務修復安全漏洞
SSH服務用於遠程連接服務器,但是如果ssh服務存在漏洞,黑客就可以通過ssh服務遠程控制服務器。
例如:以下版本范圍內都存在漏洞。
更新ssh版本修復漏洞
版本升級的兩種方法:(本文章推薦使用第二種編譯安裝方式)
方法一:使用yum升級ssh的版本
如果你的ssh版本過低可以通過yum直接升級sshd服務。如果可以使用yum升級盡量使用yum進行升級,因為yum升級維護起來很方便。
先看一下當前的ssh版本
此方法需要自行尋找一下第三方openssh的最新版本的yum倉庫地址,並在本地配置后方可使用yum直接升級
[root@servera ~]# yum update openssh -y
方法二:源碼編譯安裝openssh
如果想安裝到最新版本的openssh則需要使用源碼安裝。
配置備用連接方式telnet,以防止配置失敗不能連接服務器。
[root@servera ~]# yum install xinetd telnet-server -y
檢查配置文件,如果存在/etc/xinetd.d/telnet文件則需要修改配置使root用戶可以通過telnet登陸,如果不存在該文件則無需配置。
[root@servera ~]# ll /etc/xinetd.d/telnet
ls: cannot access /etc/xinetd.d/telnet: No such file or directory
擴展:/etc/xinetd.d/telnet文件配置
改:
disable = no
為:
disable = yes
配置telnet登錄的終端類型,添加pst終端,文件末尾添加即可。
[root@servera ~]# vim /etc/securett
pts/0
pts/1
pts/2
pts/3
配置開機自啟
systemctl enable xinetd && systemctl start xinetd
systemctl start telnet.socket
telnet連接服務器,xshell協議選擇telnet。
確定-登錄連接,輸入root用戶和密碼登錄到服務器
1、先安裝相關的依賴庫:
yum -y install gcc gcc-c++ zlib zlib-devel openssl openssl-devel pam-devel libselinux-devel
2、下載openssh的源碼包
wget -c https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-8.3p1.tar.gz
3、先備份一下現有的ssh配置文件
mkdir /opt/sshbak && mv /etc/ssh/* /opt/sshbak/
4、創建新的安裝目錄
mkdir /usr/local/sshd
5、解壓源碼包,進行編譯安裝
[root@servera ~]# tar xf openssh-8.3p1.tar.gz -C /usr/local/src/
# 進入到源碼目錄下,開始檢查環境、編譯、安裝等
[root@servera ~]# cd /usr/local/src/openssh-8.3p1/
[root@servera openssh-8.3p1]# ./configure --with-md5-passwords --with-pam --with-selinux --with-privsep-path=/usr/local/sshd/ --sysconfdir=/etc/ssh && make -j4 && make install
編譯安裝完成!
6、修改sshd服務端的新配置文件,使root用戶可以遠程登錄。(建議先備份一下/etc/ssh/下生成的相關配置文件,也可以直接把之前的配置文件copy到此目錄下使用)
備份新生成的文件
# cp -a /etc/ssh/* /bak/sshbak-`date +%F`
#修改服務端配置文件
[root@servera openssh-8.3p1]# vim /etc/ssh/sshd_config
# ①允許root遠程登錄
改:
32 #PermitRootLogin prohibit-password
為:
32 PermitRootLogin yes
② 允許使用公鑰認證
改:(去掉注釋即可)
37 #PubkeyAuthentication yes
為:
37 PubkeyAuthentication yes
# 禁止解析
改:(去掉注釋即可,此處是優化項如果使用DNS解析速度會很慢)
98 #UseDNS no
為:
98 UseDNS no
7、拷貝開機啟動腳本
拷貝開機啟動腳本
[root@servera openssh-8.3p1]# cp -a contrib/redhat/sshd.init /etc/init.d/sshd
# 使用sed或手動修改一下啟動腳本中的路徑信息
[root@servera openssh-8.3p1]# sed -i 's#SSHD=/usr/sbin/sshd#SSHD=/usr/local/sbin/sshd#g' /etc/init.d/sshd
[root@servera openssh-8.3p1]# sed -i 's#/usr/bin/ssh-keygen#/usr/local/bin/ssh-keygen#g' /etc/init.d/sshd
8、加入到開機啟動服務列表並設置為開機自啟動
chkconfig --add sshd && chkconfig sshd on
刪除或移走原有的sshd服務啟動腳本
[root@servera openssh-8.3p1]# mv /usr/lib/systemd/system/sshd.service /opt/sshbak/
服務管理測試
/etc/init.d/sshd restart
netstat -ntup | grep 22
先查看一下ssh的版本和服務狀態
使用遠程工具連接到服務器,此時ssh的版本已升級為8.3版本,升級成功
sshd服務成功登錄后可以禁用telnet服務
systemctl disable xinetd.service && systemctl stop xinetd.service
systemctl disable telnet.socket && systemctl stop telnet.socket
如果想要隱藏掉遠程登錄時的版本信息內容,可以使用以下方法重新編譯安裝即可
重新編譯安裝ssh,使其連接時屏蔽SSHD版本信息,提升安全性
[root@servera openssh-8.3p1]# cd /usr/local/src/openssh-8.3p1
[root@servera openssh-8.3p1]# vim version.h
改:第3行內容
3 #define SSH_VERSION "OpenSSH_8.3"
為:修改為
3 #define SSH_VERSION "ctyun_cloud"
再次編譯安裝
[root@servera openssh-8.3p1]# ./configure --with-md5-passwords --with-pam --with-selinux --with-privsep-path=/usr/local/sshd/ --sysconfdir=/etc/ssh && make -j4 && make install
# 重新編譯不會改變配置文件,直接重新啟動服務使版本信息生效
systemctl restart sshd
新建選項卡連接sshd,此時ssh的版本信息已被修改為我們自定義的版本信息內容