禁用SSL证书检查双绞线代理

问题描述 投票:3回答:1

我使用的双绞线(16.3)和Treq的(15.1),以使在Python(2.7)异步请求。

我有过一些HTTPS请求的问题。

有些网站有一个无效的证书,从而使请求给他们的时候,我得到这样的:

twisted.python.failure.Failure OpenSSL.SSL.Error

我希望我的客户信任任何服务器,包括那些没有证书或自签名的证书。

如何将我的客户端上禁用证书检查?

这是一个问题基本上是相同的,以我的:https://stackoverflow.com/questions/34357439/ssl-options-for-twisted-agents

谢谢!

python ssl twisted twisted.web twisted.internet
1个回答
5
投票

Update Feb 7, 2019

这里有一个简单的方法,使一个域名白名单treq

from treq.client import HTTPClient
from twisted.web.iweb import IPolicyForHTTPS
from twisted.web.client import BrowserLikePolicyForHTTPS, Agent
from twisted.internet.ssl import CertificateOptions
from twisted.internet import task, defer, ssl
from zope.interface import implementer

@implementer(IPolicyForHTTPS)
class WhitelistContextFactory(object):
    def __init__(self, good_domains=None):
        """
        :param good_domains: List of domains. The URLs must be in bytes
        """
        if not good_domains:
            self.good_domains = []
        else:
            self.good_domains = good_domains

        # by default, handle requests like a browser would
        self.default_policy = BrowserLikePolicyForHTTPS()

    def creatorForNetloc(self, hostname, port):
        # check if the hostname is in the the whitelist, otherwise return the default policy
        if hostname in self.good_domains:
            return ssl.CertificateOptions(verify=False)
        return self.default_policy.creatorForNetloc(hostname, port)

@task.react
@defer.inlineCallbacks
def main(reactor):
    # make a custom client, agent, and context factory
    # NOTE: WhitelistContextFactory() takes a list of BYTES
    treq = HTTPClient(Agent(reactor, contextFactory=WhitelistContextFactory([b'example.net'])))
    response = yield treq.get('https://example.net/version')
    content = yield response.content()
    print(content)

WhitelistContextFactory需要的URL(在list)和检查的bytes如果hostname是在列表中忽略TLS验证。你可以看中,并使用正则表达式了。荣誉给https://github.com/twisted/treq/issues/213

DO NOT DO THIS!

我一直在努力做到这一点也是在过去的几天也是如此。随着我投入绕过证书验证了所有的努力,我可以很容易刚刚创建一对密钥,并一直在我快乐的方式:d。我发现this commenttreq问题板,monkeypatches问题:

from twisted.internet import _sslverify
_sslverify.platformTrust = lambda : None

我敢肯定有“正确”这样做的一个令人费解的方式,但它不会是值得在我看来的努力。我做了一个补丁,使得它不会覆盖platformTrust(),我会设法得到它合并了,但我不认为我的呼吸。从一些错误意见,我已经看到了关于信任的根,SSL和证书的语气,我不认为它会被合并。希望这有助于虽然。

© www.soinside.com 2019 - 2024. All rights reserved.