请求筛选规则 <filteringRules>
概述
<filteringRules>
元素可指定自定义请求筛选规则的集合。 此元素允许管理员为其服务器创建自定义筛选规则,以扩展请求筛选功能的基本功能。
每个 <filteringRule>
元素指定自定义属性和元素的集合,它们根据用户定义的条件定义请求筛选行为。 例如,每个请求筛选规则可能包含以下属性:
denyUnescapedPercent
- 此属性指定请求筛选是否应拒绝请求(如果请求包含未转义的百分比符号)。scanAllRaw
- 此属性指定请求筛选是否应扫描denyStrings
元素中指定的字符串的原始标头。scanQueryString
- 此属性指定请求筛选是否应扫描denyStrings
元素中指定的字符串的查询字符串。 如果<requestFiltering>
元素的unescapeQueryString
属性设置为 true,则将对查询字符串进行两次扫描:第一次扫描原始查询字符串,第二次扫描未转义的查询字符串。scanUrl
- 此属性指定请求筛选是否应扫描denyStrings
元素中指定的字符串的 URL。
此外,每个请求筛选规则可能包含以下子元素:
<appliesTo>
- 指定请求筛选规则应用到的文件扩展名列表。注意
如果此部分为空,则规则适用于所有请求。
<denyStrings>
- 指定要拒绝请求筛选规则的字符串列表。<scanHeaders>
- 指定要扫描的 HTTP 标头的列表。
注意
当请求筛选因筛选规则而阻止 HTTP 请求时,IIS 7 将向客户端返回 HTTP 404 错误,并记录以下 HTTP 唯一子状态,以确定拒绝请求的原因:
HTTP 子状态 | 说明 |
---|---|
404.19 |
被筛选规则拒绝 |
此子状态允许 Web 管理员分析其 IIS 日志并识别潜在威胁。
兼容性
版本 | 说明 |
---|---|
IIS 10.0 | <filteringRules> 元素在 IIS 10.0 中未进行修改。 |
IIS 8.5 | <filteringRules> 元素在 IIS 8.5 中未进行修改。 |
IIS 8.0 | <filteringRules> 元素在 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
- 在任务栏上,单击 “服务器管理器”。
- 在“服务器管理器”中,单击“管理”菜单,然后单击“添加角色和功能”。
- 在“添加角色和功能”向导中,单击“下一步”。 选择安装类型,然后单击“下一步”。 选择目标服务器,然后单击“下一步”。
- 在“服务器角色”页上,依次展开“Web 服务器 (IIS)”、“Web 服务器”和“安全性”,然后选择“请求筛选”。 单击 “下一步” 。
。
- 在“选择功能”页上,单击“下一步”。
- 在“确认安装选择”页上,单击“安装”。
- 在“结果” 页面中单击“关闭” 。
Windows 8 或 Windows 8.1
- 在“开始”屏幕上,将指针一直移动到左下角,右键单击“开始”按钮,然后单击“控制面板”。
- 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
- 依次展开“Internet Information Services”、“万维网服务”和“安全性”,然后选择“请求筛选”。
- 单击“确定”。
- 单击“关闭” 。
Windows Server 2008 或 Windows Server 2008 R2
- 在任务栏上,单击“开始”,指向“管理工具”,然后单击“服务器管理器”。
- 在“服务器管理器”层次结构窗格中,展开“角色”,然后单击“Web 服务器(IIS)”。
- 在“Web 服务器(IIS)”窗格中,滚动到“角色服务”部分,然后单击“添加角色服务”。
- 在“添加角色服务向导”的“选择角色服务”页上,选择“请求筛选”,然后单击“下一步”。
- 在“确认安装选择”页中,单击“安装”。
- 在“结果” 页面中单击“关闭” 。
Windows Vista 或 Windows 7
- 在任务栏上,单击“开始”,然后单击“控制面板”。
- 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
- 扩展“Internet Information Services”,展开“万维网服务”,然后展开“安全性”。
- 选择“请求筛选”,然后单击“确定”。
操作方式
如何添加请求筛选规则
打开 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) 管理器”。
在“连接”窗格中,转到要为其配置请求筛选的站点、应用程序或目录。
在“主页”窗格中,双击“请求过滤”。
在“操作”窗格中,单击“添加筛选规则”。
在“添加筛选规则”对话框中输入筛选规则的以下信息:
单击“确定”关闭“添加筛选规则”对话框。
配置
<requestFiltering>
元素的 <filteringRules>
元素在站点、应用程序或目录级别配置。
特性
无。
子元素
元素 | 说明 |
---|---|
filteringRule |
可选元素。 将规则添加到自定义请求筛选规则的集合。 |
配置示例
下面的示例显示一个 <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>
以下示例显示了一个定义请求筛选规则的 <requestFiltering>
元素,该规则会拒绝 SQL 注入攻击中经常用于 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>
元素为默认网站添加请求筛选规则,以防止特定用户代理窃取 (leeching) 图像。 下面是此示例的场景:如果你检测到 Web 站点上的图像被特定用户代理窃取,则可以创建一个请求过滤规则,拒绝该特定用户代理访问图像文件。 在这个特定示例中,请求筛选规则将在 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()