简体   繁体   English

python中使用ssl握手失败如何解决?

[英]How to solve the handshake failure using ssl in python?

I try connect to specific https server:我尝试连接到特定的 https 服务器:

socketHandler = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socketWraped = ssl.wrap_socket(socketHandler)
socketWraped.connect(('certificatedetails.com', 443))

But the python says:但是 python 说:

File "/usr/lib/python3.6/ssl.py", line 1109, in connect
self._real_connect(addr, False)
File "/usr/lib/python3.6/ssl.py", line 1100, in _real_connect
self.do_handshake()
File "/usr/lib/python3.6/ssl.py", line 1077, in do_handshake
self._sslobj.do_handshake()
File "/usr/lib/python3.6/ssl.py", line 689, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:852)

I try using TLS1:我尝试使用 TLS1:

socketWraped = ssl.wrap_socket(
    socketHandler,
    ssl_version=ssl.PROTOCOL_TLSv1,
    ciphers='ADH-AES256-SHA'
)

But says:但是说:

ssl.SSLError: [SSL: NO_CIPHERS_AVAILABLE] no ciphers available (_ssl.c:852)

Have a upgraded ssl in python and operative system:在python升级ssl和操作系统:

$ hostnamectl
   Static hostname: machine
         Icon name: computer-desktop
           Chassis: desktop
        Machine ID: ...
           Boot ID: ...
  Operating System: Ubuntu 18.04.2 LTS
            Kernel: Linux 4.15.0-51-generic
      Architecture: x86-64
$ openssl version
OpenSSL 1.1.1c  28 May 2019
$ python3 -c "import ssl; print(ssl.OPENSSL_VERSION)"
OpenSSL 1.1.1c  28 May 2019

From.netcat can connect without problems: From.netcat 可以毫无问题地连接:

$ ncat --ssl -v certificatedetails.com 443
Ncat: Version 7.60 ( https://nmap.org/ncat )
Ncat: SSL connection to 104.28.6.163:443.
Ncat: SHA-1 fingerprint: 75B3 C6AD 7A72 62B5 7104 0632 0585 A82A F542 641B

What is the problem and how to solve this?问题是什么以及如何解决这个问题?

From the documentation :文档

Since Python 3.2 and 2.7.9, it is recommended to use the SSLContext.wrap_socket() of anSSLContext instance to wrap sockets as SSLSocket objects.从 Python 3.2 和 2.7.9 开始,建议使用SSLContext实例的SSLContext.wrap_socket()将套接字包装为SSLSocket对象。 The helper functions create_default_context() returns a new context with secure default settings.辅助函数create_default_context()返回具有安全默认设置的新上下文。 The old wrap_socket() function is deprecated since it is both inefficient and has no support for server name indication (SNI) and hostname matching.旧的wrap_socket()函数已被弃用,因为它既低效又不支持服务器名称指示 (SNI) 和主机名匹配。

When I use SSLContext.wrap_socket() instead of the deprecated wrap_socket(), it works:当我使用 SSLContext.wrap_socket() 而不是已弃用的 wrap_socket() 时,它可以工作:

socketHandler = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socketWraped = ssl.create_default_context().wrap_socket(socketHandler, server_hostname='certificatedetails.com')
socketWraped.connect(('certificatedetails.com', 443))

To solve both SSLV3_ALERT_HANDSHAKE_FAILURE NO_CIPHERS_AVAILABLE the approch I recommend is要同时解决 SSLV3_ALERT_HANDSHAKE_FAILURE NO_CIPHERS_AVAILABLE 我推荐的方法是

a) Find the maximum protocol accepted from the server using s_client on a linux box, for example: a) 在 linux 框上使用 s_client 查找服务器接受的最大协议,例如:

 openssl s_client -connect my_host:443 -tls1

(check man s_client for all possible protocols). (检查 man s_client 以了解所有可能的协议)。

b) Once Connected, note the Cipher used by openssl b) 连接后,记下 openssl 使用的密码

New, SSLv3, Cipher is AES256-SHA
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
No ALPN negotiated

c) Create your sslcontext accordingly c) 相应地创建你的 sslcontext

sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
sslcontext.set_ciphers("AES256-SHA")
sslcontext.load_default_certs()

Works for all librairies (requests, aiohttp...)适用于所有图书馆(请求、aiohttp...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM