nginx安全:修改对外的服务软件名称并隐藏版本号(nginx1.18.0)

一,为什么要隐藏nginx真实的软件名称?

1,nginx响应的Server头部都会携带上服务软件的名字和版本信息,

服务器软件的版本信息暴光在外部,很容易被黑客了解到,就通过相应版本的漏洞来攻击服务器,引发安全问题。

针对生产环境的服务器,有必要隐藏或者修改软件版本信息,以避免黑客的指向性攻击.

 

2,看一下nginx作为web server返回的head信息

[root@centos8 ~]# curl --head http://127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Fri, 24 Apr 2020 06:51:51 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 22 Apr 2020 09:22:38 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "5ea00cde-264"
Accept-Ranges: bytes

真实的软件名称和版本号都显示出来了

 

3,在这个例子里,我们把nginx的软件名称改名为LWS

 

说明:刘宏缔的架构森林是一个专注架构的博客,

网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/22/nginx-xiu-gai-dui-wai-de-fu-wu-ruan-jian-ming-cheng-bing-yin-cang-ban-ben-hao-nginx1180/

         对应的源码可以访问这里获取: https://github.com/liuhongdi/

说明:作者:刘宏缔 邮箱: 371125307@qq.com

 

二,通过修改nginx源代码文件来修改软件名称

1,复制一个文件夹,与无修改的源码文件夹区分开

[root@centos8 source]# cp -axv nginx-1.18.0 nginx-1.18.0-LWS

然后我们基于新源码文件夹做修改

 

2,修改第一个文件:核心的头文件

[root@centos8 nginx-1.18.0-LWS]# vi src/core/nginx.h 

#define NGINX_VER          "nginx/" NGINX_VERSION

修改为:

#define NGINX_VER          "lws/" NGINX_VERSION

 

#define NGINX_VAR          "NGINX"

修改为

#define NGINX_VAR          "LWS"

 

3,第二个文件:

[root@centos8 nginx-1.18.0-LWS]# vi src/http/ngx_http_header_filter_module.c 

static u_char ngx_http_server_string[] = "Server: nginx" CRLF;

修改为:

static u_char ngx_http_server_string[] = "Server: lws" CRLF;

 

4,第三个文件:内置的响应信息:比如404之类的错误提示页面

[root@centos8 nginx-1.18.0-LWS]# vi src/http/ngx_http_special_response.c 

"<hr><center>nginx</center>" CRLF

修改为:

"<hr><center>lws</center>" CRLF

 

5,第四个文件:

[root@centos8 nginx-1.18.0-LWS]# vi src/http/v2/ngx_http_v2_filter_module.c 

把第480行:

  "http2 output header: \"server: nginx\"");

修改为:

  "http2 output header: \"server: lws\"");

 

三,重新安装nginx

configure

[root@centos8 nginx-1.18.0-LWS]# ./configure --prefix=/usr/local/soft/nginx-1.18.0 --with-http_stub_status_module --with-http_ssl_module

安装

[root@centos8 nginx-1.18.0-LWS]# make && make install

 

四,隐藏nginx版本号

[root@centos8 conf]# vi nginx.conf 

在http段内增加:

    server_tokens off;

 

五,重启服务并测试效果

重启服务

[root@centos8 conf]# systemctl stop nginx
[root@centos8 conf]# systemctl start nginx

 

测试效果

[root@centos8 soft]# curl --head http://127.0.0.1
HTTP/1.1 200 OK
Server: lws
Date: Fri, 24 Apr 2020 07:34:38 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 24 Apr 2020 07:22:13 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "5ea293a5-264"
Accept-Ranges: bytes

 

也可以从浏览器端查看:

报错页面变成了lws

响应的头信息变成了lws

 

六,服务端查看nginx的版本

[root@centos8 soft]# /usr/local/soft/nginx-1.18.0/sbin/nginx -v
nginx version: lws/1.18.0

 

七,对修改nginx软件名和版本号的建议:

1,我们不建议从源码修改版本号,隐藏即可,

真实的版本号还是要保存下来后续的开发维护中参考

2,如果在生产环境中没有修改nginx的软件名称,

那么版本号一定要做到隐藏 

 

posted @ 2020-04-25 09:13  刘宏缔的架构森林  阅读(3734)  评论(0编辑  收藏  举报