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

Skip to content

Commit 64bb57d

Browse files
committed
Minor bug fix to make the Partial UNION query SQL injection technique
work properly also on Oracle and Microsoft SQL Server.
1 parent 1f7810e commit 64bb57d

5 files changed

Lines changed: 24 additions & 19 deletions

File tree

doc/ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ sqlmap (0.6.4-1) stable; urgency=low
1717
--is-dba query at the moment;
1818
* Major bug fix to avoid tracebacks when multiple targets are specified
1919
and one of them is not reachable;
20+
* Minor bug fix to make the Partial UNION query SQL injection technique
21+
work properly also on Oracle and Microsoft SQL Server;
2022
* Minor bug fix to make the --postfix work even if --prefix is not
2123
provided;
2224

lib/core/agent.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,6 @@ def forgeInbandQuery(self, query, exprPosition=None):
416416
conditionIndex = query.index(" FROM ")
417417
inbandQuery += query[conditionIndex:]
418418

419-
if " ORDER BY " in inbandQuery and "(SELECT " in inbandQuery:
420-
orderIndex = inbandQuery.index(" ORDER BY ")
421-
inbandQuery += inbandQuery[orderIndex:].replace(")", "")
422-
423419
if kb.dbms == "Oracle":
424420
if " FROM " not in inbandQuery:
425421
inbandQuery += " FROM DUAL"
@@ -461,19 +457,20 @@ def limitQuery(self, num, query, field):
461457
limitStr = queries[kb.dbms].limit % (num, 1)
462458
limitedQuery += " %s" % limitStr
463459

464-
# TODO: fix Partial UNION query SQL injection technique for Oracle
465460
elif kb.dbms == "Oracle":
461+
if " ORDER BY " in limitedQuery and "(SELECT " in limitedQuery:
462+
limitedQuery = limitedQuery[:limitedQuery.index(" ORDER BY ")]
463+
466464
if query.startswith("SELECT "):
467465
limitedQuery = "%s FROM (%s, %s" % (untilFrom, untilFrom, limitStr)
468466
else:
469-
limitedQuery = "%s FROM (SELECT %s, %s" % (untilFrom, field, limitStr)
467+
limitedQuery = "%s FROM (SELECT %s, %s" % (untilFrom, ", ".join(f for f in field), limitStr)
470468
limitedQuery = limitedQuery % fromFrom
471469
limitedQuery += "=%d" % (num + 1)
472470

473471
elif kb.dbms == "Microsoft SQL Server":
474-
if re.search(" ORDER BY ", limitedQuery, re.I):
475-
untilOrderChar = limitedQuery.index(" ORDER BY ")
476-
limitedQuery = limitedQuery[:untilOrderChar]
472+
if " ORDER BY " in limitedQuery:
473+
limitedQuery = limitedQuery[:limitedQuery.index(" ORDER BY ")]
477474

478475
limitedQuery = limitedQuery.replace("SELECT ", (limitStr % 1), 1)
479476
limitedQuery = "%s WHERE %s " % (limitedQuery, field)

lib/core/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"SQL SELECT statement": (
7575
"select ",
7676
" from ",
77+
" from dual",
7778
" where ",
7879
" group by ",
7980
" order by ",

lib/request/inject.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,7 @@ def __goInferenceFields(expression, expressionFields, expressionFieldsList, payl
8282
expression = agent.limitQuery(num, expression, field)
8383

8484
expressionReplaced = expression.replace(expressionFields, field, 1)
85-
86-
if " ORDER BY " in expressionReplaced and "(SELECT " in expressionReplaced:
87-
orderIndex = expressionReplaced.index(" ORDER BY ")
88-
expressionReplaced += expressionReplaced[orderIndex:].replace(")", "")
89-
90-
output = resume(expressionReplaced, payload)
85+
output = resume(expressionReplaced, payload)
9186

9287
if not output or ( expected == "int" and not output.isdigit() ):
9388
if output:
@@ -326,6 +321,9 @@ def getValue(expression, blind=True, inband=True, fromUser=False, expected=None)
326321
value = None
327322

328323
if inband and conf.unionUse and kb.dbms:
324+
if kb.dbms == "Oracle" and " ORDER BY " in expression:
325+
expression = expression[:expression.index(" ORDER BY ")]
326+
329327
value = __goInband(expression, expected)
330328

331329
if not value:

lib/techniques/inband/union/use.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,19 @@ def unionUse(expression, direct=False, unescape=True, resetCounter=False):
261261
return
262262

263263
for num in xrange(startLimit, stopLimit):
264-
orderBy = re.search(" ORDER BY ([\w\_]+)", expression, re.I)
264+
if kb.dbms == "Microsoft SQL Server":
265+
orderBy = re.search(" ORDER BY ([\w\_]+)", expression, re.I)
266+
267+
if orderBy:
268+
field = orderBy.group(1)
269+
else:
270+
field = expressionFieldsList[0]
271+
272+
elif kb.dbms == "Oracle":
273+
field = expressionFieldsList
265274

266-
if orderBy:
267-
field = orderBy.group(1)
268275
else:
269-
field = expressionFieldsList[0]
276+
field = None
270277

271278
limitedExpr = agent.limitQuery(num, expression, field)
272279
output = unionUse(limitedExpr, direct=True, unescape=False)

0 commit comments

Comments
 (0)