Active FTP vs. Passive FTP, a Definitive Explanation

原文:http://slacksite.com/other/ftp.html
中文翻译:http://www.phpweblog.net/killjin/archive/2008/01/06/2653.html

Contents:

Introduction

One of the most commonly seen questions when dealing with firewalls andother Internet connectivity issues is the difference betweenactive and passive FTP and how best to support either or both ofthem. Hopefully the following text will help to clear up some of theconfusion over how to support FTP in a firewalled environment.

This may not be the definitive explanation, as the title claims, however, I‘ve heard enough good feedback and seen this document linked in enough places to know that quite a few people have found it to be useful. I am always looking for ways to improve things though, and if you find something that is not quite clear or needs more explanation, please let me know! Recent additions to this document include the examples of both active and passive command line FTP sessions. These session examples should help make things a bit clearer. They also provide a nice picture into what goes on behind the scenes during an FTP session. Now, on to the information...

The Basics

FTP is a TCP based service exclusively. There is no UDP component toFTP. FTP is an unusual service in that it utilizes two ports, a ‘data‘ port and a ‘command‘ port (also known as the control port). Traditionallythese are port 21 for the command port and port 20 for the dataport. The confusion begins however, when we find that depending on themode, the data port is not always on port 20.

Active FTP

In active mode FTP the client connects from a random unprivilegedport (N > 1023) to the FTP server‘s command port, port 21. Then, theclient starts listening to port N+1 and sends the FTP command PORTN+1 to the FTP server. The server will then connect back to theclient‘s specified data port from its local data port, which is port 20.

From the server-side firewall‘s standpoint, to support active mode FTPthe following communication channels need to be opened:

  • FTP server‘s port 21 from anywhere (Client initiates connection)
  • FTP server‘s port 21 to ports > 1023 (Server responds toclient‘s control port)
  • FTP server‘s port 20 to ports > 1023 (Server initiates dataconnection to client‘s data port)
  • FTP server‘s port 20 from ports > 1023 (Client sends ACKs toserver‘s data port)

When drawn out, the connection appears as follows:

In step 1, the client‘s command port contacts the server‘s command portand sends the command PORT 1027. The server then sends anACK back to the client‘s command port in step 2. In step 3 the serverinitiates a connection on its local data port to the data port the clientspecified earlier. Finally, the client sends an ACK back as shown in step4.

The main problem with active mode FTP actually falls on the clientside. The FTP client doesn‘t make the actual connection to the data portof the server--it simply tells the server what port it is listening on andthe server connects back to the specified port on the client. From theclient side firewall this appears to be an outside system initiating aconnection to an internal client--something that is usually blocked.

Active FTP Example

Below is an actual example of an active FTP session. The only things that have been changed are the server names, IP addresses, and user names. In this example an FTP session is initiated from testbox1.slacksite.com (192.168.150.80), a linux box running the standard FTP command line client, to testbox2.slacksite.com (192.168.150.90), a linux box running ProFTPd 1.2.2RC2. The debugging (-d) flag is used with the FTP client to show what is going on behind the scenes. Everything in red is the debugging output which shows the actual FTP commands being sent to the server and the responses generated from those commands. Normal server output is shown in black, and user input is in bold.

There are a few interesting things to consider about this dialog. Notice that when the PORT command is issued, it specifies a port on the client (192.168.150.80) system, rather than the server. We will see the opposite behavior when we use passive FTP. While we are on the subject, a quick note about the format of the PORT command. As you can see in the example below it is formatted as a series of six numbers separated by commas. The first four octets are the IP address while the last two octets comprise the port that will be used for the data connection. To find the actual port multiply the fifth octet by 256 and then add the sixth octet to the total. Thus in the example below the port number is ( (14*256) + 178), or 3762. A quick check with netstat should confirm this information.

