实战-通过升级 SSH 服务修复安全漏洞
SSH 服务用于远程连接服务器,但是如果 ssh 服务存在漏洞,黑客就可以通过 ssh 服务远程控制服务器。
例如:以下版本范围内都存在漏洞。
更新 ssh 版本修复漏洞
1.通过yum升级
如果你的 ssh 版本过低可以通过 yum 直接升级 sshd 服务。如果可以使用 yum 升级尽量使用 yum
进行升级,因为 yum 升级维护起来很方便。
[root@xuegod63 ~]# yum update openssh -y
2.通过源码升级
如果想安装到最新版本的 openssh 则需要使用源码安装。
配置备用连接方式 telnet,以防止配置失败不能连接服务器。
①配置telnet连接
[root@xuegod63 ~]# yum install xinetd telnet-server -y
xinetd 是telnet-server的守护进程,telnet-server依赖于xinetd
检查配置文件,如果存在/etc/xinetd.d/telnet 文件则需要修改配置使 root 用户可以通过 telnet 登陆,如果不存在该文件则无需配置。
[root@xuegod63 ~]# 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 登录的终端类型,添加 pts 终端,文件末尾添加即可。
[root@xuegod63 ~]# vim /etc/securetty
pts/0 pts/1 pts/2 pts/3
配置开机自启
[root@xuegod63 ~]# systemctl enable xinetd && systemctl start xinetd
[root@xuegod63 ~]# systemctl start telnet.socket
telnet 连接服务器,xshell 协议选择 telnet。
②升级ssh
安装依赖库:
[root@xuegod63 ~]# yum -y install gcc gcc-c++ zlib zlib-devel openssl openssl-devel pam-devel libselinux-devel
下载源码包
[root@xuegod63 ~]# wget -c https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-8.3p1.tar.gz /版本自定
-c 断点续传
备份现有 ssh 配置文件
[root@xuegod63 ~]# mkdir /opt/sshbak
[root@xuegod63 ~]# mv /etc/ssh/* /opt/sshbak/ 创建新的安装目录
[root@xuegod63 ~]# mkdir /usr/local/sshd
解压源码包
[root@xuegod63 ~]# tar xf openssh-8.3p1.tar.gz -C /usr/local/sshd/
[root@xuegod63 ~]# cd /usr/local/sshd/openssh-8.3p1/
[root@xuegod63 openssh-8.3p1]# ./configure —with-md5-passwords —with-pam —with-selinux —with-privsep-path=/usr/local/sshd/ —sysconfdir=/etc/ssh
由于启用了 PAM 则需要安装一个配置文件,该文件在 contrib 目录下。
[root@xuegod63 openssh-8.3p1]# cp -a contrib/redhat/sshd.pam /etc/pam.d/sshd.pam
拷贝开机启动脚本
[root@xuegod63 openssh-8.3p1]# cp -a contrib/redhat/sshd.init /etc/init.d/sshd
编译安装
[root@xuegod63 openssh-8.3p1]# make -j 4 && make install
[root@xuegod63 openssh-8.3p1]# vim /etc/ssh/sshd_config 改:
32 #PermitRootLogin prohibit-password
为:
32 PermitRootLogin yes
改:(去掉注释即可)
37 #PubkeyAuthentication yes
为:
37 PubkeyAuthentication yes
改:(去掉注释即可,此处是优化项如果使用 DNS 解析速度会很慢)
98 #UseDNS no
为:
98 UseDNS no
systemctl disable sshd && systemctl stop sshd && mv /usr/lib/systemd/system/sshd.service /opt/sshbak/ &&
/etc/init.d/sshd restart
停止原版本自启动
[root@xuegod63 openssh-8.3p1]# systemctl stop sshd
[root@xuegod63 openssh-8.3p1]# systemctl disabld sshd #删除原有的 system 开机启动项
[root@xuegod63 openssh-8.3p1]# mv /usr/lib/systemd/system/sshd.service /opt/sshbak/
配置开机自启
[root@xuegod63 openssh-8.3p1]# chkconfig —add sshd
[root@xuegod63 openssh-8.3p1]# chkconfig sshd on
服务管理测试
[root@xuegod63 openssh-8.3p1]# /etc/init.d/sshd restart
[root@xuegod63 openssh-8.3p1]# ss -antup|grep 22
[root@xuegod63 openssh-8.3p1]# systemctl stop sshd
[root@xuegod63 openssh-8.3p1]# ss -antup|grep 22
[root@xuegod63 openssh-8.3p1]# systemctl start sshd
[root@xuegod63 ~]# ssh -V
OpenSSH_8.3p1, OpenSSL 1.0.2k-fips 26 Jan 2017
禁用 telnet 服务
[root@xuegod63 openssh-8.3p1]# systemctl disable xinetd.service && systemctl stop xinetd.service
[root@xuegod63 openssh-8.3p1]# systemctl disable telnet.socket && systemctl stop telnet.socket