Skip to content

Commit 770e000

Browse files
committed
Fixed another bug on Microsoft SQL Server custom "limited" query reported by Konrads Smelkovs
1 parent 9ab174a commit 770e000

File tree

4 files changed

+36
-19
lines changed

4 files changed

+36
-19
lines changed

doc/THANKS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ M Simkin <mlsimkin@cox.net>
127127
for suggesting a feature
128128

129129
Konrads Smelkovs <konrads@smelkovs.com>
130-
for reporting two bugs in --sql-shell and --sql-query
130+
for reporting a few bugs in --sql-shell and --sql-query on Microsoft
131+
SQL Server
131132

132133
Jason Swan <jasoneswan@gmail.com>
133134
for reporting a bug when enumerating columns on Microsoft SQL Server

lib/core/agent.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -471,23 +471,33 @@ def limitQuery(self, num, query, field):
471471
limitedQuery += "=%d" % (num + 1)
472472

473473
elif kb.dbms == "Microsoft SQL Server":
474+
forgeNotIn = True
475+
474476
if " ORDER BY " in limitedQuery:
475477
limitedQuery = limitedQuery[:limitedQuery.index(" ORDER BY ")]
476478

477-
if not limitedQuery.startswith("SELECT TOP ") and not limitedQuery.startswith("TOP "):
479+
if limitedQuery.startswith("SELECT TOP ") or limitedQuery.startswith("TOP "):
480+
topNums = re.search(queries[kb.dbms].limitregexp, limitedQuery, re.I)
481+
482+
if topNums:
483+
topNums = topNums.groups()
484+
quantityTopNums = topNums[0]
485+
limitedQuery = limitedQuery.replace("TOP %s" % quantityTopNums, "TOP 1", 1)
486+
startTopNums = topNums[1]
487+
limitedQuery = limitedQuery.replace(" (SELECT TOP %s" % startTopNums, " (SELECT TOP %d" % num)
488+
forgeNotIn = False
489+
else:
490+
topNum = re.search("TOP\s+([\d]+)\s+", limitedQuery, re.I).group(1)
491+
limitedQuery = limitedQuery.replace("TOP %s " % topNum, "")
492+
493+
if forgeNotIn == True:
478494
limitedQuery = limitedQuery.replace("SELECT ", (limitStr % 1), 1)
479495
if " WHERE " in limitedQuery:
480496
limitedQuery = "%s AND %s " % (limitedQuery, field)
481497
else:
482498
limitedQuery = "%s WHERE %s " % (limitedQuery, field)
483499
limitedQuery += "NOT IN (%s" % (limitStr % num)
484500
limitedQuery += "%s %s)" % (field, fromFrom)
485-
else:
486-
topNums = re.search("TOP\s+([\d]+)\s+.+?\s+FROM\s+.+?\s+WHERE\s+.+?\s+NOT\s+IN\s+\(SELECT\s+TOP\s+([\d]+)\s+", limitedQuery, re.I).groups()
487-
quantityTopNums = topNums[0]
488-
limitedQuery = limitedQuery.replace("TOP %s" % quantityTopNums, "TOP 1", 1)
489-
startTopNums = topNums[1]
490-
limitedQuery = limitedQuery.replace(" (SELECT TOP %s" % startTopNums, " (SELECT TOP %d" % num)
491501

492502
return limitedQuery
493503

lib/core/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131

3232
# sqlmap version and site
33-
VERSION = "0.6.4-rc5"
33+
VERSION = "0.6.4-rc6"
3434
VERSION_STRING = "sqlmap/%s" % VERSION
3535
SITE = "http://sqlmap.sourceforge.net"
3636

@@ -73,7 +73,7 @@
7373
SQL_STATEMENTS = {
7474
"SQL SELECT statement": (
7575
"select ",
76-
"select top ",
76+
" top ",
7777
" from ",
7878
" from dual",
7979
" where ",

lib/request/inject.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ def __goInferenceProxy(expression, fromUser=False, expected=None):
147147
# can return multiple entries
148148
if fromUser and " FROM " in expression:
149149
limitRegExp = re.search(queries[kb.dbms].limitregexp, expression, re.I)
150+
topLimit = re.search("TOP\s+([\d]+)\s+", expression, re.I)
150151

151-
if limitRegExp:
152+
if limitRegExp or ( kb.dbms == "Microsoft SQL Server" and topLimit ):
152153
if kb.dbms in ( "MySQL", "PostgreSQL" ):
153154
limitGroupStart = queries[kb.dbms].limitgroupstart
154155
limitGroupStop = queries[kb.dbms].limitgroupstop
@@ -160,14 +161,19 @@ def __goInferenceProxy(expression, fromUser=False, expected=None):
160161
limitCond = int(stopLimit) > 1
161162

162163
elif kb.dbms == "Microsoft SQL Server":
163-
limitGroupStart = queries[kb.dbms].limitgroupstart
164-
limitGroupStop = queries[kb.dbms].limitgroupstop
165-
166-
if limitGroupStart.isdigit():
167-
startLimit = int(limitRegExp.group(int(limitGroupStart)))
168-
169-
stopLimit = limitRegExp.group(int(limitGroupStop))
170-
limitCond = int(stopLimit) > 1
164+
if limitRegExp:
165+
limitGroupStart = queries[kb.dbms].limitgroupstart
166+
limitGroupStop = queries[kb.dbms].limitgroupstop
167+
168+
if limitGroupStart.isdigit():
169+
startLimit = int(limitRegExp.group(int(limitGroupStart)))
170+
171+
stopLimit = limitRegExp.group(int(limitGroupStop))
172+
limitCond = int(stopLimit) > 1
173+
elif topLimit:
174+
startLimit = 0
175+
stopLimit = int(topLimit.group(1))
176+
limitCond = int(stopLimit) > 1
171177

172178
elif kb.dbms == "Oracle":
173179
limitCond = False

0 commit comments

Comments
 (0)