共用方式為


要求篩選規則 < 篩選Rules>

概觀

元素 <filteringRules> 會指定自訂要求篩選規則的集合。 此元素可讓系統管理員為其伺服器建立自訂的篩選規則,以擴充要求篩選功能的基本功能。

每個 <filteringRule> 元素都會指定自訂屬性和元素的集合,這些屬性和元素會根據使用者定義的準則定義要求篩選行為。 例如,每個要求篩選規則可能包含下列屬性:

  • denyUnescapedPercent - 這個屬性會指定如果要求篩選包含未逸出的百分比符號,是否應該拒絕要求。
  • scanAllRaw - 這個屬性會指定要求篩選是否應該掃描原始標頭中指定于 元素中的 denyStrings 字串。
  • scanQueryString - 這個屬性會指定要求篩選是否應該掃描查詢字串中指定于 元素中的 denyStrings 字串。 unescapeQueryString如果元素的 <requestFiltering> 屬性設定為true,則會對查詢字串進行兩次掃描:一次使用原始查詢字串進行掃描,另一次掃描查詢字串,其中未逸出查詢字串。
  • scanUrl - 這個屬性會指定要求篩選是否應該掃描 URL 中指定于 元素中的 denyStrings 字串。

此外,每個要求篩選規則可能包含下列子項目:

  • <appliesTo> - 指定要求篩選規則所套用的副檔名清單。

    注意

    如果本節是空白的,規則會套用至所有要求。

  • <denyStrings> - 指定要拒絕要求篩選規則的字串清單。

  • <scanHeaders> - 指定要掃描的 HTTP 標頭清單。

注意

當要求篩選因為篩選規則而封鎖 HTTP 要求時,IIS 7 會將 HTTP 404 錯誤傳回給用戶端,並以唯一的子狀態記錄下列 HTTP 狀態,以識別要求遭到拒絕的原因:

HTTP 子狀態 描述
404.19 篩選規則拒絕

此子狀態可讓 Web 系統管理員分析其 IIS 記錄,並識別潛在的威脅。

相容性

