目录

OPENSSL & OPENSSH 升级

17
SSL、SSH、OPENSSL 、OPENSSH 的区别

SSL是通讯链路的附加层。可以包含很多协议。https, ftps, .....

SSL是一种国际标准的加密及身份认证通信协议,您用的浏览器就支持此协议。SSL(Secure Sockets Layer)最初是由美国Netscape公司研究出来的,后来成为了Internet网上安全通讯与交易的标准。SSL协议使用通讯双方的客户证书以及CA根证书,允许客户/服务器应用以一种不能被偷听的方式通讯,在通讯双方间建立起了一条安全的、可信任的通讯通道。它具备以下基本特征:信息保密性、信息完整性、相互鉴定。 主要用于提高应用程序之间数据的安全系数。SSL协议的整个概念可以被总结为:一个保证任何安装了安全套接字的客户和服务器间事务安全的协议,它涉及所有TC/IP应用程序。

SSH只是加密的shell,最初是用来替代telnet的。通过port forward,也可以让其他协议通过ssh的隧道而起到加密的效果。

SSH的英文全称是Secure SHell。通过使用SSH,你可以把所有传输的数据进行加密,这样“中间人”这种攻击方式就不可能实现了,而且也能够防止DNS和IP欺骗。还有一个额外的好处就是传输的数据是经过压缩的,所以可以加快传输的速度。SSH有很多功能,它既可以代替telnet,又可以为ftp、pop、甚至ppp提供一个安全的“通道”。SSH是由客户端和服务端的软件组成的,有两个不兼容的版本分别是:1.x和2.x。用SSH 2.x的客户程序是不能连接到SSH 1.x的服务程序上去的。OpenSSH 2.x同时支持SSH 1.x和2.x。SSH的安全验证是如何工作的从客户端来看,SSH提供两种级别的安全验证。第一种级别(基于口令的安全验证)只要你知道自己帐号和口令,就可以登录到远程主机。所有传输的数据都会被加密,但是不能保证你正在连接的服务器就是你想连接的服务器。可能会有别的服务器在冒充真正的服务器,也就是受到“中间人”这种方式的攻击。第二种级别(基于密匙的安全验证)需要依靠密匙,也就是你必须为自己创建一对密匙,并把公用密匙放在需要访问的服务器上。如果你要连接到SSH服务器上,客户端软件就会向服务器发出请求,请求用你的密匙进行安全验证。服务器收到请求之后,先在你在该服务器的家目录下寻找你的公用密匙,然后把它和你发送过来的公用密匙进行比较。如果两个密匙一致,服务器就用公用密匙加密“质询”(challenge)并把它发送给客户端软件。客户端软件收到“质询”之后就可以用你的私人密匙解密再把它发送给服务器。用这种方式,你必须知道自己密匙的口令。但是,与第一种级别相比,第二种级别不需要在网络上传送口令。第二种级别不仅加密所有传送的数据,而且“中间人”这种攻击方式也是不可能的(因为他没有你的私人密匙)。但是整个登录的过程可能需要10秒。

OpenSSL------一个C语言函数库,是对SSL协议的实现。

SSH 利用 OpenSSL 提供的库。OpenSSL 中也有个叫做 OpenSSL 的工具,是 OpenSSL 中的库的命令行接口。

OpenSSH-----是对SSH协议的实现。

OpenSSH依赖于openssl,没有openssl的话openssh就编译不过去,也运行不了。

HTTPS可以使用TLS或者SSL协议,而openssl是TLS、SSL协议的开源实现,提供开发库和命令行程序。openssl很优秀,所以很多涉及到数据加密、传输加密的地方都会使用openssl的库来做。可以理解成所有的HTTPS都使用了openssl。


PS: 重中之重,在验证升级完成前,千万不要关闭升级时的正常连接!!!

一、下载相应安装包

首先介绍一下openssh、openssl、zlib的关系:

openssl依赖于zlib,openssh依赖于openssl和zlib,所以我们要先安装zlib,然后是openssl,最后是openssh。

wget https://www.zlib.net/zlib-1.2.13.tar.gz
tar -xvf zlib-1.2.13.tar.gz
wget https://www.openssl.org/source/openssl-1.1.1t.tar.gz
tar -xvf openssl-1.1.1t.tar.gz
#openssh有多个镜像站提供下载,随意挑选能访问的既可
wget https://ftp.jaist.ac.jp/pub/OpenBSD/OpenSSH/portable/openssh-9.3p1.tar.gz
tar -xvf openssh-9.3p1.tar.gz

二、安装zlib

#进入解压好的目录    
cd zlib-1.2.13
# ./configure --prefix=/usr 意思是将该软件安装在 /usr 下面,执行文件就会安装在 /usr/bin ,资源文件就会安装在 /usr/share
# 如果没有使用--prefix指定路径那么都是走默认路径:可执行文件默认放在/usr/local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc,其它的资源文件放在/usr/local/share,比较凌乱
# 如果执行提示编译报错,请安装gcc
./configure --prefix=/usr/local/zlib
make && make install

三、升级openssl

