1. 首页
  2. 专题栏目
  3. Nginx专题
  4. 隐藏nginx响应头中的server信息(HTTP服务器版本信息泄漏)

隐藏nginx响应头中的server信息(HTTP服务器版本信息泄漏)

  • 发布于 2024-08-13
  • 13 次阅读

本文由 简悦 SimpRead 转码, 原文地址 blog.csdn.net

安全审计中有时会有

漏洞名称 HTTP 服务器版本信息泄漏
漏洞描述 目标服务器返回的信息头中包含了 Web Server 的软件或者版本信息。

可以安装 nginx 的 headers-more-nginx-module 模块修改或隐藏响应头信息

一、安装

  1. 下载 headers-more-nginx-module

下载地址
https://github.com/openresty/headers-more-nginx-module/tags

[root@localhost /]# cd /www/tools
[root@localhost tools]# wget https://github.com/openresty/headers-more-nginx-module/archive/refs/tags/v0.37.tar.gz
  1. 解压
    解压后会多出一个 headers-more-nginx-module-0.37 文件夹
[root@localhost tools]# tar -zxvf v0.37.tar.gz

# 进入
[root@localhost tools]# cd headers-more-nginx-module-0.37/

# 查看当前完整路径
[root@localhost headers-more-nginx-module-0.37]# pwd

# 复制装盘备用
/www/tools/headers-more-nginx-module-0.37

[root@localhost headers-more-nginx-module-0.37]# cd ..
  1. 查看当前系统 nginx 信息
// 查看当前nginx加载的模块
[root@localhost tools]# /www/server/nginx/sbin/nginx -V 
nginx version: nginx/1.22.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.1.1q  5 Jul 2022
TLS SNI support enabled
# 复制装盘备用
configure arguments: --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=/www/server/nginx/src/pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --add-module=/www/server/nginx/src/ngx_http_substitutions_filter_module-master --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module
  1. 下载对应版本的 nginx

下载对应版本的 nginx

# 下载
[root@localhost tools]# wget http://nginx.org/download/nginx-1.22.1.tar.gz 

# 解压
[root@localhost tools]# tar -zxvf nginx-1.22.1.tar.gz
  1. 编译

在末尾添加 --add-module=/www/tools/headers-more-nginx-module-0.37
注意路径【/www/tools/headers-more-nginx-module-0.37】要换成你自己的

# 进入 nginx-1.22.1目录
[root@localhost tools]# cd nginx-1.22.1

# 先备份
[root@localhost nginx-1.12.1]# cp /www/server/nginx/sbin/nginx /www/server/nginx/sbin/nginx_old

# 编译
[root@localhost nginx-1.12.1]# ./configure --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=/www/server/nginx/src/pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --add-module=/www/server/nginx/src/ngx_http_substitutions_filter_module-master --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module --add-module=/www/tools/headers-more-nginx-module-0.37

# configure完成后进行make(如原本无nginx,make后还需make install)
[root@localhost nginx-1.12.1]# make
  1. 复制新的 nginx 到目录
[root@localhost nginx-1.12.1]# mv /www/server/nginx/sbin/nginx /www/server/nginx/sbin/nginx_bak
[root@localhost nginx-1.12.1]# cp objs/nginx /www/server/nginx/sbin/nginx
  1. 重新 nginx 生效

二、食用

修改 nginx 配置文件,增加代码块

# set the Server output header
 # 隐藏Server
 more_clear_headers 'Server';
 
 # 伪装Server
 more_set_headers 'Server: my-server';

 # set and clear output headers
 location /bar {
     more_set_headers 'X-MyHeader: blah' 'X-MyHeader2: foo';
     more_set_headers -t 'text/plain text/css' 'Content-Type: text/foo';
     more_set_headers -s '400 404 500 503' -s 413 'Foo: Bar';
     more_clear_headers 'Content-Type';

     # your proxy_pass/memcached_pass/or any other config goes here...
 }

 # set output headers
 location /type {
     more_set_headers 'Content-Type: text/plain';
     # ...
 }

 # set input headers
 location /foo {
     set $my_host 'my dog';
     more_set_input_headers 'Host: $my_host';
     more_set_input_headers -t 'text/plain' 'X-Foo: bah';

     # now $host and $http_host have their new values...
     # ...
 }

 # replace input header X-Foo *only* if it already exists
 more_set_input_headers -r 'X-Foo: howdy';