【发布时间】:2019-05-10 01:07:27
【问题描述】:
我正在尝试使用 python 2.7.15 从(IP,destport 443)获取 SNI,并且我正在使用最新版本的 OpenSSL 和 ssl 模块。
这是我的代码:
import OpenSSL as OSsl #This two modules are imported for the only purpose of getting the SNI using function defined by us down here getSNI
import ssl
ip = "52.85.25.17"
dport = "443"
#With this function we get the Server Name Identification for the trasmissions with Secure Socket Layer identified by the port 443. We only care about the destinationIP and the destinationPort
def getSNI(ip, dport):
if dport != "443":
commonName = "Not SSL"
print commonName
else:
server_certificate = ssl.get_server_certificate((ip, dport))
x509 = OSsl.crypto.load_certificate(OSsl.crypto.FILETYPE_PEM, server_certificate) #x509 is referred to the standard used for PKI (Public Key Infrastructure) used in this case for ciphering our informations about certificate
#FILETYPE_PEM serializes data to a Base64-Encoded
#getting the informations about Certificate
certInfo = x509.get_subject()
commonName = certInfo.commonName
print (commonName)
return commonName
getSNI(ip,dport)
这可行,但是对于指定的地址(在我在此处发布的代码的 sn-p 中)我收到此错误:
Traceback (most recent call last):
File "getSNI.py", line 31, in <module>
getSNI(ip,dport)
File "getSNI.py", line 17, in getSNI
server_certificate = ssl.get_server_certificate((ip, dport))
File "/usr/lib/python2.7/ssl.py", line 1023, in get_server_certificate
with closing(context.wrap_socket(sock)) as sslsock:
File "/usr/lib/python2.7/ssl.py", line 369, in wrap_socket
_context=self)
File "/usr/lib/python2.7/ssl.py", line 617, in __init__
self.do_handshake()
File "/usr/lib/python2.7/ssl.py", line 846, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:726)
我已经升级了所有模块和包,我阅读了很多关于这个主题的问题,但我不知道如何解决这个问题
Edit1:执行 whois 我发现这个 IpAddress 连接到 Amazon,那么关于 Amazon 和 SNI 有什么特别的问题吗?
【问题讨论】:
-
这似乎是握手失败。请注意,SSLv3 已过时,最新版本的 SSL 很可能默认不会尝试使用它,您必须告诉他们接受过时的加密才能使其正常工作。或者问题可能出在服务器上。您是否尝试在调用
get_server_certificate时指定ssl_version=ssl.PROTOCOL_SSLv3?从 python3.5 开始,默认是 TLS。 -
我使用的是 python 2.7.15rc。我试图设置 ssl_version = ssl.PROTOCOL_SSLv3 并且回溯是“'module'对象没有属性'PROTOCOL_SSLv3'”我应该安装其他任何东西或者直接在模块库中更改它吗?
-
您确定服务器支持 SSLv3 吗?尝试指定
PROTOCOL_TLS。 -
尝试使用 PROTOCOL_TLS 并没有任何变化。如果我不能确定服务器支持什么?
-
"...get the SNI from a (IP..." - 这没有意义。SNI需要由客户端提供给服务器,这样服务器可以决定提供哪个证书。在同一个 IP 地址上可以有多个有效的 SNI。您无法从客户端查询可能的 SNI。如果没有给出 SNI,您的代码只会检查返回的证书,这可能会导致错误或返回一些可能但不需要与使用 SNI 时提供的证书有任何关系的默认证书。
标签: python python-2.7 ssl openssl python-requests