升级openssl并重新编译Nginx

2023-03-12,,

在漏洞扫描的时候出现“启用TLS1.0”的安全漏洞,描述为:不被视为 PCI 数据安全标准,推荐使用TLS1.2及以上版本;
我这边服务器使用的是CentOS7,默认自带的openssl是1.0.2版本,当前的最新稳定版本是1.1.1k,支持TLS1.2和TLS1.3;

文档内容:

  --->升级 openssl 编译安装

  --->重新编译安装 Nginx 并配置测试

升级 openssl 编译安装

首先解决环境所依赖的各种软件包:

[root@kafka-test ~]# yum -y install perl perl-devel gcc gcc-c++

前往官网下载最新稳定版本:
https://www.openssl.org/source/

将下载的tgz包解压,并进行后续的编译安装

    ##查看安装包
[root@kafka-test ~]# cd /opt/
[root@kafka-test opt]# ls -l openssl-1.1.1k.tar.gz
-rw-r--r--. 1 root root 9823400 Jun 19 22:57 openssl-1.1.1k.tar.gz ##解压软件压缩包
[root@kafka-test opt]# tar -xvf openssl-1.1.1k.tar.gz
... ...
... ...
[root@kafka-test opt]# cd openssl-1.1.1k ##进行编译安装
##这里我没有指定安装的路径,使用默认,有需求请 --prefix= 指定路径
[root@kafka-test openssl-1.1.1k]# ./config
Operating system: x86_64-whatever-linux2
Configuring OpenSSL version 1.1.1k (0x101010bfL) for linux-x86_64
Using os-specific seed configuration
Creating configdata.pm
Creating Makefile **********************************************************************
*** ***
*** OpenSSL has been successfully configured ***
*** ***
*** If you encounter a problem while building, please open an ***
*** issue on GitHub <https://github.com/openssl/openssl/issues> ***
*** 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 file first) ***
*** ***
********************************************************************** [root@kafka-test openssl-1.1.1k]# make && make install
... ...
... ...

此时查看新版本信息会报错

    ##此时的报错是缺少文件,一共两个,当处理完一个还会有另一个报错
[root@kafka-test ~]# /usr/local/bin/openssl version
/usr/local/bin/openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory [root@kafka-test ~]# /usr/local/bin/openssl version
/usr/local/bin/openssl: error while loading shared libraries: libcrypto.so.1.1: cannot open shared object file: No such file or directory ##将两个缺少的库文件做软连接即可
[root@kafka-test ~]# ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/
[root@kafka-test ~]# ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/ ##此时再次查看新版本信息,安装完成
[root@kafka-test ~]# /usr/local/bin/openssl version
OpenSSL 1.1.1k 25 Mar 2021

确定系统默认的openssl是哪个版本,是否需要新旧替换

  这里有一个需要说明的问题:

    我在公司的服务器上安装新版的openssl之后,默认的openssl还是原来的 1.0.2k 版本,而在家测试安装的时候却直接变为新版的 1.1.1k;

    但这个命令原本用的是 /usr/bin/openssl ,新版安装后变为 /usr/local/bin/openssl ,所以默认使用直接变为了新版;

    说实话,我自己也没有深究为什么有这个情况,但是我有应对依然默认旧版得到方法,下面写下新旧替换的方法;

    ##使用 which 命令来查看执行命令实际使用的是那个路径下的文件
##情况分为两种,第一种则是直接变更为了新版,无需你再做新旧替换
##第二种则是依然使用的旧版,需要我们手动去替换命令
[root@kafka-test ~]# which openssl
/usr/local/bin/openssl ##这个是我家里测试的情况,默认命令的路径已经更改
##当你使用 openssl 命令的时候,它已经是 1.1.1k 的版本了
[root@kafka-test ~]# /usr/local/bin/openssl version
OpenSSL 1.1.1k 25 Mar 2021 ##其他的命令路径还有如下两个,其版本为旧版的 1.0.2k
##随后我又开了另一个虚拟机,其使用的是 /usr/bin/openssl 这个路径下的命令
[root@kafka-test ~]# /usr/bin/openssl version
OpenSSL 1.0.2k-fips 26 Jan 2017 [root@kafka-test ~]# /bin/openssl version
OpenSSL 1.0.2k-fips 26 Jan 2017

新旧版本替换

    ##查看命令所使用的路径,将旧版的命令重命名
##然后进行新版命令的软连接创建即可替换完成
[root@kafka-test ~]# which openssl
/usr/bin/openssl
[root@kafka-test ~]# cd /usr/bin/
[root@kafka-test bin]# mv openssl openssl102
[root@kafka-test bin]# ln -s /usr/local/bin/openssl openssl

至此,openssl的升级已经完成

--返回目录--


下面是应对我所遇到的漏洞修复,对Nginx的重新编译

重新编译安装 Nginx 并配置测试

升级 openssl 主要的目的是使 Nginx 可以使用更安全的TLS,以及相应的密码套件;

重新编译(或初次)安装时记得加 --with-openssl 选项来指定使用的 openssl,安装新的 openssl 时我是没有指定路径,追加选项 --with-openssl=/usr/local/

