FTP 主动模式与被动模式

目录

    • 目录
    • 基础知识
    • 主动模式
      • 主动模式的问题 - 局域网和防火墙
    • 被动模式
      • 被动模式的问题
      • 案例

基础知识

  • 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 data port is not always on port 20, depending on the mode(active mode and passive mode).

主动模式

FTP 主动模式与被动模式_第1张图片
1. Server 监听 21 端口,Client 使用随机端口 N(1026) 主动连接 Server 21 端口,并将端口 N+1(1027) 发送给 Server, 告诉 Server Client 监听 N+1 端口,用作数据传输。
2. Server 给 Client 回 ACK 包。
3. Server 使用端口 20 主动连接 Client 的 N+1 端口。
4. Client 给 Server 回 ACK 包。

主动模式的问题 - 局域网和防火墙

  1. 一般客户端处于局域网中,开启监听的数据传输用的地址是内网地址,外网 FTP server 连接该地址;
  2. 即使客户端直接使用外网 IP, 防火墙的配置也是个问题,因为客户端开启的数据传输监听端口是随机的,防火墙只能按 IP 配置,如果 FTP server 是一个接入了多个 IP 的域名,那按 IP 配置也将行不通。(Nowadays, it is typical that the client is behind a firewall (e.g. built-in Windows firewall) or NAT router (e.g. ADSL modem), unable to accept incoming TCP connections. FTP Connection Modes (Active vs. Passive))

被动模式

FTP 主动模式与被动模式_第2张图片
1. Server 监听 21 端口,Client 使用随机端口 N(1026) 主动连接 Server 21 端口,并告诉 Server 使用 PASV 模式。
2. Server 告诉 Client 使用随机端口 2024 作为数据传输端口。
3. Client 使用 N+1(1027) 端口连接 Server 2024 端口。
4. Server 给 Client ACK 包。

被动模式的问题

  1. 被动模式会使 Server 负载增大;
  2. 同主动模式一样,被动模式依然会有局域网的问题,如果 server 处于 NAT 网络中,其监听的地址实际上为内网地址,数据包出去时再转换成外网地址,但是被动模式开启的数据传输监听地址是通过 FTP 应用层消息传递的,NAT 无法转换应用层消息中的地址。不过这个问题比较容易解决,目前有两种解决方法:1. 一般的 FTP server 支持将被动模式开启的数据传输监听地址配置成自定义地址,即在给 client 发送数据传输监听地址时用配置的地址替换 server 监听的 IP, 例如 vsftpd 的 pasv_address 配置项及 Pure-FTPd 的 ForcePassiveIP 配置项;2. 在开启的应用层网关(ALG)的网络环境中,ALG 会识别 FTP 消息,将 FTP 消息中的内网地址替换成外网地址。

案例

$ ftp -d 210.140.218.13
Connected to 210.140.218.13 (210.140.218.13).
220 (vsFTPd 2.2.2)
Name (210.140.218.13:xxx): xxx
---> USER xxx
331 Please specify the password.
Password:
---> PASS XXXX
230 Login successful.
---> SYST
215 UNIX Type: L8
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
ftp: setsockopt (ignored): Permission denied
---> PASV
227 Entering Passive Mode (210,140,218,13,199,150).
---> LIST
150 Here comes the directory listing.
drwxr-xr-x    4 502      502          4096 Mar 21 12:20 0
drwxr-xr-x    3 502      502          4096 Mar 22 06:42 test
226 Directory send OK.
ftp> cd test
---> CWD test
250 Directory successfully changed.
ftp> ls
ftp: setsockopt (ignored): Permission denied
---> PASV
227 Entering Passive Mode (210,140,218,13,41,209).
---> LIST
150 Here comes the directory listing.
-rw-r--r--    1 502      502      36598395 Mar 22 11:17 file
226 Directory send OK.
ftp> put file2
local: file2 remote: file2
---> TYPE I
200 Switching to Binary mode.
ftp: setsockopt (ignored): Permission denied
---> PASV
227 Entering Passive Mode (210,140,218,13,25,42).
---> STOR file2
150 Ok to send data.
226 Transfer complete.
36598395 bytes sent in 177 secs (207.02 Kbytes/sec)
ftp> quit
---> QUIT
421 Timeout.

问题: 这个案例使用被动模式,并且服务端只对外开放了 21 和 20 端口,但是这里的数据传输端口是 (25*256+42)=6442, 这个端口是如何接受客户端来的连接的呢?初步分析是对方网络网关支持 ALG, 能动态开放 FTP 随机端口。关于 ALG 防火墙动态开放端口的说明可以看这里:

allowing client applications to use dynamic ephemeral TCP/ UDP ports to communicate with the known ports used by the server applications, even though a firewall configuration may allow only a limited number of known ports. In the absence of an ALG, either the ports would get blocked or the network administrator would need to explicitly open up a large number of ports in the firewall — rendering the network vulnerable to attacks on those ports.

Application-level gateway

参考
Active FTP vs. Passive FTP, a Definitive Explanation
Active v.s. Passive FTP Simplified - Understanding FTP Ports
FTP的主动模式和被动模式

你可能感兴趣的:(FTP)