CentOS7 修复OpenSSH安全漏洞教程

起因: 由于openssh爆出一个特殊漏洞,涉及到8.3p1及以下版本,博客特意编译了一个8.6p1版本进行分享

  • 检查环境:
    1
    2
    [root@test]# ssh -V
    OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017
  • 为保证顺利升级:
    注意:如果机器做过安全基线整改,建议先自行备份/etc/pam.d/sshd文件,升级后,此文件会被覆盖,如果未修改过,按照文章后续的进行覆盖即可。亦请务必确定系统版本为:CentOS7。
    请确定openssh版本为7.x,openssl版本为 OpenSSL 1.0.2k及以上。(正常来说,系统都为以上版本。)
    下载地址:
    1
    2
    3
    mkdir -p /root/source
    wget https://media.bnickolas.com/images/openssh-8.6p1-2.tar -O /root/source/openssh8.6.tar
    tar -xvf /root/source/openssh8.6.tar
  • 安装方法:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    rpm -Uvh *.rpm
    安装后会如下提示:
    [root@test ~]# rpm -Uvh *.rpm
    Preparing... ################################# [100%]
    Updating / installing...
    1:openssh-8.6p1-2.el7 ################################# [ 14%]
    2:openssh-clients-8.6p1-2.el7 ################################# [ 29%]
    3:openssh-server-8.6p1-2.el7 ################################# [ 43%]
    4:openssh-debuginfo-8.6p1-2.el7 ################################# [ 57%]
    Cleaning up / removing...
    5:openssh-server-7.4p1-16.el7 ################################# [ 71%]
    6:openssh-clients-7.4p1-16.el7 ################################# [ 86%]
    7:openssh-7.4p1-16.el7 ################################# [100%]
    [root@test ~]# ssh -V
    OpenSSH_8.6p1-2, OpenSSL 1.0.2k-fips 26 Jan 2017
    [root@768 ~]#
  • 修改权限
    1
    2
    cd /etc/ssh/
    chmod 400 ssh_host_ecdsa_key ssh_host_ed25519_key ssh_host_rsa_key
  • 允许 root登录
    1
    echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
  • 不修改这个文件,会出现密码是对的,却无法登陆。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    cat <<EOF>/etc/pam.d/sshd
    #%PAM-1.0
    auth required pam_sepermit.so
    auth include password-auth
    account required pam_nologin.so
    account include password-auth
    password include password-auth
    ## pam_selinux.so close should be the first session rule
    session required pam_selinux.so close
    session required pam_loginuid.so
    ## pam_selinux.so open should only be followed by sessions to be executed in the user context
    session required pam_selinux.so open env_params
    session optional pam_keyinit.so force revoke
    session include password-auth
    EOF
  • 重启服务
    1
    systemctl restart sshd

注意:升级后重启SSH可能出现以下错误

1
2
3
4
5
6
7
8
9
10
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Unable to load host key "/etc/ssh/ssh_host_ed25519_key": bad permissions
Unable to load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.
[FAILED]
sshd.service: control process exited, code=exited status=1
Failed to start SYSV: OpenSSH server daemon.
Unit sshd.service entered failed state.
sshd.service failed.

解决办法:

1
2
chmod 0600 /etc/ssh/ssh_host_ed25519_key
service sshd restart

注意:如果新开终端连接的时,root密码报错,并且已经根据上面后续操作,那可能就是SElinux的问题,我们进行临时禁用:

1
setenforce 0

即可正常登录,然后修改/etc/selinux/config 文件:

1
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

进行永久禁用SElinux即可。

  • 注意:
    如果Centos7默认openssl版本不为OpenSSL 1.0.2k,就需要先进行升级:
    1
    yum install openssl -y
  • 然后回到第一步进行安装即可。