IIS 10.0 版本 1709 HTTP 嚴格傳輸安全性 (HSTS) 支援

作者: Shibing Shi

在 IIS 10.0 1709 版中,系統管理員可以選擇在月臺層級啟用 HSTS 和 HTTP 至 HTTPS 重新導向。

相容性

版本 備註
IIS 10.0 1709 版 本文所述的功能是在 IIS 10.0 1709 版中引進
IIS 10.0 和更早版本 IIS 10.0 0 1709 版之前不支援本文所述的功能

HTTP 嚴格的傳輸安全性 (HSTS)

RFC 6797中指定的 HTTP Strict Transport Security (HSTS) ,可讓網站將本身宣告為安全主機,並通知瀏覽器應該只透過 HTTPS 連線連絡。 HSTS 是一種加入宣告安全性增強功能,可強制執行 HTTPS,並大幅減少攔截伺服器與用戶端之間要求和回應的攔截型別攻擊的能力。

HSTS 會透過需要網頁伺服器和瀏覽器支援的原則強制執行 HTTPS 的使用。 啟用 HSTS 的 Web 主機可以包含特殊的 HTTP 回應標頭 「Strict-Transport-Security」 (STS) ,以及 HTTPS 回應中的 「max-age」 指示詞,以要求瀏覽器使用 HTTPS 進行進一步通訊。 瀏覽器會收到標頭,並記住 HSTS 原則,以取得 「max-age」 指示詞所指定的秒數。 在此期間內,如果使用者嘗試流覽相同的網站,但類型 HTTP:// 或完全省略配置,瀏覽器會自動將不安全的連結轉為安全的一個 (HTTPs://) ,並建立與伺服器的 HTTPS 連線。 一旦透過 HTTPS 收到回應,瀏覽器也會防止使用者「按一下」任何安全性警告 (例如關於無效伺服器憑證的警告) 。 為了利用 HSTS,瀏覽器必須至少查看 HSTS 標頭一次。 為了保護特定網域第一個連線中的使用者,HSTS 有個別的機制可將已註冊網域的清單預先載入瀏覽器現成可用的瀏覽器。

在 IIS 10.0 版本 1709 之前啟用 HSTS 的挑戰

在 IIS 10.0 1709 版之前,在 IIS 伺服器上啟用 HSTS 需要複雜的設定。

提供兩個在 IIS 10.0 1709 版之前啟用 HSTS 的解決方案範例案例:Web 系統管理員想要為可接受 HTTP 和 HTTPS 連線的網域 contoso.com 啟用 HSTS,並將所有 HTTP 流量重新導向至 HTTPS。 此案例中的重新導向本質上不安全,但它仍然是一種模式,後面接著許多支援 HTTPS 的網站。 仍在接聽 HTTP 的基本原因是網站無法控制訪客如何嘗試連線-透過 HTTPS 或純 HTTP。 啟用 HSTS 可大幅減少瀏覽器在第一次成功 HTTPS 連線期間看到 STS 標頭的狀況下,不安全的 HTTP 到 HTTPS 重新導向數目, (透過直接流覽或透過重新導向) 。

解決方案 1:HTTP 重新導向模組 + 自訂標頭

您可以使用 HTTP 重新導向模組 搭配兩個不同的網站,一個用於 HTTP,另一個用於 HTTP,另一個用於 HTTPS,以避免無限的重新導向迴圈,將所有 HTTP 流量重新導向至 HTTPS。

<sites>
    <site name="Contoso-http" id="1" serverAutoStart="true">
        <application path="/" applicationPool="Contoso-http">
            <virtualDirectory path="/" physicalPath="C:\inetpub\Contoso-http" />
        </application>
        <bindings>
            <binding protocol="http" bindingInformation="*:80:contoso.com" />
        </bindings>
    </site>
    <site name="Contoso-https" id="2" serverAutoStart="true">
        <application path="/" applicationPool="Contoso-https">
            <virtualDirectory path="/" physicalPath="C:\inetpub\Contoso-https" />
        </application>
        <bindings>
            <binding protocol="https" bindingInformation="*:443:contoso.com" sslFlags="0" />
        </bindings>
    </site>
    <siteDefaults>
        <logFile logFormat="W3C" directory="%SystemDrive%\inetpub\logs\LogFiles" />
        <traceFailedRequestsLogging directory="%SystemDrive%\inetpub\logs\FailedReqLogFiles" />
    </siteDefaults>
    <applicationDefaults applicationPool="DefaultAppPool" />
    <virtualDirectoryDefaults allowSubDirConfig="true" />
</sites>

重新導向規則會在 HTTP 網站的web.config中設定,以將其所有流量路由傳送至 HTTPS 網站,而稍後實際上會提供內容。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpRedirect enabled="true" destination="https://contoso.com" httpResponseStatus="Permanent" />
    </system.webServer>
</configuration>

您可以透過 自訂標頭 新增 STS 標頭,方法是設定 HTTPS 網站的web.config。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <add name="Strict-Transport-Security" value="max-age=31536000" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
</configuration>

解決方案 2:URL 重寫模組

