Skip to content

Commit 0ab152c

Browse files
authored
bpo-43285 Make ftplib not trust the PASV response. (GH-24838)
bpo-43285: Make ftplib not trust the PASV response. The IPv4 address value returned from the server in response to the PASV command should not be trusted. This prevents a malicious FTP server from using the response to probe IPv4 address and port combinations on the client network. Instead of using the returned address, we use the IP address we're already connected to. This is the strategy other ftp clients adopted, and matches the only strategy available for the modern IPv6 EPSV command where the server response must return a port number and nothing else. For the rare user who _wants_ this ugly behavior, set a `trust_server_pasv_ipv4_address` attribute on your `ftplib.FTP` instance to True.
1 parent 93d33b4 commit 0ab152c

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

Lib/ftplib.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ class FTP:
102102
sock = None
103103
file = None
104104
welcome = None
105-
passiveserver = 1
105+
passiveserver = True
106+
# Disables https://bugs.python.org/issue43285 security if set to True.
107+
trust_server_pasv_ipv4_address = False
106108

107109
def __init__(self, host='', user='', passwd='', acct='',
108110
timeout=_GLOBAL_DEFAULT_TIMEOUT, source_address=None, *,
@@ -320,8 +322,13 @@ def makeport(self):
320322
return sock
321323

322324
def makepasv(self):
325+
"""Internal: Does the PASV or EPSV handshake -> (address, port)"""
323326
if self.af == socket.AF_INET:
324-
host, port = parse227(self.sendcmd('PASV'))
327+
untrusted_host, port = parse227(self.sendcmd('PASV'))
328+
if self.trust_server_pasv_ipv4_address:
329+
host = untrusted_host
330+
else:
331+
host = self.sock.getpeername()[0]
325332
else:
326333
host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername())
327334
return host, port

Lib/test/test_ftplib.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ def __init__(self, conn, encoding=DEFAULT_ENCODING):
103103
self.next_retr_data = RETR_DATA
104104
self.push('220 welcome')
105105
self.encoding = encoding
106+
# We use this as the string IPv4 address to direct the client
107+
# to in response to a PASV command. To test security behavior.
108+
# https://bugs.python.org/issue43285/.
109+
self.fake_pasv_server_ip = '252.253.254.255'
106110

107111
def collect_incoming_data(self, data):
108112
self.in_buffer.append(data)
@@ -143,7 +147,8 @@ def cmd_port(self, arg):
143147
def cmd_pasv(self, arg):
144148
with socket.create_server((self.socket.getsockname()[0], 0)) as sock:
145149
sock.settimeout(TIMEOUT)
146-
ip, port = sock.getsockname()[:2]
150+
port = sock.getsockname()[1]
151+
ip = self.fake_pasv_server_ip
147152
ip = ip.replace('.', ','); p1 = port / 256; p2 = port % 256
148153
self.push('227 entering passive mode (%s,%d,%d)' %(ip, p1, p2))
149154
conn, addr = sock.accept()
@@ -707,6 +712,26 @@ def test_makepasv(self):
707712
# IPv4 is in use, just make sure send_epsv has not been used
708713
self.assertEqual(self.server.handler_instance.last_received_cmd, 'pasv')
709714

715+
def test_makepasv_issue43285_security_disabled(self):
716+
"""Test the opt-in to the old vulnerable behavior."""
717+
self.client.trust_server_pasv_ipv4_address = True
718+
bad_host, port = self.client.makepasv()
719+
self.assertEqual(
720+
bad_host, self.server.handler_instance.fake_pasv_server_ip)
721+
# Opening and closing a connection keeps the dummy server happy
722+
# instead of timing out on accept.
723+
socket.create_connection((self.client.sock.getpeername()[0], port),
724+
timeout=TIMEOUT).close()
725+
726+
def test_makepasv_issue43285_security_enabled_default(self):
727+
self.assertFalse(self.client.trust_server_pasv_ipv4_address)
728+
trusted_host, port = self.client.makepasv()
729+
self.assertNotEqual(
730+
trusted_host, self.server.handler_instance.fake_pasv_server_ip)
731+
# Opening and closing a connection keeps the dummy server happy
732+
# instead of timing out on accept.
733+
socket.create_connection((trusted_host, port), timeout=TIMEOUT).close()
734+
710735
def test_with_statement(self):
711736
self.client.quit()
712737

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
:mod:`ftplib` no longer trusts the IP address value returned from the server
2+
in response to the PASV command by default. This prevents a malicious FTP
3+
server from using the response to probe IPv4 address and port combinations
4+
on the client network.
5+
6+
Code that requires the former vulnerable behavior may set a
7+
``trust_server_pasv_ipv4_address`` attribute on their
8+
:class:`ftplib.FTP` instances to ``True`` to re-enable it.

0 commit comments

Comments
 (0)