2019-03-21 16:00:09 +03:00
|
|
|
#!/usr/bin/env python2
|
2012-06-04 23:46:28 +04:00
|
|
|
|
|
|
|
"""
|
2019-01-05 23:38:52 +03:00
|
|
|
Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/)
|
2017-10-11 15:50:46 +03:00
|
|
|
See the file 'LICENSE' for copying permission
|
2012-06-04 23:46:28 +04:00
|
|
|
"""
|
|
|
|
|
2015-12-18 19:15:59 +03:00
|
|
|
import distutils.version
|
2012-06-04 23:46:28 +04:00
|
|
|
import httplib
|
2016-11-04 17:04:38 +03:00
|
|
|
import re
|
2012-06-04 23:46:28 +04:00
|
|
|
import socket
|
|
|
|
import urllib2
|
|
|
|
|
2015-09-10 16:51:33 +03:00
|
|
|
from lib.core.common import getSafeExString
|
2018-06-08 16:13:50 +03:00
|
|
|
from lib.core.data import conf
|
2015-06-01 11:45:16 +03:00
|
|
|
from lib.core.data import kb
|
2012-06-04 23:46:28 +04:00
|
|
|
from lib.core.data import logger
|
2012-12-06 17:14:19 +04:00
|
|
|
from lib.core.exception import SqlmapConnectionException
|
2015-12-18 19:15:59 +03:00
|
|
|
from lib.core.settings import PYVERSION
|
2012-06-04 23:46:28 +04:00
|
|
|
|
|
|
|
ssl = None
|
|
|
|
try:
|
|
|
|
import ssl as _ssl
|
|
|
|
ssl = _ssl
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2015-06-01 11:45:16 +03:00
|
|
|
_protocols = filter(None, (getattr(ssl, _, None) for _ in ("PROTOCOL_TLSv1_2", "PROTOCOL_TLSv1_1", "PROTOCOL_TLSv1", "PROTOCOL_SSLv3", "PROTOCOL_SSLv23", "PROTOCOL_SSLv2")))
|
2012-06-04 23:46:28 +04:00
|
|
|
|
|
|
|
class HTTPSConnection(httplib.HTTPSConnection):
|
|
|
|
"""
|
|
|
|
Connection class that enables usage of newer SSL protocols.
|
|
|
|
|
|
|
|
Reference: http://bugs.python.org/msg128686
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
httplib.HTTPSConnection.__init__(self, *args, **kwargs)
|
|
|
|
|
|
|
|
def connect(self):
|
|
|
|
def create_sock():
|
|
|
|
sock = socket.create_connection((self.host, self.port), self.timeout)
|
|
|
|
if getattr(self, "_tunnel_host", None):
|
|
|
|
self.sock = sock
|
|
|
|
self._tunnel()
|
|
|
|
return sock
|
|
|
|
|
|
|
|
success = False
|
2012-06-04 23:52:51 +04:00
|
|
|
|
2015-11-25 15:04:34 +03:00
|
|
|
# Reference(s): https://docs.python.org/2/library/ssl.html#ssl.SSLContext
|
|
|
|
# https://www.mnot.net/blog/2014/12/27/python_2_and_tls_sni
|
2018-06-08 16:13:50 +03:00
|
|
|
if re.search(r"\A[\d.]+\Z", self.host) is None and kb.tlsSNI.get(self.host) is not False and not any((conf.proxy, conf.tor)) and hasattr(ssl, "SSLContext"):
|
2015-11-25 15:04:34 +03:00
|
|
|
for protocol in filter(lambda _: _ >= ssl.PROTOCOL_TLSv1, _protocols):
|
2015-06-01 11:45:16 +03:00
|
|
|
try:
|
|
|
|
sock = create_sock()
|
2015-11-25 15:04:34 +03:00
|
|
|
context = ssl.SSLContext(protocol)
|
2015-12-07 01:49:22 +03:00
|
|
|
_ = context.wrap_socket(sock, do_handshake_on_connect=True, server_hostname=self.host)
|
2015-06-01 11:45:16 +03:00
|
|
|
if _:
|
|
|
|
success = True
|
|
|
|
self.sock = _
|
|
|
|
_protocols.remove(protocol)
|
|
|
|
_protocols.insert(0, protocol)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
sock.close()
|
2019-01-22 02:40:48 +03:00
|
|
|
except (ssl.SSLError, socket.error, httplib.BadStatusLine) as ex:
|
2015-06-01 11:45:16 +03:00
|
|
|
self._tunnel_host = None
|
2015-09-10 16:51:33 +03:00
|
|
|
logger.debug("SSL connection error occurred ('%s')" % getSafeExString(ex))
|
2015-06-01 11:45:16 +03:00
|
|
|
|
2015-12-07 01:49:22 +03:00
|
|
|
if kb.tlsSNI.get(self.host) is None:
|
|
|
|
kb.tlsSNI[self.host] = success
|
2015-11-25 15:04:34 +03:00
|
|
|
|
|
|
|
if not success:
|
|
|
|
for protocol in _protocols:
|
2015-06-01 11:45:16 +03:00
|
|
|
try:
|
|
|
|
sock = create_sock()
|
2015-11-25 15:04:34 +03:00
|
|
|
_ = ssl.wrap_socket(sock, self.key_file, self.cert_file, ssl_version=protocol)
|
2015-06-01 11:45:16 +03:00
|
|
|
if _:
|
2015-11-25 15:04:34 +03:00
|
|
|
success = True
|
2015-06-01 11:45:16 +03:00
|
|
|
self.sock = _
|
|
|
|
_protocols.remove(protocol)
|
|
|
|
_protocols.insert(0, protocol)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
sock.close()
|
2019-01-22 02:40:48 +03:00
|
|
|
except (ssl.SSLError, socket.error, httplib.BadStatusLine) as ex:
|
2015-06-01 11:45:16 +03:00
|
|
|
self._tunnel_host = None
|
2015-09-10 16:51:33 +03:00
|
|
|
logger.debug("SSL connection error occurred ('%s')" % getSafeExString(ex))
|
2012-06-04 23:46:28 +04:00
|
|
|
|
|
|
|
if not success:
|
2015-12-18 19:15:59 +03:00
|
|
|
errMsg = "can't establish SSL connection"
|
2016-08-02 13:38:57 +03:00
|
|
|
# Reference: https://docs.python.org/2/library/ssl.html
|
|
|
|
if distutils.version.LooseVersion(PYVERSION) < distutils.version.LooseVersion("2.7.9"):
|
|
|
|
errMsg += " (please retry with Python >= 2.7.9)"
|
2015-12-18 19:15:59 +03:00
|
|
|
raise SqlmapConnectionException(errMsg)
|
2012-06-04 23:46:28 +04:00
|
|
|
|
|
|
|
class HTTPSHandler(urllib2.HTTPSHandler):
|
|
|
|
def https_open(self, req):
|
|
|
|
return self.do_open(HTTPSConnection if ssl else httplib.HTTPSConnection, req)
|
2013-04-26 16:49:24 +04:00
|
|
|
|
|
|
|
# Bug fix (http://bugs.python.org/issue17849)
|
|
|
|
|
|
|
|
def _(self, *args):
|
|
|
|
return self._readline()
|
|
|
|
|
|
|
|
httplib.LineAndFileWrapper._readline = httplib.LineAndFileWrapper.readline
|
|
|
|
httplib.LineAndFileWrapper.readline = _
|