pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

使用 ftplib 連接到 FTP TLS 1.2 服務器

Connect to FTP TLS 1.2 Server with ftplib(使用 ftplib 連接到 FTP TLS 1.2 服務器)
本文介紹了使用 ftplib 連接到 FTP TLS 1.2 服務器的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我嘗試連接到僅支持 TLS 1.2 的 FTP 服務器使用 Python 3.4.1

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

我的代碼:

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'))

客戶端錯誤:

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

服務器端出錯:

  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.

推薦答案

最終解決方案見文末.其余的是調試問題所需的步驟.

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

我嘗試使用 Python 3.4.1 連接到僅支持 TLS 1.2 的 FTP 服務器

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

你怎么知道的?

ssl.SSLEOFError: EOF 發生違反協議 (_ssl.c:598)

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

我會建議客戶端和服務器之間的許多 SSL 問題之一,例如服務器不支持 TLS 1.2、沒有通用密碼等.這些問題很難調試,因為您要么只收到一些 SSL 警報,要么服務器將簡單地關閉沒有任何明顯原因的連接.如果您有權訪問服務器,請在服務器端查找錯誤消息.

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.

您也可以嘗試不強制使用 SSL 版本,而是使用默認版本,以便客戶端和服務器同意兩者都支持的最佳 SSL 版本.如果這仍然不起作用,請嘗試使用已知與該服務器一起工作的客戶端,并捕獲好連接和壞連接的數據包并進行比較.如果您需要有關該帖子的幫助,請將數據包捕獲發送到 cloudshark.org.

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:剛剛在測試服務器上使用 python 3.4.0 和 3.4.2 進行了嘗試:

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

  • python 3.4.0 執行 TLS 1.0 握手,即忽略設置
  • python 3.4.2 成功與 TLS 1.2 握手

在這兩個版本中,ftplib 都有一個小錯誤,如果 ftps.ssl_version 是別的東西,它會發送 AUTH SSL 而不是 AUTH TLSTLS 1.0,即 SSLv3 或 TLS1.1.+.雖然我懷疑這是問題的根源,但實際上可能是 FTP 服務器以不同方式處理 AUTH TLSAUTH SSL.

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.

編輯#2 和解決方案:

數據包捕獲顯示設置 ftps.ssl_version 無效,SSL 握手仍將僅使用 TLS 1.0 完成.查看 3.4.0 中 ftplib 的源代碼:

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

由于在調用 ftplib.FTP_TLS() 時調用了 __init__,因此 SSL 上下文將使用 ftplib 使用的默認 ssl_version 創建(ssl.PROTOCOL_TLSv1) 而不是你自己的版本.要強制執行另一個 SSL 版本,您必須為您自己的上下文提供所需的 SSL 版本.以下對我有用:

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'))

或者,您可以全局設置協議版本,而不僅僅是為此 FTP_TLS 對象:

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()

還有一個小而重要的觀察:看起來 ftplib 不進行任何類型的證書驗證,因為它接受了這個與名稱不匹配的自簽名證書而不會抱怨.這使得主動中間人攻擊成為可能.希望他們將來能修復這種不安全的行為,在這種情況下,這里的代碼會因為證書無效而失敗.

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.

