2010年2月4日 星期四

Active FTP 及 Passive FTP

寫的很清楚非常值得參考
原文連結


Active FTP 及 Passive FTP

在大部份的防火牆設定中,使用 FTP 傳輸檔案時經常會遇到需要設定使用 passive mode 的 FTP.但大部份的 FTP client 軟體,default 大都是 active mode FTP,這二種 FTP 模式在傳輸上有何不同?先了解 FTP 的工作方式,有助於了解 active FTP 及 passive FTP 的不同.

一般的 FTP 使用 TCP port 20 及 21 二個 port. 正常來說, port 20 是傳輸資料用,而 port 21 傳輸命令.但事實上並不是一直使用 port 20 來傳輸資料.

Active mode FTP (主動模式 FTP)
在 Active 模式的 FTP client 一開始時會先使用一個大於 1024(N) 的 TCP port 連結至 FTP server 的port 21. 然後 FTP client 會打開該 port 的下一個數字的 port (N+1) 並送至 FTPserver 說期望下次的溝通可以經由新的 port (N+1),這樣比較不會占用到 FTP server 的 port 21 而影響別的 FTP sessions.同時 FTP server 會使用 port 20 連結至 FTP client 新送過來的 port 號來傳資料.
所以在防火牆上需要打開:

FTP server port 21 from anywhere # 給 FTP client 開始連接
FTP server port 21 to port > 1024 # 給 FTP server 回應 client 命令
FTP server port 20 to port > 1024 # 給 FTP server 開始資料傳輸
FTP server port 20 from port > 1024 # 給 FTP cleint 傳送 data ACK 給 server

所以連接順序為:
1. FTP client use TCP port 1026 for command to FTP server command port 21
2. FTP server use TCP port 21 responed to FTP client command port 1026
3. FTP server use TCP port 20 for sending data to FTP client data port 1027 (1026 + 1)
4. FTP client use TCP port 1027 ( 1026 + 1) for data ACK to FTP server port 20

問題在於使用 active mode FTP 時主控權在 FTP client, FTP client 並不是真正的連至 FTP server 的data port (TCP port 20),它只是告訴 FTP server 它準備在那個 port 接收資料及port+1 傳送 ACK.並期望 FTP server 可以主動連接至 FTP client 所期望的 port.這在使用防火牆的環境中就會造成問題.


Passive mode FTP (被動模式 FTP)
解決主動模式在防火牆外面時所發生無法連接的問題,可以用被動模式 FTP. 在Passive mode FTP 下,FTP client一開始和 active mode FTP 一樣,打開二個 port (N > 1024 及 N+1), 使用第一個port 連接至 FTP server的 port 21,並送出命令 PORT 讓 server 連至 N port 再送出 PASV 命令給 server,這樣會造成 server使用 一個大於 1024 的 port 連接至 client 並送出命令 PORT N ( N >1024 ) 給client. 樣 client 就會使用 N + 1 並開始傳輸資料.

這樣子在防火牆上需要設定

FTP server TCP port 21 from anywhere # FTP client 開始連接至 FTP server
FTP server TCP port 21 to port > 1024 # FTP server 回應 client 控制
FTP server TCP port > 1024 from anywhere # FTP client 資料傳輸
FTP server TCP port > 1024 to client port > 1024 # FTP server 回應資料 ACK至 client data port

連接順序為:
1. FTP client use TCP port 1026 to FTP server command port 21 via command PASV
2. FTP server use TCP port 21 to FTP client 1026 command port for reply data port 2024
3. FTP client use TCP port 1027 (N+1) to FTP server 2024 for data transmit and ACK
4. FTP server use TCP port 2024 to FTP client for data transmit and ACK


因為 passive mode FTP 是由 FTP server 發起一個大於 1024 的 port 至 client,在防火牆上的設定就相對簡單,而可以傳輸資料而不會被防火牆 block.


參考資料 RFC595

註:在 Windows 下原來的 ftp client 並沒有 passive 這個指令,但可以利用 literal 指今來下給 FTP server FTP session 為 passive mode

方法如下:

C:\>ftp 10.10.100.121
Connected to 10.10.100.121.
220 (vsFTPd 2.0.1)
User (10.10.100.121:(none)): ftp
331 Please specify the password.
Password:
230 Login successful.
ftp> literal pasv
227 Entering Passive Mode (10,10,100,121,242,172)
ftp>

可以看到進入 passive mode 了.使用的 port 號碼是 (242*256)+172= 62124

若是 UNIX 但 FTP client 不支援 passive mode 指令可以用 quote pasv

沒有留言: