IP 安全 <ipSecurity>

概述

<ipSecurity> 元素定义 IIS 7 及更高版本中基于 IP 的安全限制的列表。 这些限制可以基于 IPv4 地址、IPv4 地址范围或 DNS 域名。

兼容性

版本 说明
IIS 10.0 <ipSecurity> 元素在 IIS 10.0 中未进行修改。
IIS 8.5 <ipSecurity> 元素在 IIS 8.5 中未进行修改。
IIS 8.0 添加了 enableProxyMode 属性,以阻止通过代理连接的客户端发出的请求。 添加了 denyAction 属性以指定 IIS 发回客户端的默认拒绝模式响应。
IIS 7.5 <ipSecurity> 元素未在 IIS 7.5 中进行修改。
IIS 7.0 <ipSecurity> 元素是在 IIS 7.0 中引入的。
IIS 6.0 <ipSecurity> 元素替代了 IIS 6.0 IPSecurity 元数据库属性。

安装

IIS 的默认安装不包括 IP 安全的角色服务或 Windows 功能。 若要在 IIS 上使用 IP 安全,必须使用以下步骤安装角色服务或 Windows 功能:

Windows Server 2012 或 Windows Server 2012 R2

  1. 在任务栏上,单击 “服务器管理器”。
  2. 在“服务器管理器”中,单击“管理”菜单,然后单击“添加角色和功能”。
  3. 在“添加角色和功能”向导中,单击“下一步”。 选择安装类型,然后单击“下一步”。 选择目标服务器,然后单击“下一步”。
  4. 在“服务器角色”页上,依次展开“Web 服务器 (IIS)”、“Web 服务器”和“安全性”,然后选择“IP 和域限制”。 单击 “下一步”
    Screenshot that shows I P and Domain Restrictions selected for Windows Server 2012.
  5. 在“选择功能”页上,单击“下一步”
  6. “确认安装选择”页上,单击“安装”
  7. 在“结果” 页面中单击“关闭”

Windows 8 或 Windows 8.1

  1. 在“开始”屏幕上,将指针一直移动到左下角,右键单击“开始”按钮,然后单击“控制面板”
  2. 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
  3. 依次展开“Internet 信息服务”、“万维网服务”和“安全性”,然后选择“IP 安全”。
    Screenshot that shows I P Security selected for Windows 8.
  4. 单击“确定”。
  5. 单击“关闭” 。

Windows Server 2008 或 Windows Server 2008 R2

  1. 在任务栏上,单击“开始”,指向“管理工具”,然后单击“服务器管理器”。

  2. 在“服务器管理器”层次结构窗格中,展开“角色”,然后单击“Web 服务器(IIS)”。

  3. 在“Web 服务器(IIS)”窗格中,滚动到“角色服务”部分,然后单击“添加角色服务”。

  4. 在“添加角色服务向导”的“选择角色服务”页上,选择“IP 和域限制”,然后单击“下一步”。

    Screenshot that shows I P and Domain Restrictions selected for Windows Server 2008.

  5. “确认安装选择”页中,单击“安装”

  6. 在“结果” 页面中单击“关闭”

Windows Vista 或 Windows 7

  1. 在任务栏上,单击“开始”,然后单击“控制面板”。

  2. 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。

  3. 扩展“Internet Information Services”,展开“万维网服务”,然后展开“安全性”。

  4. 选择“IP 安全”,然后单击“确定”

    Screenshot that shows I P Security selected for Windows Vista or Windows 7.

操作方式

如何添加 IP 限制以拒绝网站访问

  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. 在“连接”窗格中,展开服务器名称,展开“站点”,然后展开要为其添加 IP 限制的站点、应用程序或 Web 服务。

  3. 在“主页”窗格中,双击“IP 地址和域限制”功能。
    Screenshot that shows the I P Address and Domain Restrictions icon in the Home pane.

  4. 在“IP 地址和域限制”功能中的“操作”窗格中,单击“添加拒绝条目...”。
    Screenshot that shows the I P Address and Domain Restrictions pane in the I I S Manager.

  5. 输入要拒绝的 IP 地址,然后单击“确定”。
    Screenshot that shows the Add Deny Restriction Rule dialog box. Specific I P address is selected.


如何编辑网站的 IP 限制功能设置

  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. 在“连接”窗格中,展开服务器名称,展开“站点”,然后展开要为其添加 IP 限制的站点、应用程序或 Web 服务。

  3. 在“主页”窗格中,双击“IP 地址和域限制”功能。
    Screenshot that shows the I P Address and Domain Restrictions icon.

  4. 在“IP 地址和域限制”功能中的“操作”窗格中,单击“编辑功能设置...”。
    Screenshot that shows the I P Address and Domain Restrictions pane.

  5. 为未指定客户端选择默认访问行为,指定是否按域名启用限制,指定是否启用“代理模式”,选择“拒绝操作类型”,然后单击“确定”
    Screenshot of the Edit I P and Domain Restrictions Settings dialog box. Forbidden is selected for Deny Action Type.

