删除服务器响应标头IIS7

103 security iis-7 header response

有没有办法从IIS7中删除"服务器"响应头?有一些文章显示使用HttpModules我们可以实现同样的目的.如果我们没有服务器的管理权限,这将非常有用.另外我不想写ISAPI过滤器.

我有我的服务器的管理员权限.所以我不想做上面的事情.所以,请帮我做同样的事.

bka*_*aid 105

将其添加到您的global.asax.cs:

protected void Application_PreSendRequestHeaders()
{
    Response.Headers.Remove("Server");
    Response.Headers.Remove("X-AspNet-Version");
    Response.Headers.Remove("X-AspNetMvc-Version");
}
Run Code Online (Sandbox Code Playgroud)

  • @PsychoDad仅适用于ASP.NET请求,不适用于.css和.js等静态文件 (47认同)
  • 不知道为什么http模块的答案高于这个,这个更容易 (11认同)
  • 在实现`IHttpModule`或`Global.asax`的类中使用`PreSendRequestHeaders`并不是一个好主意.我目睹了事件在压力负载下冻结服务器上的应用程序.`BeginRequest`事件应该可以使响应头更改.请参阅http://www.hanselman.com/blog/ChecklistWhatNOTToDoInASPNET.aspx. (7认同)
  • 如果你依赖于`HttpContext.Current`,你可能会发现你在Cassini中得到一个'NullReferenceException`.[此博客文章](http://www.bugwriter.me/2010/01/removing-unnecessary-http-header-server.html)显示了如何这样做,同时避免破坏Cassini支持,如果这对您很重要. (2认同)

luk*_*fer 77

在IIS7中,您必须使用HTTP模块.在VS中将以下内容构建为类库:

namespace StrongNamespace.HttpModules
{
  public class CustomHeaderModule : IHttpModule
  { 
    public void Init(HttpApplication context)
    {
      context.PreSendRequestHeaders += OnPreSendRequestHeaders;
    } 

    public void Dispose() { } 