替代解決方案是安裝 URL 重寫模組 ,並針對具有 HTTP 和 HTTPS 系結的單一網站設定重寫規則。 HTTP 到 HTTPS 重新導向可由輸入規則指定,同時將 STS 標頭新增至 HTTPS 回復可由輸出規則達成。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
                </rule>
            </rules>
            <outboundRules>
                <rule name="Add the STS header in HTTPS responses">
                    <match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />
                    <conditions>
                        <add input="{HTTPS}" pattern="on" />
                    </conditions>
                    <action type="Rewrite" value="max-age=31536000" />
                </rule>
            </outboundRules>
        </rewrite>
    </system.webServer>
</configuration>

IIS 10.0 版本 1709 原生 HSTS 支援

使用 IIS 10.0 1709 版的版本,HSTS 現在以原生方式支援。 啟用 HSTS 的組態大幅簡化 - HSTS 可在網站層級啟用,方法是在每個元素下設定元素的屬性 <hsts> ,您可以在網站 < HSTS 的 > HSTS 設定參考中找到更多詳細資料。 <site>

使用IISAdministration PowerShell Cmdlet設定網站的 、 max-ageredirectHttpToHttps 屬性 <hsts> ,即可直接達成 enabled 此範例案例。

Import-Module IISAdministration
Reset-IISServerManager -Confirm:$false
Start-IISCommitDelay

$sitesCollection = Get-IISConfigSection -SectionPath "system.applicationHost/sites" | Get-IISConfigCollection
$siteElement = Get-IISConfigCollectionElement -ConfigCollection $sitesCollection -ConfigAttribute @{"name"="Contoso"}
$hstsElement = Get-IISConfigElement -ConfigElement $siteElement -ChildElementName "hsts"
Set-IISConfigAttributeValue -ConfigElement $hstsElement -AttributeName "enabled" -AttributeValue $true
Set-IISConfigAttributeValue -ConfigElement $hstsElement -AttributeName "max-age" -AttributeValue 31536000
Set-IISConfigAttributeValue -ConfigElement $hstsElement -AttributeName "redirectHttpToHttps" -AttributeValue $true

Stop-IISCommitDelay
Remove-Module IISAdministration

網站的 HSTS 設定如下所示:

<site name="Contoso" id="1">
    <application path="/" applicationPool="Contoso">
        <virtualDirectory path="/" physicalPath="C:\Contoso\Content" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:80:contoso.com" />
        <binding protocol="https" bindingInformation="*:443:contoso.com" sslFlags="0" />
    </bindings>
    <hsts enabled="true" max-age="31536000" redirectHttpToHttps="true" />
</site>

HSTS 的原生支援也可以與 HTTP 重新導向模組 搭配使用,以取得更複雜的案例。

例如,網站 contoso.com 會將所有流量重新導向至其子域 www.contoso.com ,且網站都接受 HTTP 和 HTTPS 連線。 如果網站最好有單一標準位址,這是典型的案例。 建議針對根域和子域啟用 HSTS,因為使用者可能會透過 HTTP 或 HTTPS 直接流覽一個。 includeSubDomains啟用根域專案的 屬性 <hsts> 會進一步增強 HSTS 原則涵蓋範圍至其所有子域。

<sites>
    <site name="Contoso" id="1">
        <application path="/" applicationPool="Contoso">
            <virtualDirectory path="/" physicalPath="C:\inetpub\Contoso" />
        </application>
        <bindings>
            <binding protocol="http" bindingInformation="*:80:contoso.com" />
            <binding protocol="https" bindingInformation="*:443:contoso.com" sslFlags="0" />
        </bindings>
        <hsts enabled="true" max-age="31536000" includeSubDomains="true" redirectHttpToHttps="true" />
    </site>
    <site name="Contoso-www" id="2">
        <application path="/" applicationPool="Contoso-www">
            <virtualDirectory path="/" physicalPath="C:\inetpub\Contoso-www" />
        </application>
        <bindings>
            <binding protocol="http" bindingInformation="*:80:www.contoso.com" />
            <binding protocol="https" bindingInformation="*:443:www.contoso.com" sslFlags="0" />
        </bindings>
        <hsts enabled="true" max-age="31536000" redirectHttpToHttps="true" />
    </site>
    <siteDefaults>
        <logFile logFormat="W3C" directory="%SystemDrive%\inetpub\logs\LogFiles" />
        <traceFailedRequestsLogging directory="%SystemDrive%\inetpub\logs\FailedReqLogFiles" />
    </siteDefaults>
    <applicationDefaults applicationPool="DefaultAppPool" />
    <virtualDirectoryDefaults allowSubDirConfig="true" />
</sites>

此外,您可以從根域重新導向至子域,透過 <httpRedirect> 根域網站web.config中的 元素進行設定。 使用這類設定時,contoso.com 的 HTTP 要求會先重新導向至 HTTPS,然後將 HTTPS 要求重新 www.contoso.com 導向至相同的網站,並在回應中新增 STS 標頭。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpRedirect enabled="true" destination="https://www.contoso.com" httpResponseStatus="Permanent" />
    </system.webServer>
</configuration>

上述範例組態也適用于將流量從來源月臺重新導向至不是來源月臺子域的目的地月臺,並稍微修改來源月臺的設定 includeSubDomains