linux升级openssh到7.9

  客户linux主机ssh存在高危漏洞,须要进行升级修复。html

  linux联网后,直接命令行:linux

[root@gw ~]# yum update openssl -y

  此命令只是小版本的升级,好比将openssl从1.0.1e-43版本升级到1.0.1e-57版本,但好多时候咱们须要升级的是大版本,而不是这种小的修复。git

  yum remove openssl命令执行时,能够看到,很是很是多的软件是依赖于openssl软件。openssl是一个很是基础的软件。编译安装一个新版本的openssl覆盖掉操做系统自带的openssl。这就会致使那些依赖于openssl的软件的openssl相关的功能变得不可用。shell

  ssh命令是openssh软件的一部分。由于上面提出的现象咱们不直接升级操做系统的openssl,可是咱们能够另外编译一个openssl,放到单独的应用目录中,与操做系统的openssl互不影响。再基于新编译出来的openssl,将新的openssh软件编译出来。 而操做系统的openssh是能够被替换的。若是你尝试执行 yum remove openssh* 命令就能够看到,没有其它软件依赖于openssh。此外,openssh软件提供了sshd服务。因此,咱们只要还要配置并搭建好sshd服务,就能够替代操做系统自带的openssh了。vim

  接下来咱们分析,如何升级openssh及其所依赖的openssl。缓存

