在 Python 中的 M2Crypto 中关闭 SSL 检查

发布于 2024-08-23 00:39:17 字数 63 浏览 5 评论 0 原文

有没有办法关闭 SSL 检查,以便在 python 中使用 SOAPpy 时不会生成 WrongHost 异常。

Is there a way to turn off SSL checking so that WrongHost Exceptions are not generated when using SOAPpy in python.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒中人 2024-08-30 00:39:17

您可以像这样禁用 M2Crypto 中的所有对等证书检查:

from M2Crypto import SSL, httpslib

context = SSL.Context("sslv3")

# Disable certificate checking
context.set_verify(0, depth = 0)

connection = httpslib.HTTPSConnection("somehostname", 443, ssl_context=context)

# Hack (!!!) for disabling host name check <CN> == <expected host name>.
# Will affect any future SSL connections made by M2Crypto!
SSL.Connection.postConnectionCheck = None

connection.connect() # <-- this would normally raise SSL verification errors
connection.request("GET", "/")

...

我希望您知道这实际上会禁用使用 M2Crypto 创建的任何 SSL 连接的安全性。因此,这根本不值得推荐,除非您只与一台服务器通信,并且认为中间人风险比未加密的 HTTP 更容易接受。

到目前为止,对于 M2Crypto 解决方案,但由于您的问题(与您的标题相反)要求 SOAPpy(我尚未使用),解决方案可能会有所不同,因为 SOAPpy config 似乎使用 socket 模块而不是M2Crypto.SSL(参见第 132 行)。我不知道如何阻止 socket.ssl 模块检查主机名。

You can disable all peer certificate checks in M2Crypto like that:

from M2Crypto import SSL, httpslib

context = SSL.Context("sslv3")

# Disable certificate checking
context.set_verify(0, depth = 0)

connection = httpslib.HTTPSConnection("somehostname", 443, ssl_context=context)

# Hack (!!!) for disabling host name check <CN> == <expected host name>.
# Will affect any future SSL connections made by M2Crypto!
SSL.Connection.postConnectionCheck = None

connection.connect() # <-- this would normally raise SSL verification errors
connection.request("GET", "/")

...

I hope you're aware that this will essentially disable security for any SSL connection created with M2Crypto. So this isn't recommendable at all, except if you're only communicating with one server and think that the man-in-the-middle risk is more acceptable than having unencrypted HTTP.

So far for the M2Crypto solution, but as your question (as opposed to your title) asks for SOAPpy (which I haven't used yet), the solution might be different because the SOAPpy config seems to use the socket module instead of M2Crypto.SSL (see line 132). I don't know how to prevent the socket.ssl module to check host names.

豆芽 2024-08-30 00:39:17

扩展 AndiDog 的答案,您可以逐个实例地设置 postConnectionCheck,并且在 M2Crypto 的 0.21.1 版本(至少)中,有 Connect.set_post_connection_check_callback() 方法可以执行此操作:

sslsock = M2Crypto.SSL.Connection(sslcontext)
# Disable checking of server certificates
sslsock.set_post_connection_check_callback(None)

请注意,禁用对连接到服务器和接受的客户端的检查(默认情况下禁用后者)。

该参数如果不是 None ,则是一个需要证书和地址的函数,即:

check(self.get_peer_cert(), self.addr[0])

有关参考,请参阅 M2Crypto 源

Expanding on AndiDog's answer, you can set postConnectionCheck on a instance-by-instance basis and in version 0.21.1 (at least) of M2Crypto, there is the Connect.set_post_connection_check_callback() method to do so:

sslsock = M2Crypto.SSL.Connection(sslcontext)
# Disable checking of server certificates
sslsock.set_post_connection_check_callback(None)

Note that disables both checking of connected to servers and accepted clients (the latter is disabled by default).

The parameter, if not None, is a function that takes a certificate and address, i.e.:

check(self.get_peer_cert(), self.addr[0])

For reference, see the M2Crypto source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文