记一次手动将OpenSSH从7.4升级到9.8的过程

文章目录

    • 背景
      • 漏洞介绍
      • 危害影响
      • 修复建议
    • 相关介绍
      • OpenSSH
      • OpenSSL
      • zlib的作用
    • 安装telnet-server
      • 检查安装状态
      • 离线安装
      • 直接安装
      • 启动服务
      • 防火墙配置
      • 使用telnet连接
      • 允许root登录
      • 连接示例
      • 问题排查
    • 安装zlib
      • 下载安装
      • 安装依赖
      • 编译安装
    • 安装openssl
      • 下载安装
      • 安装依赖(可选)
      • 配置编译
    • 备份并卸载老版本 OpenSSH
      • 备份 SSH 配置文件
      • 备份 SSH 文件
      • 停止 SSH 服务
      • 查询并卸载原有 OpenSSH 包
      • 卸载 OpenSSH 包
      • 验证卸载结果
    • OpenSSH 升级指南
      • 1. 下载并解压 OpenSSH 源码包
      • 2. 配置编译参数
      • 3. 编译并安装
      • 4. 配置 SSH 服务
      • 5. 启动并验证服务

背景

OpenSSH远程代码执行漏洞(CNVD-2024-29805)

漏洞介绍

OpenSSH是由加拿大OpenBSD计划组开发的一套用于安全访问远程计算机的连接工具。作为SSH协议的开源实现,它支持对所有传输进行加密,有效防止窃听、连接劫持等网络攻击。该漏洞源于信号处理程序中的竞争条件,攻击者可借此在无需认证的情况下远程执行任意代码,获取系统控制权。

危害影响

该漏洞影响OpenSSH 8.5p1至9.8p1之前的所有版本:
8.5p1 <= OpenSSH < 9.8p1

修复建议

OpenSSH官方已发布新版本修复该漏洞,建议用户及时确认产品版本并采取修补措施。官方更新版本下载链接:
https://www.openssh.com/txt/release-9.8

相关介绍

OpenSSH

OpenSSH是SSH(Secure Shell)协议的实现,用于在不安全的网络上提供安全通信。它广泛应用于远程登录、命令执行和文件传输,主要工具包括:

  • ssh:远程登录
  • scp和sftp:文件传输
  • sshd:SSH守护进程
    OpenSSH确保通信的机密性和完整性,有效防止中间人攻击。

OpenSSL

OpenSSL是一个功能强大的加密库,提供多种加密算法、哈希函数和加密协议(如TLS/SSL)的实现。它广泛用于保护网络通信,确保数据传输过程中的安全性,主要功能包括:

  • 加密和解密
  • 密钥生成和管理
  • 数字证书处理

zlib的作用

zlib在OpenSSH中主要用于:

  1. 数据压缩:通过压缩传输数据,减少数据量,提高传输效率
  2. 节省带宽:显著降低SSH会话中的数据传输量,特别适用于大文件传输
  3. 提升效率:对文本数据的压缩效果尤为明显,可减少传输时间

安装telnet-server

为避免SSH关闭后无法远程连接,建议安装telnet-server。

检查安装状态

rpm -q telnet-server  # 检查telnet服务端
rpm -q telnet         # 检查telnet客户端

离线安装

  1. 通过有网主机下载:
sudo yum install yum-utils
mkdir telnet-server-install
cd telnet-server-install
yumdownloader --resolve telnet-server
  1. 通过FTP上传后安装:
cd ~/telnet-server-install
sudo rpm -ivh *.rpm

直接安装

yum install telnet -y

启动服务

systemctl enable telnet.socket  # 设置开机启动
systemctl start telnet.socket   # 启动服务

防火墙配置

sudo firewall-cmd --permanent --add-service=telnet
sudo firewall-cmd --reload

使用telnet连接

telnet <IP地址>

允许root登录

默认系统禁止root用户通过telnet远程登录,需执行:

echo 'pts/0' >> /etc/securetty
echo 'pts/1' >> /etc/securetty

连接示例

