OpenSSH 9.0升级

没想到学校的网络中心居然不是干吃饭的,偶尔还会干点像用nmap啥的查开放端口和进行弱密码攻击测试的事的。上月底好像说要求托管的服务器要升级到最新的OpenSSH以及OpenSSL修复一些CVE漏洞(依稀记得是scp的提权漏洞啥的?)。不过2202年了谁还用scp呢,主流应该还是sftp和rsync吧。

既然被叫到了,那开搞就是了。

1. 检查OpenSSL和SSH版本#

  • ssh -V: OpenSSH_8.2p1 Ubuntu-4ubuntu0.4, OpenSSL 1.1.1o 3 May 2022
  • sshd -V: OpenSSH_8.2p1 Ubuntu-4ubuntu0.4, OpenSSL 1.1.1o 3 May 2022
  • openssl version -a: OpenSSL 1.1.1o 3 May 2022
  • 安装编译环境sudo apt install build-essential

2. 更新最新版OpenSSL(可选)#

根据网络中心的通知,OpenSSL版本只能使用最新的1.1.1o1.1.1n及之前的版本都被列入受影响版本),因此需要从源代码进行编译

  1. 安装编译依赖项:sudo apt install zlib1g-dev
  2. 下载OpenSSL最新版源码并解压:wget https://www.openssl.org/source/openssl-1.1.1o.tar.gz && tar xvf openssl-1.1.1o.tar.gz
  3. 配置编译环境:./config --prefix=/usr/local/ssl --openssldir=/usr/local/ssl shared zlib(注:这里是安装到/usr/local/ssl路径,有需要自行调整,若采用覆盖安装的形式,注意提前做好备份,若系统安装的是dev版本的openssl,同时需要备份include文件)
  4. 编译:make -j4
  5. 测试OpenSSL:make test
  6. 整合到系统环境:sudo make install
  7. 若第3步不是覆盖安装,需要调整系统的openssl(若安装的是dev版本的openssl,需要同时备份include文件夹,非dev版本的可以跳过备份include文件夹):
    1. 备份原来的openssl和c_rehash:sudo mv /usr/bin/openssl /usr/bin/openssl.baksudo mv /usr/bin/c_rehash /usr/bin/c_rehash.bak以及sudo mv /usr/include/openssl /usr/include/openssl.bak
    2. 为构建的openssl创建软链接到原系统环境:sudo ln -s /usr/local/ssl/bin/openssl /usr/bin/opensslsudo ln -s /usr/local/ssl/bin/c_rehash /usr/bin/c_rehash以及sudo ln -s /usr/local/ssl/include/openssl /usr/include/openssl
  8. 配置lib文件夹:sudo bash -c "echo '/usr/local/ssl/lib' > /etc/ld.so.conf.d/openssl.conf"
  9. 重新加载lib:sudo ldconfig

然后重新检查一遍openssl版本,应该是没问题的。

3. 停止SSH服务并备份SSH可执行文件#

注:停止SSH服务前清务必留有一个以上可用的ssh连接,并保持网络全程畅通!

  1. sudo systemctl stop ssh.service
  2. sudo cp /usr/bin/ssh /usr/bin/ssh-8.2p1
  3. sudo cp /usr/sbin/sshd /usr/sbin/sshd-8.2p1

4. 找到最新版OpenSSH#

所有的OpenSSH版本都可以在这个链接获取: https://www.openssh.com/portable.html#http ,国内用户推荐使用阿里云的镜像: https://mirrors.aliyun.com/pub/OpenBSD/OpenSSH/portable/

下载最新的OpenSSH(目前的最新版为9.0p1):wget https://mirrors.aliyun.com/pub/OpenBSD/OpenSSH/portable/openssh-9.0p1.tar.gz

5. 通过源码构建OpenSSH#

  1. 安装OpenSSH依赖项:sudo apt install libpam0g-dev libselinux1-dev
  2. 解压代码:tar xvf openssh-9.0p1.tar.gz && cd openssh-9.0p1
  3. 对于ubuntu用户,需要对源代码进行一些修改才能让sshd能够正常响应systemctl事件:
    1. 安装systemd依赖:sudo apt install libsystemd-dev
    2. 根据这个patch修改代码
    3. 运行autoreconf重新生成配置文件
    4. 配置编译环境时需要加上--with-systemd flag
  4. 配置编译环境:
    1
    ./configure --prefix=/usr --sysconfdir=/etc/ssh --with-pam --with-selinux --with-zlib --with-systemd --with-ssl-dir=/usr/local/ssl --with-privsep-path=/var/lib/sshd
    配置完成后最后的输出为:
    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
    OpenSSH has been configured with the following options:
    User binaries: /usr/bin
    System binaries: /usr/sbin
    Configuration files: /etc/ssh
    Askpass program: /usr/libexec/ssh-askpass
    Manual pages: /usr/share/man/manX
    PID file: /var/run
    Privilege separation chroot path: /var/lib/sshd
    sshd default user PATH: /usr/bin:/bin:/usr/sbin:/sbin
    Manpage format: doc
    PAM support: yes
    OSF SIA support: no
    KerberosV support: no
    SELinux support: yes
    libedit support: no
    libldns support: no
    Solaris process contract support: no
    Solaris project support: no
    Solaris privilege support: no
    systemd support: yes
    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
    Compiler flags: -g -O2 -pipe -Wno-error=format-truncation -Wall -Wpointer-arith -Wuninitialized -Wsign-compare -Wformat-security -Wsizeof-pointer-memaccess -Wno-pointer-sign -Wno-unused-result -Wimplicit-fallthrough -Wmisleading-indentation -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -ftrapv -fno-builtin-memset -fstack-protector-strong -fPIE
    Preprocessor flags: -I/usr/local/ssl/include -D_XOPEN_SOURCE=600 -D_BSD_SOURCE -D_DEFAULT_SOURCE
    Linker flags: -L/usr/local/ssl/lib -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -fstack-protector-strong -pie
    Libraries: -lcrypto -ldl -lutil -lz -lcrypt -lresolv -lselinux
    +for sshd: -lpam

    PAM is enabled. You may need to install a PAM control file
    for sshd, otherwise password authentication may fail.
    Example PAM control files can be found in the contrib/
    subdirectory
  5. 构建:make -j4
  6. 安装:sudo make install(注:当前的编译配置会覆盖原来的ssh,有需要自行调整)

重启OpenSSH服务#

运行sudo systemctl start ssh.service即可