升級openssl並重新編譯Nginx


在漏洞掃描的時候出現“啟用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

 

--返回目錄--


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM