FTP over SSL <ssl>
概述
<ssl>
元素指定 FTP 服务的基于安全套接字层 (SSL) 的 FTP 设置;FTP 7.0 中首次针对 IIS 7 引入了 FTP over SSL。
与使用 HTTP over SSL(需要单独的端口和连接来进行安全 (HTTPS) 通信)不同,安全 FTP 通信与非安全通信使用相同的端口。 FTP 7 支持两种不同形式的 FTP over SSL:
- 显式 FTPS:默认情况下,FTP 站点和客户端使用端口 21 作为控制通道,服务器和客户端将协商使用辅助端口作为数据通道连接。 在典型的 FTP 请求中,FTP 客户端将通过控制通道连接到 FTP 站点,然后客户端可以就控制通道或数据通道与服务器协商 SSL/TLS。 使用 FTP 7 时,如果启用 FTPS 并将 FTP 站点分配到端口 990 以外的任何端口,则使用的是显式 SSL。
- 隐式 FTPS:隐式 FTPS 是一种较早的 FTP over SSL,仍受 FTP 7 支持。 对于隐式 FTPS,必须先协商 SSL 握手,然后客户端才能发送 FTP 命令。 此外,尽管显式 FTPS 允许客户端任意决定是否使用 SSL,但隐式 FTPS 要求必须对整个 FTP 会话进行加密。 使用 FTP 7 时,如果启用 FTPS 并将 FTP 站点分配到端口 990,则使用的是隐式 SSL。
根据在 controlChannelPolicy
和 dataChannelPolicy
属性中配置的安全选项,FTP 客户端可能会在单个显式 FTPS 会话中多次在安全和非安全之间切换。 根据业务需求,可以通过多种方式实现此目的:
controlChannelPolicy | dataChannelPolicy | 说明 |
---|---|---|
SslAllow |
SslAllow |
此配置允许客户端决定是否对 FTP 会话的任何部分进行加密。 |
SslRequireCredentialsOnly |
SslAllow |
此配置可保护 FTP 客户端凭据免遭电子窃听,并允许客户端决定是否对数据传输进行加密。 |
SslRequireCredentialsOnly |
SslRequire |
此配置要求客户端的凭据必须安全,然后允许客户端决定是否对 FTP 命令进行加密。 但是,所有数据传输都必须加密。 |
SslRequire |
SslRequire |
此配置是最安全的,客户端必须使用 FTPS 相关命令协商 SSL,然后才允许使用其他 FTP 命令,并且所有数据传输都必须加密。 |
兼容性
版本 | 说明 |
---|---|
IIS 10.0 | <ssl> 元素在 IIS 10.0 中未进行修改。 |
IIS 8.5 | <ssl> 元素在 IIS 8.5 中未进行修改。 |
IIS 8.0 | <ssl> 元素在 IIS 8.0 中未进行修改。 |
IIS 7.5 | <security> 元素的 <ssl> 元素作为 IIS 7.5 的功能提供。 |
IIS 7.0 | <security> 元素的 <ssl> 元素是在 IIS 7.0 的独立下载项 FTP 7.0 中引入的。 |
IIS 6.0 | IIS 6.0 中的 FTP 服务不支持 FTP over SSL。 |
在 Windows 7 和 Windows Server 2008 R2 中,FTP 7.5 服务作为 IIS 7.5 的功能提供,因此无需再下载 FTP 服务。
安装
若要支持 Web 服务器的 FTP 发布,必须安装 FTP 服务。 为此,请按照以下步骤操作。
Windows Server 2012 或 Windows Server 2012 R2
在任务栏上,单击 “服务器管理器”。
在“服务器管理器”中,单击“管理”菜单,然后单击“添加角色和功能”。
在“添加角色和功能”向导中,单击“下一步”。 选择安装类型,然后单击“下一步”。 选择目标服务器,然后单击“下一步”。
在“服务器角色”页上,展开“Web 服务器(IIS)”,然后选择“FTP 服务器”。
单击“下一步”,然后在“选择功能”页上,再次单击“下一步”。
在“确认安装选择”页上,单击“安装”。
在“结果” 页面中单击“关闭” 。
Windows 8 或 Windows 8.1
在“开始”屏幕上,将指针一直移动到左下角,右键单击“开始”按钮,然后单击“控制面板”。
在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
展开“Internet Information Services”,然后选择“FTP 服务器”。
单击“确定”。
单击“关闭” 。
Windows Server 2008 R2
在任务栏上,单击“开始”,指向“管理工具”,然后单击“服务器管理器”。
在“服务器管理器”层次结构窗格中,展开“角色”,然后单击“Web 服务器(IIS)”。
在“Web 服务器(IIS)”窗格中,滚动到“角色服务”部分,然后单击“添加角色服务”。
在“添加角色服务向导”的“选择角色服务”页,展开“FTP 服务器”。
选择“FTP 服务”。
单击 “下一步” 。
在“确认安装选择”页中,单击“安装”。
在“结果” 页面中单击“关闭” 。
Windows 7
在任务栏上,单击“开始”,然后单击“控制面板”。
在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
展开“Internet Information Services”,然后展开“FTP 服务器”。
选择“FTP 服务”。
单击“确定”。
Windows Server 2008 或 Windows Vista
从以下 URL 下载安装包:
按照以下演练中的说明安装 FTP 服务:
操作方式
如何为 FTP 站点配置 SSL 选项
打开 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)管理器”。
在“连接”窗格中,展开服务器名称,展开“站点”节点,然后单击站点名称。
在“SSL 策略”下,选择以下选项之一:
允许 SSL 连接:允许 FTP 服务器支持与客户端之间的非 SSL 和 SSL 连接。
要求 SSL 连接:要求对 FTP 服务器与客户端之间的通信进行 SSL 加密。
自定义:允许为控制通道和数据通道配置不同的 SSL 加密策略。 如果选择此选项,请单击“高级...”按钮。 显示“高级 SSL 策略”对话框时,选择以下选项:
在“控制通道”下,选择以下选项之一,以通过控制通道进行 SSL 加密:
- 允许:指定允许在控制通道中使用 SSL;FTP 客户端可以在控制通道中使用 SSL,但这不是必需的。
- 要求:指定要求在控制通道中使用 SSL;FTP 客户端不得将控制通道切换为非安全通信模式。
- 仅要求凭据:指定只有用户凭据必须通过 SSL 会话发送;FTP 客户端必须使用 SSL 获取用户名和密码,但登录后无需在控制通道中使用 SSL。
在“数据通道”下,选择以下选项之一,以通过数据通道进行 SSL 加密:
- 允许:允许在数据通道中使用 SSL;FTP 客户端可以在数据通道中使用 SSL,但这不是必需的。
- 要求:要求在数据通道中使用 SSL;FTP 客户端不得将数据通道切换为非安全通信模式。
- 拒绝:拒绝在数据通道中使用 SSL;FTP 客户端不得在数据通道中使用 SSL。
单击“确定”,关闭“高级 SSL 策略”对话框。
在“操作”窗格中,单击“应用”。
配置
<ssl>
元素在站点级别配置。
特性
属性 | 说明 | ||||||||
---|---|---|---|---|---|---|---|---|---|
controlChannelPolicy |
可选 enum 属性。 指定 FTP 控制通道的 SSL 策略。 注意:没有可拒绝在命令通道中使用 SSL 的枚举值;若要拒绝 SSL,请勿通过在 serverCertHash 属性中指定证书哈希来将 SSL 证书绑定到 FTP 站点。
SslRequire 。 |
||||||||
dataChannelPolicy |
可选 enum 属性。 指定 FTP 数据通道的 SSL 策略。
SslRequire 。 |
||||||||
serverCertHash |
可选的字符串属性。 指定用于 SSL 连接的服务器端证书的指纹哈希。 没有默认值。 |
||||||||
serverCertStoreName |
可选的字符串属性。 指定服务器 SSL 证书的证书存储。 默认值为 MY 。 |
||||||||
ssl128 |
可选的 Boolean 属性。 指定是否需要 128 位 SSL。 默认值为 false 。 |
子元素
无。
配置示例
以下示例展示 FTP 站点的 <ftpServer>
元素中的多个配置设置。 更具体而言,此示例中的 <site>
设置演示如何:
- 创建 FTP 站点,并为端口 21 的 FTP 协议添加绑定。
- 配置 FTP SSL 选项,以允许使用证书对控制通道和数据通道进行安全访问。
- 禁用“匿名身份验证”,然后为 FTP 启用“基本身份验证”。
- 拒绝 FTP SYST 命令的访问。
- 指定 UNIX 目录列表格式。
- 配置日志记录选项。
- 指定自定义欢迎消息并启用本地详细错误消息。
- 指定用户从基于其登录名的主目录中开始,前提是该目录存在。
<site name="ftp.example.com" id="5">
<application path="/">
<virtualDirectory path="/" physicalPath="c:\inetpub\www.example.com" />
</application>
<bindings>
<binding protocol="ftp" bindingInformation="*:21:" />
</bindings>
<ftpServer>
<security>
<ssl controlChannelPolicy="SslAllow"
dataChannelPolicy="SslAllow"
serverCertHash="57686f6120447564652c2049495320526f636b73" />
<authentication>
<basicAuthentication enabled="true" />
<anonymousAuthentication enabled="false" />
</authentication>
<commandFiltering maxCommandLine="4096" allowUnlisted="true">
<add command="SYST" allowed="false" />
</commandFiltering>
</security>
<directoryBrowse showFlags="StyleUnix" />
<logFile logExtFileFlags="Date, Time, ClientIP, UserName, ServerIP, Method, UriStem, FtpStatus, Win32Status, ServerPort, FtpSubStatus, Session, FullPath, Info" />
<messages expandVariables="true"
greetingMessage="Welcome %UserName%!"
allowLocalDetailedErrors="true" />
<userIsolation mode="StartInUsersDirectory" />
</ftpServer>
</site>
代码示例
以下示例配置 FTP 站点,以便要求在数据通道和控制通道中使用 SSL。
AppCmd.exe
appcmd.exe set config -section:system.applicationHost/sites /[name='ftp.example.com'].ftpServer.security.ssl.serverCertHash:"57686f6120447564652c2049495320526f636b73" /commit:apphost
appcmd.exe set config -section:system.applicationHost/sites /[name='ftp.example.com'].ftpServer.security.ssl.controlChannelPolicy:"SslRequire" /commit:apphost
appcmd.exe set config -section:system.applicationHost/sites /[name='ftp.example.com'].ftpServer.security.ssl.dataChannelPolicy:"SslRequire" /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 sitesSection = config.GetSection("system.applicationHost/sites");
ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();
ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", @"ftp.example.com");
if (siteElement == null) throw new InvalidOperationException("Element not found!");
ConfigurationElement ftpServerElement = siteElement.GetChildElement("ftpServer");
ConfigurationElement securityElement = ftpServerElement.GetChildElement("security");
ConfigurationElement sslElement = securityElement.GetChildElement("ssl");
sslElement["serverCertHash"] = @"57686f6120447564652c2049495320526f636b73";
sslElement["controlChannelPolicy"] = @"SslRequire";
sslElement["dataChannelPolicy"] = @"SslRequire";
serverManager.CommitChanges();
}
}
private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues)
{
foreach (ConfigurationElement element in collection)
{
if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase))
{
bool matches = true;
for (int i = 0; i < keyValues.Length; i += 2)
{
object o = element.GetAttributeValue(keyValues[i]);
string value = null;
if (o != null)
{
value = o.ToString();
}
if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase))
{
matches = false;
break;
}
}
if (matches)
{
return element;
}
}
}
return null;
}
}
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 sitesSection As ConfigurationSection = config.GetSection("system.applicationHost/sites")
Dim sitesCollection As ConfigurationElementCollection = sitesSection.GetCollection
Dim siteElement As ConfigurationElement = FindElement(sitesCollection, "site", "name", "ftp.example.com")
If (siteElement Is Nothing) Then
Throw New InvalidOperationException("Element not found!")
End If
Dim ftpServerElement As ConfigurationElement = siteElement.GetChildElement("ftpServer")
Dim securityElement As ConfigurationElement = ftpServerElement.GetChildElement("security")
Dim sslElement As ConfigurationElement = securityElement.GetChildElement("ssl")
sslElement("serverCertHash") = "57686f6120447564652c2049495320526f636b73"
sslElement("controlChannelPolicy") = "SslRequire"
sslElement("dataChannelPolicy") = "SslRequire"
serverManager.CommitChanges()
End Sub
Private Function FindElement(ByVal collection As ConfigurationElementCollection, ByVal elementTagName As String, ByVal ParamArray keyValues() As String) As ConfigurationElement
For Each element As ConfigurationElement In collection
If String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase) Then
Dim matches As Boolean = True
Dim i As Integer
For i = 0 To keyValues.Length - 1 Step 2
Dim o As Object = element.GetAttributeValue(keyValues(i))
Dim value As String = Nothing
If (Not (o) Is Nothing) Then
value = o.ToString
End If
If Not String.Equals(value, keyValues((i + 1)), StringComparison.OrdinalIgnoreCase) Then
matches = False
Exit For
End If
Next
If matches Then
Return element
End If
End If
Next
Return Nothing
End Function
End Module
JavaScript
var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST");
var sitesCollection = sitesSection.Collection;
var siteElementPos = FindElement(sitesCollection, "site", ["name", "ftp.example.com"]);
if (siteElementPos == -1) throw "Element not found!";
var siteElement = sitesCollection.Item(siteElementPos);
var ftpServerElement = siteElement.ChildElements.Item("ftpServer");
var securityElement = ftpServerElement.ChildElements.Item("security");
var sslElement = securityElement.ChildElements.Item("ssl");
sslElement.Properties.Item("serverCertHash").Value = "57686f6120447564652c2049495320526f636b73";
sslElement.Properties.Item("controlChannelPolicy").Value = "SslRequire";
sslElement.Properties.Item("dataChannelPolicy").Value = "SslRequire";
adminManager.CommitChanges();
function FindElement(collection, elementTagName, valuesToMatch) {
for (var i = 0; i < collection.Count; i++) {
var element = collection.Item(i);
if (element.Name == elementTagName) {
var matches = true;
for (var iVal = 0; iVal < valuesToMatch.length; iVal += 2) {
var property = element.GetPropertyByName(valuesToMatch[iVal]);
var value = property.Value;
if (value != null) {
value = value.toString();
}
if (value != valuesToMatch[iVal + 1]) {
matches = false;
break;
}
}
if (matches) {
return i;
}
}
}
return -1;
}
VBScript
Set adminManager = createObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST")
Set sitesCollection = sitesSection.Collection
siteElementPos = FindElement(sitesCollection, "site", Array("name", "ftp.example.com"))
If siteElementPos = -1 Then
Wscript.Echo "Element not found!"
WScript.Quit
End If
Set siteElement = sitesCollection.Item(siteElementPos)
Set ftpServerElement = siteElement.ChildElements.Item("ftpServer")
Set securityElement = ftpServerElement.ChildElements.Item("security")
Set sslElement = securityElement.ChildElements.Item("ssl")
sslElement.Properties.Item("serverCertHash").Value = "57686f6120447564652c2049495320526f636b73"
sslElement.Properties.Item("controlChannelPolicy").Value = "SslRequire"
sslElement.Properties.Item("dataChannelPolicy").Value = "SslRequire"
adminManager.CommitChanges()
Function FindElement(collection, elementTagName, valuesToMatch)
For i = 0 To CInt(collection.Count) - 1
Set element = collection.Item(i)
If element.Name = elementTagName Then
matches = True
For iVal = 0 To UBound(valuesToMatch) Step 2
Set property = element.GetPropertyByName(valuesToMatch(iVal))
value = property.Value
If Not IsNull(value) Then
value = CStr(value)
End If
If Not value = CStr(valuesToMatch(iVal + 1)) Then
matches = False
Exit For
End If
Next
If matches Then
Exit For
End If
End If
Next
If matches Then
FindElement = i
Else
FindElement = -1
End If
End Function