я пытаюсь получить SNI от (IP, Destport 443) с Python 2.7.15, и я использую последнюю версию 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)
Это работает, но для указанного адреса (во фрагменте кода, который я разместил здесь) я получаю эту ошибку:
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?
Суть SNI в том, что может существовать несколько доменов, которые разрешены к конкретному IP-адресу. Итак, предоставленный вами IP-адрес (52.85.25.17
) является одним из таких адресов. Сервер не может решить, какой сертификат домена вы запрашиваете, поэтому он завершает соединение с ошибкой.
Приложение 1. Перехват исключений SSLError
Вы можете поймать ssl.SSLError
следующим образом:
try:
server_certificate = ssl.get_server_certificate((ip, dport))
...
except ssl.SSLError as e:
common_name = "Handshake Failed"
ssl.SSLError
.
ssl_version=ssl.PROTOCOL_SSLv3
при вызовеget_server_certificate
? Начиная с python3.5 по умолчанию используется TLS.