testbox1: {/home/p-t/slacker/public_html} % ftp -d testbox2Connected to testbox2.slacksite.com.220 testbox2.slacksite.com FTP server ready.Name (testbox2:slacker): slacker---> USER slacker331 Password required for slacker.Password: TmpPass---> PASS XXXX230 User slacker logged in.---> SYST215 UNIX Type: L8Remote system type is UNIX.Using binary mode to transfer files.ftp> lsftp: setsockopt (ignored): Permission denied---> PORT 192,168,150,80,14,178200 PORT command successful.---> LIST150 Opening ASCII mode data connection for file list.drwx------   3 slacker    users         104 Jul 27 01:45 public_html226 Transfer complete.ftp> quit---> QUIT221 Goodbye.

Passive FTP

In order to resolve the issue of the server initiating the connection tothe client a different method for FTP connections was developed. This wasknown as passive mode, or PASV, after the command used by theclient to tell the server it is in passive mode.

In passive mode FTP the client initiates both connections to the server,solving the problem of firewalls filtering the incoming data portconnection to the client from the server. When opening an FTP connection,the client opens two random unprivileged ports locally (N > 1023 andN+1). The first port contacts the server on port 21, but instead of thenissuing a PORT command and allowing the server to connectback to its data port, the client will issue the PASVcommand. The result of this is that the server then opens a randomunprivileged port (P > 1023) and sends P back to the client in response to the PASV command. The client then initiates the connection from portN+1 to port P on the server to transfer data.

From the server-side firewall‘s standpoint, to support passive mode FTPthe following communication channels need to be opened:

  • FTP server‘s port 21 from anywhere (Client initiates connection)
  • FTP server‘s port 21 to ports > 1023 (Server responds to client‘scontrol port)
  • FTP server‘s ports > 1023 from anywhere (Client initiates dataconnection to random port specified by server)
  • FTP server‘s ports > 1023 to remote ports > 1023 (Server sendsACKs (and data) to client‘s data port)

When drawn, a passive mode FTP connection looks like this:

In step 1, the client contacts the server on the command port and issuesthe PASV command. The server then replies in step 2 withPORT 2024, telling the client which port it is listening tofor the data connection. In step 3 the client then initiates the dataconnection from its data port to the specified server data port. Finally,the server sends back an ACK in step 4 to the client‘s data port.

While passive mode FTP solves many of the problems from the client side,it opens up a whole range of problems on the server side. The biggestissue is the need to allow any remote connection to high numbered ports onthe server. Fortunately, many FTP daemons, including the popular WU-FTPDallow the administrator to specify a range of ports which the FTP serverwill use. See Appendix 1 for more information.

The second issue involves supporting and troubleshooting clients which do (or do not) support passive mode. As an example, thecommand line FTP utility provided with Solaris does not support passivemode, necessitating a third-party FTP client, such as ncftp.
NOTE: This is no longer the case--use the -p option with the Solaris FTP client toenable passive mode!

With the massive popularity of the World Wide Web, many people prefer touse their web browser as an FTP client. Most browsers only supportpassive mode when accessing ftp:// URLs. This can either be good or baddepending on what the servers and firewalls are configured to support.

Passive FTP Example

Below is an actual example of a passive FTP session. The only things that have been changed are the server names, IP addresses, and user names. In this example an FTP session is initiated from testbox1.slacksite.com (192.168.150.80), a linux box running the standard FTP command line client, to testbox2.slacksite.com (192.168.150.90), a linux box running ProFTPd 1.2.2RC2. The debugging (-d) flag is used with the FTP client to show what is going on behind the scenes. Everything in red is the debugging output which shows the actual FTP commands being sent to the server and the responses generated from those commands. Normal server output is shown in black, and user input is in bold.

Notice the difference in the PORT command in this example as opposed to the active FTP example. Here, we see a port being opened on the server (192.168.150.90) system, rather than the client. See the discussion about the format of the PORT command above, in the Active FTP Example section.

testbox1: {/home/p-t/slacker/public_html} % ftp -d testbox2Connected to testbox2.slacksite.com.220 testbox2.slacksite.com FTP server ready.Name (testbox2:slacker): slacker---> USER slacker331 Password required for slacker.Password: TmpPass---> PASS XXXX230 User slacker logged in.---> SYST215 UNIX Type: L8Remote system type is UNIX.Using binary mode to transfer files.ftp> passivePassive mode on.ftp> lsftp: setsockopt (ignored): Permission denied---> PASV227 Entering Passive Mode (192,168,150,90,195,149).---> LIST150 Opening ASCII mode data connection for file listdrwx------   3 slacker    users         104 Jul 27 01:45 public_html226 Transfer complete.ftp> quit---> QUIT221 Goodbye.

Other Notes

A reader, Maarten Sjouw, pointed out that active FTP will not function when used in conjunction with a client-side NAT (Network Address Translation) device which is not smart enough to alter the IP address info in FTP packets.

Summary

The following chart should help admins remember how each FTP mode works:

Active FTP :    command : client >1023 -> server 21     data    : client >1023 <- server 20 Passive FTP : command : client >1023 -> server 21     data    : client >1024 -> server >1023

A quick summary of the pros and cons of active vs. passive FTP is also in order:

Active FTP is beneficial to the FTP server admin, but detrimental to the client side admin. The FTP server attempts to make connections to random high ports on the client, which would almost certainly be blocked by a firewall on the client side. Passive FTP is beneficial to the client, but detrimental to the FTP server admin. The client will make both connections to the server, but one of them will be to a random high port, which would almost certainly be blocked by a firewall on the server side.

Luckily, there is somewhat of a compromise. Since admins running FTP servers will need to make their servers accessible to the greatest number of clients, they will almost certainly need to support passive FTP. The exposure of high level ports on the server can be minimized by specifying a limited port range for the FTP server to use. Thus, everything except for this range of ports can be firewalled on the server side. While this doesn‘t eliminate all risk to the server, it decreases it tremendously. See Appendix 1 for more information.

References

An excellent reference on how various internet protocols work and theissues involved in firewalling them can be found in the O‘Reilly andAssociates book, Building Internet Firewalls, 2nd Ed, by BrentChapman and Elizabeth Zwicky.
Note 2012:This book is VERY old and the information contained therein may be outdated!

Finally, the definitive reference on FTP would be RFC 959, which sets forth the official specifications of the FTP protocol. RFCs can be downloaded from numerous locations, including http://www.faqs.org/rfcs/rfc959.html.

原文地址:https://www.cnblogs.com/dingzk/p/12703625.html

时间: 2024-08-08 02:12:41

Active FTP vs. Passive FTP, a Definitive Explanation的相关文章

FTP操作/Passive/Active控制

1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.IO; 5 using System.Net; 6 using System.Globalization; 7 using System.IO.Compression; 8 using ICSharpCode.SharpZipLib.Zip; 9 using ICSharpCode.SharpZipLib.GZip; 10

主动模式FTP与被动模式FTP该如何选择

主动模式FTP与被动模式FTP该如何选择  无论是主动模式还是被动模式,其要进行文件传输都必须依次建立两个连接,分别为命令连接与数据连结.而主动模式与被动模式的差异主要体现在数据连结通道上.为了说明两者的差异,我将主要对这个数据连结进行比较详细的说明.  当FTP客户端需要登陆到FTP服务器上的时候,服务器与客户端需要进行一系列的身份验证过程,这个过程就叫做命令连接.如在客户端向服务器发起连接请 求的时候,客户端会随即的选择某个TCP端口来跟FTP服务器的21号端口进行连接,这主要是通过TCP三

FTP服务器配置和FTP基于MariaDB实现访问控制

FTP服务器简介 FTP服务器是提供文件存储和访问服务的服务器,通过ftp(文件传输协议)实现数据传输,而且FTP是仅基于TCP的服务,不支持UDP.FTP应用是一种C/S架构的应用,客户端和服务器端都需要安装相关的软件才能实现相互之间的数据传输.常见的软件套件有FileZilla,Server-U,VsFTP,Pure-FTPd ,ProFTPD等,其中VsFTP,Pure-FTPd ,ProFTPD是单纯的服务器程序,常见客户端程序有ftp,lftp.本文实验环境中采用的服务器程序是vsft

如何架设内网ftp服务器 搭建ftp yum源

1.启动系统,我是用iso镜像挂载,挂载到/iso mkdir  /iso mount -a -t iso9660 -o loop /dev/cdrom /iso2.安装FTP服务(如果系统已带则在桌面 管理-服务器设置-服务 可以很方便的启动vsftpd服务)cd /iso/Packages rpm -ivh vsftpd* chkconfig vsftpd on service vsftpd start3.安装createrepo的软件包cd /iso/Packages rpm -ivh c

Filezilla Server支持FTP的Passive被动模式

Filezilla Server的配置: 1.Filezilla默认的模式是Port模式,不是Passive被动模式.为了解决防火墙后的客户端连接问题,最好是启用Passive模式.要启动被动模式,首先打开管理控制台,点击左起第三个图标 进入系统设置. 点击左侧"Welcom message"菜单,即FTP登录后的欢迎信息. 为了安全起见,强烈建议修改默认的欢迎信息为"Welcom to Serv-U FTP Server",这样Filezilla在欢迎消息中就会S

捷克200套UR51出货新版本FTP问题(FTP主动模式无法正常传输数据问题)

FTP alg功能 普通NAT实现了对UDP或TCP报文头中的的IP地址及端口转换功能,但对应用层数据载荷中的字段无能为力,在许多应用层协议中,比如多媒体协议(H.323.SIP等).FTP.SQLNET等,TCP/UDP载荷中带有地址或者端口信息,这些内容不能被NAT进行有效的转换,就可能导致问题.而NAT ALG(Application Level Gateway,应用层网关)技术能对多通道协议进行应用层报文信息的解析和地址转换,将载荷中需要进行地址转换的IP地址和端口或者需特殊处理的字段进

linux下FTP拒绝(ftp: connect: Connection refused,500 OOPS: cannot read config file)

使用Ubunto15.0.4环境下,使用APT命令下载的vsftpd程序 在/etc/vsftpd.conf中开启匿名用户权限(anonymous_enable=YES),重新启动vsftpd后,在用ftp localhost,竟然会报ftp: connect: Connection refused的错误,使用sudo service vsftpd start,或sudo /etc/init.d/vsftpd start,竟然启动不起来.报500 OOPS: cannot read config

4. 用Ubuntu Server架设基于独立硬盘的Windows文件共享和FTP服务器(FTP架设)

之前将storage文件夹(背后是挂载的整块新硬盘)通过samba服务与Windows共享.之所以先共享,是为了完成FTP后方便文件管理. 软件:vsftpd 目标:让用户登录FTP后可以上传文件(主要用于iPhone照片备份),不同用户备份在各自的文件夹下.Windows下打开共享的storage文件夹可以看到这些备份.高级一点的话,用户之间设置一下权限,不能相互读写(我个人不需要这个设定). 第一步,安装vsftpd sudo apt-get install vsftpd 第二步,在stor

FTP(二)ftp部署与防火墙配置

一.ftp部署 继上文对ftp原理的分析说明,接下实战部署ftp服务器. 1.环境 CentOS6.8 X64    vsftp #yum install vsftpd -y #rpm -qa |grep vsftpd#vsftpd-2.2.2-21.el6.x86_64 2.配置 cat /etc/vsftpd/vsftpd.conf|egrep -v '(^$|^#)' #修改默认连接端口为10021 listen_port=10021 #不允许匿名连接 anonymous_enable=N