Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit664d1d1

Browse files
authored
[3.8]bpo-43285 Make ftplib not trust the PASV response. (GH-24838) (GH-24881)
bpo-43285: Make ftplib not trust the PASV response.The IPv4 address value returned from the server in response to the PASV commandshould not be trusted. This prevents a malicious FTP server from using theresponse to probe IPv4 address and port combinations on the client network.Instead of using the returned address, we use the IP address we'realready connected to. This is the strategy other ftp clients adopted,and matches the only strategy available for the modern IPv6 EPSV commandwhere 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 commit0ab152c)Co-authored-by: Gregory P. Smith <greg@krypto.org>
1 parent233f58f commit664d1d1

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

‎Lib/ftplib.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ class FTP:
104104
welcome=None
105105
passiveserver=1
106106
encoding="latin-1"
107+
# Disables https://bugs.python.org/issue43285 security if set to True.
108+
trust_server_pasv_ipv4_address=False
107109

108110
# Initialization method (called by class instantiation).
109111
# Initialize host to localhost, port to standard ftp port
@@ -316,8 +318,13 @@ def makeport(self):
316318
returnsock
317319

318320
defmakepasv(self):
321+
"""Internal: Does the PASV or EPSV handshake -> (address, port)"""
319322
ifself.af==socket.AF_INET:
320-
host,port=parse227(self.sendcmd('PASV'))
323+
untrusted_host,port=parse227(self.sendcmd('PASV'))
324+
ifself.trust_server_pasv_ipv4_address:
325+
host=untrusted_host
326+
else:
327+
host=self.sock.getpeername()[0]
321328
else:
322329
host,port=parse229(self.sendcmd('EPSV'),self.sock.getpeername())
323330
returnhost,port

‎Lib/test/test_ftplib.py‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ def __init__(self, conn):
9494
self.rest=None
9595
self.next_retr_data=RETR_DATA
9696
self.push('220 welcome')
97+
# We use this as the string IPv4 address to direct the client
98+
# to in response to a PASV command. To test security behavior.
99+
# https://bugs.python.org/issue43285/.
100+
self.fake_pasv_server_ip='252.253.254.255'
97101

98102
defcollect_incoming_data(self,data):
99103
self.in_buffer.append(data)
@@ -134,7 +138,8 @@ def cmd_port(self, arg):
134138
defcmd_pasv(self,arg):
135139
withsocket.create_server((self.socket.getsockname()[0],0))assock:
136140
sock.settimeout(TIMEOUT)
137-
ip,port=sock.getsockname()[:2]
141+
port=sock.getsockname()[1]
142+
ip=self.fake_pasv_server_ip
138143
ip=ip.replace('.',',');p1=port/256;p2=port%256
139144
self.push('227 entering passive mode (%s,%d,%d)'%(ip,p1,p2))
140145
conn,addr=sock.accept()
@@ -695,6 +700,26 @@ def test_makepasv(self):
695700
# IPv4 is in use, just make sure send_epsv has not been used
696701
self.assertEqual(self.server.handler_instance.last_received_cmd,'pasv')
697702

703+
deftest_makepasv_issue43285_security_disabled(self):
704+
"""Test the opt-in to the old vulnerable behavior."""
705+
self.client.trust_server_pasv_ipv4_address=True
706+
bad_host,port=self.client.makepasv()
707+
self.assertEqual(
708+
bad_host,self.server.handler_instance.fake_pasv_server_ip)
709+
# Opening and closing a connection keeps the dummy server happy
710+
# instead of timing out on accept.
711+
socket.create_connection((self.client.sock.getpeername()[0],port),
712+
timeout=TIMEOUT).close()
713+
714+
deftest_makepasv_issue43285_security_enabled_default(self):
715+
self.assertFalse(self.client.trust_server_pasv_ipv4_address)
716+
trusted_host,port=self.client.makepasv()
717+
self.assertNotEqual(
718+
trusted_host,self.server.handler_instance.fake_pasv_server_ip)
719+
# Opening and closing a connection keeps the dummy server happy
720+
# instead of timing out on accept.
721+
socket.create_connection((trusted_host,port),timeout=TIMEOUT).close()
722+
698723
deftest_with_statement(self):
699724
self.client.quit()
700725

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)

[8]ページ先頭

©2009-2025 Movatter.jp