web-dev-qa-db-ja.com

サーバー応答ヘッダーIIS7を削除する

IIS7から「サーバー」応答ヘッダーを削除する方法はありますか? HttpModulesを使用しても同じことができることを示す記事がいくつかあります。これは、サーバーに対する管理者権限がない場合に役立ちます。また、ISAPIフィルターを作成したくありません。

サーバーの管理者権限があります。ですから、上記のことはしたくありません。それで、私が同じことをするのを手伝ってください。

103
ram

これをglobal.asax.csに追加します。

protected void Application_PreSendRequestHeaders()
{
    Response.Headers.Remove("Server");
    Response.Headers.Remove("X-AspNet-Version");
    Response.Headers.Remove("X-AspNetMvc-Version");
}
104
bkaid

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");
    }
  }
}

次に、web.configに以下を追加するか、IIS内で構成します(IIS内で構成する場合、アセンブリはGAC内にある必要があります)。

<configuration>
  <system.webServer>
    <modules>
      <add name="CustomHeaderModule"
       type="StrongNamespace.HttpModules.CustomHeaderModule" />
    </modules>
  </system.webServer>
</configuration>
77
lukiffer

IIS(UrlRewrite)のURL書き換えモジュールバージョン2.0を有効にした状態で、構成セクション<configuration><system.webServer><rewrite>にアウトバウンドルールを追加します。

<outboundRules>
  <rule name="Remove RESPONSE_Server" >
    <match serverVariable="RESPONSE_Server" pattern=".+" />
    <action type="Rewrite" value="" />
  </rule>
</outboundRules>
36
JeffZhnn

Scott Mitchellは、ブログ投稿で 不要なヘッダーの削除 のソリューションを提供しています。

他の回答ですでに述べたように、Serverヘッダーには、 httpモジュールソリューション またはUrlScanモジュールがあります。 (URLScanモジュールはIIS7.5 +では使用できなくなりました。 空白にする代わりにURLRewriteを使用してください 。)

X-AspNet-VersionX-AspNetMvc-Versionについて、彼は各応答でそれらを削除するよりも優れた方法を提供します:単にそれらをまったく生成しない。

Web.configでX-AspNet-Versionを無効にするためにenableVersionHeaderを使用します

<httpRuntime enableVersionHeader="false" />

MvcHandler.DisableMvcResponseHeaderを無効にするには、.Net Application_StartイベントでX-AspNetMvc-Versionを使用します

MvcHandler.DisableMvcResponseHeader = true;

最後に、IIS構成でX-Powered-Byカスタムヘッダーを削除します。 (これはweb.configで行うことができます、configuration/system.webServer/httpProtocol/customHeaders/remove[name=X-Powered-By]

ARR(アプリケーションリクエストルーティング)がある場合は、独自のX-Powered-Byも追加することに注意してください。これは、カスタムヘッダー設定によって削除されません。これは、IIS Manager、IISルート(サイト上ではない)のエディター構成で削除する必要があります。system.webServer/proxyノードに移動し、arrResponseHeaderfalseに設定します。 IISResetの後に、それが考慮されます。
(私はこれを見つけました ここ 。ただし、この投稿は古いIIS 6.0の設定方法に関するものです。)

アプリケーションコードによるソリューションは、静的コンテンツで生成されたヘッダーにデフォルトでは適用されないことを忘れないでください(変更するためにrunAllManagedModulesForAllRequestsをアクティブにできますが、すべてのリクエストで.Netパイプラインが実行されます)。 X-AspNetMvc-Versionの問題ではありません(少なくとも静的な要求が.Netパイプラインで実行されない場合)。

サイドノート:使用するテクノロジーをクロークする場合は、標準の.Net Cookie名も変更する必要があります(フォーム認証が有効になっている場合は(.ASPXAUTH(web.configのnameタグでforms属性を使用))、ASP.NET_SessionId<sessionState cookieName="yourName" />system.webを使用してください)タグ)、__RequestVerificationToken(コードによってAntiForgeryConfig.CookieNameで変更しますが、残念ながら、このシステムがhtmlで生成する非表示の入力には適用されません))。

27
Frédéric

実際、上記のコード化されたモジュールとGlobal.asaxの例は、有効なリクエストに対してのみ機能します。

たとえば、URLの最後に<を追加すると、サーバーヘッダーを公開したままの「Bad request」ページが表示されます。多くの開発者はこれを見落としています。

表示されるレジストリ設定も機能しません。 URLScanは、「サーバー」ヘッダーを削除する唯一の方法です(少なくともIIS 7.5で)。

18
Dan Ware

または、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>
13
Anders

RL Rewrite answer への追加、ここにweb.configの完全なXMLがあります

<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>

RL書き換え

11
Vaibhav Garg

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");
        }
    }
}

Azure/IIS7のすべての関連ヘッダーを削除し、Cassiniでも機能する完全なソリューションが必要な場合は、 このリンク を参照してください。これは、HttpModulesまたはURLScanを使用せずにこれらのヘッダーを無効にする最良の方法を示しています。

10
Nick Evans

この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>

これにより、他のすべてのアプローチと同様に、「アプリケーション」のすべてのヘッダーが非表示になることに注意してください。あなたがIIS自体またはアプリケーション外のASP.NETによって生成されたデフォルトページまたはエラーページに到達し、これらのルールは適用されません。したがって、理想的には、それらはIISのルートレベルにあるべきであり、その敷居はIIS自体に対してエラー応答を残す場合があります。

追伸IIS 10には bug があり、正しい設定でもサーバーヘッダーを表示することがあります。これで修正されるはずですが、IIS/Windowsを更新する必要があります。

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");
        }
    }
}

そしてWeb.configで:

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="CustomHeaderModule" type="Site.HideServerHeaderModule" />
  </modules>
</system.webServer>
9
Drew Noakes

HKLM\SYSTEM\CurrentControlSet\Services\HTTP\Parameters\DisableServerHeaderレジストリエントリをREG_DWORD1に設定してみてください。

5
Richard Deeming

rlScan は、AlternateServerName=の下で[options]を使用してサーバーヘッダーを削除することもできます。

4
eddiegroves

eddiegroves 'answer をフォローアップすると、URLScanのバージョンに応じて、代わりにRemoveServerHeader=1の下の[options]を選択できます。

このオプションが追加されたURLScanのバージョンはわかりませんが、バージョン2.5以降で使用可能です。

2
techtician

レジストリ編集とUrlScanなどのツールを使用してIISを適切にセットアップする必要がある理由を説明する記事を見つけました。私たちのサーバーでそれを追跡しましたが、動作します: http://blogs.msdn.com/b/varunm/archive/2013/04/23/remove-unwanted-http-response-headers.aspx 。 URLScanのみを使用し、レジストリの変更を行わない場合、World Wide Publishing Serviceを停止している間に、サーバーはHTTP.sysファイルからサーバーhttp応答を返します。また、ここにUrlScanツールの使用に関する一般的な落とし穴があります。 http://msdn.Microsoft.com/en-us/library/ff648552.aspx#ht_urlscan_008

2
Pawel

IIS 10では、ドリューのアプローチと同様のソリューションを使用します。

using System;
using System.Web;

namespace Common.Web.Modules.Http
{
    /// <summary>
    /// Sets custom headers in all requests (e.g. "Server" header) or simply remove some.
    /// </summary>
    public class CustomHeaderModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.PreSendRequestHeaders += OnPreSendRequestHeaders;
        }

        public void Dispose() { }

        /// <summary>
        /// Event handler that implements the desired behavior for the PreSendRequestHeaders event,
        /// that occurs just before ASP.NET sends HTTP headers to the client.
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnPreSendRequestHeaders(object sender, EventArgs e)
        {
            //HttpContext.Current.Response.Headers.Remove("Server");
            HttpContext.Current.Response.Headers.Set("Server", "MyServer");
        }
    }
}

そして明らかに、プロジェクト内のそのdllへの参照と、必要な構成内のモジュールを追加します。

<system.webServer>
    <modules>
      <!--Use http module to remove/customize IIS "Server" header-->
      <add name="CustomHeaderModule" type="Common.Web.Modules.Http.CustomHeaderModule" />
    </modules>
</system.webServer>

重要な注1:このソリューションには、統合として設定されたアプリケーションプールが必要です。

重要な注意2:Webアプリ内のすべての応答は、この影響を受けます(cssとjsが含まれます)。

2
xautau

私はこれを調査しましたが、URLRewriteメソッドはうまく機能します。どこでもスクリプト化された変更を見つけることができないようです。 PowerShell v2以降と互換性のあるこれを作成し、IIS 7.5でテストしました。

# Add Allowed Server Variable
    Add-WebConfiguration /system.webServer/rewrite/allowedServerVariables -atIndex 0 -value @{name="RESPONSE_SERVER"}
# Rule Name
    $ruleName = "Remove Server Response Header"
# Add outbound IIS Rewrite Rule
    Add-WebConfigurationProperty -pspath "iis:\" -filter "system.webServer/rewrite/outboundrules" -name "." -value @{name=$ruleName; stopProcessing='False'}
#Set Properties of newly created outbound rule 
    Set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST"  -filter "system.webServer/rewrite/outboundRules/rule[@name='$ruleName']/match" -name "serverVariable" -value "RESPONSE_SERVER"
    Set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST"  -filter "system.webServer/rewrite/outboundRules/rule[@name='$ruleName']/match" -name "pattern" -value ".*"
    Set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST"  -filter "system.webServer/rewrite/outboundRules/rule[@name='$ruleName']/action" -name "type" -value "Rewrite"
0
Bill M

ここで、および他のいくつかの同様のスタックオーバーフロースレッドですべてを試しました。

設定を変更した後、ブラウザのキャッシュをクリアするのを忘れたため、少しハングアップしました。これを行わず、ファイルがローカルキャッシュにある場合、元のヘッダー(duh)を使用してファイルを返します。

RunAllManagedModulesForAllRequestsをremoving

<modules runAllManagedModulesForAllRequests="true">

これにより、静的ファイルのmostから無関係なヘッダーが削除されましたが、私はswaggerでWebAPIプロジェクトの静的ファイルの「サーバー」ヘッダーを取得していました。

私は最終的にこの解決策を見つけて適用し、今では不要なヘッダーのすべてがすべてなくなっています:

https://www.dionach.com/blog/easily-remove-unwanted-http-headers-in-iis-70-to-85

ここにある彼のコードについて説明します:

https://github.com/Dionach/StripHeaders/releases/tag/v1.0.5

これはネイティブコードモジュールです。値を空白にするだけでなく、サーバーヘッダーをremoveすることができます。デフォルトでは以下を削除します。

  • サーバ
  • X-Powered-By
  • X-Aspnet-Version
  • サーバー:Microsoft-HTTPAPI/2.0-「リクエストがIISに渡されない」場合に返されます
0
TechSavvySam

Global.asax.csファイルに以下のコードを追加できます

    protected void Application_PreSendRequestHeaders()
    {
        Response.Headers.Remove("Server");
    }