2016-08-17 147 views
0

我的代碼示例:PyOpenSSL獲得服務器證書從SNI主機與未知的主機名

import OpenSSL 
    import socket 

    ctx = OpenSSL.SSL.Context(OpenSSL.SSL.SSLv23_METHOD) 
    s = socket.socket(AF_INET, SOCK_STREAM) 
    connection = OpenSSL.SSL.Connection(ctx, s) 
    connection.connect((str(ip), port)) 
    connection.setblocking(1) 
    connection.do_handshake() 
    chain = connection.get_peer_cert_chain() 

的情況是,如果主機有SNI擴展我得到一個錯誤:

[('SSL routines', 'SSL3_READ_BYTES', 'sslv3 alert handshake failure')]

我相信我能使用OpenSSL.SSL.Connection.set_tlsext_host_name(name)克服這一點,但對於用戶主機名是未知的,我想連接到每個可用的主機名。

所以我的問題是:有沒有辦法通過ip連接到主機,並檢索提供證書的所有可用主機名?還是有辦法從SNI主機中檢索所有證書?

+0

使用' s_client'獲取IP地址的證書並打印證書中的DNS名稱:'openssl s_client -connect -tls1 -servername | openssl x509 -text -noout'。使用IP地址作爲SNI服務器名稱。然後,連接DNS名稱。在使用虛擬服務器的託管環境中,這可能無法按預期工作。您可能必須執行DNS反向查找才能獲取名稱。反向查找假定DNS按預期配置,但情況可能並非如此。 – jww

回答

0

Is there a way to connect to host by ip and retrieve all available hostnames that provide certificates? Or is there a way to just retrieve all certificates from a SNI host?

這些都不可能使用HTTPS。如果幸運的話,您可以收集有關服務器某處可能的主機名的信息,或者通過嗅探解析到服務器IP的DNS查找,或者使用其他方法來確定哪些名稱屬於此IP地址:How can I find all the domain names that resolve to one ip address?

+0

不幸的是,對於我感興趣的主機,DNS查找失敗。 –