[root@kafka-test nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --user=nginx \
> --group=nginx --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module \
> --with-http_sub_module --with-http_gzip_static_module --with-http_realip_module --with-stream \
> --with-openssl=/usr/local

当前情况下,执行 make 会报错

[root@kafka-test nginx-1.18.0]# make
make -f objs/Makefile
make[1]: Entering directory `/opt/nginx-1.18.0'
cd /usr/local \
&& if [ -f Makefile ]; then make clean; fi \
&& ./config --prefix=/usr/local/.openssl no-shared no-threads \
&& make \
&& make install_sw LIBDIR=lib
/bin/sh: line 2: ./config: No such file or directory
make[1]: *** [/usr/local/.openssl/include/openssl/ssl.h] Error 127
make[1]: Leaving directory `/opt/nginx-1.18.0'
make: *** [build] Error 2

原因是其找不到openssl相关的文件:make[1]: *** [/usr/local/.openssl/include/openssl/ssl.h] Error 127

这里需要修改编译的配置文件,在 nginx 解压目录下的 auto/lib/openssl/conf 中;
找相应的路径,将 .openssl 去掉,整合成相应可以找到的路径
  例如:CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"
  完整的路径为/usr/local/include/openssl/ssl.h

[root@kafka-test nginx-1.18.0]# vi auto/lib/openssl/conf

    ##原版配置文件
36 have=NGX_OPENSSL . auto/have
37 have=NGX_SSL . auto/have
38
39 CORE_INCS="$CORE_INCS $OPENSSL/.openssl/include"
40 CORE_DEPS="$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h"
41 CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a"
42 CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a"
43 CORE_LIBS="$CORE_LIBS $NGX_LIBDL"
44 CORE_LIBS="$CORE_LIBS $NGX_LIBPTHREAD" ##我们要修改
36 have=NGX_OPENSSL . auto/have
37 have=NGX_SSL . auto/have
38
39 CORE_INCS="$CORE_INCS $OPENSSL/include"
40 CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"
41 CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libssl.a"
42 CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libcrypto.a"
43 CORE_LIBS="$CORE_LIBS $NGX_LIBDL"
44 CORE_LIBS="$CORE_LIBS $NGX_LIBPTHREAD"

此时完成还会出现问题,会提示你找不到 libssl.a 和 libcrypto.a

[root@kafka-test nginx-1.18.0]# make
make -f objs/Makefile
make[1]: Entering directory `/opt/nginx-1.18.0'
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I
... ...
... ...
cc: error: /usr/local/lib/libssl.a: No such file or directory
cc: error: /usr/local/lib/libcrypto.a: No such file or directory
make[1]: *** [objs/nginx] Error 1
make[1]: Leaving directory `/opt/nginx-1.18.0'
make: *** [build] Error 2

这两个文件在我们压缩包解压目录下,将 libssl.a 和 libcrypto.a 放到相应目录下

    ##我们下载的包里面有,找到我们解压的目录
[root@kafka-test ~]# ls -l /opt/openssl-1.1.1k/{libssl.a,libcrypto.a}
-rw-r--r--. 1 root root 5640890 Jun 19 23:06 /opt/openssl-1.1.1k/libcrypto.a
-rw-r--r--. 1 root root 1024544 Jun 19 23:06 /opt/openssl-1.1.1k/libssl.a ##放到上面 conf 配置文件中配置的目录下
[root@kafka-test ~]# cp -a /opt/openssl-1.1.1k/libssl.a /usr/local/lib/
[root@kafka-test ~]# cp -a /opt/openssl-1.1.1k/libcrypto.a /usr/local/lib/

处理这些之后我们在编译安装就已经正常进行了;

注意:

  重新编译nginx的配置文件都是新的,可能并不是以前相同的配置,比如nginx的版本信息是否隐藏了;

  信息都是重新编译的,这种重新编译安装nginx的方法也适用于nginx升级,而且,它是不需要停服务的;

    ##重新编译安装
[root@kafka-test ~]# cd /opt/nginx-1.18.0
[root@kafka-test nginx-1.18.0]# make clean
rm -rf Makefile objs
[root@kafka-test nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --user=nginx \
> --group=nginx --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module \
> --with-http_sub_module --with-http_gzip_static_module --with-http_realip_module --with-stream \
> --with-openssl=/usr/local
... ...
... ... ##当你是重新编译安装的,结束看到的是提示,并不是报错
##初次安装是正常的打印信息
[root@kafka-test nginx-1.18.0]# make && make install
... ...
... ...
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory `/opt/nginx-1.18.0'

修改 nginx.conf 配置并验证

[root@kafka-test ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/200
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.1.1k 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx
--with-http_stub_status_module --with-http_v2_module --with-http_ssl_module
--with-http_sub_module --with-http_gzip_static_module --with-http_realip_module
--with-stream --with-openssl=/usr/local ##查看web信息
[root@kafka-test ~]# openssl s_client -connect 域名地址:443 -tls1_2
[root@kafka-test ~]# openssl s_client -connect 域名地址:443 -tls1_3

--返回目录--


升级openssl并重新编译Nginx的相关教程结束。

《升级openssl并重新编译Nginx.doc》

下载本文的Word格式文档,以方便收藏与打印。