[root@standby opt]# telnet 10.10.10.171
Trying 10.10.10.171...
Connected to 10.10.10.171.
Escape character is '^]'.

Kernel 3.10.0-1160.el7.x86_64 on an x86_64
localhost login: root
Password: 
Last failed login: Thu Jul  4 13:56:07 CST 2024 from ::ffff:10.10.10.42 on pts/0
There were 3 failed login attempts since the last successful login.
Last login: Thu Jul  4 10:30:43 from 172.20.1.1

问题排查

若登录失败,可检查日志:

tail /var/log/secure

若出现"access denied: tty ‘pts/3’ is not secure",需添加:

echo 'pts/3' >> /etc/securetty

重启telnet服务后即可正常登录。

安装zlib

下载安装

cd /usr/local/src/
wget https://www.zlib.net/fossils/zlib-1.3.1.tar.gz
tar zxvf zlib-1.3.tar.gz
cd zlib-1.3

安装依赖

yum install gcc gcc-c++ make -y

编译安装

./configure --prefix=/usr/local/zlib
make && make install

安装openssl

下载安装

cd /usr/local/src/
wget https://www.openssl.org/source/openssl-3.2.2.tar.gz
tar zxvf openssl-3.2.0.tar.gz
cd openssl-3.2.0

安装依赖(可选)

yum install -y perl-CPAN perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker

配置编译

./config --prefix=/usr/local/ssl --shared

完成后输出:

[root@localhost openssl-3.2.2]# ./config --prefix=/usr/local/ssl --shared
Configuring OpenSSL version 3.2.2 for target linux-x86_64
Using os-specific seed configuration
Created configdata.pm
Running configdata.pm
Created Makefile.in
Created Makefile
Created include/openssl/configuration.h

**********************************************************************
***                                                                ***
***   OpenSSL has been successfully configured                     ***
***                                                                ***
***   If you encounter a problem while building, please open an    ***
***   issue on GitHub   ***
***   and include the output from the following command:           ***
***                                                                ***
***       perl configdata.pm --dump                                ***
***                                                                ***
***   (If you are new to OpenSSL, you might want to consult the    ***
***   'Troubleshooting' section in the INSTALL.md file first)      ***
***                                                                ***
**********************************************************************
make && make install

编译安装ssl,这个安装过程很长大概有10分钟左右

echo '/usr/local/ssl/lib64' >> /etc/ld.so.conf

路径写入etc/ld.so.conf

备份并卸载老版本 OpenSSH

备份 SSH 配置文件

cp -p /usr/bin/ssh /usr/bin/ssh.bak
cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
cp -p /usr/sbin/sshd /usr/sbin/sshd.bak
cp -p /usr/bin/ssh-keygen /usr/bin/ssh-keygen.bak
cp -p /etc/ssh/ssh_host_ecdsa_key.pub /etc/ssh/ssh_host_ecdsa_key.pub.bak

备份 SSH 文件

cp -r /etc/ssh /etc/ssh.old

停止 SSH 服务

systemctl stop sshd

注意:切勿关闭远程连接

查询并卸载原有 OpenSSH 包

rpm -qa | grep openssh

输出示例:

openssh-server-7.4p1-21.el7.x86_64
openssh-7.4p1-21.el7.x86_64
openssh-clients-7.4p1-21.el7.x86_64

卸载 OpenSSH 包

yum remove openssh-7.4p1-21.el7.x86_64 openssh-7.4p1-21.el7.x86_64 openssh-clients-7.4p1-21.el7.x86_64

验证卸载结果

rpm -qa | grep openssh

OpenSSH 升级指南

1. 下载并解压 OpenSSH 源码包

cd /usr/local/src/
wget http://ftp.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-9.8p1.tar.gz
tar zxvf openssh-9.8p1.tar.gz
cd openssh-9.8p1

2. 配置编译参数

./configure --prefix=/usr/local/openssh \
            --with-zlib=/usr/local/zlib \
            --with-ssl-dir=/usr/local/ssl

配置完成后将显示以下信息:

