Skip to content

Commit e1aac02

Browse files
committed
More improvements for JSON_AGG retrieval
1 parent 35fefc3 commit e1aac02

3 files changed

Lines changed: 41 additions & 16 deletions

File tree

data/txt/sha256sums.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ ccc4a717e887652b1fcce073d9409d9c59a3b28548c703a9e453d15845f90cd7 lib/core/patch
189189
48797d6c34dd9bb8a53f7f3794c85f4288d82a9a1d6be7fcf317d388cb20d4b3 lib/core/replication.py
190190
0b8c38a01bb01f843d94a6c5f2075ee47520d0c4aa799cecea9c3e2c5a4a23a6 lib/core/revision.py
191191
888daba83fd4a34e9503fe21f01fef4cc730e5cde871b1d40e15d4cbc847d56c lib/core/session.py
192-
f75f15165173becddf439996a85f011262178e1bf5d2d2bf8028455b7ff3ff94 lib/core/settings.py
192+
f79f96c5f073b663cc494c57b9641dc41e7ed13a28d5cec62bb9ca8904110d9c lib/core/settings.py
193193
cd5a66deee8963ba8e7e9af3dd36eb5e8127d4d68698811c29e789655f507f82 lib/core/shell.py
194194
bcb5d8090d5e3e0ef2a586ba09ba80eef0c6d51feb0f611ed25299fbb254f725 lib/core/subprocessng.py
195195
70ea3768f1b3062b22d20644df41c86238157ec80dd43da40545c620714273c6 lib/core/target.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-
3f834b877f0fb684e402d07af1d8a7c7d0cdb4c0a3f9f15fe8488a08d88db4f2 lib/techniques/union/use.py
244+
c65766f71e285fc85cdf58e7448c4c1d015af2a9dbb44fa3b665a9f13362fbcc 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/settings.py

Lines changed: 1 addition & 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.131"
23+
VERSION = "1.10.6.132"
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)