配置

规则按列表中显示的顺序从上到下进行处理。 最后处理 allowUnlisted 属性。 关于 Internet 协议安全性 (IPsec) 限制,最佳做法是先列出拒绝规则。 如果此属性设置为 false,则无法清除 allowUnlisted 属性。

以下默认 <ipSecurity> 元素是在 IIS 7.0 更高版本的根 ApplicationHost.config 文件中配置的。 除非使用 <clear> 元素,否则此配置节将继承默认配置设置。

<ipSecurity allowUnlisted="true" />

特性

属性 说明
allowUnlisted 可选布尔属性。

指定是否允许未列出的 IP 地址。 将 allowUnlisted 属性设置为 true 将允许未列出的 IP 地址访问服务器。 将 allowUnlisted 属性设置为 false 将锁定服务器,防止访问所有 IP 地址,列出的 IP 地址除外。 如果要将此属性设置为 false,并且不将本地环回地址 (127.0.0.0.1) 列为允许的 IP 地址,则无法使用本地控制台中的浏览器访问服务器。

此属性还可能会影响委派。 如果要在父配置中将此属性设置为 false,则无法使用 <clear> 元素在子配置文件中清除此配置。

默认值为 true
denyAction 可选的枚举属性。

指定 IIS 应发送回客户端的默认拒绝模式响应。

默认值为 forbidden
说明
AbortRequest 指定默认情况下,IIS 应向客户端发送 Abort 拒绝模式响应。

数值为 0

Unauthorized 指定默认情况下,IIS 应向客户端发送 Unauthorized 拒绝模式响应。 如果远程端正在使用基于浏览器的客户端,则返回“未授权”可能会导致在远程客户端显示身份验证对话框,从而导致对 IIS 发起虚假的身份验证尝试。

数值为 401

Forbidden 指定默认情况下,IIS 应向客户端发送 Forbidden 拒绝模式响应。

数值为 403

NotFound 指定默认情况下,IIS 应向客户端发送 Not Found 拒绝模式响应。

数值为 404

enableProxyMode 可选布尔属性。

启用后 IIS 不仅可以阻止来自 IIS 看到的客户端 IP 的请求,还可以阻止来自 x-forwarded-for HTTP 标头中收到的 IP 地址的请求。 此标头可识别通过 HTTP 代理或负载均衡器连接的客户端的发起 IP 地址。 这称为代理模式。

默认值为 false
enableReverseDns 可选布尔属性。

指定为 Web 服务器启用或禁用反向域名系统 (DNS) 查找。 反向查找涉及在已知 IP 地址时查找域名。

警告:反向 DNS 查找将使用大量资源和时间。

默认值为 false

子元素

元素 说明
add 可选元素。

将 IP 限制添加到 IP 地址限制的集合中。
remove 可选元素。

<ipSecurity> 集合中删除对限制的引用。
clear 可选元素。

<ipSecurity> 集合中移除对限制的所有引用。

配置示例

以下配置示例将两个 IP 限制添加到默认网站;第一个限制拒绝 IP 地址 192.168.100.1 的访问,第二个限制拒绝整个 169.254.0.0 网络的访问。

<location path="Default Web Site">
   <system.webServer>
      <security>
         <ipSecurity>
            <add ipAddress="192.168.100.1" />
            <add ipAddress="169.254.0.0" subnetMask="255.255.0.0" />
         </ipSecurity>
      </security>
   </system.webServer>
</location>

代码示例

以下代码示例对默认网站启用了反向 DNS 查找。

AppCmd.exe

appcmd.exe set config "Default Web Site" -section:system.webServer/security/ipSecurity /enableReverseDns:true /commit:apphost

注意

使用 AppCmd.exe 配置这些设置时,必须确保将 commit 参数设置为 apphost。 这会将配置设置提交到 ApplicationHost.config 文件中的相应位置部分。

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.GetApplicationHostConfiguration();
         ConfigurationSection ipSecuritySection = config.GetSection("system.webServer/security/ipSecurity", "Default Web Site");
         ipSecuritySection["enableReverseDns"]=true;

         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.GetApplicationHostConfiguration
      Dim ipSecuritySection As ConfigurationSection = config.GetSection("system.webServer/security/ipSecurity", "Default Web Site")
      
      ipSecuritySection("enableReverseDns") = True
      
      serverManager.CommitChanges()
   End Sub

End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var ipSecuritySection = adminManager.GetAdminSection("system.webServer/security/ipSecurity", "MACHINE/WEBROOT/APPHOST/Default Web Site");

ipSecuritySection.Properties.Item("enableReverseDns").Value = True;

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set ipSecuritySection = adminManager.GetAdminSection("system.webServer/security/ipSecurity", "MACHINE/WEBROOT/APPHOST/Default Web Site")

ipSecuritySection.Properties.Item("enableReverseDns").Value = True

adminManager.CommitChanges()