Skip to content

Commit 41b8dfa

Browse files
committed
Implementation for an Issue sqlmapproject#1540
1 parent 4335ae8 commit 41b8dfa

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

lib/core/option.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
from lib.core.settings import PARAMETER_SPLITTING_REGEX
123123
from lib.core.settings import PROBLEMATIC_CUSTOM_INJECTION_PATTERNS
124124
from lib.core.settings import SITE
125+
from lib.core.settings import SOCKET_PRE_CONNECT_QUEUE_SIZE
125126
from lib.core.settings import SQLMAP_ENVIRONMENT_PREFIX
126127
from lib.core.settings import SUPPORTED_DBMS
127128
from lib.core.settings import SUPPORTED_OS
@@ -1014,10 +1015,44 @@ def _getaddrinfo(*args, **kwargs):
10141015
kb.cache[args] = socket._getaddrinfo(*args, **kwargs)
10151016
return kb.cache[args]
10161017

1017-
if not hasattr(socket, '_getaddrinfo'):
1018+
if not hasattr(socket, "_getaddrinfo"):
10181019
socket._getaddrinfo = socket.getaddrinfo
10191020
socket.getaddrinfo = _getaddrinfo
10201021

1022+
def _setSocketPreConnect():
1023+
"""
1024+
Makes a pre-connect version of socket.connect
1025+
"""
1026+
1027+
def _():
1028+
while kb.threadContinue:
1029+
for address in socket._ready:
1030+
if len(socket._ready[address]) < SOCKET_PRE_CONNECT_QUEUE_SIZE:
1031+
s = socket.socket()
1032+
s._connect(address)
1033+
socket._ready[address].append(s._sock)
1034+
time.sleep(0.001)
1035+
1036+
def connect(self, address):
1037+
found = False
1038+
with kb.locks.socket:
1039+
if address not in socket._ready:
1040+
socket._ready[address] = []
1041+
if len(socket._ready[address]) > 0:
1042+
self._sock = socket._ready[address].pop(0)
1043+
found = True
1044+
if not found:
1045+
self._connect(address)
1046+
1047+
if not hasattr(socket, "_connect"):
1048+
socket._ready = {}
1049+
socket.socket._connect = socket.socket.connect
1050+
socket.socket.connect = connect
1051+
1052+
thread = threading.Thread(target=_)
1053+
thread.daemon = True
1054+
thread.start()
1055+
10211056
def _setHTTPHandlers():
10221057
"""
10231058
Check and set the HTTP/SOCKS proxy for all HTTP requests.
@@ -1803,7 +1838,7 @@ def _setKnowledgeBaseAttributes(flushAll=True):
18031838
kb.lastParserStatus = None
18041839

18051840
kb.locks = AttribDict()
1806-
for _ in ("cache", "count", "index", "io", "limit", "log", "redirect", "request", "value"):
1841+
for _ in ("cache", "count", "index", "io", "limit", "log", "socket", "redirect", "request", "value"):
18071842
kb.locks[_] = threading.Lock()
18081843

18091844
kb.matchRatio = None
@@ -2517,6 +2552,7 @@ def init():
25172552
_setHTTPAuthentication()
25182553
_setHTTPHandlers()
25192554
_setDNSCache()
2555+
_setSocketPreConnect()
25202556
_setSafeVisit()
25212557
_doSearch()
25222558
_setBulkMultipleTargets()

lib/core/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,9 @@
466466
# Approximate chunk length (in bytes) used by BigArray objects (only last chunk and cached one are held in memory)
467467
BIGARRAY_CHUNK_SIZE = 1024 * 1024
468468

469+
# Maximum number of socket pre-connects
470+
SOCKET_PRE_CONNECT_QUEUE_SIZE = 3
471+
469472
# Only console display last n table rows
470473
TRIM_STDOUT_DUMP_SIZE = 256
471474

0 commit comments

Comments
 (0)