    void OnPreSendRequestHeaders(object sender, EventArgs e)
    {
      HttpContext.Current.Response.Headers.Set("Server", "Box of Bolts");
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后将以下内容添加到web.config中,或者在IIS中进行配置(如果在IIS中进行配置,则程序集必须位于GAC中).

<configuration>
  <system.webServer>
    <modules>
      <add name="CustomHeaderModule"
       type="StrongNamespace.HttpModules.CustomHeaderModule" />
    </modules>
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

  • IIS为静态文件(css/less/images/etc)发送304 Not Modified标头时不会调用该模块,因为这不会到达ASP.NET管道,因此在这种情况下服务器:Microsoft IIS/7.5仍然呈现 (4认同)
  • @UpTheCreek ASP.Net开发服务器(Cassini)不喜欢那个代码; [这篇博客文章](http://www.bugwriter.me/2010/01/removing-unnecessary-http-header-server.html)有一个解决方案,但是 - 你需要检查`HttpApplication`, `HttpRequest`,`HttpContext`和`HttpResponse`不是`null`,还检查`HttpRequest.IsLocal`是'false`. (2认同)
  • 修改`PreSendRequestHeaders`中的标题可能会导致HttpCacheModule出现问题(http://blogs.msdn.com/b/asiatech/archive/2010/10/18/heap-corruption-in-httpcachemodule-while-you-尝试删除http-headers-in-your-custom-http-module.aspx),你应该使用类似`PostReleaseRequestState`的东西. (2认同)

Jef*_*hnn 39

使用IIS UrlRewrite 2.0

<outboundRules>
  <rule name="Remove RESPONSE_Server" >
    <match serverVariable="RESPONSE_Server" pattern=".+" />
    <action type="Rewrite" value="" />
  </rule>
</outboundRules>
Run Code Online (Sandbox Code Playgroud)

  • 请注意,这只会使服务器标头空白,但不会将其删除. (11认同)
  • @vignesh这是一些UrlRewrite配置子节点.你必须将它们放在`system.webServer`中的`rewrite`节点下.请注意,如果服务器上未安装UrlRewrite,这将导致您的网站崩溃.您最好首先使用IIS配置控制台来检查它如何写下这些配置节点. (4认同)

Fré*_*ric 28

Scott Mitchell在博客文章中提供了删除不必要标题的解决方案.

正如在其他答案中已经说过的那样,对于Server标题,有http模块解决方案或UrlScan模块.(IIS7.5 +中不再提供URLScan模块.请使用URLRewrite来消隐它.)

对于X-AspNet-VersionX-AspNetMvc-Version,他提供了一种比在每个响应中删除它们更好的方法:根本就不生成它们.

使用enableVersionHeader禁用X-AspNet-Version,在web.config中

<httpRuntime enableVersionHeader="false" />
Run Code Online (Sandbox Code Playgroud)

使用MvcHandler.DisableMvcResponseHeader在.net Application_Start事件禁用X-AspNetMvc-Version

MvcHandler.DisableMvcResponseHeader = true;
Run Code Online (Sandbox Code Playgroud)

最后,在IIS配置中删除X-Powered-By自定义标头.(这可以在web.config中完成configuration/system.webServer/httpProtocol/customHeaders/remove[name=X-Powered-By])

请注意,如果您有ARR(应用程序请求路由),它也会添加自己的ARR X-Powered-By,自定义标头设置不会删除它.必须通过IIS管理器上的IIS管理器(不在站点上)上进行编辑器配置来删除这个:转到system.webServer/proxy节点并设置arrResponseHeaderfalse.之后IISReset,它被考虑在内.
(我在这里找到了这个,除了这篇文章是关于旧IIS 6.0配置事物的方式.)

不要忘记应用程序代码的解决方案默认情况下不适用于在静态内容上生成的标头(您可以激活runAllManagedModulesForAllRequests用于更改它的标题,但它会导致所有请求运行.Net管道).这不是问题,X-AspNetMvc-Version因为它没有添加到静态内容上(至少如果静态请求不在.Net管道中运行).

附注:当目的是为了掩盖使用的技术,你也应该改变标准.NET cookie名称(.ASPXAUTH如果窗体身份验证激活(使用name的属性forms在web.config中标记), ASP.NET_SessionId(使用<sessionState cookieName="yourName" />在web.config文件下的system.web标签), __RequestVerificationToken(改通过代码AntiForgeryConfig.CookieName,但遗憾的是不适用于此系统在html中生成的隐藏输入)).

  • 问题是针对 IIS 7 的,这在 IIS 7 中不起作用 (2认同)
  • 我还是无法得到它。过时的内容是一场瘟疫。我打算继续完成我的答案,以使其保持最新状态,就像我在这里所做的那样:提及它适用的版本并保留提到的旧解决方案。“Server”标头的 IIS 10+ 解决方案上方的句子是关于它们的,链接到针对 IIS &lt; 10 解决这一点的其他答案。替代方案是什么?为每个新版本的 IIS 重复这个问题?这将导致很难找到每个读者案例的最佳答案。 (2认同)

Dan*_*are 18

实际上,上面显示的编码模块和Global.asax示例仅适用于有效请求.

例如,在URL的末尾添加<,您将收到一个"错误请求"页面,该页面仍然显示服务器标头.许多开发人员忽视了这一点.

显示的注册表设置也不起作用.URLScan是删除"服务器"标头的唯一方法(至少在IIS 7.5中).


小智 13

或者在web.config中添加:

<system.webServer>
    <httpProtocol>
        <customHeaders>
            <remove name="X-AspNet-Version" />
            <remove name="X-AspNetMvc-Version" />
            <remove name="X-Powered-By" />
            <!-- <remove name="Server" />  this one doesn't work -->
        </customHeaders>
    </httpProtocol>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

  • 此方法不会删除"服务器"标头.其他人被删除. (3认同)

Vai*_*arg 11

除了URL Rewrite答案之外,这里还有完整的XMLweb.config

<system.webServer>
  <rewrite>
    <outboundRules>
      <rule name="Remove RESPONSE_Server" >
        <match serverVariable="RESPONSE_Server" pattern=".+" />
        <action type="Rewrite" value="Company name" />
      </rule>
    </outboundRules>
  </rewrite>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

URL重写


Nic*_*ans 10

要删除Server:标题,请转到Global.asax,查找/创建Application_PreSendRequestHeaders事件并添加如下行(感谢BK此博客,这也不会在Cassini/local dev上失败):

protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
    // Remove the "Server" HTTP Header from response
    HttpApplication app = sender as HttpApplication;
    if (null != app && null != app.Request && !app.Request.IsLocal &&
        null != app.Context && null != app.Context.Response)
    {
        NameValueCollection headers = app.Context.Response.Headers;
        if (null != headers)
        {
            headers.Remove("Server");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您想要一个完整的解决方案来删除Azure/IIS7上的所有相关标头并且也可以使用Cassini,请参阅此链接,该链接显示了在不使用HttpModules或URLScan的情况下禁用这些标头的最佳方法.


Ily*_*dik 10

web.config安装程序可以从ASP.NET响应中删除所有不必要的标头(至少从IIS 10开始):

<!--Removes version headers from response -->
<httpRuntime enableVersionHeader="false" />

<httpProtocol>
  <customHeaders>
    <!--Removes X-Powered-By header from response -->
    <clear />
  </customHeaders>
</httpProtocol>

<security>
  <!--Removes Server header from response-->
  <requestFiltering removeServerHeader ="true" />
</security>
Run Code Online (Sandbox Code Playgroud)

请注意,这将隐藏“应用程序”的所有标头,其他所有方法也是如此。如果您到达应用程序外部的IIS本身或ASP.NET生成的某些默认页面或错误页面,则这些规则将不适用。因此,理想情况下,它们应该位于IIS的根级别,并且该底线可能会对IIS本身留下一些错误响应。

PS:IIS 10中存在一个错误,即使使用正确的配置,它有时也会显示服务器标头。现在应该修复它,但是必须更新IIS / Windows。


Dre*_*kes 9

如果您只想删除标题,可以使用缩短版本的lukiffer的答案:

using System.Web;

namespace Site
{
    public sealed class HideServerHeaderModule : IHttpModule
    {
        public void Dispose() { }

        public void Init(HttpApplication context)
        {
            context.PreSendRequestHeaders +=
            (sender, e) => HttpContext.Current.Response.Headers.Remove("Server");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在Web.config:

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="CustomHeaderModule" type="Site.HideServerHeaderModule" />
  </modules>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)


Ric*_*ing 5

尝试将HKLM\SYSTEM\CurrentControlSet\Services\HTTP\Parameters\DisableServerHeader注册表项设置REG_DWORD1.

  • 不幸的是,注册表项似乎不适用于IIS 7.5 (3认同)
  • 令人沮丧的是,即使重新启动虚拟机,这对我也没有任何影响.我们在Windows Server 2008 R2 Standard上运行IIS 7.5,"版本6.1(Build 7601:Service Pack 1)".同样,由于某种原因,我的`OnPreSendRequestHeaders`事件处理程序(见上文)永远不会被触发. (2认同)