当网站使用nginx作为web服务器时,访问者可以在浏览器轻松的获得nginx的版本,从而利用版本漏洞对网站进行攻击,在下面的图中可以看到,网站当前nginx的版本是1.24:
接下来介绍一种方法,帮助我们修改或者server信息,就是利用headers-more-nginx-module这个模块:
#添加、修改、清除响应HTTP头
more_set_header 'Server:srebudong' #修改server为srebudong
#删除指定的HTTP头
more_clear_headers 'Server'; #删除响应头中的server字段
#添加、修改、删除传入请求的HTTP头
more_set_input_headers 'X-Forward-Proto:https'; #把所有传入请求添加或者修改X-Forward-Proto
#删除传入请求的指定HTTP头
more_clear_input_headers 'User-Agent'; 删除传入请求中的User-Agent头
下载模块:headers-more-nginx-module
wget -O /opt https://github.com/openresty/headers-more-nginx-module/archive/refs/tags/v0.37.tar.gz
#解压
cd /opt
tar -zxvf v0.37.tar.gz
查看当前nginx编译参数:
nginx -V
把编译参数复制下来,并到nginx源码文件夹下重新编译,配置编译参数时加上--add-module=/path/of/module
./configure --prefix=/user/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --add-module=/opt/headers-more-nginx-module-0.37
完成后make,千万不要make install
关闭nginx
/usr/local/nginx/sbin/nginx -s stop
备份原来的nginx并复制新的nginx可执行文件:
mv /usr/local/nginx/sbin/nginx{,.bak}
cp -f ./objs/nginx /usr/local/nginx/sbin/nginx
编辑nginx配置文件:
#全局生效
http {
more_set_header 'Server:srebudong'
}
#单个server配置,局部生效
server {
more_set_header 'Server:srebudong'
}
检查配置文件确定无误后启动nginx:
[root@budong nginx]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@budong nginx]# /usr/local/nginx/sbin/nginx
浏览器强制刷新(CTRL+F5)再次查看server信息:
可以看到,Server信息已经修改为srebudong。