版本 備註
IIS 10.0 未在 IIS 10.0 中修改專案 <filteringRules>
IIS 8.5 未在 IIS 8.5 中修改專案 <filteringRules>
IIS 8.0 未在 IIS 8.0 中修改專案 <filteringRules>
IIS 7.5 元素 <filteringRules><requestFiltering> 元素隨附為 IIS 7.5 的功能。
IIS 7.0 元素 <filteringRules><requestFiltering> 元素已引進為 IIS 7.0 的更新,可透過 Microsoft 知識庫文章957508 (https://support.microsoft.com/kb/957508) 取得。
IIS 6.0 元素 <filteringRules> 大致上類似于新增至 URLScan 3.0 的 RuleList 功能。

安裝程式

IIS 7 和更新版本的預設安裝包含要求篩選角色服務或功能。 如果卸載要求篩選角色服務或功能,您可以使用下列步驟重新安裝它。

Windows Server 2012 或 Windows Server 2012 R2

  1. 在工作列上,按一下 [伺服器管理員]
  2. [伺服器管理員] 中,按一下 [管理] 功能表,然後按一下 [新增角色及功能]。
  3. 在 [ 新增角色及功能精 靈] 中,按 [ 下一步]。 選取安裝類型,然後按 [ 下一步]。 選取目的地伺服器,然後按 [ 下一步]。
  4. 在 [ 伺服器角色] 頁面上,依序展開 [ Web 服務器] ([IIS) ]、[ Web 服務器]、[ 安全性],然後選取 [ 要求篩選]。 按一下 [下一步] 。
    此螢幕擷取畫面顯示已展開 [Web 服務器和安全性] 窗格,並已選取 [要求篩選]。 .
  5. 在 [選取功能] 頁面上,按 [下一步]
  6. 在 [確認安裝選項] 頁面上,按一下 [安裝]
  7. 在 [結果] 頁面上,按一下 [關閉]

Windows 8 或 Windows 8.1

  1. 在 [開始] 畫面上,將指標一路移至左下角,以滑鼠右鍵按一下 [開始] 按鈕,然後按一下[主控台]。
  2. 主控台中,按一下 [程式和功能],然後按一下 [開啟或關閉 Windows 功能]。
  3. 序展開 [Internet Information Services]、[ 萬維網服務]、[ 安全性],然後選取 [ 要求篩選]。
    萬維網服務和安全性窗格的螢幕擷取畫面,其中已醒目提示 [要求篩選]。
  4. 按一下 [確定]。
  5. 按一下 [關閉] 。

Windows Server 2008 或 Windows Server 2008 R2

  1. 在工作列上,按一下 [開始],指向 [系統管理工具],然後按一下[伺服器管理員]。
  2. [伺服器管理員階層] 窗格中,展開 [角色],然後按一下 [Web 服務器] (IIS)
  3. 在 [ Web Server (IIS) ] 窗格中,捲動至 [ 角色服務 ] 區段,然後按一下 [ 新增角色服務]。
  4. 在 [新增角色服務精靈] 的 [選取角色服務] 頁面上,選取 [要求篩選],然後按 [下一步]。
    螢幕擷取畫面顯示 [新增角色服務精靈] 中的 [選取角色服務] 頁面,其中已展開 [安全性] 窗格並已選取 [要求篩選]。
  5. 在 [確認安裝選項] 頁面上,按一下 [安裝]
  6. 在 [結果] 頁面上,按一下 [關閉]

Windows Vista 或 Windows 7

  1. 在工作列上,按一下 [開始],然後按一下[主控台]。
  2. 主控台中,按一下 [程式和功能],然後按一下[開啟或關閉 Windows 功能]。
  3. 依序展開 [Internet Information Services]、[ World Wide Web 服務] 和 [ 安全性]。
  4. 選取 [ 要求篩選],然後按一下 [ 確定]。
    螢幕擷取畫面顯示 [開啟或關閉 Windows 功能] 和 [要求篩選] 中展開的安全性節點。

作法

如何新增要求篩選規則

  1. 開啟 [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
  2. 在 [ 連線] 窗格中,移至您要設定要求篩選的月臺、應用程式或目錄。

  3. 在 [ 首頁] 窗格中,按兩下 [ 要求篩選]。

  4. 在 [ 要求篩選 ] 窗格中,按一下 [ 規則] 索引標籤。
    [要求篩選] 窗格的螢幕擷取畫面,其中顯示 [規則] 索引標籤。[新增篩選規則] 選項會顯示在 [動作] 窗格中。

  5. 在 [ 動作 ] 窗格中,按一下 [ 新增篩選規則]。

  6. 在 [ 新增篩選規則 ] 對話方塊中輸入下列篩選規則資訊:

    • 在 [ 名稱 ] 欄位中輸入篩選規則的易記名稱。
    • 如果您想要篩選規則掃描要求的 URL 存根,請選取 [掃描 URL]。
    • 如果您想要篩選規則掃描要求的查詢字串,請選取 [掃描查詢字串 ]。
    • 輸入掃描標頭集合中要掃描的任何 HTTP 標頭
    • 輸入要與 [套用至 ] 集合中的篩選規則搭配使用的副檔名。
    • 輸入要拒絕的字串集合,以用於 拒絕字串 集合中的篩選規則。
      顯示 [新增篩選規則] 對話方塊的螢幕擷取畫面,其中已核取 [名稱] 欄位、[掃描您為 l] 和 [掃描查詢字串] 方塊。
  7. 按一下 [確定 ] 以關閉 [ 新增篩選規則] 對話方塊。

組態

專案的 <filteringRules> 元素 <requestFiltering> 是在月臺、應用程式或目錄層級設定。

屬性

無。

子元素

元素 描述
filteringRule 選擇性項目。

將規則新增至自訂要求篩選規則的集合。

組態範例

下列範例顯示 <requestFiltering> 使用 <denyStrings><appliesTo><scanHeaders> 元素來定義要求篩選規則,以防止特定使用者代理程式的影像竊取 () 。

<requestFiltering>
   <filteringRules>
      <filteringRule name="Block Image Leeching" scanUrl="false" scanQueryString="false" scanAllRaw="false">
         <scanHeaders>
            <add requestHeader="User-agent" />
         </scanHeaders>
         <appliesTo>
            <add fileExtension=".gif" />
            <add fileExtension=".jpg" />
            <add fileExtension=".png" />
         </appliesTo>
         <denyStrings>
            <add string="leech-bot" />
         </denyStrings>
      </filteringRule>
   </filteringRules>
</requestFiltering>

下列範例顯示定義 <requestFiltering> 要求篩選規則的元素,藉由拒絕查詢字串中常用於 SQL 插入式攻擊的文字字串集合來防止 SQL 插入式攻擊。

<requestFiltering>
   <filteringRules>
      <filteringRule name="SQLInjection" scanUrl="false" scanQueryString="true">
         <appliesTo>
            <clear />
            <add fileExtension=".asp" />
            <add fileExtension=".aspx" />
            <add fileExtension=".php" />
         </appliesTo>
         <denyStrings>
            <clear />
            <add string="--" />
            <add string=";" />
            <add string="/*" />
            <add string="@" />
            <add string="char" />
            <add string="alter" />
            <add string="begin" />
            <add string="cast" />
            <add string="create" />
            <add string="cursor" />
            <add string="declare" />
            <add string="delete" />
            <add string="drop" />
            <add string="end" />
            <add string="exec" />
            <add string="fetch" />
            <add string="insert" />
            <add string="kill" />
            <add string="open" />
            <add string="select" />
            <add string="sys" />
            <add string="table" />
            <add string="update" />
         </denyStrings>
         <scanHeaders>
            <clear />
         </scanHeaders>
      </filteringRule>
   </filteringRules>
</requestFiltering>

範例程式碼

下列範例示範如何使用 <denyStrings><appliesTo><scanHeaders> 元素,為預設網站新增要求篩選規則,以防止特定使用者代理程式的影像竊取 (竊) 。 以下是此範例的案例:如果您偵測到網站上的影像遭到特定使用者代理程式佔用,您可以建立要求篩選規則,以拒絕存取該特定使用者代理程式的影像檔案。 在此特定範例中,如果使用者代理程式標頭包含搜尋字串,則要求篩選規則會搜尋字串 「leech-bot」 的 HTTP 使用者代理程式標頭,並拒絕存取 GIF、JPG 和 PNG 檔案。

AppCmd.exe

appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"filteringRules.[name='Block Image Leeching',scanUrl='False',scanQueryString='False',scanAllRaw='False']" 

appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"filteringRules.[name='Block Image Leeching'].scanHeaders.[requestHeader='User-agent']" 

appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"filteringRules.[name='Block Image Leeching'].appliesTo.[fileExtension='.gif']" 

appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"filteringRules.[name='Block Image Leeching'].appliesTo.[fileExtension='.jpg']" 

appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"filteringRules.[name='Block Image Leeching'].appliesTo.[fileExtension='.png']" 

appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"filteringRules.[name='Block Image Leeching'].denyStrings.[string='leech-bot']"

PowerShell

Start-IISCommitDelay

$filteringRules = Get-IISConfigSection -CommitPath 'Default Web Site' -SectionPath 'system.webServer/security/requestFiltering' | Get-IISConfigCollection -CollectionName 'filteringRules' 
New-IISConfigCollectionElement -ConfigCollection $filteringRules -ConfigAttribute @{ 'name' = 'Block Image Leeching'; 'scanUrl' = $false; 'scanQueryString' = $false; 'scanAllRaw' = $false; } 

$Rule = Get-IISConfigCollectionElement -ConfigCollection $filteringRules -ConfigAttribute @{ 'name' = 'Block Image Leeching' }

$ruleScanHeaders = Get-IISConfigCollection -ConfigElement $Rule -CollectionName 'scanHeaders' 
New-IISConfigCollectionElement -ConfigCollection $ruleScanHeaders -ConfigAttribute @{ 'requestHeader' = 'User-Agent' }

$ruleAppliesTo = Get-IISConfigCollection -ConfigElement $Rule -CollectionName 'appliesTo'
New-IISConfigCollectionElement -ConfigCollection $ruleAppliesTo -ConfigAttribute @{ 'fileExtension' = '.gif' }
New-IISConfigCollectionElement -ConfigCollection $ruleAppliesTo -ConfigAttribute @{ 'fileExtension' = '.jpg' }
New-IISConfigCollectionElement -ConfigCollection $ruleAppliesTo -ConfigAttribute @{ 'fileExtension' = '.png' }

$ruleDenyStrings = Get-IISConfigCollection -ConfigElement $Rule -CollectionName 'denyStrings'
New-IISConfigCollectionElement -ConfigCollection $ruleDenyStrings -ConfigAttribute @{ 'string' = 'leech-bot' }

Stop-IISCommitDelay

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");
         ConfigurationElementCollection filteringRulesCollection = requestFilteringSection.GetCollection("filteringRules");

         ConfigurationElement filteringRuleElement = filteringRulesCollection.CreateElement("filteringRule");
         filteringRuleElement["name"] = @"Block Image Leeching";
         filteringRuleElement["scanUrl"] = false;
         filteringRuleElement["scanQueryString"] = false;
         filteringRuleElement["scanAllRaw"] = false;

         ConfigurationElementCollection scanHeadersCollection = filteringRuleElement.GetCollection("scanHeaders");
         ConfigurationElement addElement = scanHeadersCollection.CreateElement("add");
         addElement["requestHeader"] = @"User-agent";
         scanHeadersCollection.Add(addElement);

         ConfigurationElementCollection appliesToCollection = filteringRuleElement.GetCollection("appliesTo");
         ConfigurationElement addElement1 = appliesToCollection.CreateElement("add");
         addElement1["fileExtension"] = @".gif";
         appliesToCollection.Add(addElement1);
         ConfigurationElement addElement2 = appliesToCollection.CreateElement("add");
         addElement2["fileExtension"] = @".jpg";
         appliesToCollection.Add(addElement2);
         ConfigurationElement addElement3 = appliesToCollection.CreateElement("add");
         addElement3["fileExtension"] = @".png";
         appliesToCollection.Add(addElement3);

         ConfigurationElementCollection denyStringsCollection = filteringRuleElement.GetCollection("denyStrings");
         ConfigurationElement addElement4 = denyStringsCollection.CreateElement("add");
         addElement4["string"] = @"leech-bot";
         denyStringsCollection.Add(addElement4);

         filteringRulesCollection.Add(filteringRuleElement);
         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 filteringRulesCollection As ConfigurationElementCollection = requestFilteringSection.GetCollection("filteringRules")

      Dim filteringRuleElement As ConfigurationElement = filteringRulesCollection.CreateElement("filteringRule")
      filteringRuleElement("name") = "Block Image Leeching"
      filteringRuleElement("scanUrl") = False
      filteringRuleElement("scanQueryString") = False
      filteringRuleElement("scanAllRaw") = False

      Dim scanHeadersCollection As ConfigurationElementCollection = filteringRuleElement.GetCollection("scanHeaders")
      Dim addElement As ConfigurationElement = scanHeadersCollection.CreateElement("add")
      addElement("requestHeader") = "User-agent"
      scanHeadersCollection.Add(addElement)

      Dim appliesToCollection As ConfigurationElementCollection = filteringRuleElement.GetCollection("appliesTo")
      Dim addElement1 As ConfigurationElement = appliesToCollection.CreateElement("add")
      addElement1("fileExtension") = ".gif"
      appliesToCollection.Add(addElement1)
      Dim addElement2 As ConfigurationElement = appliesToCollection.CreateElement("add")
      addElement2("fileExtension") = ".jpg"
      appliesToCollection.Add(addElement2)
      Dim addElement3 As ConfigurationElement = appliesToCollection.CreateElement("add")
      addElement3("fileExtension") = ".png"
      appliesToCollection.Add(addElement3)

      Dim denyStringsCollection As ConfigurationElementCollection = filteringRuleElement.GetCollection("denyStrings")
      Dim addElement4 As ConfigurationElement = denyStringsCollection.CreateElement("add")
      addElement4("string") = "leech-bot"
      denyStringsCollection.Add(addElement4)

      filteringRulesCollection.Add(filteringRuleElement)
      serverManager.CommitChanges()
   End Sub
End Module

JavaScript

adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site";
var requestFilteringSection = adminManager.GetAdminSection("system.webServer/security/requestFiltering", "MACHINE/WEBROOT/APPHOST/Default Web Site");
var filteringRulesCollection = requestFilteringSection.ChildElements.Item("filteringRules").Collection;

var filteringRuleElement = filteringRulesCollection.CreateNewElement("filteringRule");
filteringRuleElement.Properties.Item("name").Value = "Block Image Leeching";
filteringRuleElement.Properties.Item("scanUrl").Value = false;
filteringRuleElement.Properties.Item("scanQueryString").Value = false;
filteringRuleElement.Properties.Item("scanAllRaw").Value = false;

var scanHeadersCollection = filteringRuleElement.ChildElements.Item("scanHeaders").Collection;
var addElement = scanHeadersCollection.CreateNewElement("add");
addElement.Properties.Item("requestHeader").Value = "User-agent";
scanHeadersCollection.AddElement(addElement);

var appliesToCollection = filteringRuleElement.ChildElements.Item("appliesTo").Collection;
var addElement1 = appliesToCollection.CreateNewElement("add");
addElement1.Properties.Item("fileExtension").Value = ".gif";
appliesToCollection.AddElement(addElement1);
var addElement2 = appliesToCollection.CreateNewElement("add");
addElement2.Properties.Item("fileExtension").Value = ".jpg";
appliesToCollection.AddElement(addElement2);
var addElement3 = appliesToCollection.CreateNewElement("add");
addElement3.Properties.Item("fileExtension").Value = ".png";
appliesToCollection.AddElement(addElement3);

var denyStringsCollection = filteringRuleElement.ChildElements.Item("denyStrings").Collection;
var addElement4 = denyStringsCollection.CreateNewElement("add");
addElement4.Properties.Item("string").Value = "leech-bot";
denyStringsCollection.AddElement(addElement4);

filteringRulesCollection.AddElement(filteringRuleElement);
adminManager.CommitChanges();

VBScript

Set adminManager = 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 filteringRulesCollection = requestFilteringSection.ChildElements.Item("filteringRules").Collection

Set filteringRuleElement = filteringRulesCollection.CreateNewElement("filteringRule")
filteringRuleElement.Properties.Item("name").Value = "Block Image Leeching"
filteringRuleElement.Properties.Item("scanUrl").Value = False
filteringRuleElement.Properties.Item("scanQueryString").Value = False
filteringRuleElement.Properties.Item("scanAllRaw").Value = False

Set scanHeadersCollection = filteringRuleElement.ChildElements.Item("scanHeaders").Collection
Set addElement = scanHeadersCollection.CreateNewElement("add")
addElement.Properties.Item("requestHeader").Value = "User-agent"
scanHeadersCollection.AddElement(addElement)

Set appliesToCollection = filteringRuleElement.ChildElements.Item("appliesTo").Collection
Set addElement1 = appliesToCollection.CreateNewElement("add")
addElement1.Properties.Item("fileExtension").Value = ".gif"
appliesToCollection.AddElement(addElement1)
Set addElement2 = appliesToCollection.CreateNewElement("add")
addElement2.Properties.Item("fileExtension").Value = ".jpg"
appliesToCollection.AddElement(addElement2)
Set addElement3 = appliesToCollection.CreateNewElement("add")
addElement3.Properties.Item("fileExtension").Value = ".png"
appliesToCollection.AddElement(addElement3)

Set denyStringsCollection = filteringRuleElement.ChildElements.Item("denyStrings").Collection
Set addElement4 = denyStringsCollection.CreateNewElement("add")
addElement4.Properties.Item("string").Value = "leech-bot"
denyStringsCollection.AddElement(addElement4)

filteringRulesCollection.AddElement(filteringRuleElement)
adminManager.CommitChanges()