从openssh官网下载openssh 7.9.p1源码包,查看里面的INSTALL文件,里面有对它的依赖关系作说明 能够依据blfs下载(http://www.linuxfromscratch.org/blfs/news.html安全

 

  openssh7.9p1下载安装说明,可参考解压后的install文件(http://www.linuxfromscratch.org/blfs/view/8.4/postlfs/openssh.htmlbash

1,提早检查必须安装的(Zlib、libcrypto或者(LibreSSL或OpenSSL))session

  • Zlib 1.1.4或1.2.1.2或更高版本(早期1.2.x版本有问题)  http://www.gzip.org/zlib/
  • libcrypto(LibreSSL或OpenSSL> = 1.0.1 <1.1.0),这个库能够用LibreSSL或OpenSSL替代,但应该将LibreSSL / OpenSSL编译为与位置无关的库(即便用-fPIC)
    不然OpenSSH将没法与之连接。若是您必须使用非位置无关的libcrypto,那么您可能须要配置OpenSSH - without-pie。 请注意,因为API更改,目前不支持OpenSSL 1.1.x.
    LibreSSL http://www.libressl.org/; 
    OpenSSL http://www.openssl.org/

2,还有一些是可选的安装,例如PAM软件等app

1. Prerequisites
----------------

A C compiler.  Any C89 or better compiler should work.  Where supported,
configure will attempt to enable the compiler's run-time integrity checking
options.  Some notes about specific compilers:
 - clang: -ftrapv and -sanitize=integer require the compiler-rt runtime
  (CC=clang LDFLAGS=--rtlib=compiler-rt ./configure)

You will need working installations of Zlib and libcrypto (LibreSSL /
OpenSSL)

Zlib 1.1.4 or 1.2.1.2 or greater (earlier 1.2.x versions have problems):
http://www.gzip.org/zlib/

libcrypto (LibreSSL or OpenSSL >= 1.0.1 < 1.1.0)
LibreSSL http://www.libressl.org/ ; or
OpenSSL http://www.openssl.org/

LibreSSL/OpenSSL should be compiled as a position-independent library
(i.e. with -fPIC) otherwise OpenSSH will not be able to link with it.
If you must use a non-position-independent libcrypto, then you may need
to configure OpenSSH --without-pie.  Note that because of API changes,
OpenSSL 1.1.x is not currently supported.

The remaining items are optional.

NB. If you operating system supports /dev/random, you should configure
libcrypto (LibreSSL/OpenSSL) to use it. OpenSSH relies on libcrypto's
direct support of /dev/random, or failing that, either prngd or egd

PRNGD:

If your system lacks kernel-based random collection, the use of Lutz
Jaenicke's PRNGd is recommended.

http://prngd.sourceforge.net/

EGD:

If the kernel lacks /dev/random the Entropy Gathering Daemon (EGD) is
supported only if libcrypto supports it.

http://egd.sourceforge.net/

PAM:

OpenSSH can utilise Pluggable Authentication Modules (PAM) if your
system supports it. PAM is standard most Linux distributions, Solaris,
HP-UX 11, AIX >= 5.2, FreeBSD and NetBSD.

Information about the various PAM implementations are available:

Solaris PAM:	http://www.sun.com/software/solaris/pam/
Linux PAM:	http://www.kernel.org/pub/linux/libs/pam/
OpenPAM:	http://www.openpam.org/

If you wish to build the GNOME passphrase requester, you will need the GNOME
libraries and headers.

GNOME:
http://www.gnome.org/

Alternatively, Jim Knoble <jmknoble@pobox.com> has written an excellent X11
passphrase requester. This is maintained separately at:

http://www.jmknoble.net/software/x11-ssh-askpass/

LibEdit:

sftp supports command-line editing via NetBSD's libedit.  If your platform
has it available natively you can use that, alternatively you might try
these multi-platform ports:

http://www.thrysoee.dk/editline/
http://sourceforge.net/projects/libedit/

LDNS:

LDNS is a DNS BSD-licensed resolver library which supports DNSSEC.

http://nlnetlabs.nl/projects/ldns/

Autoconf:

If you modify configure.ac or configure doesn't exist (eg if you checked
the code out of git yourself) then you will need autoconf-2.69 to rebuild
the automatically generated files by running "autoreconf".  Earlier
versions may also work but this is not guaranteed.

http://www.gnu.org/software/autoconf/

Basic Security Module (BSM):

Native BSM support is known to exist in Solaris from at least 2.5.1,
FreeBSD 6.1 and OS X.  Alternatively, you may use the OpenBSM
implementation (http://www.openbsm.org).

makedepend:

https://www.x.org/archive/individual/util/

If you are making significant changes to the code you may need to rebuild
the dependency (.depend) file using "make depend", which requires the
"makedepend" tool from the X11 distribution.

安装Zlib

Zlib用于压缩和解压缩的功能。操做系统已经自带了zlib,能够查看是否符合要求。实际上,openssl和openssh都依赖于zlib。执行下面的命令,安装zlib开发包:

 rpm -qa|grep zlib
yum -y install zlib-devel

安装PAM

PAM(Pluggable Authentication Modules,可插拔认证模块)用于提供安全控制。操做系统也已经自带了PAM,版本也是能够的。执行下面的命令,安装PAM开发包:

rpm -qa|grep pam
yum -y install pam-devel

 

安装tcp_wrappers

tcp_wrappers是一种安全工具,一般,咱们在/etc/hosts.allow或/etc/hosts.deny文件中配置的过滤规则就是使用的tcp_wrappers的功能了。openssh在编译时的确是能够选择支持tcp_wrappers的。执行下面的命令,安装tcp_wrappers开发包:

rpm -qa|grep tcp_wrappers
yum install tcp_wrappers-devel -y

系统最小化安装时,须要安装包

通常出现这个-bash: make: command not found提示,是由于安装系统的时候使用的是最小化mini安装,系统没有安装make、vim等经常使用命令,直接yum安装下便可。

yum -y install gcc automake autoconf libtool make

 

安装OpenSSL

因为OpenSSH 7.9p1要求OpenSSL的版本> = 1.0.1 <1.1.0,所以,当前符合要求的最新OpenSSL版本为1.0.2l https://www.openssl.org/source/old/1.0.2/

首先,从openssl官网下载源码包openssl-fips-2.0.16.tar.gz(https://www.openssl.org/source/old/fips/)将其安装到/opt/fips-2.0目录下。

编译安装FIPS宏(https://bbs.csdn.net/wap/topics/390830236):

mkdir /opt/fips-2.0
# export FIPSDIR=/opt/fips-2.0                 --指定另外的安装目录,默认会安装在/usr/local/ssl/fips­2.0目录
# tar -xvf openssl-fips-2.0.16.tar.gz
# cd openssl-fips-2.0.16
# ./config
# make
# make install

从openssl官网下载源码包openssl-1.0.2l.tar.gz,将其安装到/opt/openssl1.0.2l_2019目录下。将openssl安装到专门的目录,这是为了不对操做系统自带的openssl形成影响。

 编译安装OpenSSL

mkdir /opt/openssl1.0.2l_2019
# tar -xvf openssl-1.0.2l.tar.gz
# cd openssl-1.0.2l
# ./config --prefix=/opt/openssl1.0.2l_2019 --openssldir=/opt/openssl1.0.2l_2019/openssl fips --with-fipsdir=/opt/fips-2.0 zlib-dynamic shared -fPIC
# make depend
# make
# make test
# make install
  • --prefix:指定openssl的安装目录。按本例中的安装方式,安装完成后该目录下会包含bin(含二进制程序)、lib(含动态库文件)、include/openssl(含报头文件)及openssl(--openssldir选项指定的)这些子目录。

  • --openssldir:指定openssl文件的安装目录。按本例中的安装方式,安装完成后该目录下会包括certs(存放证书文件)、man(存放man文件)、misc(存放各类脚本)、private(存放私钥文件)这些子目录及openssl.cnf配置文件。

  • fips:集成FIPS模块。

  • --with-fipsdir:指向FIPS模块的安装目录位置。

  • zlib-dynamic:编译支持zlib压缩/解压缩,让openssl加载zlib动态库。该选项只在支持加载动态库的操做系统上才支持。这是默认选项。

  • shared:除了静态库之外,让openssl(在支持的平台上)也编译生成openssl动态库。

  • -fPIC:将openssl动态库编译成位置无关(position-independent)的代码。

安装完成后,将OpenSSL的库文件目录添加到/etc/ld.so.conf文件中,并加载到系统内存缓存中:

# echo '/opt/openssl1.0.2l_2019/lib' >> /etc/ld.so.conf
# ldconfig

安装OpenSSH

从openssl官网下载源码包openssh-7.9p1.tar.gz,将其安装到/opt/openssh7.9.p1_2019目录下。将openssh安装到专门的目录,这是为了不与操做系统自带的openssh形成没必要要的冲突。

编译安装OpenSSH:

mkdir /opt/openssh7.9.p1_2019
# tar -xvf openssh-7.9p1.tar.gz # cd openssh-7.9p1 # ./configure --prefix=/opt/openssh7.9.p1_2019 --with-ssl-dir=/opt/openssl1.0.2l_2019 --with-pam --with-tcp-wrappers # make # make install
  • --prefix:指定安装目录

  • --with-ssl-dir=DIR:指向LibreSSL/OpenSSL库的安装目录的所在路径。

  • --with-pam:启用PAM支持。根据OpenSSH的安装说明,若是在编译时启用了PAM,那么在安装完成后,也必须在sshd服务的配置文件sshd_config中启用它(使用UsePAM指令)。

 

根据OpenSSH的安装说明,若是有启用PAM,那么就须要手工安装一个给sshd程序使用的PAM配置文件,不然安装好OpenSSH后你可能会没法使用密码登陆系统。在编译时,我使用 --with-pam 选项启用了对PAM的支持,可是,编译OpenSSH时并无编译选项让你指定PAM配置文件的位置,那么咱们要怎么提供这个配置文件呢?

事实上,OpenSSH有另一个编译选项--with-pam-service=name能够指定PAM服务名,它的默认值是sshd。而操做系统自带的PAM软件默认将全部PAM配置文件都放置在/etc/pam.d目录下。结合这两个信息,就可肯定OpenSSH的PAM配置文件应为/etc/pam.d/sshd文件。而这个文件原来就有了,因此咱们不用额外手工建立一个。

 

设置PATH路径:

# echo 'export PATH=/opt/openssh7.9.p1_2019/bin:/opt/openssh7.9.p1_2019/sbin:$PATH' >> /etc/profile.d/path.sh
# . /etc/profile.d/path.sh

此时,使用ssh -V命令就能够看到新版本号了

ssh -V

 

配置OpenSSH

前面已经安装好了openssh,可是咱们还须要配置它,以保证sshd服务能够启起来。咱们能够先看一下原有的sshd服务(属于openssh-server软件包)都有哪些配置文件:

# rpm -ql openssh-server | grep -i --color etc
/etc/pam.d/ssh-keycat
/etc/pam.d/sshd
/etc/rc.d/init.d/sshd
/etc/ssh/sshd_config
/etc/sysconfig/sshd

能够看到,sshd服务的配置文件为/etc/ssh/sshd_config,它的pam配置文件为/etc/pam.d/sshd和/etc/pam.d/ssh-keycat,启动脚本文件为/etc/rc.d/init.d/sshd,启动脚本里面有引用到文件/etc/sysconfig/sshd。

参照系统原有的配置文件修改咱们软件的sshd_config配置文件,这是sshd服务的配置文件(红色字体的为新增或修改的部分):

# vim /opt/openssh7.9.p1_2019/etc/sshd_config
Protocol 2 SyslogFacility AUTHPRIV PermitRootLogin yes
AuthorizedKeysFile      .ssh/authorized_keys
PasswordAuthentication yes ChallengeResponseAuthentication no
#GSSAPIAuthentication yes                                                    //该选项目前还不支持
#GSSAPICleanupCredentials yes                                            //该选项目前还不支持
UsePAM yes
 
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE AcceptEnv XMODIFIERS X11Forwarding yes
Subsystem       sftp    /opt/openssh7.9.p1_2019/libexec/sftp-server

注意,UsePAM必定要启用,OpenSSH的安装说明里有提到,若是编译时启用了PAM支持,那么就必须在sshd_config文件中启用它。

拷贝系统原有的配置文件/etc/sysconfig/sshd到咱们软件下面,这个配置文件用于设置启动sshd服务所需的环境变量,在sshd服务的启动脚本里有调用到该配置文件:

# cp -a /etc/sysconfig/sshd /opt/openssh7.9.p1_2019/etc/sshd

接下来要修改sshd服务的启动脚本/etc/rc.d/init.d/sshd。先将启动脚本备份一份为sshd.old,并添加至chkconfig管理: 

# cp /etc/rc.d/init.d/sshd /etc/rc.d/init.d/sshd.old
# chkconfig --add sshd.old

再根据咱们的OpenSSH的安装路径,来修改原有的启动脚本(红色字体为有新增或修改的部分-注:linux7以后没有这个文件,能够本身新建一个启动文件,手动添加这个服务,开机自启动等启动sshd):

修改配置文件权限

chmod u+x /etc/rc.d/init.d/sshdnew

添加sshd服务

chkconfig --add sshdnew
设置开机自启动
chkconfig sshdnew on 验证开机启动 chkconfig
--list|grep sshdnew service sshdnew start systemctl is-active sshdnew

 

vim /etc/rc.d/init.d/sshd
#!/bin/bash
#
# sshd        Start up the OpenSSH server daemon
#
# chkconfig: 2345 55 25
# description: SSH is a protocol for secure remote shell access. \
#              This service starts up the OpenSSH server daemon.
#
# processname: sshd
# config: /etc/ssh/ssh_host_key
# config: /etc/ssh/ssh_host_key.pub
# config: /etc/ssh/ssh_random_seed
# config: /etc/ssh/sshd_config
# pidfile: /var/run/sshd.pid

### BEGIN INIT INFO
# Provides: sshd
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $syslog
# Should-Start: $syslog
# Should-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start up the OpenSSH server daemon
# Description:       SSH is a protocol for secure remote shell access.
#             This service starts up the OpenSSH server daemon.
### END INIT INFO

# source function library
. /etc/rc.d/init.d/functions

# pull in sysconfig settings
[ -f /opt/openssh7.9.p1_2019/etc/sshd ] && . /opt/openssh7.9.p1_2019/etc/sshd

RETVAL=0
prog="sshd"
lockfile=/var/lock/subsys/$prog

# Some functions to make the below more readable
KEYGEN=/opt/openssh7.9.p1_2019/bin/ssh-keygen
SSHD=/opt/openssh7.9.p1_2019/sbin/sshd
RSA1_KEY=/etc/ssh/ssh_host_key
RSA_KEY=/opt/openssh7.9.p1_2019/etc/ssh_host_rsa_key
DSA_KEY=/opt/openssh7.9.p1_2019/etc/ssh_host_dsa_key
PID_FILE=/var/run/sshd.pid     # PID文件的所在路径,这个变量的值不要改

runlevel=$(set -- $(runlevel); eval "echo \$$#" )

fips_enabled() {
    if [ -r /proc/sys/crypto/fips_enabled ]; then
        cat /proc/sys/crypto/fips_enabled
    else
        echo 0
    fi
}

do_rsa1_keygen() {
    if [ ! -s $RSA1_KEY -a `fips_enabled` -eq 0 ]; then
        echo -n $"Generating SSH1 RSA host key: "
        rm -f $RSA1_KEY
        if test ! -f $RSA1_KEY && $KEYGEN -q -t rsa1 -f $RSA1_KEY -C '' -N '' >&/dev/null; then
            chmod 600 $RSA1_KEY
            chmod 644 $RSA1_KEY.pub
            if [ -x /sbin/restorecon ]; then
                /sbin/restorecon $RSA1_KEY.pub
            fi
            success $"RSA1 key generation"
            echo
        else
            failure $"RSA1 key generation"
            echo
            exit 1
        fi
    fi
}

do_rsa_keygen() {
    if [ ! -s $RSA_KEY ]; then
        echo -n $"Generating SSH2 RSA host key: "
        rm -f $RSA_KEY
        if test ! -f $RSA_KEY && $KEYGEN -q -t rsa -f $RSA_KEY -C '' -N '' >&/dev/null; then
            chmod 600 $RSA_KEY
            chmod 644 $RSA_KEY.pub
            if [ -x /sbin/restorecon ]; then
                /sbin/restorecon $RSA_KEY.pub
            fi
            success $"RSA key generation"
            echo
        else
            failure $"RSA key generation"
            echo
            exit 1
        fi
    fi
}

do_dsa_keygen() {
    if [ ! -s $DSA_KEY ]; then
        echo -n $"Generating SSH2 DSA host key: "
        rm -f $DSA_KEY
        if test ! -f $DSA_KEY && $KEYGEN -q -t dsa -f $DSA_KEY -C '' -N '' >&/dev/null; then
            chmod 600 $DSA_KEY
            chmod 644 $DSA_KEY.pub
            if [ -x /sbin/restorecon ]; then
                /sbin/restorecon $DSA_KEY.pub
            fi
            success $"DSA key generation"
            echo
        else
            failure $"DSA key generation"
            echo
            exit 1
        fi
    fi
}

do_restart_sanity_check()
{
    $SSHD -t
    RETVAL=$?
    if [ $RETVAL -ne  0 ]; then
        failure $"Configuration file or keys are invalid"
        echo
    fi
}

start()
{
    [ -x $SSHD ] || exit 5
    [ -f /opt/openssh7.9.p1_2019/etc/sshd_config ] || exit 6
    # Create keys if necessary
    if [ "x${AUTOCREATE_SERVER_KEYS}" != xNO ]; then
    #    do_rsa1_keygen             # 注释掉这条语句
        do_rsa_keygen
        do_dsa_keygen
    fi

    echo -n $"Starting $prog: "
    $SSHD $OPTIONS && success || failure
    RETVAL=$?
    [ $RETVAL -eq 0 ] && touch $lockfile
    echo
    return $RETVAL
}

stop()
{
    echo -n $"Stopping $prog: "
    killproc -p $PID_FILE $SSHD
    RETVAL=$?
    # if we are in halt or reboot runlevel kill all running sessions
    # so the TCP connections are closed cleanly
    if [ "x$runlevel" = x0 -o "x$runlevel" = x6 ] ; then
        trap '' TERM
        killall $prog 2>/dev/null
        trap TERM
    fi
    [ $RETVAL -eq 0 ] && rm -f $lockfile
    echo
}

reload()
{
    echo -n $"Reloading $prog: "
    killproc -p $PID_FILE $SSHD -HUP
    RETVAL=$?
    echo
}

restart() {
    stop
    start
}

force_reload() {
    restart
}

rh_status() {
    status -p $PID_FILE openssh-daemon
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        start
        ;;
    stop)
        if ! rh_status_q; then
            rm -f $lockfile
            exit 0
        fi
        stop
        ;;
    restart)
        restart
        ;;
    reload)
        rh_status_q || exit 7
        reload
        ;;
    force-reload)
        force_reload
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        if [ -f $lockfile ] ; then
            do_restart_sanity_check
            if [ $RETVAL -eq 0 ] ; then
                stop
                # avoid race
                sleep 3
                start
            else
                RETVAL=6
            fi
        fi
        ;;
    status)
        rh_status
        RETVAL=$?
        if [ $RETVAL -eq 3 -a -f $lockfile ] ; then
            RETVAL=2
        fi
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status}"
        RETVAL=2
