首页 > 解决方案 > SSLv3 与 Python 中的 ssl 库的连接

问题描述

我想制作一个程序(Python 2.7)来检测网站上可用的 ssl/tls 版本。我只想使用标准的 Python 库。

这是我的代码:

#encoding=utf-8

import ssl
import socket
import traceback
import logging
import sys
import json

class AnalyseSSL:

cipher_list="RC4-SHA".split(":")    



list_version_ssl_tls = [
    ("SSLv2", ssl.OP_ALL | ssl.OP_NO_SSLv3 | ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 | ssl.OP_NO_TLSv1_2),
    ("SSLv3", ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 | ssl.OP_NO_TLSv1_2),
    ("TLSv1", ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3 | ssl.OP_NO_TLSv1_1 | ssl.OP_NO_TLSv1_2),
    ("TLSv1_1", ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3 | ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_2),
    ("TLSv1_2", ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3 | ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1),
]





def __init__(self, hostname, port):
    self.hostname = hostname
    self.port = port


# try to connect to the hostname with all cipher suite for each SSL/TLS version
def try_all_ssl_tls_version(self):
    logging.warning("---------------------------------------- %s", port)
    nb_tentative_max = 5
    cpt_tentative_max = 0
    resultat = {}

    try:
        print 'hostname : ', hostname
        for version in self.list_version_ssl_tls:                                           # Pour chaque version de SSL/TLS
            cpt_nb_tentative_max = 0
            is_version_supported = False
            if cpt_tentative_max >= 5:
                    break;
            for cipher_suite in self.cipher_list:                                                # Pour chaque cipher suite
                print cipher_suite
        context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)                   # création du context
                context.check_hostname = False
                context.verify_mode = ssl.CERT_NONE
                context.options = version[1]                                                   # on spécifie la version de SSL/TLS qu'on veut utiliser
        print context.options

        try:
                    context.set_ciphers(cipher_suite)                                       # on spécifie la cipher suite à utiliser
                except Exception as e:
                    print "Exception : ", e
        pass                        
        traceback.print_exc(e)


                s_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s = context.wrap_socket(s_)
                #s = context.wrap_socket(s_, server_hostname=hostname)
                #print "timeout : ", s.gettimeout()
                s.settimeout(5)
                #print "timeout : ", s.gettimeout()


                try:
                    s.connect((hostname, port))                                                 # on tente de se connecter
                    if (is_version_supported == False):
                        print version[0], "supporté"
                        is_version_supported = True
                    print s.cipher()
                    #logging.info("---------------------------------------- %s %s", %(version[0], s.cipher()))
                    s.close()

                except socket.timeout:
                    cpt_tentative_max += 1
                    if cpt_tentative_max >= 5:
                        break;
                except Exception as e:                                                                         # si la connexion a échoué
                    #print "[version ", version[0], " with ", cipher_suite, " :: ", e
                    #print s.getpeercert()
                    #traceback.print_exc(e)
        print e
                    pass
            if is_version_supported == False:
                print version[0], "non supporté"

            print "\n"
    except Exception as e:
        print e
    traceback.print_exec(e)
    pass

hostname = 'PUT YOUR IP HERE'
port = 443

A = AnalyseSSL(hostname, port)
A.try_all_ssl_tls_version()

问题是我无法建立 sslv3 连接。我有一个 ip(我确定 sslv3 在这个 ip 上启用了密码套件套件 RC4-SHA,我用 openssl 和 testssl.sh 对其进行了测试)。

我的程序适用于第三个 tls 版本,但无法使用 sslv3 或 sslv2。这是我遇到的错误:

[SSL: UNSUPPORTED_PROTOCOL] unsupported protocol (_ssl.c:581)

我不能使用 SSLv3,但为什么?(我重新编译了我的 openssl 库以启用 sslv3 并且它可以工作,因为如果我使用:

 openssl s_client -connect IP -ssl3 -ciphers RC4-SHA  

那行得通。

我该如何解决这个问题?谢谢 :)

标签: python-2.7sslopensslsslv3

解决方案


尝试在每个请求上设置适当的context.minimum_version以避免不受支持的协议错误和/或随机连接从服务器端关闭。


推荐阅读