筛选规则的 HTTP 标头 <scanHeaders>

概述

<filteringRule> 元素的 <scanHeaders> 元素定义 HTTP 标头集合,请求筛选规则将扫描这些标头是否包含 <denyStrings> 集合中指定的字符串。 <scanHeaders> 元素包含一系列 <add> 元素,其中每个元素指定要添加到集合的唯一 HTTP 标头。

兼容性

版本 说明
IIS 10.0 <scanHeaders> 元素在 IIS 10.0 中未进行修改。
IIS 8.5 <scanHeaders> 元素在 IIS 8.5 中未进行修改。
IIS 8.0 <scanHeaders> 元素在 IIS 8.0 中未进行修改。
IIS 7.5 <requestFiltering> 元素的 <filteringRules> 元素作为 IIS 7.5 的功能提供。
IIS 7.0 <requestFiltering> 元素的 <filteringRules> 元素作为 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 服务器”和“安全性”,然后选择“请求筛选”。 单击 “下一步”
    Screenshot of Server Role page displaying the Web Server I I S and Security node expanded and Request Filtering selected.
  5. 在“选择功能”页上,单击“下一步”
  6. “确认安装选择”页上,单击“安装”
  7. 在“结果” 页面中单击“关闭”

Windows 8 或 Windows 8.1

  1. 在“开始”屏幕上,将指针一直移动到左下角,右键单击“开始”按钮,然后单击“控制面板”
  2. 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
  3. 依次展开“Internet Information Services”、“万维网服务”和“安全性”,然后选择“请求筛选”。
    Screenshot displays the World Wide Web Services and Security pane expanded with Request Filtering selected.
  4. 单击“确定”。
  5. 单击“关闭” 。

Windows Server 2008 或 Windows Server 2008 R2

  1. 在任务栏上,单击“开始”,指向“管理工具”,然后单击“服务器管理器”。
  2. 在“服务器管理器”层次结构窗格中,展开“角色”,然后单击“Web 服务器 (IIS)”。
  3. 在“Web 服务器 (IIS)”窗格中,滚动到“角色服务”部分,然后单击“添加角色服务”。
  4. 在“添加角色服务向导”的“选择角色服务”页上,选择“请求筛选”,然后单击“下一步”。
    Screenshot of Security pane expanded and Request Filtering selected.
  5. “确认安装选择”页中,单击“安装”
  6. 在“结果” 页面中单击“关闭”

Windows Vista 或 Windows 7

  1. 在任务栏上,单击“开始”,然后单击“控制面板”。
  2. 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
  3. 扩展“Internet Information Services”,展开“万维网服务”,然后展开“安全性”。
  4. 选择“请求筛选”,然后单击“确定”。
    Screenshot that shows the Internet Information Services and Security node expanded. Request Filtering is highlighted.

操作方式

如何添加请求筛选规则

  1. 打开 Internet Information Services (IIS) 管理器:

    • 如果使用的是 Windows Server 2012 或 Windows Server 2012 R2:

      • 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS) 管理器”
    • 如果使用 Windows 8 或 Windows 8.1:

      • 按住 Windows 键,按字母 X,然后单击“控制面板”。
      • 单击“管理工具”,然后双击“Internet Information Services (IIS) 管理器”。
    • 如果使用的是 Windows Server 2008 或 Windows Server 2008 R2:

      • 在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS) 管理器”
    • 如果使用的是 Windows Vista 或 Windows 7:

      • 在任务栏上,单击“开始”,然后单击“控制面板”。
      • 双击“管理工具”,然后双击“Internet Information Services (IIS) 管理器”。
  2. 在“连接”窗格中,转到要为其配置请求筛选的站点、应用程序或目录。

  3. 在“主页”窗格中,双击“请求过滤”。

  4. 在“请求筛选”窗格中,单击“规则”选项卡。
    Screenshot of Request Filtering pane displaying the Rules tab and Add Filtering Rule option in the Actions pane.

  5. 在“操作”窗格中,单击“添加筛选规则”。

  6. 在“添加筛选规则”对话框中输入筛选规则的以下信息:

    • 在“名称”字段中输入筛选规则的友好名称。
    • 如果希望筛选规则扫描请求的 URL 存根,请选择“扫描 URL”。
    • 如果希望筛选规则扫描请求的查询字符串,请选择“扫描查询字符串”。
    • 在“扫描标头”集合中输入要扫描的任何 HTTP 标头。
    • 在“应用于”集合中,输入要用于筛选规则的文件扩展名。
    • 在“拒绝字符串”集合中输入要拒绝使用筛选规则的字符串集合。
      Screenshot that shows the Add Filtering Rule dialog box. Scan u r l and Scan query string options are both selected.
  7. 单击“确定”关闭“添加筛选规则”对话框。

配置

<filteringRule> 元素的 <scanHeaders> 元素在站点、应用程序或目录级别配置。

特性

无。

子元素

元素 说明
add 可选元素。

将唯一的 HTTP 标头添加到请求筛选规则将扫描的 HTTP 标头集合。
clear 可选元素。

清除请求筛选规则将扫描的 HTTP 标头集合。
remove 可选元素。

从请求筛选规则将扫描的 HTTP 标头集合删除唯一的 HTTP 标头。

配置示例

下面的示例显示一个 <requestFiltering> 元素,该元素使用 <denyStrings><appliesTo><scanHeaders> 元素来定义一个请求筛选规则,以阻止特定用户代理窃取 (leeching) 图像。

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

代码示例

下面的示例演示如何使用 <denyStrings><appliesTo><scanHeaders> 元素为默认网站添加请求筛选规则,以防止特定用户代理窃取 (leeching) 图像。 下面是此示例的场景:如果你检测到网站上的图像被特定用户代理窃取,则可以创建一个请求过滤规则,拒绝该特定用户代理访问图像文件。 在这个特定示例中,请求筛选规则将在 HTTP 用户代理标头中搜索“leech-bot”字符串,如果用户代理标头包含搜索字符串,则拒绝其访问 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()