添加要拒绝的 URL 序列 <add>

概述

<denyUrlSequences> 集合的 <add> 元素指定要添加到 Internet 信息服务 (IIS) 7 的拒绝 URL 序列集合中的唯一字符序列。

例如,在 URL 中使用两个句点 ("..") 将指示服务器处理下一个较高目录中的 URL,但也可能表明攻击者正在尝试访问内容区域之外的区域。 阻止这种字符模式将消除攻击者能够利用此 URL 序列的机会。

注意

当请求筛选因 URL 序列遭拒而阻止 HTTP 请求时,IIS 7 将向客户端返回 HTTP 404 错误,并记录以下 HTTP 唯一子状态,以确定拒绝请求的原因:

HTTP 子状态 说明
404.5 URL 序列被拒绝

此子状态允许 Web 管理员分析其 IIS 日志并识别潜在威胁。

注意

从 IIS 7.5 开始,可以将 URL 序列添加到 <alwaysAllowedUrls> 集合,以此来替代 <denyUrlSequences> 集合中的 URL 序列。

兼容性

版本 说明
IIS 10.0 <add> 元素在 IIS 10.0 中未进行修改。
IIS 8.5 <add> 元素在 IIS 8.5 中未进行修改。
IIS 8.0 <add> 元素在 IIS 8.0 中未进行修改。
IIS 7.5 <denyUrlSequences> 元素在 IIS 7.5 中未进行修改。

注意: IIS 7.5 允许通过将 URL 序列添加到 <alwaysAllowedUrls> 集合来替代 <denyUrlSequences> 元素中的 URL 序列。
IIS 7.0 IIS 7.0 中引入了 <requestFiltering> 集合的 <denyUrlSequences> 元素。
IIS 6.0 <denyUrlSequences> 元素替代了 IIS 6.0 UrlScan [DenyUrlSequences] 功能。

安装

IIS 7 及更高版本的默认安装包括请求筛选角色服务或功能。 如果卸载请求筛选角色服务或功能,可以使用以下步骤重新安装它。

Windows Server 2012 或 Windows Server 2012 R2

  1. 在任务栏上,单击 “服务器管理器”。
  2. 在“服务器管理器”中,单击“管理”菜单,然后单击“添加角色和功能”。
  3. 在“添加角色和功能”向导中,单击“下一步”。 选择安装类型,然后单击“下一步”。 选择目标服务器,然后单击“下一步”。
  4. 在“服务器角色”页上,依次展开“Web 服务器 (IIS)”、“Web 服务器”和“安全性”,然后选择“请求筛选”。 单击 “下一步”
    Screenshot of the Server Roles page. The Request Filtering node is highlighted and selected.
  5. 在“选择功能”页上,单击“下一步”
  6. “确认安装选择”页上,单击“安装”
  7. 在“结果” 页面中单击“关闭”

Windows 8 或 Windows 8.1

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

Windows Server 2008 或 Windows Server 2008 R2

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

Windows Vista 或 Windows 7

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

操作方式

IIS 7.0 用户请注意:本节中的某些步骤可能需要安装 Microsoft Administration Pack for IIS 7.0,其中包括请求筛选用户界面。 若要安装 Microsoft Administration Pack for IIS 7.0,请参阅以下 URL:

如何拒绝 URL 序列

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

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

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

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

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

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

  3. 在“主页”窗格中,双击“请求过滤”。
    Screenshot of the Default Web Site Home page. The Request Filtering icon is highlighted.

  4. 在“请求筛选”窗格中,单击“拒绝 URL 序列”选项卡,然后在“操作”窗格中单击“添加 URL 序列...”。
    Screenshot of the Request Filtering pane. The Deny U R L Sequences tab is selected. Add U R L Sequence is located in the Actions pane.

  5. 在“添加拒绝序列”对话框中,输入要阻止的 URL 序列,然后单击“确定”。
    Screenshot of the Add Deny Sequence dialog box. The U R L sequence box is shown.
    例如,若要防止服务器上的目录遍历,将在对话框中输入两个句点 ("..")。

配置

特性

属性 说明
sequence 必需的字符串属性。

指定 Web 服务器永远不应处理的 URL 中的字符序列,有助于防止对 Web 服务器进行基于 URL 的攻击。

子元素

无。

配置示例

以下 Web.config 文件示例将拒绝对三个 URL 序列的访问。 第一个序列可阻止目录遍历,第二个序列可阻止访问备用数据流,第三个序列可阻止在 URL 中使用反斜杠。

<configuration>
   <system.webServer>
      <security>
         <requestFiltering>
            <denyUrlSequences>
               <add sequence=".." />
               <add sequence=":" />
               <add sequence="\" />
            </denyUrlSequences>
         </requestFiltering>
      </security>
   </system.webServer>
