TExcelTAD

FTP, FTPS and SFTP

File transfer is where cleartext protocols survive longest: a scanner that uploads scans to an FTP share, a partner integration "that has always worked", a backup job from 2009. Three names cause confusion. FTP (RFC 959) sends commands, passwords and files in cleartext over two TCP connections. FTPS is FTP wrapped in TLS. SFTP has nothing to do with FTP: it is a file-transfer protocol that runs inside SSH. This lesson shows an FTP session in a synthetic capture, decodes the data-channel announcement that leaks internal addresses and complicates firewalls, and compares the secure alternatives.

An FTP session in a capture

FTP uses a control connection to port 21 for text commands and replies, and a separate data connection for each file or listing. The session builds a control connection of a lab client logging in and fetching a file in passive mode, then lists commands and replies.

bash · try it yourself
python3 - <<'PY'
from scapy.all import Ether, IP, TCP, Raw, wrpcap
t0, pkts = 1768471200.0, []
C, S = ("10.10.0.5", 50600, "02:00:00:0a:0a:05"), ("10.10.0.21", 21, "02:00:00:0a:0a:15")
seq = {C: 1000, S: 8000}
def send(a, b, data, dt):
    p = Ether(src=a[2], dst=b[2])/IP(src=a[0], dst=b[0])/TCP(sport=a[1], dport=b[1], flags="PA", seq=seq[a], ack=seq[b])/Raw(data)
    p.time = t0 + dt; pkts.append(p); seq[a] += len(data)
dialog = [(S, b"220 lab-ftp ready\r\n"), (C, b"USER lab-user\r\n"), (S, b"331 Password required\r\n"),
          (C, b"PASS lab-only-pw\r\n"), (S, b"230 Logged in\r\n"), (C, b"PASV\r\n"),
          (S, b"227 Entering Passive Mode (10,10,0,21,195,80)\r\n"), (C, b"RETR report.csv\r\n"),
          (S, b"150 Opening data connection\r\n"), (S, b"226 Transfer complete\r\n"), (C, b"QUIT\r\n")]
for i, (who, data) in enumerate(dialog):
    send(who, C if who is S else S, data, i * 0.2)
wrpcap("ftp.pcap", pkts)
PY
tshark -r ftp.pcap -Y ftp -T fields -e ip.src -e ftp.request.command -e ftp.request.arg -e ftp.response.code -e ftp.response.arg
echo "--- passwords sent in clear (count):"
tshark -r ftp.pcap -Y 'ftp.request.command == "PASS"' | wc -l
10.10.0.21			220	lab-ftp ready
10.10.0.5	USER	lab-user
10.10.0.21			331	Password required
10.10.0.5	PASS	lab-only-pw
10.10.0.21			230	Logged in
10.10.0.5	PASV
10.10.0.21			227	Entering Passive Mode (10,10,0,21,195,80)
10.10.0.5	RETR	report.csv
10.10.0.21			150	Opening data connection
10.10.0.21			226	Transfer complete
10.10.0.5	QUIT
--- passwords sent in clear (count):
1

The username, the password, the file name and (in the data connection, not built here) the file content are all readable. The 227 reply tells the client where to open the data connection: an address and a port encoded as six numbers.

Decoding the data-channel announcement

python · try it yourself
import re

def pasv(reply):
    h1, h2, h3, h4, p1, p2 = map(int, re.search(r"\((\d+),(\d+),(\d+),(\d+),(\d+),(\d+)\)", reply).groups())
    return f"{h1}.{h2}.{h3}.{h4}", p1 * 256 + p2

ip, port = pasv("227 Entering Passive Mode (10,10,0,21,195,80)")
print(f"data connection to {ip}:{port}")

# The same server behind NAT, misconfigured: it announces its private address to an Internet client
ip, port = pasv("227 Entering Passive Mode (10,10,0,21,195,81)")
print(f"announced to an outside client: {ip}:{port}  -> leaks the internal address; client can't connect")

# Active mode: the client tells the server where to connect back (PORT h1,h2,h3,h4,p1,p2)
def port_cmd(cmd):
    return pasv("(" + cmd.split()[1] + ")")
