因失败而拒绝 <denyByFailure>

概述

<denyByFailure> 元素将 FTP 服务配置为根据 FTP 客户端子在指定时间段内的身份验证失败次数来拒绝对 FTP 服务的访问。 登录失败次数达到指定数值后,FTP 连接将强行关闭,并且将在某一时间段(由 entryExpiration 属性设置)内阻止客户端 IP 地址访问 FTP 服务。 只能为服务器启用“按失败率拒绝访问”,不能为单独站点启用这一特征。

注意

如果客户端的 IP 地址与服务器级别指定的“FTP IP 地址和域限制”功能中的“允许输入特定 IP 地址”匹配,则客户端将获得额外的信任,并允许绕过 FTP 登录尝试限制检查。

兼容性

版本 说明
IIS 10.0 <denyByFailure> 元素在 IIS 10.0 中未进行修改。
IIS 8.5 <denyByFailure> 元素在 IIS 8.5 中未进行修改。
IIS 8.0 <denyByFailure> 元素是在 IIS 8.0 中引入的。
IIS 7.5 空值
IIS 7.0 空值
IIS 6.0 空值

安装

若要支持在 FTP 服务器上通过身份验证拒绝访问,必须安装 FTP 服务角色服务。

Windows Server 2012

  1. 按 Windows 徽标键,然后单击“服务器管理器”。

  2. 在“服务器管理器”中,单击“管理”,然后单击“添加角色和功能”

  3. 在“添加角色和功能”向导中

    • 开始之前页面上,单击下一步
    • 在“安装类型”页上,选择安装类型,然后单击“下一步”
    • 在“服务器选择”页上,选择适当的服务器,然后单击“下一步”
    • 在“服务器角色”页上,确保选中“Web 服务器(IIS)”,然后将其展开
    • 展开“FTP 服务器”,然后选择“FTP 服务”和“FTP 扩展性”,然后单击“下一步”
    • 在“功能”页上,单击“下一步”。
    • “确认安装选择”页中,单击“安装”
    • 在“结果” 页面中单击“关闭”

Windows 8

  1. 打开 Windows“控制面板”。
  2. 在 Windows 控制面板中,打开“程序和功能”
  3. 在“程序和功能”中,单击“打开或关闭 Windows 功能”
  4. 在“Windows 功能”对话框中,展开“Internet Information Services”,然后展开“FTP 服务器”
  5. 在“FTP 服务器”下面,选择“FTP 服务”和“FTP 扩展性”,然后单击“确定”。

操作方式

如何基于登录失败率拒绝对 FTP 服务器的访问

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

    • 如果使用 Windows Server 2012 或更高版本:

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

      • 按住 Windows 键,按字母 X,然后单击“控制面板”。
      • 单击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
  2. 在“连接”窗格中,选择服务器

  3. 在“主页”窗格中,双击“FTP 登录尝试限制”功能。

    Screenshot of the Enable F T P Logon Attempt Restrictions, Maximum number of failed login attempts, and time period in seconds fields.

  4. 在“FTP 登录尝试限制”对话框中,单击“启用”以启用限制,然后输入最大失败次数,还要输入在到达最大失败次数后,提示拒绝对 FTP 服务的访问的时间段。

  5. 单击“仅写入日志”将记录已满足的条件,但不阻止登录尝试。

  6. 在“操作”窗格中,单击“应用”

配置

<denyByFailure> 元素在服务器级别配置。

特性

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

使 FTP 服务能够基于失败率拒绝访问。

默认值为 False
maxFailure 可选 uint 属性。

指定特定时间长度内的失败次数,在特定时间达到此失败次数后,将向用户发出“FTP 服务访问遭到拒绝”的提示。

默认值为 4
entryExpiration 可选的 TimeSpan 属性。

指定一个时间段,在该时间段内达到指定的失败次数后,将提示拒绝对 FTP 服务的访问。

默认值为 00:00:30
loggingOnlyMode 可选布尔属性。

指定将不会由于失败率而拒绝访问,但将记录该事件。

默认值为 False

子元素

无。

配置示例

以下示例配置 <denyByFailure> 元素。

<system.ftpServer>
   <security>
      <authentication>
         <denyByFailure enabled="true" maxFailure="5" entryExpiration="00:00:45" loggingOnlyMode="false" />
      </authentication>
   </security>
</system.ftpServer>

代码示例

以下代码示例根据登录失败率配置 FTP 服务器拒绝访问。

AppCmd.exe

appcmd.exe set config  -section:system.ftpServer/security/authentication /denyByFailure.enabled:"True" /denyByFailure.maxFailure:"10" /denyByFailure.entryExpiration:"00:00:35" /denyByFailure.loggingOnlyMode:"False"  /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 authenticationSection = config.GetSection("system.ftpServer/security/authentication");
            
            ConfigurationElement denyByFailureElement = authenticationSection.GetChildElement("denyByFailure");
            denyByFailureElement["enabled"] = true;
            denyByFailureElement["maxFailure"] = 10;
            denyByFailureElement["entryExpiration"] = TimeSpan.Parse("00:00:35");
            denyByFailureElement["loggingOnlyMode"] = false;
            
            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 authenticationSection As ConfigurationSection = config.GetSection ("system.ftpServer/security/authentication")
      Dim denyByFailureElement As ConfigurationElement = authenticationSection.GetChildElement("denyByFailure")
      denyByFailureElement("enabled") = true
      denyByFailureElement("maxFailure") = 10
      denyByFailureElement("entryExpiration") = TimeSpan.Parse("00:00:35")
      denyByFailureElement("loggingOnlyMode") = false
      serverManager.CommitChanges
   End Sub
End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";

var authenticationSection = adminManager.GetAdminSection("system.ftpServer/security/authentication", "MACHINE/WEBROOT/APPHOST");
var denyByFailureElement = authenticationSection.ChildElements.Item("denyByFailure");
denyByFailureElement.Properties.Item("enabled").Value = true;
denyByFailureElement.Properties.Item("maxFailure").Value = 10;
denyByFailureElement.Properties.Item("entryExpiration").Value = "00:00:35";
denyByFailureElement.Properties.Item("loggingOnlyMode").Value = false;

adminManager.CommitChanges();

VBScript

Set adminManager = CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"

Set authenticationSection = adminManager.GetAdminSection("system.ftpServer/security/authentication", "MACHINE/WEBROOT/APPHOST")
Set denyByFailureElement = authenticationSection.ChildElements.Item("denyByFailure")
denyByFailureElement.Properties.Item("enabled").Value = true
denyByFailureElement.Properties.Item("maxFailure").Value = 10
denyByFailureElement.Properties.Item("entryExpiration").Value = "00:00:35"
denyByFailureElement.Properties.Item("loggingOnlyMode").Value = false

adminManager.CommitChanges()

PowerShell

Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.ftpServer/security/authentication/denyByFailure" -name "enabled" -value "True"
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.ftpServer/security/authentication/denyByFailure" -name "maxFailure" -value 10
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.ftpServer/security/authentication/denyByFailure" -name "entryExpiration" -value "00:00:35"
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.ftpServer/security/authentication/denyByFailure" -name "loggingOnlyMode" -value "False"