您如何强制mechanize将SSLv3用于需要它的HTTPS网址?如果我尝试对所有仅SSLv3的URL使用mechanize,我会收到错误:
URLError: <urlopen error [Errno 1] _ssl.c:504: error:140773E8:SSL routines:SSL23_GET_SERVER_HELLO:reason(1000)>
答案 0 :(得分:2)
一个肮脏的答案......不需要修补。
import ssl
from ssl import PROTOCOL_SSLv23, PROTOCOL_SSLv3, CERT_NONE, SSLSocket
def monkey_wrap_socket(sock, keyfile=None, certfile=None,
server_side=False, cert_reqs=CERT_NONE,
ssl_version=PROTOCOL_SSLv23, ca_certs=None,
do_handshake_on_connect=True,
suppress_ragged_eofs=True, ciphers=None):
ssl_version=PROTOCOL_SSLv3
return SSLSocket(sock, keyfile=keyfile, certfile=certfile,
server_side=server_side, cert_reqs=cert_reqs,
ssl_version=ssl_version, ca_certs=ca_certs,
do_handshake_on_connect=do_handshake_on_connect,
suppress_ragged_eofs=suppress_ragged_eofs,
ciphers=ciphers)
ssl.wrap_socket = monkey_wrap_socket
...在您的代码之前。
答案 1 :(得分:1)
关于Python问题的最后评论EiyrioüvonKauyf上面提到的是我在forked version of mechanize中实现的解决方案。机械化/ _opener.py的差异如下。它修复了mechanize.urlopen(),但没有修复mechanize.Browser()的open()方法:
diff --git a/mechanize/_opener.py b/mechanize/_opener.py
index ad8412d..e6d1ebc 100644
--- a/mechanize/_opener.py
+++ b/mechanize/_opener.py
@@ -25,9 +25,27 @@ import _rfc3986
import _sockettimeout
import _urllib2_fork
from _util import isstringlike
+import ssl, socket
open_file = open
+class HTTPSConnectionV3(httplib.HTTPSConnection):
+ def __init__(self, *args, **kwargs):
+ httplib.HTTPSConnection.__init__(self, *args, **kwargs)
+
+ def connect(self):
+ sock = socket.create_connection((self.host, self.port), self.timeout)
+ if self._tunnel_host:
+ self.sock = sock
+ self._tunnel()
+ try:
+ self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file, ssl_version=ssl.PROTOCOL_SSLv3)
+ except ssl.SSLError, e:
+ self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file, ssl_version=ssl.PROTOCOL_SSLv23)
+
+class HTTPSHandlerV3(urllib2.HTTPSHandler):
+ def https_open(self, req):
+ return self.do_open(HTTPSConnectionV3, req)
class ContentTooShortError(urllib2.URLError):
def __init__(self, reason, result):
@@ -370,7 +388,7 @@ class OpenerFactory:
_urllib2_fork.HTTPErrorProcessor,
]
if hasattr(httplib, 'HTTPS'):
- default_classes.append(_urllib2_fork.HTTPSHandler)
+ default_classes.append(HTTPSHandlerV3)
handlers = []
replacement_handlers = []
答案 2 :(得分:0)
你可以修补ssl.wrap_socket()来使用TLSv1,这在Poodle漏洞之后似乎是唯一可行的左派。这将强制所有SSL连接使用TLSv1,而不管更高级别的库。
import ssl
from functools import wraps
def sslwrap(func):
@wraps(func)
def bar(*args, **kw):
kw['ssl_version'] = ssl.PROTOCOL_TLSv1
return func(*args, **kw)
return bar
ssl.wrap_socket = sslwrap(ssl.wrap_socket)