新增標頭限制 <>
概觀
<add>
集合的 <headerLimits>
元素會指定 HTTP 標頭的大小上限。
注意
當要求篩選因為 HTTP 要求超過標頭限制而封鎖 HTTP 要求時,IIS 7 會將 HTTP 404 錯誤傳回給用戶端,並記錄下列子狀態,以識別要求遭到拒絕的原因:
HTTP 子狀態 | 描述 |
---|---|
431 |
要求標頭太長 |
此子狀態可讓 Web 系統管理員分析其 IIS 記錄,並識別潛在的威脅。
相容性
版本 | 備註 |
---|---|
IIS 10.0 | 未在 IIS 10.0 中修改專案 <add> 。 |
IIS 8.5 | 未在 IIS 8.5 中修改專案 <add> 。 |
IIS 8.0 | 未在 IIS 8.0 中修改專案 <add> 。 |
IIS 7.5 | 未在 IIS 7.5 中修改專案 <add> 。 |
IIS 7.0 | <requestLimits> 集合的 <requestFiltering> 元素是在 IIS 7.0 中引進。 |
IIS 6.0 | 元素 <requestLimits> 會取代 IIS 6.0 UrlScan [RequestLimits] 功能。 |
安裝程式
IIS 7 和更新版本的預設安裝包含要求篩選角色服務或功能。 如果卸載要求篩選角色服務或功能,您可以使用下列步驟重新安裝它。
Windows Server 2012 或 Windows Server 2012 R2
- 在工作列上,按一下 [伺服器管理員]。
- 在[伺服器管理員] 中,按一下 [管理] 功能表,然後按一下 [新增角色及功能]。
- 在 [ 新增角色及功能精 靈] 中,按 [ 下一步]。 選取安裝類型,然後按 [ 下一步]。 選取目的地伺服器,然後按 [ 下一步]。
- 在 [ 伺服器角色] 頁面上,依序展開 [ Web 服務器] ([IIS) ]、[ Web 服務器]、[ 安全性],然後選取 [ 要求篩選]。 按一下 [下一步] 。
. - 在 [選取功能] 頁面上,按 [下一步]。
- 在 [確認安裝選項] 頁面上,按一下 [安裝]。
- 在 [結果] 頁面上,按一下 [關閉]。
Windows 8 或 Windows 8.1
- 在 [開始] 畫面上,將指標一路移至左下角,以滑鼠右鍵按一下 [開始] 按鈕,然後按一下[主控台]。
- 在主控台中,按一下 [程式和功能],然後按一下 [開啟或關閉 Windows 功能]。
- 依 序展開 [Internet Information Services]、[ 萬維網服務]、[ 安全性],然後選取 [ 要求篩選]。
- 按一下 [確定]。
- 按一下 [關閉] 。
Windows Server 2008 或 Windows Server 2008 R2
- 在工作列上,按一下 [開始],指向 [系統管理工具],然後按一下[伺服器管理員]。
- 在[伺服器管理員階層] 窗格中,展開 [角色],然後按一下 [Web 服務器] (IIS) 。
- 在 [ Web Server (IIS) ] 窗格中,捲動至 [ 角色服務 ] 區段,然後按一下 [ 新增角色服務]。
- 在 [新增角色服務精靈] 的 [選取角色服務] 頁面上,選取 [要求篩選],然後按 [下一步]。
- 在 [確認安裝選項] 頁面上,按一下 [安裝]。
- 在 [結果] 頁面上,按一下 [關閉]。
Windows Vista 或 Windows 7
- 在工作列上,按一下 [開始],然後按一下[主控台]。
- 在主控台中,按一下 [程式和功能],然後按一下[開啟或關閉 Windows 功能]。
- 依序展開 [Internet Information Services]、[ World Wide Web 服務] 和 [ 安全性]。
- 選取 [ 要求篩選],然後按一下 [ 確定]。
作法
IIS 7.0 使用者的注意事項:本節中的某些步驟可能需要您安裝適用于 IIS 7.0 的 Microsoft 系統管理元件,其中包含用於要求篩選的使用者介面。 若要安裝適用于 IIS 7.0 的 Microsoft 系統管理元件,請參閱下列 URL:
如何新增 HTTP 標頭的限制
開啟 [Internet Information Services (IIS) 管理員:
如果您使用 Windows Server 2012 或 Windows Server 2012 R2:
- 在工作列上,依序按一下 [伺服器管理員]、[工具],然後按一下 [Internet Information Services (IIS) Manager]。
如果您使用 Windows 8 或 Windows 8.1:
- 按住Windows鍵,按字母X,然後按一下[主控台]。
- 按一下 [系統管理工具],然後按兩下 [ Internet Information Services (IIS) Manager]。
如果您使用 Windows Server 2008 或 Windows Server 2008 R2:
- 在工作列上,按一下 [ 開始],指向 [ 系統管理工具],然後按一下 [ Internet Information Services (IIS) 管理員]。
如果您使用 Windows Vista 或 Windows 7:
- 在工作列上,按一下 [開始],然後按一下[主控台]。
- 按兩下 [系統管理工具],然後按兩下 [ Internet Information Services] (IIS) Manager。
在 [ 連線] 窗格中,移至您要修改要求篩選設定的連線、月臺、應用程式或目錄。
在 [ 新增標頭 ] 對話方塊中,輸入 HTTP 標頭和您想要用於標頭限制的大小上限,然後按一下 [ 確定]。
例如,「Content-type」 標頭包含要求的 MIME 類型。 指定 100 的值會將 「Content-type」 標頭的長度限制為 100 個位元組。
組態
屬性
屬性 | 描述 |
---|---|
header |
必要的字串屬性。 指定 HTTP 標頭的名稱。 |
sizeLimit |
必要的 uint 屬性。 指定 HTTP 標頭的大小上限,以位元組為單位。 將值設定為 0,有效地拒絕包含 標頭的所有要求。 |
子元素
無。
組態範例
下列範例Web.config檔案會將 IIS 設定為拒絕 HTTP 要求的存取,其中 「Content-type」 標頭的長度大於 100 個位元組。
<configuration>
<system.webServer>
<security>
<requestFiltering>
<requestLimits>
<headerLimits>
<add header="Content-type" sizeLimit="100" />
</headerLimits>
</requestLimits>
</requestFiltering>
</security>
</system.webServer>
</configuration>
範例程式碼
下列程式碼範例會將 IIS 設定為拒絕 HTTP 要求的存取,其中「Content-type」 標頭的長度大於 100 個位元組。
AppCmd.exe
appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"requestLimits.headerLimits.[header='Content-type',sizeLimit='100']"
PowerShell
$requestLimits = Get-IISConfigSection -CommitPath 'Default Web Site' -SectionPath 'system.webServer/security/requestFiltering' | Get-IISConfigElement -ChildElementName 'requestLimits'
$headerLimits = Get-IISConfigCollection -ConfigElement $requestLimits -CollectionName 'headerLimits'
New-IISConfigCollectionElement -ConfigCollection $headerLimits -ConfigAttribute @{ 'header'='Content-Type'; 'sizeLimit'=100 }
C#
using System;
using System.Text;
using Microsoft.Web.Administration;
internal static class Sample
{
private static void Main()
{
using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetWebConfiguration("Default Web Site");
ConfigurationSection requestFilteringSection = config.GetSection("system.webServer/security/requestFiltering");
ConfigurationElement requestLimitsElement = requestFilteringSection.GetChildElement("requestLimits");
ConfigurationElementCollection headerLimitsCollection = requestLimitsElement.GetCollection("headerLimits");
ConfigurationElement addElement = headerLimitsCollection.CreateElement("add");
addElement["header"] = @"Content-type";
addElement["sizeLimit"] = 100;
headerLimitsCollection.Add(addElement);
serverManager.CommitChanges();
}
}
}
VB.NET
Imports System
Imports System.Text
Imports Microsoft.Web.Administration
Module Sample
Sub Main()
Dim serverManager As ServerManager = New ServerManager
Dim config As Configuration = serverManager.GetWebConfiguration("Default Web Site")
Dim requestFilteringSection As ConfigurationSection = config.GetSection("system.webServer/security/requestFiltering")
Dim requestLimitsElement As ConfigurationElement = requestFilteringSection.GetChildElement("requestLimits")
Dim headerLimitsCollection As ConfigurationElementCollection = requestLimitsElement.GetCollection("headerLimits")
Dim addElement As ConfigurationElement = headerLimitsCollection.CreateElement("add")
addElement("header") = "Content-type"
addElement("sizeLimit") = 100
headerLimitsCollection.Add(addElement)
serverManager.CommitChanges()
End Sub
End Module
JavaScript
var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site";
var requestFilteringSection = adminManager.GetAdminSection("system.webServer/security/requestFiltering", "MACHINE/WEBROOT/APPHOST/Default Web Site");
var requestLimitsElement = requestFilteringSection.ChildElements.Item("requestLimits");
var headerLimitsCollection = requestLimitsElement.ChildElements.Item("headerLimits").Collection;
var addElement = headerLimitsCollection.CreateNewElement("add");
addElement.Properties.Item("header").Value = "Content-type";
addElement.Properties.Item("sizeLimit").Value = 100;
headerLimitsCollection.AddElement(addElement);
adminManager.CommitChanges();
VBScript
Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site"
Set requestFilteringSection = adminManager.GetAdminSection("system.webServer/security/requestFiltering", "MACHINE/WEBROOT/APPHOST/Default Web Site")
Set requestLimitsElement = requestFilteringSection.ChildElements.Item("requestLimits")
Set headerLimitsCollection = requestLimitsElement.ChildElements.Item("headerLimits").Collection
Set addElement = headerLimitsCollection.CreateNewElement("add")
addElement.Properties.Item("header").Value = "Content-type"
addElement.Properties.Item("sizeLimit").Value = 100
headerLimitsCollection.AddElement(addElement)
adminManager.CommitChanges()
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應