Skip to content

Commit fe31479

Browse files
miss-islingtonmgorny
authored andcommitted
[3.6] bpo-43285 Make ftplib not trust the PASV response. (pythonGH-24838) (pythonGH-24881) (pythonGH-24882)
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.. (cherry picked from commit 0ab152c) Co-authored-by: Gregory P. Smith <greg@krypto.org> (cherry picked from commit 664d1d1) Rebased for Python 2.7 by Michał Górny <mgorny@gentoo.org>
1 parent ed1aa2f commit fe31479

3 files changed

Lines changed: 43 additions & 3 deletions

File tree

‎Lib/ftplib.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ class FTP:
108108
file = None
109109
welcome = None
110110
passiveserver = 1
111+
# Disables https://bugs.python.org/issue43285 security if set to True.
112+
trust_server_pasv_ipv4_address = False
111113

112114
# Initialization method (called by class instantiation).
113115
# Initialize host to localhost, port to standard ftp port
@@ -310,8 +312,13 @@ def makeport(self):
310312
return sock
311313

312314
def makepasv(self):
315+
"""Internal: Does the PASV or EPSV handshake -> (address, port)"""
313316
if self.af == socket.AF_INET:
314-
host, port = parse227(self.sendcmd('PASV'))
317+
untrusted_host, port = parse227(self.sendcmd('PASV'))
318+
if self.trust_server_pasv_ipv4_address:
319+
host = untrusted_host
320+
else:
321+
host = self.sock.getpeername()[0]
315322
else:
316323
host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername())
317324
return host, port

‎Lib/test/test_ftplib.py‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ def __init__(self, conn):
6767
self.rest = None
6868
self.next_retr_data = RETR_DATA
6969
self.push('220 welcome')
70+
# We use this as the string IPv4 address to direct the client
71+
# to in response to a PASV command. To test security behavior.
72+
# https://bugs.python.org/issue43285/.
73+
self.fake_pasv_server_ip = '252.253.254.255'
7074

7175
def collect_incoming_data(self, data):
7276
self.in_buffer.append(data)
@@ -109,8 +113,9 @@ def cmd_pasv(self, arg):
109113
sock.bind((self.socket.getsockname()[0], 0))
110114
sock.listen(5)
111115
sock.settimeout(10)
112-
ip, port = sock.getsockname()[:2]
113-
ip = ip.replace('.', ',')
116+
port = sock.getsockname()[1]
117+
ip = self.fake_pasv_server_ip
118+
ip = ip.replace('.', ','); p1 = port // 256; p2 = port % 256
114119
p1, p2 = divmod(port, 256)
115120
self.push('227 entering passive mode (%s,%d,%d)' %(ip, p1, p2))
116121
conn, addr = sock.accept()
@@ -577,6 +582,26 @@ def test_makepasv(self):
577582
# IPv4 is in use, just make sure send_epsv has not been used
578583
self.assertEqual(self.server.handler_instance.last_received_cmd, 'pasv')
579584

585+
def test_makepasv_issue43285_security_disabled(self):
586+
"""Test the opt-in to the old vulnerable behavior."""
587+
self.client.trust_server_pasv_ipv4_address = True
588+
bad_host, port = self.client.makepasv()
589+
self.assertEqual(
590+
bad_host, self.server.handler_instance.fake_pasv_server_ip)
591+
# Opening and closing a connection keeps the dummy server happy
592+
# instead of timing out on accept.
593+
socket.create_connection((self.client.sock.getpeername()[0], port),
594+
timeout=TIMEOUT).close()
595+
596+
def test_makepasv_issue43285_security_enabled_default(self):
597+
self.assertFalse(self.client.trust_server_pasv_ipv4_address)
598+
trusted_host, port = self.client.makepasv()
599+
self.assertNotEqual(
600+
trusted_host, self.server.handler_instance.fake_pasv_server_ip)
601+
# Opening and closing a connection keeps the dummy server happy
602+
# instead of timing out on accept.
603+
socket.create_connection((trusted_host, port), timeout=TIMEOUT).close()
604+
580605
def test_line_too_long(self):
581606
self.assertRaises(ftplib.Error, self.client.sendcmd,
582607
'x' * self.client.maxline * 2)
Lines changed: 8 additions & 0 deletions
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)