OpenSSH has been configured with the following options:
                     User binaries: /usr/local/openssh/bin
                   System binaries: /usr/local/openssh/sbin
               Configuration files: /usr/local/openssh/etc
                   Askpass program: /usr/local/openssh/libexec/ssh-askpass
                      Manual pages: /usr/local/openssh/share/man/manX
                          PID file: /var/run
  Privilege separation chroot path: /var/empty
            sshd default user PATH: /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/openssh/bin
                    Manpage format: doc
                       PAM support: no
                   OSF SIA support: no
                 KerberosV support: no
                   SELinux support: no
                   libedit support: no
                   libldns support: no
  Solaris process contract support: no
           Solaris project support: no
         Solaris privilege support: no
       IP address in $DISPLAY hack: no
           Translate v4 in v6 hack: yes
                  BSD Auth support: no
              Random number source: OpenSSL internal ONLY
             Privsep sandbox style: seccomp_filter
                   PKCS#11 support: yes
                  U2F/FIDO support: yes

              Host: x86_64-pc-linux-gnu
          Compiler: cc -std=gnu11
    Compiler flags: -g -O2 -pipe -Wall -Wextra -Wpointer-arith -Wuninitialized -Wsign-compare -Wformat-security -Wsizeof-pointer-memaccess -Wno-pointer-sign -Wno-unused-parameter -Wno-unused-result -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -ftrapv -fno-builtin-memset -fstack-protector-strong -fPIE  
Preprocessor flags: -I/usr/local/ssl/include -I/usr/local/zlib/include  -D_XOPEN_SOURCE=600 -D_BSD_SOURCE -D_DEFAULT_SOURCE -D_GNU_SOURCE -DOPENSSL_API_COMPAT=0x10100000L
      Linker flags: -L/usr/local/ssl/lib64 -L/usr/local/zlib/lib  -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -fstack-protector-strong -pie 
         Libraries: -ldl -lutil  -lresolv
     +for channels: -lcrypto  -lz
         +for sshd: -lcrypt 

3. 编译并安装

make && make install

4. 配置 SSH 服务

# 允许 root 登录并启用密码验证
echo 'PermitRootLogin yes' >> /usr/local/openssh/etc/sshd_config
echo 'PubkeyAuthentication yes' >> /usr/local/openssh/etc/sshd_config
echo 'PasswordAuthentication yes' >> /usr/local/openssh/etc/sshd_config

# 复制配置文件到系统目录
cp /usr/local/openssh/etc/sshd_config /etc/ssh/sshd_config
cp /usr/local/openssh/sbin/sshd /usr/sbin/sshd
cp /usr/local/openssh/bin/ssh /usr/bin/ssh
cp /usr/local/openssh/bin/ssh-keygen /usr/bin/ssh-keygen
cp /usr/local/openssh/etc/ssh_host_ecdsa_key.pub /etc/ssh/ssh_host_ecdsa_key.pub

# 复制并设置启动脚本
cp -p contrib/redhat/sshd.init /etc/init.d/sshd
chmod +x /etc/init.d/sshd

另一种配置方法是通过编辑 /usr/lib/systemd/system/sshd.service 文件:

[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

默认情况下,ExecStart 使用 /usr/local/openssh/etc/sshd_config 作为配置文件。如需指定其他配置文件,可使用 -f 参数,例如:ExecStart=/usr/sbin/sshd -f /etc/ssh/sshd_config

如需修改 SSH 默认端口,可编辑 sshd_config 文件中的 Port 配置项。

配置修改完成后,执行以下命令使更改生效:

systemctl daemon-reload
systemctl reload sshd

5. 启动并验证服务

# 设置开机自启并重启服务
systemctl enable sshd
systemctl restart sshd

# 检查服务状态
systemctl status sshd

# 验证版本
ssh -V
# 输出示例:OpenSSH_9.8p1, OpenSSL 3.2.2 4 Jun 2024

至此,OpenSSH 升级完成。如有任何问题,欢迎交流指正。

你可能感兴趣的:(Linux,CNVD,linux)