Skip to content
SSRF Disclosed report Intermediate CVE-2021-31810CVE-2020-8284CVE-2007-1562

lib/net/ftp.rb: trusting PASV responses allow client abuse

The Ruby net/ftp library trusts the IP address and port in a PASV response, so a malicious FTP server can make the client connect to an arbitrary internal address and port. This allows port scanning and service banner extraction, especially when net/ftp processes a user-supplied URL.

sighook Follow

Originally disclosed on HackerOne

  • Triaged in 3 days
  • Fixed in 3 months
  • Paid in 3 months
  • Disclosed after 3 months

When net/ftp performs a passive FTP transfer, it tries to using PASV. Passive mode is what net/ftp uses by default. A server response to a PASV command includes the (IPv4) address and port number for the client to connect back to in order to perform the actual data transfer.

This is how the FTP protocol is designed to work.[^1]

A malicious server can use the PASV response to trick net/ftp into connecting back to a given IP address and port, and this way potentially make it extract information about services that are otherwise private and not disclosed, for example doing port scanning and service banner extractions. If net/ftp operates on a URL provided by a user (with by all means is an unwise setup), a user can exploit that and pass in a URL to a malicious FTP server instance without needing any server breach to perform the attack.

Other FTP clients have in the past also had this flaw and have fixed it at different points in time:

[^1]: With one exception: EPSV. The correct behaviour is first try the EPSV command and if that is not supported, fall back to using PASV.

Impact

This behavior is by design (unless EPSV ALL is sent) but it could still lead to security issues depending on the context. I encountered this issue within a web application with a server-side request forgery (SSRF) issue (but this issue applies to any form of SSRF with net/ftp as the request processor). In that context, one can get the following additional capabilities:

  • Reliable tcp port scanning (this is not normally possible by just providing a random ip:port to net/ftp)
  • Network service banner extraction (we setup the data channel on the target ip:port and extract for example an ssh banner: SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.8 without any errors)
  • Potential bypass of ip/port restrictions, e.g. the server might be filtering internal IPs or allowing only specific ports (but still allowing FTP)

PoC

I used the following simple code:

Ruby
require 'net/ftp'
ftp = Net::FTP.new
ftp.connect(ARGV[0], ARGV[1])
ftp.login
#ftp.passive = true # by default
ftp.getbinaryfile('/whatever', 'whatever')
ftp.close

And the custom ftp-server:

[Parent] Got connection from 192.168.100.2:43520... Spawned process 31749 to handle connection
[PID 31749] SEND: 220 FTP PASV Demo Server v1.0
[PID 31749] RECV: USER anonymous
[PID 31749] SEND: 331 Please specify the password.
[PID 31749] RECV: PASS anonymous@
[PID 31749] SEND: 230 Login successful.
[PID 31749] RECV: TYPE I
[PID 31749] SEND: 200 Switching to Binary mode.
[PID 31749] RECV: PASV
[PID 31750] Handling incoming request to PASV port
>>> Sending 127.0.0.1:8123
[PID 31750] SEND: 227 Entering Passive Mode (127,0,0,1,31,187)
[PID 31750] Exiting
-------------------------------- The Port is Open ---------------------------------
[PID 31749] RECV: RETR /whatever
[PID 31749] SEND: 150 Opening BINARY mode data connection for /whatever (0 bytes).
[PID 31749] SEND: 226 File send OK.
[PID 31749] Exiting
----------------------------------------------------------------------------------
[Parent] Got connection from 192.168.100.2:43524... Spawned process 31787 to handle connection
[PID 31787] SEND: 220 FTP PASV Demo Server v1.0
[PID 31787] RECV: USER anonymous
[PID 31787] SEND: 331 Please specify the password.
[PID 31787] RECV: PASS anonymous@
[PID 31787] SEND: 230 Login successful.
[PID 31787] RECV: TYPE I
[PID 31787] SEND: 200 Switching to Binary mode.
[PID 31787] RECV: PASV
[PID 31788] Handling incoming request to PASV port
>>> 127.0.0.1:8080
[PID 31788] SEND: 227 Entering Passive Mode (127,0,0,1,31,144)
[PID 31788] Exiting
------------------------------- The Port is Closed --------------------------------
[PID 31787] RECV: ERROR: unmatched reply
[PID 31787] Exiting
----------------------------------------------------------------------------------

Mitigation

Currently, net/ftp can mitigate this flaw by disabling passive mode, which is enabled by default. But this is not the best solution to this problem, perhaps, as well as disabling passive mode by default.

For example, firefox just ignores the ip address that is sent from the server. But Curl provides the option which tell to not use the IP address the server suggests in its response to curl's PASV command when curl connects the data connection. Instead curl will re-use the same IP address it already uses for the control connection. The second seems more reasonable.

The original report as published on HackerOne, by sighook. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the client does not validate that the PASV response address matches the control connection address, so it connects to whatever the server specifies.

Impact

An attacker could perform reliable TCP port scanning and extract service banners, and potentially bypass IP or port restrictions, when net/ftp is used in an SSRF context.

What the attacker needed

Net/ftp used in SSRF context; attacker controls FTP server

The fix

The client could ignore the server-provided IP and reuse the control connection's IP, as curl does.

How it was found

  1. Connect to a malicious FTP server and log in.
  2. The server responds to PASV with an arbitrary IP and port.
  3. The client connects to that address for the data transfer.
  4. The server observes whether the connection succeeds, revealing port state.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

FTP uses a separate connection for data. In passive mode, the server tells the client where to connect. If the client blindly follows that address, a bad server can point it at internal services, letting an attacker probe the network.

What is SSRF?

The server can be made to fetch an address the attacker chooses, reaching internal services, cloud metadata or other customers' data.

More by sighook

View all

10 more disclosed reports, best first.

More from Ruby

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Server-side request forgery (SSRF)

The lesson behind this report.

The server can be made to fetch an address the attacker chooses, reaching internal services, cloud metadata or other customers' data.