</configuration>

下面的示例演示了 <denyUrlSequences> 元素和 <alwaysAllowedUrls> 元素的组合,如果它们包含两个特定字符序列中的任一,则会拒绝任何 URL,但如果特定 URL 包含以特定顺序出现的这两个特定字符序列,则将被始终允许。

<system.webServer>
   <security>
      <requestFiltering>
         <denyUrlSequences>
            <add sequence="bad" />
            <add sequence="sequence" />
         </denyUrlSequences>
         <alwaysAllowedUrls>
            <add url="/bad_sequence.txt" />
         </alwaysAllowedUrls>
      </requestFiltering>
   </security>
</system.webServer>

代码示例

下面的代码示例演示如何拒绝对默认网站的三个 URL 序列的访问:目录遍历 ("..")、备用数据流 (":") 和反斜杠 ("")。

AppCmd.exe

appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"denyUrlSequences.[sequence='..']" 

appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"denyUrlSequences.[sequence=':']" 

appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"denyUrlSequences.[sequence='\']"

PowerShell

Start-IISCommitDelay

$denyUrlSequences = Get-IISConfigSection -CommitPath 'Default Web Site' -SectionPath 'system.webServer/security/requestFiltering' | Get-IISConfigCollection -CollectionName 'denyUrlSequences'

New-IISConfigCollectionElement -ConfigCollection $denyUrlSequences -ConfigAttribute @{ 'sequence' = '..' }
New-IISConfigCollectionElement -ConfigCollection $denyUrlSequences -ConfigAttribute @{ 'sequence' = ':' }
New-IISConfigCollectionElement -ConfigCollection $denyUrlSequences -ConfigAttribute @{ 'sequence' = '\' }

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 denyUrlSequencesCollection = requestFilteringSection.GetCollection("denyUrlSequences");

         ConfigurationElement addElement = denyUrlSequencesCollection.CreateElement("add");
         addElement["sequence"] = @"..";
         denyUrlSequencesCollection.Add(addElement);

         ConfigurationElement addElement1 = denyUrlSequencesCollection.CreateElement("add");
         addElement1["sequence"] = @":";
         denyUrlSequencesCollection.Add(addElement1);

         ConfigurationElement addElement2 = denyUrlSequencesCollection.CreateElement("add");
         addElement2["sequence"] = @"\";
         denyUrlSequencesCollection.Add(addElement2);

         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 denyUrlSequencesCollection As ConfigurationElementCollection = requestFilteringSection.GetCollection("denyUrlSequences")

      Dim addElement As ConfigurationElement = denyUrlSequencesCollection.CreateElement("add")
      addElement("sequence") = ".."
      denyUrlSequencesCollection.Add(addElement)

      Dim addElement1 As ConfigurationElement = denyUrlSequencesCollection.CreateElement("add")
      addElement1("sequence") = ":"
      denyUrlSequencesCollection.Add(addElement1)

      Dim addElement2 As ConfigurationElement = denyUrlSequencesCollection.CreateElement("add")
      addElement2("sequence") = "\"
      denyUrlSequencesCollection.Add(addElement2)

      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 denyUrlSequencesCollection = requestFilteringSection.ChildElements.Item("denyUrlSequences").Collection;

var addElement = denyUrlSequencesCollection.CreateNewElement("add");
addElement.Properties.Item("sequence").Value = "..";
denyUrlSequencesCollection.AddElement(addElement);

var addElement1 = denyUrlSequencesCollection.CreateNewElement("add");
addElement1.Properties.Item("sequence").Value = ":";
denyUrlSequencesCollection.AddElement(addElement1);

var addElement2 = denyUrlSequencesCollection.CreateNewElement("add");
addElement2.Properties.Item("sequence").Value = "\\";
denyUrlSequencesCollection.AddElement(addElement2);

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 denyUrlSequencesCollection = requestFilteringSection.ChildElements.Item("denyUrlSequences").Collection

Set addElement = denyUrlSequencesCollection.CreateNewElement("add")
addElement.Properties.Item("sequence").Value = ".."
denyUrlSequencesCollection.AddElement(addElement)

Set addElement1 = denyUrlSequencesCollection.CreateNewElement("add")
addElement1.Properties.Item("sequence").Value = ":"
denyUrlSequencesCollection.AddElement(addElement1)

Set addElement2 = denyUrlSequencesCollection.CreateNewElement("add")
addElement2.Properties.Item("sequence").Value = "\"
denyUrlSequencesCollection.AddElement(addElement2)

adminManager.CommitChanges()