DevOps文化 & SRE实战分享平台

0%

Rocky Linux 8.3 RC1 OpenSSH升级至openssh-8.6p1


文章声明:此文基于木子实操撰写
生产环境:Rocky Linux release 8.3, Ubuntu 20.04.2 LTS, openssh-8.6p1
问题关键字:OpenSSH 升级, OpenSSH 更新, OpenSSH 漏洞修复


前述

OpenSSH(OpenBSD Secure Shell)是OpenBSD计划组的一套用于安全访问远程计算机的连接工具。该工具是SSH协议的开源实现,支持对所有的传输进行加密,可有效阻止窃听、连接劫持以及其他网络级的攻击。
OpenSSH 8.3p1及之前版本中scp的scp.c文件存在操作系统命令注入漏洞。该漏洞源于外部输入数据构造操作系统可执行命令过程中,网络系统或产品未正确过滤其中的特殊字符、命令等。攻击者可利用该漏洞执行非法操作系统命令。
详细参考国家信息安全漏洞库信息: CVE-2020-15778
目前Rocky Linux 8.3 RC1 采用的为OpenSSH_8.0p1,所以建议通过升级OpenSSH修复此问题。同样OpenSSH before 8.5也存在漏洞,所以建议修复至最新版本OpenSSH_8.6p1。
注:此操作步骤同样适用于Red Hat系所有7.x 8.x服务器系统。


在升级之前,建议打开多个SSH终端连接,并安装telnet服务器,确保在SSH服务器升级异常时,可以通过telnet服务器远程连接,进行紧急问题修复处理。
Red Hat & CentOS 7.x 服务器上,只需要将dnf命令替换成yum命令即可。

Telnet服务器部署

1
2
3
4
5
6
7
8
9
10
11
12
# 安装telnet服务器
dnf install -y telnet-server
# 启动telnet服务器
systemctl start telnet.socket
# 配置防火墙,放行23端口
firewall-cmd --zone=public --permanent --add-port=23/tcp
# or (永久生效加 --permanent)
firewall-cmd --add-service=telnet --zone=public
# 重载防火墙配置
firewall-cmd --reload
# SELinux配置
semanage port -a -t telnetd_port_t -p tcp

升级OpenSSH

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 查看当前安装包
[[email protected] ~]# rpm -qa | grep openssh
openssh-clients-8.0p1-5.el8.x86_64
openssh-8.0p1-5.el8.x86_64
openssh-server-8.0p1-5.el8.x86_64

# 查看当前OpenSSH版本(Rocky Linux 默认使用OpenSSH 8.0p1)
[[email protected] ~]# ssh -V
OpenSSH_8.0p1, OpenSSL 1.1.1g FIPS 21 Apr 2020

# 备份现有SSH
[[email protected] ~]# mv /etc/ssh/ /etc/ssh.bak
[[email protected] ~]# mv /usr/bin/ssh /usr/bin/ssh.bak
[[email protected] ~]# mv /usr/sbin/sshd /usr/sbin/sshd.bak

# 如果您是第一次升级,备份/etc/init.d/sshd时会不存在,不影响后续操作
[[email protected] ~]# mv /etc/init.d/sshd /etc/init.d/sshd.bak
mv: 无法获取'/etc/init.d/sshd' 的文件状态(stat): No such file or directory

# 卸载现有OpenSSH
[[email protected] ~]# rpm -e --nodeps $(rpm -qa |grep openssh)
警告:文件 /etc/ssh/sshd_config:移除失败:No such file or directory
警告:文件 /etc/ssh/ssh_config.d/05-redhat.conf:移除失败:No such file or directory
警告:文件 /etc/ssh/ssh_config.d:移除失败:No such file or directory
警告:文件 /etc/ssh/ssh_config:移除失败:No such file or directory
警告:文件 /etc/ssh/moduli:移除失败:No such file or directory
警告:文件 /etc/ssh:移除失败:No such file or directory

# 确保已经卸载成功
[[email protected] ~]# rpm -qa | grep openssh

# 安装编译openssh所需要使用的包
[[email protected] ~]# dnf install wget gcc openssl-devel pam-devel rpm-build -y

