去除 Server 响应头实践

这篇文章我们来解决 AppScan 扫描漏洞中出现的另外一个漏洞"在应用程序中发现不必要的 Http 响应头"。

出现这个漏洞是因为我们返回的响应头出现了 Server 这个请求头:

查阅网上的资料,许多使用到了 server_tokens off; 命令,那我们在配置文件中的 http 块下添加这个命令看看:

更新 nginx 配置,刷新浏览器,我们可以看到 nginx 的版本号被隐藏了:

但是这并不能解决刚才提到的漏洞问题,漏洞的解决需要我们把这个 Server 头去掉,这需要我们使用到一个插件,这个插件可以帮我们设置和清除输入和输出标头 :

GitHub - openresty/headers-more-nginx-module: Set, add, and clear arbitrary output headers in NGINX http servers

在这里我使用 nginx-1.21.4 和 headers-more-nginx-module-0.34 做演示:

通过 ftp 工具将两个包上传到我们的虚拟机或者服务器后,将包解压:

arduino 复制代码
tar -zxvf nginx-1.21.4.tar.gz
tar -zxvf headers-more-nginx-module-0.34.tar.gz

然后我们在构建我们的 nginx 时,使用以下命令:

bash 复制代码
cd nginx-1.21.4
# --prefix 指定 nginx 的安装目录
# --add-module 添加第三方模块
./configure --prefix=/usr/nginx \
     --add-module=/your_path/headers-more-nginx-module
# make 编译
# make install 安装
make & make install

接着我们在 nginx 配置文件中的 http 块中加入 more_clear_headers 'Server'; 指令:

然后我们启动 nginx,响应头里的 Server 头就去掉了:

如果你需要使用 docker 来部署 nginx,可以使用这样如下 dockerfile 构建:

bash 复制代码
# 基础镜像
FROM ubuntu:20.04 AS builder

# 定义版本号
ENV NGINX_VERSION 1.21.4
ENV MODULE_VERSION 0.34

# 更新证书
RUN apt-get update -y && \
    apt-get install -y --reinstall ca-certificates

# 更换为中科大镜像源
RUN echo "deb https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse" > /etc/apt/sources.list && \
    echo "deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiversec https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse" >> /etc/apt/sources.list

# 安装需要的依赖
RUN rm -rf /var/lib/apt/lists/* && \
    rm /var/lib/apt/lists/* -vf && \
    apt-get update -y && \
    apt-get install -y libpcre3-dev && \
    apt-get install -y zlib1g.dev && \
    apt-get install -y build-essential 

# 将本地的 nginx 安装包复制到容器内    
COPY nginx-${NGINX_VERSION}.tar.gz /tmp/nginx-${NGINX_VERSION}.tar.gz

# 将本地的第三方模块安装包复制到容器内
COPY headers-more-nginx-module-${MODULE_VERSION}.tar.gz /tmp/headers-more-nginx-module-${MODULE_VERSION}.tar.gz

RUN cd /tmp && \
    tar -xvzf nginx-${NGINX_VERSION}.tar.gz && \
    tar -xvzf headers-more-nginx-module-${MODULE_VERSION}.tar.gz

# 添加动态模块并重新编译
RUN cd /tmp/nginx-${NGINX_VERSION} && \
    ./configure --with-compat --add-dynamic-module=/tmp/headers-more-nginx-module-${MODULE_VERSION} && \
    make modules

# 定义一个全新的 nginx 容器并把刚才编译的内容复制到新的 nginx 容器内
FROM nginx:1.21.4
COPY --from=builder /tmp/nginx-${NGINX_VERSION}/objs/* /etc/nginx/modules/
COPY --from=builder /usr/local/lib/* /lib/x86_64-linux-gnu/
COPY nginx.conf  /etc/nginx/nginx.conf

首先,我创建一个 Nginx 容器,其中包含 Nginx 压缩包和编译模块的构建工具。然后,第二个阶段是使用预构建的 Nginx 镜像,将编译好的资源从构建器容器复制到预构建的 Nginx 容器中。

如果你的 Linux 是 ARM 架构的,请使用如下 dockerfile:

bash 复制代码
# 基础镜像
FROM arm64v8/ubuntu:20.04 AS builder

# 定义版本号
ENV NGINX_VERSION 1.21.4
ENV MODULE_VERSION 0.34

# 更新证书
RUN apt-get update -y && \
    apt-get install -y --reinstall ca-certificates

# 更换为中科大镜像源
RUN echo "deb https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse" > /etc/apt/sources.list && \
    echo "deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiversec https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse" >> /etc/apt/sources.list

# 安装需要的依赖
RUN rm -rf /var/lib/apt/lists/* && \
    rm /var/lib/apt/lists/* -vf && \
    apt-get update -y && \
    apt-get install -y libpcre3-dev && \
    apt-get install -y zlib1g.dev && \
    apt-get install -y build-essential 

# 将本地的 nginx 安装包复制到容器内    
COPY nginx-${NGINX_VERSION}.tar.gz /tmp/nginx-${NGINX_VERSION}.tar.gz

# 将本地的第三方模块安装包复制到容器内
COPY headers-more-nginx-module-${MODULE_VERSION}.tar.gz /tmp/headers-more-nginx-module-${MODULE_VERSION}.tar.gz

RUN cd /tmp && \
    tar -xvzf nginx-${NGINX_VERSION}.tar.gz && \
    tar -xvzf headers-more-nginx-module-${MODULE_VERSION}.tar.gz

# 添加动态模块并重新编译
RUN cd /tmp/nginx-${NGINX_VERSION} && \
    ./configure --with-compat --add-dynamic-module=/tmp/headers-more-nginx-module-${MODULE_VERSION} && \
    make modules

# 定义一个全新的 nginx 容器并把刚才编译的内容复制到新的 nginx 容器内
FROM arm64v8/nginx:1.21.4
COPY --from=builder /tmp/nginx-${NGINX_VERSION}/objs/* /etc/nginx/modules/
COPY --from=builder /usr/local/lib/* /lib/aarch64-linux-gnu/
COPY nginx.conf  /etc/nginx/nginx.conf

从 NGINX 1.9.11 开始,可以将该模块编译为动态模块,使用 --add-dynamic-module=PATH 选项,你可以在 nginx.conf 中通过 load_module 指令显式加载该模块,使用到的 nginx.conf 内容如下:

dart 复制代码
load_module modules/ngx_http_headers_more_filter_module.so;
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    more_clear_headers 'Server';

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
相关推荐
向哆哆2 小时前
Java 安全:如何防止 DDoS 攻击?
java·安全·ddos
浩浩测试一下2 小时前
计算机网络中的DHCP是什么呀? 详情解答
android·网络·计算机网络·安全·web安全·网络安全·安全架构
fxshy4 小时前
ai聊天流式响应,阻塞式和流式响应 nginx遇到的坑
运维·javascript·nginx
浩浩测试一下5 小时前
SQL注入高级绕过手法汇总 重点
数据库·sql·安全·web安全·网络安全·oracle·安全架构
极小狐9 小时前
极狐GitLab 项目功能和权限解读
运维·git·安全·gitlab·极狐gitlab
国科安芯10 小时前
面向高性能运动控制的MCU:架构创新、算法优化与应用分析
单片机·嵌入式硬件·安全·架构·机器人·汽车·risc-v
迷路的小绅士12 小时前
常见网络安全攻击类型深度剖析(四):跨站脚本攻击(XSS)——分类、漏洞利用与前端安全防护
前端·安全·web安全
自由鬼12 小时前
开源漏洞扫描器:OpenVAS
运维·服务器·安全·网络安全·开源·漏洞管理
Python_金钱豹13 小时前
Text2SQL零代码实战!RAGFlow 实现自然语言转 SQL 的终极指南
前端·数据库·sql·安全·ui·langchain·机器人
广东航连科技13 小时前
银行网点款箱交接权限认证开锁与密钥时效双重监控
物联网·安全·银行·精细化管理·锁控·智能锁·款箱