Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec87d8e

Browse files
committed
Adding a support for SNI (Issue #1256)
1 parent 341d2a6 commit ec87d8e

2 files changed

Lines changed: 40 additions & 16 deletions

File tree

lib/core/option.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,6 +1836,7 @@ def _setKnowledgeBaseAttributes(flushAll=True):
18361836
kb.safeReq = AttribDict()
18371837
kb.singleLogFlags = set()
18381838
kb.reduceTests = None
1839+
kb.tlsSNI = None
18391840
kb.stickyDBMS = False
18401841
kb.stickyLevel = None
18411842
kb.storeCrawlingChoice = None

lib/request/httpshandler.py

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
import httplib
99
import socket
10+
import sys
1011
import urllib2
1112

13+
from lib.core.data import kb
1214
from lib.core.data import logger
1315
from lib.core.exception import SqlmapConnectionException
1416

@@ -19,7 +21,7 @@
1921
except ImportError:
2022
pass
2123

22-
_protocols = filter(None, (getattr(ssl, _, None) for _ in ("PROTOCOL_SSLv3", "PROTOCOL_TLSv1", "PROTOCOL_SSLv23", "PROTOCOL_SSLv2")))
24+
_protocols = filter(None, (getattr(ssl, _, None) for _ in ("PROTOCOL_TLSv1_2", "PROTOCOL_TLSv1_1", "PROTOCOL_TLSv1", "PROTOCOL_SSLv3", "PROTOCOL_SSLv23", "PROTOCOL_SSLv2")))
2325

2426
class HTTPSConnection(httplib.HTTPSConnection):
2527
"""
@@ -41,21 +43,42 @@ def create_sock():
4143

4244
success = False
4345

44-
for protocol in _protocols:
45-
try:
46-
sock = create_sock()
47-
_ = ssl.wrap_socket(sock, self.key_file, self.cert_file, ssl_version=protocol)
48-
if _:
49-
success = True
50-
self.sock = _
51-
_protocols.remove(protocol)
52-
_protocols.insert(0, protocol)
53-
break
54-
else:
55-
sock.close()
56-
except (ssl.SSLError, socket.error, httplib.BadStatusLine), errMsg:
57-
self._tunnel_host = None
58-
logger.debug("SSL connection error occurred ('%s')" % errMsg)
46+
if not kb.tlsSNI:
47+
for protocol in _protocols:
48+
try:
49+
sock = create_sock()
50+
_ = ssl.wrap_socket(sock, self.key_file, self.cert_file, ssl_version=protocol)
51+
if _:
52+
success = True
53+
self.sock = _
54+
_protocols.remove(protocol)
55+
_protocols.insert(0, protocol)
56+
break
57+
else:
58+
sock.close()
59+
except (ssl.SSLError, socket.error, httplib.BadStatusLine), errMsg:
60+
self._tunnel_host = None
61+
logger.debug("SSL connection error occurred ('%s')" % errMsg)
62+
63+
# Reference(s): https://docs.python.org/2/library/ssl.html#ssl.SSLContext
64+
# https://www.mnot.net/blog/2014/12/27/python_2_and_tls_sni
65+
if not success and hasattr(ssl, "SSLContext"):
66+
for protocol in filter(lambda _: _ >= ssl.PROTOCOL_TLSv1, _protocols):
67+
try:
68+
sock = create_sock()
69+
context = ssl.SSLContext(protocol)
70+
_ = context.wrap_socket(sock, do_handshake_on_connect=False, server_hostname=self.host)
71+
if _:
72+
kb.tlsSNI = success = True
73+
self.sock = _
74+
_protocols.remove(protocol)
75+
_protocols.insert(0, protocol)
76+
break
77+
else:
78+
sock.close()
79+
except (ssl.SSLError, socket.error, httplib.BadStatusLine), errMsg:
80+
self._tunnel_host = None
81+
logger.debug("SSL connection error occurred ('%s')" % errMsg)
5982

6083
if not success:
6184
raise SqlmapConnectionException("can't establish SSL connection")

0 commit comments

Comments
 (0)