隐藏 Nginx 版本号

Nginx 的 版本号暴露在网络上偶尔会有不安全的情况,比如某个版本被人发现漏洞以后,会被黑客通过版本扫描来攻击。所以最好把 Nginx 的版本好隐藏起来。

隐藏版本号

Nginx 的版本号可以通过 curl 命令来查看,比如下所示,另外在浏览器的调试模式或者其他的地方都能看到。

其中 Server: nginx/1.12.2 就是当前访问的这个地址的 Nginx 版本号。

[root@localhost ~]# curl -I http://192.168.1.140/  
HTTP/1.1 200 OK
Server: nginx/1.12.2 
Date: Tue, 12 Nov 2019 14:23:24 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 12 Nov 2020 13:46:35 GMT
Connection: keep-alive
ETag: "5dcab7bb-264"
Accept-Ranges: bytes

看到了版本号,那么我们可以通过修改 Nginx 配置文件的方式来修改版本号。比如


[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 

http {      ##在http下添加
    include    mime.types;
    default_type application/octet-stream;
    server_tokens off;  ##关闭版本号

修改完以后重启 Nginx 服务

[root@localhost ~]# systemctl restart nginx

这个时候再查看 Nginx 的版本会发现,在本来显示版本号的地方显示了 Server: nginx , 这样版本号就被隐藏了起来。

[root@localhost ~]# curl -I http://192.168.1.140/ 
HTTP/1.1 200 OK   
Server: nginx     
Date: Tue, 12 Nov 2020 14:22:00 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 12 Nov 2020 13:46:35 GMT
Connection: keep-alive
ETag: "5dcab7bb-264"
Accept-Ranges: bytes

伪造版本号

1,开启版本号

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {
    include    mime.types;
    default_type application/octet-stream;
    server_tokens on;  ##开启版本号

2,修改Nginx源码包文件

[root@localhost ~]# cd /opt/nginx-1.12.2/src/core/ ##切换到src源码包目录
[root@localhost core]# vim nginx.h ##修改文件

#define NGINX_VERSION   "1.1.1" ##此处版本号伪造成1.1.1

3,重新编译安装

[root@localhost core]# cd /opt/nginx-1.12.2/  ##切换目录到Nginx下
[root@localhost nginx-1.12.2]# ./configure \   ##重新配置
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@localhost nginx-1.12.0]# make   ##重新编译
...
[root@localhost nginx-1.12.0]# make install  ##重新安装
...

4,重启Nginx服务,查看版本信息

[root@localhost ~]# systemctl stop nginx ##关闭
[root@localhost ~]# systemctl start nginx ##开启
[root@localhost ~]# curl -I http://192.168.13.140/  ##查看Nginx信息
HTTP/1.1 200 OK 
Server: nginx/1.1.1    ##此时的版本号就是伪造的版本号
Date: Tue, 12 Nov 2019 14:34:02 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 12 Nov 2019 13:46:35 GMT
Connection: keep-alive
ETag: "5dcab7bb-264"

results matching ""

    No results matching ""