esac
exit $RETVAL

 root输入密码登陆拒毫不成功能够:

在$SSHD $OPTIONS && success || failure这句话前面加一句:

OPTIONS="-f /etc/ssh/sshd_config"

配置以下:

        echo -n $"Starting $prog:"
        OPTIONS="-f /opt/ssh7.9/etc/sshd_config"  --同上目录/opt/openssh7.9.p1_2019/etc/sshd_config
 $SSHD $OPTIONS && success || failure RETVAL=$?

 

 因为OpenSSH依赖的OpenSSL已不支持rsa1,因此我将启动脚本中生成rsa1秘钥的指令注释掉了。

接下来,关键的一步来了,咱们要关闭旧的sshd服务,启动新的sshd服务。这个操做若是失败了,不会致使现有的ssh远程链接断开。因此咱们能够先关闭旧的sshd程序,再启动新的sshd程序:

# service sshd.old stop
# service sshd start

若是新的sshd服务启动成功了,咱们能够先简单测试下,好比,看看普通用户和root用户是否能正常经过ssh登陆。若是没有没有问题,咱们能够在测测其它的,好比scp、sftp是否正常等。固然,若是有条件的话,可使用漏洞扫描工具扫一下,看看有没有什么咱们没有注意到的地方。 

最后,就能够删除掉旧sshd服务的启动脚本了,以避免冲突:

# rm -f /etc/init.d/sshd.old

openssl和openssh的编译选项基原本说我也只是使用了必要的选项,咱们编译出来的openssl和openssh软件在功能特性上只是尽量地接近原有的,安全性和性能可能也是有差别。

复用了操做系统原有的sshd服务的配置文件和启动脚本,这可能没法充分利用新版本openssh的特性。openssl和openssh原本也是挺复杂的东西,一时半会可能也很难彻底弄明白。

这种升级方式,应该会比强制升级openssl和openssh的方式好不少。

参考https://blog.51cto.com/techsnail/2138927,独指蜗牛 只作平常积累,不作商业用途,侵权即删。

相关文章
相关标签/搜索