這篇關于使用 ftplib 連接到 FTP TLS 1.2 服務器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why I cannot make an insert to Python list?(為什么我不能插入 Python 列表?)
Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的開頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒有插入到 postgresql 表中)
list extend() to index, inserting list elements not only to the end(list extend() 索引,不僅將列表元素插入到末尾)
How to add element in Python to the end of list using list.insert?(如何使用 list.insert 將 Python 中的元素添加到列表末尾?)
TypeError: #39;float#39; object is not subscriptable(TypeError:“浮動對象不可下標)
主站蜘蛛池模板: 回转窑-水泥|石灰|冶金-巩义市瑞光金属制品有限责任公司 | 焊锡丝|焊锡条|无铅锡条|无铅锡丝|无铅焊锡线|低温锡膏-深圳市川崎锡业科技有限公司 | 【中联邦】增稠剂_增稠粉_水性增稠剂_涂料增稠剂_工业增稠剂生产厂家 | 爱佩恒温恒湿测试箱|高低温实验箱|高低温冲击试验箱|冷热冲击试验箱-您身边的模拟环境试验设备技术专家-合作热线:400-6727-800-广东爱佩试验设备有限公司 | 防爆型气象站_农业气象站_校园气象站_农业四情监测系统「山东万象环境科技有限公司」 | 【灵硕展览集团】展台展会设计_展览会展台搭建_展览展示设计一站式服务公司 | 自动化生产线-自动化装配线-直流电机自动化生产线-东莞市慧百自动化有限公司 | 溶氧传感器-pH传感器|哈美顿(hamilton) | 矿用履带式平板车|探水钻机|气动架柱式钻机|架柱式液压回转钻机|履带式钻机-启睿探水钻机厂家 | 富森高压水枪-柴油驱动-养殖场高压清洗机-山东龙腾环保科技有限公司 | 涂层测厚仪_光泽度仪_uv能量计_紫外辐照计_太阳膜测试仪_透光率仪-林上科技 | 纯化水设备-纯水设备-超纯水设备-[大鹏水处理]纯水设备一站式服务商-东莞市大鹏水处理科技有限公司 | 全自动过滤器_反冲洗过滤器_自清洗过滤器_量子除垢环_量子环除垢_量子除垢 - 安士睿(北京)过滤设备有限公司 | 深圳南财多媒体有限公司介绍 | 帽子厂家_帽子工厂_帽子定做_义乌帽厂_帽厂_制帽厂_帽子厂_浙江高普制帽厂 | PSI渗透压仪,TPS酸度计,美国CHAI PCR仪,渗透压仪厂家_价格,微生物快速检测仪-华泰和合(北京)商贸有限公司 | 环氧铁红防锈漆_环氧漆_无溶剂环氧涂料_环氧防腐漆-华川涂料 | 浙江美尔凯特智能厨卫股份有限公司 | 垃圾处理设备_餐厨垃圾处理设备_厨余垃圾处理设备_果蔬垃圾处理设备-深圳市三盛环保科技有限公司 | 全自动实验室洗瓶机,移液管|培养皿|进样瓶清洗机,清洗剂-广州摩特伟希尔机械设备有限责任公司 | PCB接线端子_栅板式端子_线路板连接器_端子排生产厂家-置恒电气 喷码机,激光喷码打码机,鸡蛋打码机,手持打码机,自动喷码机,一物一码防伪溯源-恒欣瑞达有限公司 假肢-假肢价格-假肢厂家-河南假肢-郑州市力康假肢矫形器有限公司 | 无锡门窗-系统门窗-阳光房-封阳台-断桥铝门窗厂[窗致美] | 道康宁消泡剂-瓦克-大川进口消泡剂供应商 | 兰州牛肉面加盟,兰州牛肉拉面加盟-京穆兰牛肉面 | 加中寰球移民官网-美国移民公司,移民机构,移民中介,移民咨询,投资移民 | TMT观察网_独特视角观察TMT行业| 青州开防盗门锁-配汽车芯片钥匙-保险箱钥匙-吉祥修锁店 | 耐高温硅酸铝板-硅酸铝棉保温施工|亿欧建设工程 | 医学模型生产厂家-显微手术模拟训练器-仿真手术模拟训练系统-北京医教科技 | 浙江美尔凯特智能厨卫股份有限公司| 膜结构停车棚-自行车棚-膜结构汽车棚加工安装厂家幸福膜结构 | 医院专用门厂家报价-医用病房门尺寸大全-抗菌木门品牌推荐 | 自动部分收集器,进口无油隔膜真空泵,SPME固相微萃取头-上海楚定分析仪器有限公司 | 过滤器_自清洗过滤器_气体过滤器_苏州华凯过滤技术有限公司 | 搪瓷反应釜厂家,淄博搪瓷反应釜-淄博卓耀| 12cr1mov无缝钢管切割-15crmog无缝钢管切割-40cr无缝钢管切割-42crmo无缝钢管切割-Q345B无缝钢管切割-45#无缝钢管切割 - 聊城宽达钢管有限公司 | 预制围墙_工程预制围墙_天津市瑞通建筑材料有限公司 | 医疗仪器模块 健康一体机 多参数监护仪 智慧医疗仪器方案定制 血氧监护 心电监护 -朗锐慧康 | 猎头招聘_深圳猎头公司_知名猎头公司| 微波消解仪器_智能微波消解仪报价_高压微波消解仪厂家_那艾 | FFU_空气初效|中效|高效过滤器_空调过滤网-广州梓净净化设备有限公司 |