# 下载OpenSSH二进制包
[[email protected] ~]# wget https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-8.6p1.tar.gz
[[email protected] ~]# tar -zxvf openssh-8.6p1.tar.gz
[[email protected] ~]# cd openssh-8.6p1

# 编译安装
[[email protected] openssh-8.6p1]# ./configure --prefix=/usr --sysconfdir=/etc/ssh --with-md5-passwords --with-pam --with-zlib --with-ssl-dir=/usr/local/ssl --without-hardening
[[email protected] openssh-8.6p1]# make
[[email protected] openssh-8.6p1]# make install

# 授权
[[email protected] openssh-8.6p1]# chmod 600 /etc/ssh/ssh_host_rsa_key /etc/ssh/ssh_host_ecdsa_key /etc/ssh/ssh_host_ed25519_key

# 复制配置文件
[[email protected] openssh-8.6p1]# cp -a contrib/redhat/sshd.init /etc/init.d/sshd
[[email protected] openssh-8.6p1]# cp -a contrib/redhat/sshd.pam /etc/pam.d/sshd.pam

# 修改配置允许root用户远程登录
[[email protected] openssh-8.6p1]# echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config
[[email protected] openssh-8.6p1]# echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
[[email protected] openssh-8.6p1]# sed -i "s/^#Port/Port/g" /etc/ssh/sshd_config
[[email protected] openssh-8.6p1]# chmod 755 /etc/init.d/sshd

# 启用sshd,生成服务配置文件
[[email protected] openssh-8.6p1]# systemctl enable sshd
sshd.service is not a native service, redirecting to systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable sshd

# 重启服务
[[email protected] openssh-8.6p1]# systemctl restart sshd

# 验证升级是否成功
[[email protected] ~]# ssh -V
OpenSSH_8.6p1, OpenSSL 1.1.1g FIPS 21 Apr 2020

禁用SCP与SFTP

有时候OpenSSH会出现一些其它漏洞,如OpenSSH 8.3p1及之前的版本,会存在scp漏洞等,建议禁用sftp及scp,详细操作如下:

1
2
3
4
5
6
7
8
9
10
11
# 禁用sftp
[[email protected] ~]# sed -i "s/^Subsystem/#Subsystem/g" /etc/ssh/sshd_config
# 确保sftp已经禁用
[[email protected] ~]# cat /etc/ssh/sshd_config | grep sftp
#Subsystem sftp /usr/libexec/sftp-server

# 因为禁用scp是一件麻烦的事情,可以直接删除scp命令或者重命名,相对来说更靠谱一些。
rm -rf /usr/bin/scp

# 重启sshd服务
[[email protected] openssh-8.6p1]# systemctl restart sshd

Ubuntu 服务器升级OpenSSH

因为木子刚好有Ubuntu 20.04.2 LTS服务器需要升级,所以这里将对应的升级方法一并提供。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 下载OpenSSH二进制包
[email protected]:~# wget https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-8.6p1.tar.gz
[email protected]:~# tar -zxvf openssh-8.6p1.tar.gz
[email protected]:~# cd openssh-8.6p1

# 编译安装
[email protected]:~# /configure --prefix=/usr --sysconfdir=/etc/ssh --with-md5-passwords --with-pam --with-zlib --with-privsep-path=/var/lib/sshd
[email protected]:~# make
[email protected]:~# make install

# 重启服务
[email protected]:~# systemctl restart sshd

# 验证升级是否成功
[email protected]:~# ssh -V
OpenSSH_8.6p1, OpenSSL 1.1.1f 31 Mar 2020

写在最后

最后再补上两句:安全无小事,防范是关键。安全无小事,责任大于天。
下篇预告:基于Rocky Linux 8.3 RC1搭建Rsync冷备容灾服务器,如果您有任何想学习了解的技术,欢迎在下方留言,木子将根据需求输出对应基础技术博文。

参考文献

[1] 国家信息安全漏洞库:http://www.cnnvd.org.cn/
[2] 腾讯安全:https://s.tencent.com/research/bsafe/


五平台同步更新:
博客: 欧巴云
知乎: 欧巴云
51CTO: 欧巴云
云+社区: 欧巴云
微信公众号: 欧巴云

坚持原创技术分享,您的支持与鼓励,是我持续创作的动力!