c语言源码 ftp tls,Connect to FTP TLS 1.2 Server with ftplib

使用Python3.4.1尝试连接只支持TLS1.2的FTP服务器时遇到错误。错误信息显示SSL连接违反协议。解决方案是创建自定义SSL上下文并设置为PROTOCOL_TLSv1_2。代码示例中展示了如何正确连接服务器并执行FTP命令。
摘要由CSDN通过智能技术生成

问题

I try to connect to a FTP Server which only supports TLS 1.2

Using Python 3.4.1

My Code:

import ftplib

import ssl

ftps = ftplib.FTP_TLS()

ftps.ssl_version = ssl.PROTOCOL_TLSv1_2

print (ftps.connect('108.61.166.122',31000))

print(ftps.login('test','test123'))

ftps.prot_p()

print (ftps.retrlines('LIST'))

Error on client side:

ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:598)

Error on server side:

Failed TLS negotiation on control channel, disconnected. (SSL_accept():

error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol)

The credentials in the example are working for testing.

回答1:

See the end of this post for the final solution. The rest are the steps needed to debug the problem.

I try to connect to a FTP Server which only supports TLS 1.2 Using Python 3.4.1

How do you know?

ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:598)

I would suggest one of the many SSL problems between client and server, like the server not supporting TLS 1.2, no common ciphers etc. These problems are hard to debug because you either get only some SSL alert or the server will simply close the connection without any obvious reason. If you have access to the server look for error messages on the server side.

You may also try to not to enforce an SSL version but use the default instead, so that client and server will agree to the best SSL version both support. If this will still not work try with a client which is known to work with this server and make a packet capture of the good and bad connections and compare. If you need help with that post the packet captures to cloudshark.org.

Edit#1: just tried it with python 3.4.0 and 3.4.2 against a test server:

python 3.4.0 does a TLS 1.0 handshake, i.e. ignores the setting

python 3.4.2 does a successful TLS 1.2 handshake

In both versions ftplib has the minor bug, that it sends AUTH SSL instead of AUTH TLS if ftps.ssl_version is something else then TLS 1.0, i.e. SSLv3 or TLS1.1.+. While I doubt that this is the origin of the problem it might actually be if the FTP server handles AUTH TLS and AUTH SSL differently.

Edit#2 and Solution:

A packet capture shows that setting ftps.ssl_version has no effect and the SSL handshake will still be done with TLS 1.0 only. Looking at the source code of ftplib in 3.4.0 gives:

ssl_version = ssl.PROTOCOL_TLSv1

def __init__(self, host='', user='', passwd='', acct='', keyfile=None,

certfile=None, context=None,

timeout=_GLOBAL_DEFAULT_TIMEOUT, source_address=None):

....

if context is None:

context = ssl._create_stdlib_context(self.ssl_version,

certfile=certfile,

keyfile=keyfile)

self.context = context

Since __init__ is called when ftplib.FTP_TLS() is called the SSL context will be created with the default ssl_version used by ftplib (ssl.PROTOCOL_TLSv1) and not with your own version. To enforce another SSL version you must to provide your own context with the needed SSL version. The following works for me:

import ftplib

import ssl

ctx = ssl._create_stdlib_context(ssl.PROTOCOL_TLSv1_2)

ftps = ftplib.FTP_TLS(context=ctx)

print (ftps.connect('108.61.166.122',31000))

print(ftps.login('test','test123'))

ftps.prot_p()

print (ftps.retrlines('LIST'))

Alternatively you could set the protocol version globally instead of only for this FTP_TLS object:

ftplib.FTP_TLS.ssl_version = ssl.PROTOCOL_TLSv1_2

ftps = ftplib.FTP_TLS()

And just a small but important observation: it looks like that ftplib does not do any kind of certificate validation, since it accepts this self-signed certificate which does not match the name without complaining. This makes a active man-in-the-middle attack possible. Hopefully they will fix this insecure behavior in the future, in which case the code here will fail because of an invalid certificate.

回答2:

Firstly AFAIK no ftp supports SSL directly, for which ftps is introduced. Also sftp and ftps are two different concepts: http://en.wikipedia.org/wiki/FTPS .Now, your problem is regarding the programming and not related to SSL or FTPs or any such client-server communication

import ftplib

import ssl

ftps = ftplib.FTP_TLS()

#ftps.ssl_version = ssl.PROTOCOL_TLSv1_2

print (ftps.connect('108.61.166.122',31000))

print(ftps.login('test','test123'))

ftps.prot_p()

print (ftps.retrlines('LIST'))

as ftplib has no attribute PROTOCOL_TLSv1_2 besides which it works fine. and well, your host is not responding !

Hopefully it helps !

来源:https://stackoverflow.com/questions/30276529/connect-to-ftp-tls-1-2-server-with-ftplib

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值