lib/techniques/union/use.py

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,37 @@ def _chunkedJsonAggUse(expression, expressionFields, expressionFieldsList, count
255255
caps. K is halved adaptively if a chunk response still gets truncated. Returns a BigArray of
256256
rows, or None to let the caller fall back to the regular per-row UNION path.
257257
258-
NOTE: MySQL only for now (windowed 'LIMIT offset,K' + JSON_ARRAYAGG); other DBMSes return None.
258+
Same DBMS coverage as the single-shot JSON-agg (per-DBMS aggregate + windowing); others -> None.
259259
"""
260-
if not Backend.isDbms(DBMS.MYSQL) or not expressionFields or not expressionFieldsList:
260+
dbms = Backend.getIdentifiedDbms()
261+
262+
if dbms not in (DBMS.MYSQL, DBMS.PGSQL, DBMS.SQLITE, DBMS.H2, DBMS.HSQLDB, DBMS.FIREBIRD) or not expressionFields or not expressionFieldsList:
261263
return None
262264

265+
start, stop, delimiter = kb.chars.start, kb.chars.stop, kb.chars.delimiter
266+
263267
# a stable total ordering (all output columns) so the LIMIT/OFFSET windows never overlap or drop rows
264268
base = re.sub(r"(?i)\s+ORDER BY\s+.+\Z", "", expression)
265269
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))
270+
nulled = [agent.nullAndCastField(_) for _ in expressionFieldsList]
271+
272+
# per-DBMS: aggregate-over-windowed-columns expression (mirrors the single-shot branches) plus
273+
# the "K rows at offset" window clause appended to the inner derived table
274+
if dbms == DBMS.MYSQL:
275+
aggExpr = "CONCAT('%s',JSON_ARRAYAGG(CONCAT_WS('%s',%s)),'%s')" % (start, delimiter, ','.join(nulled), stop)
276+
window = lambda o, k: "%s LIMIT %d,%d" % (orderBy, o, k)
277+
elif dbms == DBMS.PGSQL:
278+
aggExpr = "STRING_AGG('%s'||%s||'%s','')" % (start, ("||'%s'||" % delimiter).join("COALESCE(%s::text,' ')" % _ for _ in expressionFieldsList), stop)
279+
window = lambda o, k: "%s LIMIT %d OFFSET %d" % (orderBy, k, o)
280+
elif dbms == DBMS.SQLITE:
281+
aggExpr = "'%s'||JSON_GROUP_ARRAY(%s)||'%s'" % (start, ("||'%s'||" % delimiter).join("COALESCE(%s,' ')" % _ for _ in expressionFieldsList), stop)
282+
window = lambda o, k: "%s LIMIT %d OFFSET %d" % (orderBy, k, o)
283+
elif dbms in (DBMS.H2, DBMS.HSQLDB):
284+
aggExpr = "GROUP_CONCAT('%s'||%s||'%s' SEPARATOR '')" % (start, ("||'%s'||" % delimiter).join(nulled), stop)
285+
window = lambda o, k: "%s LIMIT %d OFFSET %d" % (orderBy, k, o)
286+
elif dbms == DBMS.FIREBIRD:
287+
aggExpr = "LIST('%s'||%s||'%s','')" % (start, ("||'%s'||" % delimiter).join(nulled), stop)
288+
window = lambda o, k: "%s ROWS %d TO %d" % (orderBy, o + 1, o + k)
267289

268290
debugMsg = "single-shot UNION dump output was too large; switching to "
269291
debugMsg += "chunked (windowed) JSON aggregation of %d entries" % count
@@ -274,8 +296,7 @@ def _chunkedJsonAggUse(expression, expressionFields, expressionFieldsList, count
274296
offset = 0
275297

276298
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)
299+
query = "SELECT %s FROM (%s %s) sqmapx" % (aggExpr, base, window(offset, chunk))
279300

280301
kb.jsonAggMode = True
281302
output = _oneShotUnionUse(query, False)
@@ -348,6 +369,18 @@ def unionUse(expression, unpack=True, dump=False):
348369
value = parseUnionPage(output)
349370
kb.jsonAggMode = False
350371

372+
# If the single-shot aggregate failed (typically too large for the DBMS packet limit /
373+
# response cap) and the table is large, retrieve the rows in bounded windows (chunked
374+
# JSON aggregation) before the slow per-row fallback. Done here (independent of the
375+
# detected UNION where-clause) so it engages for any dumpable FROM-table query.
376+
if value is None and " FROM " in expression.upper() and not re.search(SQL_SCALAR_REGEX, expression, re.I) and not any((kb.forcePartialUnion, conf.forcePartial, conf.disableJson, conf.binaryFields, conf.limitStart, conf.limitStop)):
377+
chunkCountExpr = expression.replace(expressionFields, queries[Backend.getIdentifiedDbms()].count.query % '*', 1)
378+
if " ORDER BY " in chunkCountExpr.upper():
379+
chunkCountExpr = chunkCountExpr[:chunkCountExpr.upper().rindex(" ORDER BY ")]
380+
chunkCount = unArrayizeValue(parseUnionPage(_oneShotUnionUse(chunkCountExpr, unpack)))
381+
if isNumPosStrValue(chunkCount) and (int(chunkCount) >= JSON_AGG_CHUNK_ROWS or kb.respTruncated):
382+
value = _chunkedJsonAggUse(expression, expressionFields, expressionFieldsList, int(chunkCount))
383+
351384
# We have to check if the SQL query might return multiple entries
352385
# if the technique is partial UNION query and in such case forge the
353386
# SQL limiting the query output one entry at a time
@@ -398,14 +431,6 @@ def unionUse(expression, unpack=True, dump=False):
398431
return value
399432

400433
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-
409434
threadData = getCurrentThreadData()
410435

411436
try:

0 commit comments

Comments
 (0)