AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 24885
Accepted
Astra
Astra
Asked: 2009-06-13 08:31:38 +0800 CST2009-06-13 08:31:38 +0800 CST 2009-06-13 08:31:38 +0800 CST

如何删除 IIS/ASP.NET 响应标头

  • 772

我有几个 IIS/6.0 服务器,安全性要求我删除几个在请求时发送到客户端浏览器的响应标头。他们担心通过响应标头泄露平台信息。我已经从网站的 IIS 配置中删除了所有 HTTP-HEADERS(X-Powered-By 或某些此类标头)。

(我个人确实知道这些信息很容易被发现,即使它是隐藏的,但这不是我的决定。)

我要删除的标题:

  • 服务器- Microsoft-IIS/6.0
  • X-AspNet 版本- 2.0.50727

我也知道 ASP.NET MVC 也会发出自己的标头,如果您也知道如何删除它,那将很有帮助。

  • X-AspNetMvc-版本- 1.0
iis asp.net http-headers
  • 7 7 个回答
  • 128840 Views

7 个回答

  • Voted
  1. Adam
    2010-06-15T08:58:47+08:002010-06-15T08:58:47+08:00

    要删除所有披露过多信息的自定义标头 - IIS 7 的方法多种多样(不幸的是):

    标题名称: X-Powered-By

    添加:

    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
      </customHeaders>
    </httpProtocol>
    

    在<system.webServer>节中。

    标头名称:服务器

    通过从 PreSendRequestHeaders 事件中调用 Response.Headers.Remove("Server") 来实现一个 httpModule 来去除此标头。另一个资源:Cloaking your ASP.NET MVC Web Application on IIS 7

    标题名称:X-AspNet-Version

    在 web.config 的 httpRuntime 部分 - 设置:

    <httpRuntime enableVersionHeader="false" />
    

    标头名称:X-AspNetMvc-Version

    从 global.asax 中的 Application_Start 事件 - 执行以下代码 (C#):

    MvcHandler.DisableMvcResponseHeader = true;
    
    • 62
  2. Best Answer
    Justin Scott
    2009-06-13T08:37:33+08:002009-06-13T08:37:33+08:00

    您的安全部门希望您这样做以使服务器类型更难识别。这可能会减少自动黑客工具的攻击,并使人们更难闯入服务器。

    在 IIS 中,打开网站属性,然后转到 HTTP 标头选项卡。大多数 X- 标头都可以在此处找到并删除。这可以针对单个站点或整个服务器完成(修改树中网站对象的属性)。

    对于服务器标头,在 IIS6 上,您可以使用 Microsoft 的URLScan工具对其进行远程处理。Port 80 Software 还制作了一款名为ServerMask的产品,它会为您解决这个问题以及更多其他问题。

    对于 IIS7(及更高版本),您可以使用URL 重写模块来重写服务器标头或空白它的值。在 web.config 中(在站点或整个服务器上),在安装 URL 重写模块后添加此内容:

    <rewrite>    
      <outboundRules rewriteBeforeCache="true">
        <rule name="Remove Server header">
          <match serverVariable="RESPONSE_Server" pattern=".+" />
          <action type="Rewrite" value="" />
        </rule>
      </outboundRules>
    </rewrite>
    

    如果您愿意,可以将自定义值放入重写操作中。此示例源自本文,其中还包含其他重要信息。

    对于 MVC 标头,在 Global.asax 中:

    MvcHandler.DisableMvcResponseHeader = true;
    

    由于 TechNet 博客链接不再有效,已于 2019 年 11 月 12 日编辑以更新 IIS7 信息。

    • 33
  3. squillman
    2009-06-13T08:43:54+08:002009-06-13T08:43:54+08:00

    将它放在 ASP.NET 应用程序的 web.config 文件中将去掉 X-AspNet-Version 标头:

    <system.web>
    <httpRuntime enableVersionHeader="false" />
    </system.web>
    

    请注意,文件中应该已经存在 system.web 标记。不要创建重复项,只需添加 httpRuntime 标记即可。httpRuntime 标记也可能已经存在。如果是这样,只需添加属性或设置它的值(如果它已经存在)。

    • 15
  4. HowardvanRooijen
    2009-08-27T00:17:07+08:002009-08-27T00:17:07+08:00

    刚刚经历了我当前项目的“强化”周期 -我在博客上介绍了我们采用的方法,其中包括一个用于删除以下标头的 HTTPModule:

    服务器,
    X-AspNet-Version,
    X-AspNetMvc-Version,
    X-Powered-By

    相关部分转载如下:

    但是没有简单的方法可以通过配置删除服务器响应标头。幸运的是 IIS7 有一个托管的可插拔模块基础结构,它允许您轻松扩展其功能。以下是用于删除指定 HTTP 响应标头列表的 HttpModule 的源代码:

    namespace Zen.Core.Web.CloakIIS
    {
        #region Using Directives
    
        using System;
        using System.Collections.Generic;
        using System.Web;
    
        #endregion
    
        /// <summary>
        /// Custom HTTP Module for Cloaking IIS7 Server Settings to allow anonymity
        /// </summary>
        public class CloakHttpHeaderModule : IHttpModule
        {
            /// <summary>
            /// List of Headers to remove
            /// </summary>
            private List<string> headersToCloak;
    
            /// <summary>
            /// Initializes a new instance of the <see cref="CloakHttpHeaderModule"/> class.
            /// </summary>
            public CloakHttpHeaderModule()
            {
                this.headersToCloak = new List<string>
                                          {
                                                  "Server",
                                                  "X-AspNet-Version",
                                                  "X-AspNetMvc-Version",
                                                  "X-Powered-By",
                                          };
            }
    
            /// <summary>
            /// Dispose the Custom HttpModule.
            /// </summary>
            public void Dispose()
            {
            }
    
            /// <summary>
            /// Handles the current request.
            /// </summary>
            /// <param name="context">
            /// The HttpApplication context.
            /// </param>
            public void Init(HttpApplication context)
            {
                context.PreSendRequestHeaders += this.OnPreSendRequestHeaders;
            }
    
            /// <summary>
            /// Remove all headers from the HTTP Response.
            /// </summary>
            /// <param name="sender">
            /// The object raising the event
            /// </param>
            /// <param name="e">
            /// The event data.
            /// </param>
            private void OnPreSendRequestHeaders(object sender, EventArgs e)
            {
                this.headersToCloak.ForEach(h => HttpContext.Current.Response.Headers.Remove(h));
            }
        }
    }
    

    确保您签署了程序集,然后您可以将其安装到您的 Web 服务器的 GAC 中,并且只需对您的应用程序的 web.config 进行以下修改(或者如果您希望它被全局应用,则对 machine.config 进行修改):

    <configuration>
        <system.webServer>
            <modules>
                <add name="CloakHttpHeaderModule" 
                     type="Zen.Core.Web.CloakIIS.CloakHttpHeaderModule, Zen.Core.Web.CloakIIS, 
                           Version=1.0.0.0, Culture=neutral, PublicKeyToken=<YOUR TOKEN HERE>" />
            </modules>
        </system.webServer>
    </configuration>
    
    • 5
  5. Nasir Mahmood
    2014-11-20T02:22:16+08:002014-11-20T02:22:16+08:00

    我使用以下代码并为我工作 iis 7.5

    protected void Application_PreSendRequestHeaders()
    {
        Response.Headers.Remove("Server");
        Response.Headers.Remove("X-AspNet-Version");
        Response.Headers.Remove("X-AspNetMvc-Version");
    }
    
    • 2
  6. mitaka
    2018-08-02T10:05:53+08:002018-08-02T10:05:53+08:00

    检查这个博客。不要使用代码来删除响应标头。根据微软不稳定

    请改用 Web.config 自定义标头部分:

    <system.webServer>          
    <httpProtocol>
        <!-- Security Hardening of HTTP response headers -->
        <customHeaders>
            <!--Sending the new X-Content-Type-Options response header with the value 'nosniff' will prevent 
                    Internet Explorer from MIME-sniffing a response away from the declared content-type. -->
            <add name="X-Content-Type-Options" value="nosniff" />
    
            <!-- X-Frame-Options tells the browser whether you want to allow your site to be framed or not. 
                     By preventing a browser from framing your site you can defend against attacks like clickjacking. 
                     Recommended value "x-frame-options: SAMEORIGIN" -->
            <add name="X-Frame-Options" value="SAMEORIGIN" />
    
            <!-- Setting X-Permitted-Cross-Domain-Policies header to “master-only” will instruct Flash and PDF files that 
                     they should only read the master crossdomain.xml file from the root of the website. 
                     https://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html -->
            <add name="X-Permitted-Cross-Domain-Policies" value="master-only" />
    
            <!-- X-XSS-Protection sets the configuration for the cross-site scripting filter built into most browsers. 
                     Recommended value "X-XSS-Protection: 1; mode=block". -->
            <add name="X-Xss-Protection" value="1; mode=block" />
    
            <!-- Referrer-Policy allows a site to control how much information the browser includes with navigations away from a document and should be set by all sites. 
                     If you have sensitive information in your URLs, you don't want to forward to other domains 
                     https://scotthelme.co.uk/a-new-security-header-referrer-policy/ -->
            <add name="Referrer-Policy" value="no-referrer-when-downgrade" />
    
            <!-- Remove x-powered-by in the response header, required by OWASP A5:2017 - Do not disclose web server configuration -->
            <remove name="X-Powered-By" />
    
            <!-- Ensure the cache-control is public, some browser won't set expiration without that  -->
            <add name="Cache-Control" value="public" />
        </customHeaders>
    </httpProtocol>
    
    <!-- Prerequisite for the <rewrite> section
                Install the URL Rewrite Module on the Web Server https://www.iis.net/downloads/microsoft/url-rewrite -->
    <rewrite>
        <!-- Remove Server response headers (OWASP Security Measure) -->
        <outboundRules rewriteBeforeCache="true">
            <rule name="Remove Server header">
                <match serverVariable="RESPONSE_Server" pattern=".+" />
    
                <!-- Use custom value for the Server info -->
                <action type="Rewrite" value="Your Custom Value Here." />
            </rule>
        </outboundRules>
    </rewrite>
    </system.webServer>
    
    • 2
  7. Jan H
    2020-02-13T04:54:19+08:002020-02-13T04:54:19+08:00

    我使用Web.config和的组合Global.asax.cs来摆脱所有自定义标题,包括以下标题:

    • 服务器
    • X-AspNet-版本
    • X-AspNetMvc-版本

    网络配置:

    <system.web> 
      <httpRuntime enableVersionHeader="false"/> 
    </system.web>
    <system.webServer>
       <httpProtocol>
          <customHeaders>
             <clear />
          </customHeaders>
       </httpProtocol>
    </system.webServer> 
    

    全球.asax.cs:

    protected void Application_Start() 
    { 
        MvcHandler.DisableMvcResponseHeader = true; 
    }
    

    另请参阅https://stackoverflow.com/a/20739875/1678525

    • 0

相关问题

  • IIS 6.0 (Windows Server 2003) 上的 HTTP 压缩

  • 这个 Web 服务器可以处理多少个站点?[复制]

  • 如何在 IIS 中发送响应标头?

  • IIS 优化

  • IIS 6.0 (Windows Server 2003) 备份的最佳实践?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    Windows 中执行反向 DNS 查找的命令行实用程序是什么?

    • 14 个回答
  • Marko Smith

    如何检查 Windows 机器上的端口是否被阻塞?

    • 4 个回答
  • Marko Smith

    我应该打开哪个端口以允许远程桌面?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    kch 如何更改我的私钥密码? 2009-08-06 21:37:57 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve