FTP两种工作模式:主动模式(Active FTP)和被动模式(Passive FTP)
http://jiahej.iteye.com/blog/179295
在主動模式下,FTP客戶端隨機開啟一個大于1024的端口N向服務器的21號端口發起連接,然后開放N+1號端口進行監聽,并向服務器發出PORT N+1命令。服務器接收到命令后,會用其本地的FTP數據端口(通常是20)來連接客戶端指定的端口N+1,進行數據傳輸。
??? 在被動模式下,FTP庫戶端隨機開啟一個大于1024的端口N向服務器的21號端口發起連接,同時會開啟N+1號端口。然后向服務器發送PASV命令,通知服務器自己處于被動模式。服務器收到命令后,會開放一個大于1024的端口P進行監聽,然后用PORT P命令通知客戶端,自己的數據端口是P。客戶端收到命令后,會通過N+1號端口連接服務器的端口P,然后在兩個端口之間進行數據傳輸。
??? 總的來說,主動模式的FTP是指服務器主動連接客戶端的數據端口,被動模式的FTP是指服務器被動地等待客戶端連接自己的數據端口。
??? 被動模式的FTP通常用在處于防火墻之后的FTP客戶訪問外界FTp服務器的情況,因為在這種情況下,防火墻通常配置為不允許外界訪問防火墻之后主機,而只允許由防火墻之后的主機發起的連接請求通過。因此,在這種情況下不能使用主動模式的FTP傳輸,而被動模式的FTP可以良好的工作。
=========================================
FTP是僅基于TCP的服務,不支持UDP。與眾不同的是FTP使用2個端口,一個數據端口和一個命令端口(也可叫做控制端口)。通常來說這兩個端口是21(命令端口)和20(數據端口)。但FTP 工作方式的不同,數據端口并不總是20。這就是主動與被動FTP的最大不同之處。
(一)主動FTP
主動方式的FTP是這樣的:客戶端從一個任意的非特權端口N(N>1024)連接到FTP服務器的命令端口,也就是21端口。然后客戶端開始監聽端口N+1,并發送FTP命令“port N+1”到FTP服務器。接著服務器會從它自己的數據端口(20)連接到客戶端指定的數據端口(N+1)。
針對FTP服務器前面的防火墻來說,必須允許以下通訊才能支持主動方式FTP:
1. 任何大于1024的端口到FTP服務器的21端口。(客戶端初始化的連接)
2. FTP服務器的21端口到大于1024的端口。 (服務器響應客戶端的控制端口)
3. FTP服務器的20端口到大于1024的端口。(服務器端初始化數據連接到客戶端的數據端口)
4. 大于1024端口到FTP服務器的20端口(客戶端發送ACK響應到服務器的數據端口)
(二)被動FTP
為了解決服務器發起到客戶的連接的問題,人們開發了一種不同的FTP連接方式。這就是所謂的被動方式,或者叫做PASV,當客戶端通知服務器它處于被動模式時才啟用。
在被動方式FTP中,命令連接和數據連接都由客戶端發起,這樣就可以解決從服務器到客戶端的數據端口的入方向連接被防火墻過濾掉的問題。
當開啟一個 FTP連接時,客戶端打開兩個任意的非特權本地端口(N > 1024和N+1)。第一個端口連接服務器的21端口,但與主動方式的FTP不同,客戶端不會提交PORT命令并允許服務器來回連它的數據端口,而是提交 PASV命令。這樣做的結果是服務器會開啟一個任意的非特權端口(P > 1024),并發送PORT P命令給客戶端。然后客戶端發起從本地端口N+1到服務器的端口P的連接用來傳送數據。
對于服務器端的防火墻來說,必須允許下面的通訊才能支持被動方式的FTP:
1. 從任何大于1024的端口到服務器的21端口 (客戶端初始化的連接)
2. 服務器的21端口到任何大于1024的端口 (服務器響應到客戶端的控制端口的連接)
3. 從任何大于1024端口到服務器的大于1024端口 (客戶端初始化數據連接到服務器指定的任意端口)
4. 服務器的大于1024端口到遠程的大于1024的端口(服務器發送ACK響應和數據到客戶端的數據端口)
以上關于主動和被動FTP的解釋,可以簡單概括為以下兩點:
1、主動FTP:
命令連接:客戶端 >1024端口 -> 服務器 21端口
數據連接:客戶端 >1024端口 1024端口 -> 服務器 21端口
數據連接:客戶端 >1024端口 -> 服務器 >1024端口
(三) 主動與被動FTP優缺點:
主動FTP對FTP服務器的管理有利,但對客戶端的管理不利。因為FTP服務器企圖與客戶端的高位隨機端口建立連接,而這個端口很有可能被客戶端的防火墻阻塞掉。被動FTP對FTP客戶端的管理有利,但對服務器端的管理不利。因為客戶端要與服務器端建立兩個連接,其中一個連到一個高位隨機端口,而這個端口很有可能被服務器端的防火墻阻塞掉。
?
============
http://slacksite.com/other/ftp.html
Contents:
- Introduction
- The Basics
- Active FTP
- Active FTP Example
- Passive FTP
- Passive FTP Example
- Other Notes
- Summary
- References
- Appendix 1: Configuration of Common FTP Servers
- Appendix 2: Firewall Configuration Guide
?
Introduction
One of the most commonly seen questions when dealing with firewalls and other Internet connectivity issues is the difference between active and passive FTP and how best to support either or both of them. Hopefully the following text will help to clear up some of the confusion 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 to FTP. FTP is an unusual service in that it utilizes two ports, a 'data' port and a 'command' port (also known as the control port). Traditionally these are port 21 for the command port and port 20 for the data port. The confusion begins however, when we find that depending on the mode, the data port is not always on port 20.
?
Active FTP
In active mode FTP the client connects from a random unprivileged port (N > 1023) to the FTP server's command port, port 21. Then, the client starts listening to port N+1 and sends the FTP command PORT N+1 to the FTP server. The server will then connect back to the client's specified data port from its local data port, which is port 20.
From the server-side firewall's standpoint, to support active mode FTP the 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's control port)
- FTP server's port 20 to ports > 1023 (Server initiates data connection to client's data port)
- FTP server's port 20 from ports > 1023 (Client sends ACKs to server's data port)
When drawn out, the connection appears as follows:
In step 1, the client's command port contacts the server's command port and sends the command PORT 1027. The server then sends an ACK back to the client's command port in step 2. In step 3 the server initiates a connection on its local data port to the data port the client specified earlier. Finally, the client sends an ACK back as shown in step 4.
The main problem with active mode FTP actually falls on the client side. The FTP client doesn't make the actual connection to the data port of the server--it simply tells the server what port it is listening on and the server connects back to the specified port on the client. From the client side firewall this appears to be an outside system initiating a connection 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 testbox2 Connected to testbox2.slacksite.com. 220 testbox2.slacksite.com FTP server ready. Name (testbox2:slacker): slacker ---> USER slacker 331 Password required for slacker. Password: TmpPass ---> PASS XXXX 230 User slacker logged in. ---> SYST 215 UNIX Type: L8 Remote system type is UNIX. Using binary mode to transfer files. ftp> ls ftp: setsockopt (ignored): Permission denied ---> PORT 192,168,150,80,14,178 200 PORT command successful. ---> LIST 150 Opening ASCII mode data connection for file list. drwx------ 3 slacker users 104 Jul 27 01:45 public_html 226 Transfer complete. ftp> quit ---> QUIT 221 Goodbye.
?
Passive FTP
In order to resolve the issue of the server initiating the connection to the client a different method for FTP connections was developed. This was known as passive mode, or PASV, after the command used by the client 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 port connection to the client from the server. When opening an FTP connection, the client opens two random unprivileged ports locally (N > 1023 and N+1). The first port contacts the server on port 21, but instead of then issuing a PORT command and allowing the server to connect back to its data port, the client will issue the PASV command. The result of this is that the server then opens a random unprivileged port (P > 1023) and sends P back to the client in response to the PASV command. The client then initiates the connection from port N+1 to port P on the server to transfer data.
From the server-side firewall's standpoint, to support passive mode FTP the 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's control port)
- FTP server's ports > 1023 from anywhere (Client initiates data connection to random port specified by server)
- FTP server's ports > 1023 to remote ports > 1023 (Server sends ACKs (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 issues the PASV command. The server then replies in step 2 with PORT 2024, telling the client which port it is listening to for the data connection. In step 3 the client then initiates the data connection 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 biggest issue is the need to allow any remote connection to high numbered ports on the server. Fortunately, many FTP daemons, including the popular WU-FTPD allow the administrator to specify a range of ports which the FTP server will 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, the command line FTP utility provided with Solaris does not support passive mode, 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 to enable passive mode!
With the massive popularity of the World Wide Web, many people prefer to use their web browser as an FTP client. Most browsers only support passive mode when accessing ftp:// URLs. This can either be good or bad depending 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 testbox2 Connected to testbox2.slacksite.com. 220 testbox2.slacksite.com FTP server ready. Name (testbox2:slacker): slacker ---> USER slacker 331 Password required for slacker. Password: TmpPass ---> PASS XXXX 230 User slacker logged in. ---> SYST 215 UNIX Type: L8 Remote system type is UNIX. Using binary mode to transfer files. ftp> passive Passive mode on. ftp> ls ftp: setsockopt (ignored): Permission denied ---> PASV 227 Entering Passive Mode (192,168,150,90,195,149). ---> LIST 150 Opening ASCII mode data connection for file list drwx------ 3 slacker users 104 Jul 27 01:45 public_html 226 Transfer complete. ftp> quit ---> QUIT 221 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 21data : client >1023 <- server 20Passive FTP :command : client >1023 -> server 21data : client >1024 -> server >1023A 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 the issues involved in firewalling them can be found in the O'Reilly and Associates book, Building Internet Firewalls, 2nd Ed, by Brent Chapman 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.
?
總結
以上是生活随笔為你收集整理的FTP两种工作模式:主动模式(Active FTP)和被动模式(Passive FTP)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: log4j xml配置详解
- 下一篇: Ant build.xml中的各种变量