如何配置Python以忽略主机名验证?

问题描述 投票:6回答:2

我们在Python中相对较新,因此可能问题太简单了。我们使用的是Python 2.7.15版。

我们试图在TLS上使用Python而没有成功。

这是我们的代码:

import ssl,socket
import urllib2

context = ssl.SSLContext(ssl.PROTOCOL_TLS)
context.verify_mode = ssl.CERT_REQUIRED
context.check_hostname = False
context.load_verify_locations("/py-test/python/bin/certificate.pem")
url = "https://10.0.0.12"
request = urllib2.Request(url)
websocket = urllib2.urlopen(request,None,None,None,None,None,context)
pages=websocket.readlines()
print pages

如您所见,我们已配置context.check_hostname = False

不幸的是,它失败并出现以下异常

Traceback (most recent call last):
 File "./test.py", line 11, in <module>
   websocket = urllib2.urlopen(request,None,None,None,None,None,context)
 File "/py-test/python/lib/python2.7/urllib2.py", line 154, in urlopen
   return opener.open(url, data, timeout)
 File "/py-test/python/lib/python2.7/urllib2.py", line 429, in open
   response = self._open(req, data)
 File "/py-test/python/lib/python2.7/urllib2.py", line 447, in _open
   '_open', req)
 File "/py-test/python/lib/python2.7/urllib2.py", line 407, in _call_chain
   result = func(*args)
 File "/py-test/python/lib/python2.7/urllib2.py", line 1241, in https_open
   context=self._context)
 File "/py-test/python/lib/python2.7/urllib2.py", line 1198, in do_open
   raise URLError(err)
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726)>

它绝对是主机名验证。如果我们使用正确的证书并更正主机名请求成功。

如果我们使用错误的证书,则会失败并出现以下异常。

File "./test.py", line 8, in <module>
   context.load_verify_locations("/py-test/python/bin/certificate_bad.pem")
ssl.SSLError: [X509] PEM lib (_ssl.c:3027)

因此,我们需要帮助来了解如何配置Python以忽略主机名验证。

还有一个问题(可以在单独的帖子中询问)。

我们在Python中有一个包含所有已知CA的信任文件吗?就像Java中的cacerts.jks一样。我们在哪里可以找到委托人?

添加

我们“想要验证证书是否由有效CA签名”,但我们“不关心它是否标识了您实际连接到的站点”。

我们需要帮助来配置Python以忽略主机名验证?我们的代码中有什么错误?我们已经尝试根据文档https://docs.python.org/2/library/ssl.html创建代码

新增2

我们投入了大量时间,但遗憾的是我们没有取得进展。

有人在Python 2.7中有工作示例吗?我的意思是如果您使用其他URL访问然后出现在证书中,代码将起作用。可能是Python 2.7无法配置忽略主机名验证?

我们的问题是什么?我们在CentOS 6上使用它。可能它与OpenSSL有关吗?我们使用最新版本openssl-1.0.1e-57.el6.x86_64。可能我们应该升级到Python 3.x?

python python-2.7 ssl
2个回答
3
投票
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

强制python默认跳过证书验证。希望能帮助到你。


3
投票

如您所发现,您可以通过自定义用于验证连接的SSLContext对象来实现此目的。

但是,要将其连接到urllib2.urlopen,您需要构建一个自定义开启器并安装它。

这是一个例子:

import httplib
import socket
import ssl
import urllib2

import certifi


class InterceptedHttpsConnection(httplib.HTTPSConnection):
    def connect(self):
        # Open an unencrypted TCP socket first
        sock = socket.create_connection((self.host, self.port), self.timeout)

        # Build up a custom SSLContext. (Might be better to do this once rather
        # than on every request.)
        context = ssl.SSLContext(ssl.PROTOCOL_TLS)

        # We require the SSL context to verify the certificate.
        context.verify_mode = ssl.CERT_REQUIRED

        # But we instruct the SSL context to *not* validate the hostname.
        context.check_hostname = False

        # Load CA certificates from the certifi bundle.
        context.load_verify_locations(cafile=certifi.where())

        # Use our SSLContext object to wrap the bare socket into an SSL socket.
        self.sock = context.wrap_socket(sock, server_hostname=self.host)


class InterceptedHttpsHandler(urllib2.HTTPSHandler):
    def https_open(self, req):
        return self.do_open(InterceptedHttpsConnection, req)


def main():
    opener = urllib2.build_opener(InterceptedHttpsHandler)
    urllib2.install_opener(opener)

    contents = urllib2.urlopen('https://example.com/').read()
    print contents
© www.soinside.com 2019 - 2024. All rights reserved.