#编译源码安装
cd openssl-1.1.1t
./config shared --prefix=/usr/local/openssl
#备份老版本,有些系统可能include目录下没有openssl。可跳过
mv /usr/bin/openssl /usr/bin/openssl.bak
mv /usr/include/openssl /usr/include/openssl.bak
#安装
make && make install
#建立软连接
ln -s /usr/local/openssl/bin/openssl   /usr/bin/openssl
ln -s /usr/local/openssl/include/openssl   /usr/include/openssl
#升级后如果执行 openssl version 命令出现openssl: error while loading shared libraries: 
#  libssl.so.1.1: cannot open shared object file: No such file or directory错误。执行一下两条命令
ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
#更新动态链接库数据并加载新配置:
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
ldconfig

四、升级openssh

#下面configure的时候使用了 --with-pam需要安装该包
wget https://github.com/linux-pam/linux-pam/releases/download/v1.5.2/Linux-PAM-1.5.2.tar.xz
cd Linux-PAM-1.5.2
./configure
make && make install

#安装openssh
#1、备份旧版文件
mv /usr/sbin/sshd /usr/sbin/sshd_old
#mv /etc/sysconfig/sshd /etc/sysconfig/sshd_old
mv /etc/ssh/sshd_config /etc/ssh/sshd_config_old
mv /etc/ssh/ssh_config /etc/ssh/ssh_config_old
mv /etc/ssh/moduli /etc/ssh/moduli_old
#mv /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/sshd.service_old


#2、卸载旧openssh
for i in $(rpm -qa |grep openssh);do rpm -e $i --nodeps ;done

#3、开始安装
cd openssh-9.3p1
#--with-zlib注意指定zlib目录
./configure --prefix=/usr/local/openssh --sysconfdir=/etc/ssh --with-md5-passwords --with-pam --with-tcp-wrappers --with-ssl-dir=/usr/local/openssl --with-zlib=/usr/local/lib --without-hardening

# 如果不升级openssl,使用自带rpm包openssl,使用一下命令
#./configure --prefix=/usr/local/openssh --sysconfdir=/etc/ssh --with-md5-passwords --with-pam --with-tcp-wrappers --with-openssl=/usr/bin/openssl --with-zlib=/usr/local/zlib --without-hardening
#openssh  make && make install 时似乎存在连用失败的情况,推荐分开使用
make
make install

#4、如需使用原有sshd_config配置,可从上面就文件恢复
\cp -a /etc/ssh/sshd_config_old /etc/ssh/sshd_config
\cp -a /etc/ssh/ssh_config_old /etc/ssh/ssh_config
\cp -a /etc/ssh/moduli_old /etc/ssh/moduli

#5、替换新版本openssh命令
\cp -arf /usr/local/openssh/bin/* /usr/bin/
\cp -arf /usr/local/openssh/sbin/sshd /usr/sbin/sshd

#6、拷贝启动脚本(openssh解压目录执行)
\cp -a contrib/redhat/sshd.init /etc/init.d/sshd 
\cp -a contrib/redhat/sshd.pam /etc/pam.d/sshd.pam
chmod +x /etc/init.d/sshd

#7、centos7.9需编辑/etc/ssh/sshd_config,将GSSAPIAuthentication、GSSAPICleanupCredentials注释掉;
sed -i 's/GSSAPIAuthentication/#&/g' /etc/ssh/sshd_config
sed -i 's/GSSAPICleanupCredentials/#&/g' /etc/ssh/sshd_config
# 去掉注释,PermitRootLogin、PasswordAuthentication、UsePAM 值应为 yes
vim /etc/ssh/sshd_config
#8、到这步设置完后重启sshd服务,可能会存在远程无法登陆,是因为我们使用了--with-pam.w我们会在confiure之后看到这个提示: 
#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
#意思是如果启用PAM,需要有一个控制文件,新建或修改/etc/pam.d/sshd文件,写入一下内容:

%PAM-1.0
auth       required     pam_sepermit.so
auth       substack     password-auth
auth       include      postlogin
Used with polkit to reauthorize users in remote sessions
-auth      optional     pam_reauthorize.so prepare
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    required     pam_namespace.so
session    optional     pam_keyinit.so force revoke
session    include      password-auth
session    include      postlogin
Used with polkit to reauthorize users in remote sessions
-session   optional     pam_reauthorize.so prepare

#注释掉一些内容
sed -i 's/%PAM-1.0/#&/g' /etc/pam.d/sshd
sed -i 's/Used with polkit to reauthorize users in remote sessions/#&/g' /etc/pam.d/sshd
sed -i 's/pam_selinux.so close should be the first session rule/#&/g' /etc/pam.d/sshd
sed -i 's/pam_selinux.so open should only be followed by sessions to be executed in the user context/#&/g' /etc/pam.d/sshd
sed -i 's/Used with polkit to reauthorize users in remote sessions/#&/g' /etc/pam.d/sshd

#9、设置sshd开机自启动,并验证版本
systemctl daemon-reload
chkconfig --add sshd && chkconfig sshd on
systemctl start sshd

五、升级完成,不要关闭现有连接,重新开启新连接进行验证

#查看openssh 版本信息
ssh -V
#查看openssl 详细版本信息
openssl version -a


#1、升级openssh可能导致sftp无法连接,则修改/etc/ssh/sshd_config文件
#	Subsystem       sftp    /usr/libexec/openssh/sftp-server
#	替换为		
#	Subsystem   		sftp   	internal-sftp 

#2、如升级后新建ssh无法连接,千万不要关闭已有连接
#查看sshd报错
systemctl status sshd
#会提示找不到组件


#3、附加操作
#把该服务器上的有关该环境的服务器的登陆记录都删掉
#  vim /home/cbgadmin/.ssh/known_hosts