Nginx range filter模塊數字錯誤漏洞修復 (Nginx平滑升級)


 

對線上生產環境服務器進行漏洞掃描, 發現有兩台前置機器存在Nginx range filter模塊數字錯誤漏洞, 當使用nginx標准模塊時,攻擊者可以通過發送包含惡意構造range域的header 請求,來獲取響應中的緩存文件頭部信息。該漏洞存在於Nginx 1.13.3以下版本中只要Ningx開啟了緩存功能, 攻擊者即可發送惡意請求進行遠程攻擊造成信息泄露。也就是說當Nginx服務器使用代理緩存的情況下, 緩存文件頭可能包含后端服務器的真實IP地址或其它敏感信息,攻擊者就可以利用該漏洞拿到這些敏感信息,從而導致信息泄露。故此漏洞存在安全隱患, 需要盡快修復. 

該漏洞修復方案一:

找到nginx的源碼解壓包, 在nginx的源碼包中找到ngx_http_range_filter_module.c
[root@dtin ~]# cd /data/software/nginx-1.12.2
[root@dtin nginx-1.12.2]# vim ./src/http/modules/ngx_http_range_filter_module.c
.......
        if (suffix) {
            start = content_length - end;
            end = content_length - 1;
        }
.......

將上面ngx_http_range_filter_module.c源碼文件中的"start = content_length - end;"改為“start = (end < content_length) ? content_length - end : 0;"
即改為:
[root@dtin nginx-1.12.2]# vim ./src/http/modules/ngx_http_range_filter_module.c
.......
        if (suffix) {
            start = (end < content_length) ? content_length - end : 0;
            end = content_length - 1;
        }
.......

接着重新編譯nginx,並進行替換即可。

該漏洞修復方案一 (推薦這一種)

由於是生產環境, 線上業務不能停, 故需要進行Nginx平滑升級, 升級過程中服務不能中斷. Nginx平滑升級原理如下介紹:
多進程模式下的請求分配方式
Nginx默認工作在多進程模式下,即主進程(master process)啟動后完成配置加載和端口綁定等動作,fork出指定數量的工作進程(worker process),這些子進程會持有監聽端口的文件描述符(fd),並通過在該描述符上添加監聽事件來接受連接(accept)。

信號的接收和處理
Nginx主進程在啟動完成后會進入等待狀態,負責響應各類系統消息,如SIGCHLD、SIGHUP、SIGUSR2等。

Nginx信號
主進程支持的信號
- TERM, INT: 立刻退出
- QUIT: 等待工作進程結束后再退出
- KILL: 強制終止進程
- HUP: 重新加載配置文件,使用新的配置啟動工作進程,並逐步關閉舊進程。
- USR1: 重新打開日志文件
- USR2: 啟動新的主進程,實現熱升級
- WINCH: 逐步關閉工作進程

工作進程支持的信號
- TERM, INT: 立刻退出
- QUIT: 等待請求處理結束后再退出
- USR1: 重新打開日志文件

=================nginx平滑升級操作記錄====================

[app@web-node01 ~]# /data/nginx/sbin/nginx -v
nginx version: nginx/1.12.2

備份之前安裝的nginx (其實主要備份的是sbin/nginx  和 conf配置目錄, 這里我全部備份)
[app@web-node01 ~]# cp -r /data/nginx /data/nginx.old
[app@web-node01 ~]# ll /data/
total 0
drwxr-xr-x 10 app app 133 Sep 15 22:31 nginx
drwxr-xr-x 10 app app 133 Nov 15 15:21 nginx.old

下載nginx1.14.1的安裝包, 解壓並進行編譯安裝. 安裝路徑需與舊版一致
[app@web-node01 software]# ll nginx-1.14.1.tar.gz 
-rw-rw-r-- 1 app app 1014040 Nov 15 11:04 nginx-1.14.1.tar.gz
[app@web-node01 software]# tar -zvxf nginx-1.14.1.tar.gz
[app@web-node01 software]# cd nginx-1.14.1/
[app@web-node01 nginx-1.14.1]# ./configure --prefix=/data/nginx --user=app --group=app --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream 
[app@web-node01 nginx-1.14.1]# make && make install

發送USR2信號
向主進程發送USR2信號,Nginx會啟動一個新版本的master進程和工作進程,和舊版一起處理請求
[app@web-node01 nginx-1.14.1]# ps -ef|grep nginx
app       1899 30168  0 15:25 pts/0    00:00:00 grep --color=auto nginx
app      19233 22333  0 Sep21 ?        00:00:00 nginx: worker process
app      19234 22333  0 Sep21 ?        00:00:00 nginx: worker process
app      19235 22333  0 Sep21 ?        00:00:00 nginx: worker process
app      19236 22333  0 Sep21 ?        00:00:01 nginx: worker process
app      19237 22333  0 Sep21 ?        00:00:00 nginx: worker process
app      19238 22333  0 Sep21 ?        00:00:01 nginx: worker process
app      19239 22333  0 Sep21 ?        00:00:05 nginx: worker process
app      19240 22333  0 Sep21 ?        00:00:01 nginx: worker process
app      19241 22333  0 Sep21 ?        00:00:27 nginx: cache manager process
root     22333     1  0 Sep15 ?        00:00:00 nginx: master process /data/nginx/sbin/nginx

[app@web-node01 nginx-1.14.1]# kill -USR2 22333            

[app@web-node01 nginx-1.14.1]# ps -ef|grep nginx    
root      1911 22333  0 15:26 ?        00:00:00 nginx: master process /data/nginx/sbin/nginx
app       1912  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1913  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1914  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1915  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1916  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1917  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1918  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1919  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1920  1911  0 15:26 ?        00:00:00 nginx: cache manager process
app       1921  1911  0 15:26 ?        00:00:00 nginx: cache loader process
app       1923 30168  0 15:26 pts/0    00:00:00 grep --color=auto nginx
app      19233 22333  0 Sep21 ?        00:00:00 nginx: worker process
app      19234 22333  0 Sep21 ?        00:00:00 nginx: worker process
app      19235 22333  0 Sep21 ?        00:00:00 nginx: worker process
app      19236 22333  0 Sep21 ?        00:00:01 nginx: worker process
app      19237 22333  0 Sep21 ?        00:00:00 nginx: worker process
app      19238 22333  0 Sep21 ?        00:00:01 nginx: worker process
app      19239 22333  0 Sep21 ?        00:00:05 nginx: worker process
app      19240 22333  0 Sep21 ?        00:00:01 nginx: worker process
app      19241 22333  0 Sep21 ?        00:00:27 nginx: cache manager process
root     22333     1  0 Sep15 ?        00:00:00 nginx: master process /data/nginx/sbin/nginx

發送WITCH信號
向原Nginx主進程發送WINCH信號,它會逐步關閉其下的工作進程(主進程不退出),這時所有請求都會由新版Nginx處理. 
[app@web-node01 nginx-1.14.1]# kill -WITCH 22333         //如果這個命令報錯,則可以忽略, 在最后一步直接kill掉原來Nginx主進程的pid即可.

發送HUP信號 (特別注意: 這一步是回退, 如果不回退, 則直接忽略這一步即可)
如果這時需要回退,可向原Nginx主進程發送HUP信號,它會重新啟動工作進程, 仍使用舊版配置文件 。
然后可以將新版Nginx進程殺死(使用QUIT、TERM、或者KILL)
[app@web-node01 nginx-1.14.1]# kill -HUP 22333

升級完畢
如果不需要回滾,可以將原Nginx主進程殺死(使用QUIT、TERM、或者KILL),至此完成熱升級。

[app@web-node01 nginx-1.14.1]# ps -ef|grep nginx
root      1911 22333  0 15:26 ?        00:00:00 nginx: master process /data/nginx/sbin/nginx
app       1912  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1913  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1914  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1915  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1916  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1917  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1918  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1919  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1920  1911  0 15:26 ?        00:00:00 nginx: cache manager process
app       2394 30168  0 15:33 pts/0    00:00:00 grep --color=auto nginx
app      19233 22333  0 Sep21 ?        00:00:00 nginx: worker process
app      19234 22333  0 Sep21 ?        00:00:00 nginx: worker process
app      19235 22333  0 Sep21 ?        00:00:00 nginx: worker process
app      19236 22333  0 Sep21 ?        00:00:01 nginx: worker process
app      19237 22333  0 Sep21 ?        00:00:00 nginx: worker process
app      19238 22333  0 Sep21 ?        00:00:01 nginx: worker process
app      19239 22333  0 Sep21 ?        00:00:05 nginx: worker process
app      19240 22333  0 Sep21 ?        00:00:01 nginx: worker process
app      19241 22333  0 Sep21 ?        00:00:27 nginx: cache manager process
root     22333     1  0 Sep15 ?        00:00:00 nginx: master process /data/nginx/sbin/nginx

[app@web-node01 nginx-1.14.1]# sudo kill -9 19233 19234 19235 19236 19237 19238 19239 19240 19241 22333

[app@web-node01 nginx-1.14.1]# ps -ef|grep nginx
root      1911     1  0 15:26 ?        00:00:00 nginx: master process /data/nginx/sbin/nginx
app       1912  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1913  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1914  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1915  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1916  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1917  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1918  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1919  1911  0 15:26 ?        00:00:00 nginx: worker process
app       1920  1911  0 15:26 ?        00:00:00 nginx: cache manager process
app       2496 30168  0 15:35 pts/0    00:00:00 grep --color=auto nginx

[app@web-node01 ~]# /data/nginx/sbin/nginx -v
nginx version: nginx/1.14.1


免責聲明!

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



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