Skip to content

Commit d05bfdd

Browse files
committed
Implementing option '--where' (Issue sqlmapproject#605)
1 parent be6767b commit d05bfdd

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed

lib/core/optiondict.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
"tbl": "string",
130130
"col": "string",
131131
"excludeCol": "string",
132+
"dumpWhere": "string",
132133
"user": "string",
133134
"excludeSysDbs": "boolean",
134135
"limitStart": "integer",

lib/parse/cmdline.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,9 @@ def cmdLineParser():
424424
help="Exclude DBMS system databases when "
425425
"enumerating tables")
426426

427+
enumeration.add_option("--where", dest="dumpWhere",
428+
help="Use WHERE condition while table dumping")
429+
427430
enumeration.add_option("--start", dest="limitStart", type="int",
428431
help="First query output entry to retrieve")
429432

lib/utils/pivotdumptable.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def pivotDumpTable(table, colList, count=None, blind=True):
3838

3939
if count is None:
4040
query = dumpNode.count % table
41+
query = whereQuery(query)
4142
count = inject.getValue(query, union=False, error=False, expected=EXPECTED.INT, charsetType=CHARSET_TYPE.DIGITS) if blind else inject.getValue(query, blind=False, time=False, expected=EXPECTED.INT)
4243

4344
if isinstance(count, basestring) and count.isdigit():
@@ -83,6 +84,7 @@ def pivotDumpTable(table, colList, count=None, blind=True):
8384
logger.info(infoMsg)
8485

8586
query = dumpNode.count2 % (column, table)
87+
query = whereQuery(query)
8688
value = inject.getValue(query, blind=blind, union=not blind, error=not blind, expected=EXPECTED.INT, charsetType=CHARSET_TYPE.DIGITS)
8789

8890
if isNumPosStrValue(value):
@@ -122,6 +124,8 @@ def _(pivotValue):
122124
else:
123125
query = dumpNode.query2.replace("'%s'", "%s") % (agent.preprocessField(table, column), table, agent.preprocessField(table, colList[0]), unescaper.escape(pivotValue, False))
124126

127+
query = whereQuery(query)
128+
125129
return unArrayizeValue(inject.getValue(query, blind=blind, time=blind, union=not blind, error=not blind))
126130

127131
value = _(pivotValue)
@@ -163,3 +167,18 @@ def _(pivotValue):
163167
logger.critical(errMsg)
164168

165169
return entries, lengths
170+
171+
def whereQuery(query):
172+
if conf.dumpWhere and query:
173+
prefix, suffix = query.split(" ORDER BY ") if " ORDER BY " in query else (query, "")
174+
175+
if "%s)" % conf.tbl.upper() in prefix.upper():
176+
prefix = re.sub(r"(?i)%s\)" % conf.tbl, "%s WHERE %s)" % (conf.tbl, conf.dumpWhere), prefix)
177+
elif re.search(r"(?i)\bWHERE\b", prefix):
178+
prefix += " AND %s" % conf.dumpWhere
179+
else:
180+
prefix += " WHERE %s" % conf.dumpWhere
181+
182+
query = "%s ORDER BY %s" % (prefix, suffix) if suffix else prefix
183+
184+
return query

plugins/generic/entries.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from lib.request import inject
4343
from lib.utils.hash import attackDumpedTable
4444
from lib.utils.pivotdumptable import pivotDumpTable
45+
from lib.utils.pivotdumptable import whereQuery
4546

4647
class Entries:
4748
"""
@@ -175,6 +176,8 @@ def dumpTable(self, foundData=None):
175176
else:
176177
query = rootQuery.inband.query % (colString, conf.db, tbl)
177178

179+
query = whereQuery(query)
180+
178181
if not entries and query:
179182
entries = inject.getValue(query, blind=False, time=False, dump=True)
180183

@@ -226,6 +229,8 @@ def dumpTable(self, foundData=None):
226229
else:
227230
query = rootQuery.blind.count % (conf.db, tbl)
228231

232+
query = whereQuery(query)
233+
229234
count = inject.getValue(query, union=False, error=False, expected=EXPECTED.INT, charsetType=CHARSET_TYPE.DIGITS)
230235

231236
lengths = {}
@@ -300,6 +305,8 @@ def dumpTable(self, foundData=None):
300305
elif Backend.isDbms(DBMS.FIREBIRD):
301306
query = rootQuery.blind.query % (index, agent.preprocessField(tbl, column), tbl)
302307

308+
query = whereQuery(query)
309+
303310
value = NULL if column in emptyColumns else inject.getValue(query, union=False, error=False, dump=True)
304311
value = '' if value is None else value
305312

sqlmap.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,9 @@ col =
458458
# Back-end database management system database table column(s) to not enumerate.
459459
excludeCol =
460460

461+
# Use WHERE condition while table dumping (e.g. "id=1").
462+
dumpWhere =
463+
461464
# Back-end database management system database user to enumerate.
462465
user =
463466

0 commit comments

Comments
 (0)