ip, port = port_cmd("PORT 192,0,2,77,0,25")
print(f"PORT asks the server to connect to {ip}:{port}: a third host's SMTP port (FTP bounce)")
data connection to 10.10.0.21:50000
announced to an outside client: 10.10.0.21:50001  -> leaks the internal address; client can't connect
PORT asks the server to connect to 192.0.2.77:25: a third host's SMTP port (FTP bounce)

Two security problems come straight from this design:

  • Firewalls and NAT must read the control channel to know which data port to allow. Stateful firewalls have FTP helpers that parse 227/PORT and open "expected" connections; those helpers have had their own vulnerabilities, and encrypted control channels (FTPS) blind them.
  • The FTP bounce attack (concept): with active mode, a client can ask the server to connect to any address and port, making the server scan or send data to third parties on the attacker's behalf. RFC 2577 suggests that servers never open data connections to ports below 1024 (like the SMTP port above) and notes that PORT can be disabled entirely; modern servers also refuse PORT addresses other than the client's own.

The secure alternatives

FTP FTPS SFTP
transport TCP 21 + data ports TLS: explicit (AUTH TLS on 21) or implicit (990) inside SSH, TCP 22
credentials cleartext encrypted encrypted
server authentication none X.509 certificate SSH host key
firewall friendliness poor (second connection) worse (helpers can't read encrypted control) one connection
typical use legacy only legacy integrations that require FTP semantics administrators, automated transfers

Other options are often better still: HTTPS uploads with authentication, object storage with signed URLs, or managed file-transfer systems that log every transfer.

How FTP use is detected

  • Flows to TCP 21 (and data connections to high ports from the same pair) in NetFlow; IDS rules for USER/PASS commands; Zeek's ftp.log, which records commands, users and file names.
  • PASS commands in captures, as above, and anonymous FTP (USER anonymous) servers on your own scan results.
  • 227 replies with private addresses crossing the perimeter (misconfigured NAT, information leak).
  • PORT commands naming third-party addresses (bounce attempts).

How it is fixed

  1. Replace FTP with SFTP (or HTTPS-based transfer); for partners who insist on FTP, offer FTPS with TLS 1.2+ and certificate validation.
  2. Where FTP must stay temporarily: separate host, no anonymous access, chroot users, restrict source addresses, monitor, and plan the removal.
  3. Treat every password ever used over FTP as exposed: rotate it when migrating.
  4. For SFTP: key-based authentication, a chrooted, SFTP-only account (ForceCommand internal-sftp), and the SSH practices from SSH versus Telnet.

Pitfalls

  • Saying "SFTP" when the device actually does FTPS, or the reverse, in a contract or firewall request. They use different ports and different trust models.
  • Explicit FTPS where the client doesn't insist on AUTH TLS: an attacker can strip the offer and the session falls back to cleartext. Require TLS on the server.
  • Leaving the firewall's FTP helper enabled globally, which lets crafted control traffic open pinholes.

Practice

1. Decode 227 Entering Passive Mode (192,0,2,30,39,16).

Address 192.0.2.30, port 39 × 256 + 16 = 10000.

2. Why does FTPS break the firewall's FTP helper, and how do FTPS deployments cope?

The helper must read 227/PORT replies to open data ports, and in FTPS those replies are encrypted. Servers are configured with a fixed passive port range that the firewall opens statically, plus the correct public address for NAT.

3. Change the capture so the client sends USER anonymous. What would you report?

An anonymous FTP server: anyone can log in, usually with an email address as "password". Report what it exposes (list the readable and writable directories), because writable anonymous FTP is used to host malware and illegal content.

Test yourself

What is SFTP?

  1. FTP over TLS
  2. A file-transfer protocol running inside SSH
  3. FTP on port 990
  4. Secure FTP with a second data channel

Which FTP reply tells the client where to open the passive data connection?

  1. 220
  2. 230
  3. 227
  4. 150

What is the FTP bounce attack?

  1. Guessing FTP passwords
  2. Using PORT to make the server connect to a third party's address and port
  3. Uploading large files
  4. Downgrading FTPS

In the capture, how many cleartext passwords were sent?

  1. 0
  2. 1
  3. 2
  4. 11