1 隱藏nginx版本號
1.1 引言
nginx作為目前較為流行的http server軟件,其相關的安全漏洞也非常多,攻擊者可以根據我們的nginx版本來了解到相關的漏洞從而針對性的進行攻擊。
通過新版本的nginx都會修復一些老版本的已知漏洞,但有時候我們生產環境不好直接進行nginx版本升級,因此我們可以將nginx版本相關信息隱藏,來降低被攻擊的風險。
1.2 server_tokens
server_tokens是nginx在ngx_http_core_module中提供的一個功能,可以用來隱藏nginx版本號信息,官方文檔如下。
http://nginx.org/en/docs/http/ngx_http_core_module.html#server_tokens
我們可以給nginx新增一個安全相關的配置文件security.conf,文件內新增"server_tokens off;",再reload一下nginx服務即可。
[root@elk ~]# cat /etc/nginx/conf.d/security.conf server_tokens off; ?##隱藏nginx版本信息
2 隱藏nginx server頭信息
2.1 介紹
上面的場景,我們只是隱藏了nginx的版本信息,在某些情況下,我們也希望能將http請求頭中的"Server:nginx"也隱藏掉,這個就需要用到headers-more-nginx-module模塊,模塊官網如下。
https://github.com/openresty/headers-more-nginx-module
同時我們需要用到nginx的動態添加模塊功能,該功能在nginx 1.9.11版本后支持
https://www.nginx.com/blog/compiling-dynamic-modules-nginx-plus/
2.2 headers模塊編譯
下載對應版本的nginx源碼,和headers-more-nginx-module模塊源碼,并進行編譯。nginx源碼下載地址如下
http://nginx.org/download/
[root@elk ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz ##下載nginx源碼 [root@elk ~]# wget https://github.com/openresty/headers-more-nginx-module/archive/refs/tags/v0.34.tar.gz ##下載headers模塊源碼 [root@elk ~]# tar xf nginx-1.20.1.tar.gz [root@elk ~]# tar xf v0.34.tar.gz [root@elk ~]# ls headers-more-nginx-module-0.34 nginx-1.20.1 nginx-1.20.1.tar.gz v0.34.tar.gz [root@elk nginx-1.20.1]# cd /root/nginx-1.20.1/ [root@elk nginx-1.20.1]# /root/nginx-1.20.1/configure --with-compat --add-dynamic-module=/root/headers-more-nginx-module-0.34/ ##--with-compat參數用來兼容動態模塊,--add-dynamic-module指定動態模塊源碼所在位置。
開始編譯
確認編譯結果
[root@elk nginx-1.20.1]# echo $?
編譯模塊
[root@elk nginx-1.20.1]# make modules
編譯完成后,會在nginx源碼的objs目錄,生成我們編譯好的動態模塊文件
2.3 使用模塊,驗證nginx http請求頭是否被隱藏
nginx目錄下新建目錄,用于存放模塊
[root@elk nginx-1.20.1]# mkdir /etc/nginx/conf.d/modules [root@elk nginx-1.20.1]# cp /root/nginx-1.20.1/objs/ngx_http_headers_more_filter_module.so /etc/nginx/conf.d/modules/
修改nginx配置文件,添加以下兩部分內容。主配置中添加 load_module /etc/nginx/conf.d/modules/ngx_http_headers_more_filter_module.so;
一行,server容器中,添加 more_set_headers "Server: singless";
一行,用來修改http請求頭信息。
?
重載nginx服務,再次檢查可以發現server頭已經被修改了
[root@elk ~]# systemctl reload nginx
?