2010-03-23 01:57:57 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
$Id$
|
|
|
|
|
2011-07-08 00:10:03 +04:00
|
|
|
Copyright (c) 2006-2011 sqlmap developers (http://www.sqlmap.org/)
|
2010-10-15 03:18:29 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
2010-03-23 01:57:57 +03:00
|
|
|
"""
|
|
|
|
|
2010-05-17 00:46:17 +04:00
|
|
|
from lib.core.agent import agent
|
2010-12-22 21:55:50 +03:00
|
|
|
from lib.core.common import arrayizeValue
|
2011-01-28 19:36:09 +03:00
|
|
|
from lib.core.common import Backend
|
2010-05-17 00:46:17 +04:00
|
|
|
from lib.core.common import getRange
|
2011-05-26 12:17:21 +04:00
|
|
|
from lib.core.common import isNoneValue
|
2010-12-18 00:45:20 +03:00
|
|
|
from lib.core.common import isNumPosStrValue
|
2011-01-12 04:13:32 +03:00
|
|
|
from lib.core.common import isTechniqueAvailable
|
2011-03-30 01:54:15 +04:00
|
|
|
from lib.core.common import safeSQLIdentificatorNaming
|
|
|
|
from lib.core.common import unsafeSQLIdentificatorNaming
|
2010-03-23 01:57:57 +03:00
|
|
|
from lib.core.data import conf
|
|
|
|
from lib.core.data import kb
|
|
|
|
from lib.core.data import logger
|
|
|
|
from lib.core.data import queries
|
2010-12-10 16:04:36 +03:00
|
|
|
from lib.core.enums import EXPECTED
|
2011-01-15 13:14:05 +03:00
|
|
|
from lib.core.enums import PAYLOAD
|
2010-03-23 01:57:57 +03:00
|
|
|
from lib.core.exception import sqlmapNoneDataException
|
|
|
|
from lib.request import inject
|
|
|
|
|
|
|
|
from plugins.generic.enumeration import Enumeration as GenericEnumeration
|
|
|
|
|
|
|
|
class Enumeration(GenericEnumeration):
|
|
|
|
def __init__(self):
|
2011-01-14 14:55:20 +03:00
|
|
|
GenericEnumeration.__init__(self)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2010-11-08 15:45:23 +03:00
|
|
|
def getPrivileges(self, *args):
|
2011-02-10 17:24:04 +03:00
|
|
|
warnMsg = "on Microsoft SQL Server it is not possible to fetch "
|
|
|
|
warnMsg += "database users privileges, sqlmap will check whether "
|
|
|
|
warnMsg += "or not the database users are database administrators"
|
2010-03-23 01:57:57 +03:00
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
2011-02-10 17:24:04 +03:00
|
|
|
users = []
|
|
|
|
areAdmins = set()
|
|
|
|
|
|
|
|
if conf.user:
|
|
|
|
users = [ conf.user ]
|
|
|
|
elif not len(kb.data.cachedUsers):
|
|
|
|
users = self.getUsers()
|
|
|
|
else:
|
|
|
|
users = kb.data.cachedUsers
|
|
|
|
|
|
|
|
for user in users:
|
|
|
|
if user is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
isDba = self.isDba(user)
|
|
|
|
|
|
|
|
if isDba is True:
|
|
|
|
areAdmins.add(user)
|
|
|
|
|
|
|
|
kb.data.cachedUsersPrivileges[user] = None
|
|
|
|
|
|
|
|
return ( kb.data.cachedUsersPrivileges, areAdmins )
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
def getTables(self):
|
2011-05-01 02:05:02 +04:00
|
|
|
if len(kb.data.cachedTables) > 0:
|
|
|
|
return kb.data.cachedTables
|
|
|
|
|
|
|
|
self.forceDbmsEnum()
|
|
|
|
|
|
|
|
if conf.db == "CD":
|
|
|
|
conf.db = self.getCurrentDb()
|
|
|
|
|
2010-03-23 01:57:57 +03:00
|
|
|
if conf.db:
|
2011-05-01 02:05:02 +04:00
|
|
|
dbs = conf.db.split(",")
|
|
|
|
else:
|
|
|
|
dbs = self.getDbs()
|
|
|
|
|
|
|
|
for db in dbs:
|
|
|
|
dbs[dbs.index(db)] = safeSQLIdentificatorNaming(db)
|
|
|
|
|
|
|
|
infoMsg = "fetching tables for database"
|
2011-07-28 13:24:37 +04:00
|
|
|
infoMsg += "%s: %s" % ("s" if len(dbs) > 1 else "", ", ".join(db for db in sorted(dbs)))
|
2010-03-23 01:57:57 +03:00
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
rootQuery = queries[Backend.getIdentifiedDbms()].tables
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2011-01-19 02:02:11 +03:00
|
|
|
if isTechniqueAvailable(PAYLOAD.TECHNIQUE.UNION) or isTechniqueAvailable(PAYLOAD.TECHNIQUE.ERROR) or conf.direct:
|
2010-03-23 01:57:57 +03:00
|
|
|
for db in dbs:
|
|
|
|
if conf.excludeSysDbs and db in self.excludeDbsList:
|
|
|
|
infoMsg = "skipping system database '%s'" % db
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
2010-10-21 17:13:12 +04:00
|
|
|
query = rootQuery.inband.query % db
|
2011-01-19 02:02:11 +03:00
|
|
|
value = inject.getValue(query, blind=False)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2011-05-26 12:17:21 +04:00
|
|
|
if not isNoneValue(value):
|
2010-12-22 21:55:50 +03:00
|
|
|
kb.data.cachedTables[db] = arrayizeValue(value)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2010-03-31 14:50:47 +04:00
|
|
|
if not kb.data.cachedTables and not conf.direct:
|
2010-03-23 01:57:57 +03:00
|
|
|
for db in dbs:
|
|
|
|
if conf.excludeSysDbs and db in self.excludeDbsList:
|
|
|
|
infoMsg = "skipping system database '%s'" % db
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
2011-04-30 17:20:05 +04:00
|
|
|
infoMsg = "fetching number of tables for "
|
2010-03-23 01:57:57 +03:00
|
|
|
infoMsg += "database '%s'" % db
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2010-10-21 17:13:12 +04:00
|
|
|
query = rootQuery.blind.count % db
|
2011-01-19 02:02:11 +03:00
|
|
|
count = inject.getValue(query, inband=False, error=False, charsetType=2)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2010-12-18 00:45:20 +03:00
|
|
|
if not isNumPosStrValue(count):
|
2011-04-30 17:20:05 +04:00
|
|
|
warnMsg = "unable to retrieve the number of "
|
2010-03-23 01:57:57 +03:00
|
|
|
warnMsg += "tables for database '%s'" % db
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
continue
|
|
|
|
|
|
|
|
tables = []
|
|
|
|
|
|
|
|
for index in range(int(count)):
|
2010-10-21 17:13:12 +04:00
|
|
|
query = rootQuery.blind.query % (db, index, db)
|
2011-01-19 02:02:11 +03:00
|
|
|
table = inject.getValue(query, inband=False, error=False)
|
2010-04-15 16:09:26 +04:00
|
|
|
kb.hintValue = table
|
2011-05-01 02:05:02 +04:00
|
|
|
table = safeSQLIdentificatorNaming(table, True)
|
|
|
|
tables.append(table)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
if tables:
|
|
|
|
kb.data.cachedTables[db] = tables
|
|
|
|
else:
|
2011-04-30 17:20:05 +04:00
|
|
|
warnMsg = "unable to retrieve the tables "
|
2010-03-23 01:57:57 +03:00
|
|
|
warnMsg += "for database '%s'" % db
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
if not kb.data.cachedTables:
|
|
|
|
errMsg = "unable to retrieve the tables for any database"
|
|
|
|
raise sqlmapNoneDataException(errMsg)
|
|
|
|
|
|
|
|
return kb.data.cachedTables
|
2010-05-17 00:46:17 +04:00
|
|
|
|
|
|
|
def searchTable(self):
|
2011-01-28 19:36:09 +03:00
|
|
|
rootQuery = queries[Backend.getIdentifiedDbms()].search_table
|
2010-05-17 00:46:17 +04:00
|
|
|
foundTbls = {}
|
|
|
|
tblList = conf.tbl.split(",")
|
2010-10-21 17:13:12 +04:00
|
|
|
tblCond = rootQuery.inband.condition
|
|
|
|
dbCond = rootQuery.inband.condition2
|
2010-05-17 00:46:17 +04:00
|
|
|
|
|
|
|
tblConsider, tblCondParam = self.likeOrExact("table")
|
|
|
|
|
|
|
|
if not len(kb.data.cachedDbs):
|
|
|
|
enumDbs = self.getDbs()
|
|
|
|
else:
|
|
|
|
enumDbs = kb.data.cachedDbs
|
|
|
|
|
|
|
|
for db in enumDbs:
|
2011-01-21 00:46:56 +03:00
|
|
|
if isinstance(db, list):
|
|
|
|
db = db[0]
|
|
|
|
|
2011-03-30 01:54:15 +04:00
|
|
|
db = safeSQLIdentificatorNaming(db)
|
2010-05-17 00:46:17 +04:00
|
|
|
foundTbls[db] = []
|
|
|
|
|
|
|
|
for tbl in tblList:
|
2011-03-30 01:54:15 +04:00
|
|
|
tbl = safeSQLIdentificatorNaming(tbl, True)
|
2011-03-28 15:01:55 +04:00
|
|
|
|
2010-05-17 00:46:17 +04:00
|
|
|
infoMsg = "searching table"
|
|
|
|
if tblConsider == "1":
|
|
|
|
infoMsg += "s like"
|
2011-03-30 01:54:15 +04:00
|
|
|
infoMsg += " '%s'" % unsafeSQLIdentificatorNaming(tbl)
|
2010-05-17 00:46:17 +04:00
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
tblQuery = "%s%s" % (tblCond, tblCondParam)
|
2011-03-30 01:54:15 +04:00
|
|
|
tblQuery = tblQuery % unsafeSQLIdentificatorNaming(tbl)
|
2010-05-17 00:46:17 +04:00
|
|
|
|
|
|
|
for db in foundTbls.keys():
|
2011-03-30 01:54:15 +04:00
|
|
|
db = safeSQLIdentificatorNaming(db)
|
2011-03-28 15:01:55 +04:00
|
|
|
|
2010-05-17 20:16:49 +04:00
|
|
|
if conf.excludeSysDbs and db in self.excludeDbsList:
|
|
|
|
infoMsg = "skipping system database '%s'" % db
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
2011-01-19 02:02:11 +03:00
|
|
|
if isTechniqueAvailable(PAYLOAD.TECHNIQUE.UNION) or isTechniqueAvailable(PAYLOAD.TECHNIQUE.ERROR) or conf.direct:
|
2010-11-12 13:11:13 +03:00
|
|
|
query = rootQuery.inband.query % db
|
2010-05-17 00:46:17 +04:00
|
|
|
query += tblQuery
|
2011-01-19 02:02:11 +03:00
|
|
|
values = inject.getValue(query, blind=False)
|
2010-05-17 00:46:17 +04:00
|
|
|
|
2011-05-26 12:17:21 +04:00
|
|
|
if not isNoneValue(values):
|
2010-05-25 14:09:35 +04:00
|
|
|
if isinstance(values, basestring):
|
2010-05-17 00:46:17 +04:00
|
|
|
values = [ values ]
|
|
|
|
|
|
|
|
for foundTbl in values:
|
2011-02-10 14:34:16 +03:00
|
|
|
if foundTbl is None:
|
|
|
|
continue
|
|
|
|
|
2010-05-17 00:46:17 +04:00
|
|
|
foundTbls[db].append(foundTbl)
|
|
|
|
else:
|
|
|
|
infoMsg = "fetching number of table"
|
|
|
|
if tblConsider == "1":
|
|
|
|
infoMsg += "s like"
|
2011-03-30 01:54:15 +04:00
|
|
|
infoMsg += " '%s' in database '%s'" % (unsafeSQLIdentificatorNaming(tbl), unsafeSQLIdentificatorNaming(db))
|
2010-05-17 00:46:17 +04:00
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2010-11-12 13:11:13 +03:00
|
|
|
query = rootQuery.blind.count2
|
2010-05-17 00:46:17 +04:00
|
|
|
query = query % db
|
|
|
|
query += " AND %s" % tblQuery
|
2011-01-19 02:02:11 +03:00
|
|
|
count = inject.getValue(query, inband=False, error=False, expected=EXPECTED.INT, charsetType=2)
|
2010-05-17 00:46:17 +04:00
|
|
|
|
2010-12-18 00:45:20 +03:00
|
|
|
if not isNumPosStrValue(count):
|
2010-05-17 00:46:17 +04:00
|
|
|
warnMsg = "no table"
|
|
|
|
if tblConsider == "1":
|
|
|
|
warnMsg += "s like"
|
2011-03-30 01:54:15 +04:00
|
|
|
warnMsg += " '%s' " % unsafeSQLIdentificatorNaming(tbl)
|
|
|
|
warnMsg += "in database '%s'" % unsafeSQLIdentificatorNaming(db)
|
2010-05-17 00:46:17 +04:00
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
indexRange = getRange(count)
|
|
|
|
|
|
|
|
for index in indexRange:
|
2010-11-12 13:11:13 +03:00
|
|
|
query = rootQuery.blind.query2
|
2010-05-17 00:46:17 +04:00
|
|
|
query = query % db
|
|
|
|
query += " AND %s" % tblQuery
|
|
|
|
query = agent.limitQuery(index, query, tblCond)
|
2011-01-19 02:02:11 +03:00
|
|
|
tbl = inject.getValue(query, inband=False, error=False)
|
2010-05-17 00:46:17 +04:00
|
|
|
kb.hintValue = tbl
|
|
|
|
foundTbls[db].append(tbl)
|
|
|
|
|
|
|
|
for db, tbls in foundTbls.items():
|
|
|
|
if len(tbls) == 0:
|
|
|
|
foundTbls.pop(db)
|
|
|
|
|
|
|
|
return foundTbls
|
2010-05-17 20:16:49 +04:00
|
|
|
|
|
|
|
def searchColumn(self):
|
2011-01-28 19:36:09 +03:00
|
|
|
rootQuery = queries[Backend.getIdentifiedDbms()].search_column
|
2010-05-17 20:16:49 +04:00
|
|
|
foundCols = {}
|
|
|
|
dbs = {}
|
|
|
|
colList = conf.col.split(",")
|
2010-10-21 17:13:12 +04:00
|
|
|
colCond = rootQuery.inband.condition
|
2010-05-17 20:16:49 +04:00
|
|
|
colConsider, colCondParam = self.likeOrExact("column")
|
|
|
|
|
|
|
|
if not len(kb.data.cachedDbs):
|
|
|
|
enumDbs = self.getDbs()
|
|
|
|
else:
|
|
|
|
enumDbs = kb.data.cachedDbs
|
|
|
|
|
|
|
|
for db in enumDbs:
|
2011-03-30 01:54:15 +04:00
|
|
|
db = safeSQLIdentificatorNaming(db)
|
2010-05-17 20:16:49 +04:00
|
|
|
dbs[db] = {}
|
|
|
|
|
|
|
|
for column in colList:
|
2011-03-30 01:54:15 +04:00
|
|
|
column = safeSQLIdentificatorNaming(column)
|
2011-03-28 15:01:55 +04:00
|
|
|
|
2010-05-17 20:16:49 +04:00
|
|
|
infoMsg = "searching column"
|
|
|
|
if colConsider == "1":
|
|
|
|
infoMsg += "s like"
|
2011-03-30 01:54:15 +04:00
|
|
|
infoMsg += " '%s'" % unsafeSQLIdentificatorNaming(column)
|
2010-05-17 20:16:49 +04:00
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
foundCols[column] = {}
|
|
|
|
|
|
|
|
colQuery = "%s%s" % (colCond, colCondParam)
|
2011-03-30 01:54:15 +04:00
|
|
|
colQuery = colQuery % unsafeSQLIdentificatorNaming(column)
|
2010-05-17 20:16:49 +04:00
|
|
|
|
|
|
|
for db in dbs.keys():
|
2011-03-30 01:54:15 +04:00
|
|
|
db = safeSQLIdentificatorNaming(db)
|
2011-03-28 15:01:55 +04:00
|
|
|
|
2010-05-17 20:16:49 +04:00
|
|
|
if conf.excludeSysDbs and db in self.excludeDbsList:
|
|
|
|
infoMsg = "skipping system database '%s'" % db
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
2011-01-19 02:02:11 +03:00
|
|
|
if isTechniqueAvailable(PAYLOAD.TECHNIQUE.UNION) or isTechniqueAvailable(PAYLOAD.TECHNIQUE.ERROR) or conf.direct:
|
2011-03-28 15:01:55 +04:00
|
|
|
query = rootQuery.inband.query % (db, db, db, db, db, db)
|
2010-05-17 20:16:49 +04:00
|
|
|
query += " AND %s" % colQuery.replace("[DB]", db)
|
2011-01-19 02:02:11 +03:00
|
|
|
values = inject.getValue(query, blind=False)
|
2010-05-17 20:16:49 +04:00
|
|
|
|
2011-05-26 12:17:21 +04:00
|
|
|
if not isNoneValue(values):
|
2010-05-25 14:09:35 +04:00
|
|
|
if isinstance(values, basestring):
|
2010-05-17 20:16:49 +04:00
|
|
|
values = [ values ]
|
|
|
|
|
|
|
|
for foundTbl in values:
|
2011-03-30 01:54:15 +04:00
|
|
|
foundTbl = safeSQLIdentificatorNaming(foundTbl, True)
|
2011-03-28 15:01:55 +04:00
|
|
|
|
2011-02-10 14:28:24 +03:00
|
|
|
if foundTbl is None:
|
|
|
|
continue
|
|
|
|
|
2010-05-17 20:16:49 +04:00
|
|
|
if foundTbl not in dbs[db]:
|
|
|
|
dbs[db][foundTbl] = {}
|
|
|
|
|
|
|
|
if colConsider == "1":
|
|
|
|
conf.db = db
|
|
|
|
conf.tbl = foundTbl
|
|
|
|
conf.col = column
|
|
|
|
|
|
|
|
self.getColumns(onlyColNames=True)
|
2011-06-16 18:27:44 +04:00
|
|
|
|
|
|
|
if db in kb.data.cachedColumns and foundTbl in kb.data.cachedColumns[db]\
|
|
|
|
and not isNoneValue(kb.data.cachedColumns[db][foundTbl]):
|
2011-03-28 15:01:55 +04:00
|
|
|
dbs[db][foundTbl].update(kb.data.cachedColumns[db][foundTbl])
|
2010-05-17 20:16:49 +04:00
|
|
|
kb.data.cachedColumns = {}
|
|
|
|
else:
|
|
|
|
dbs[db][foundTbl][column] = None
|
|
|
|
|
|
|
|
if db in foundCols[column]:
|
|
|
|
foundCols[column][db].append(foundTbl)
|
|
|
|
else:
|
|
|
|
foundCols[column][db] = [ foundTbl ]
|
|
|
|
else:
|
|
|
|
foundCols[column][db] = []
|
|
|
|
|
|
|
|
infoMsg = "fetching number of tables containing column"
|
|
|
|
if colConsider == "1":
|
|
|
|
infoMsg += "s like"
|
|
|
|
infoMsg += " '%s' in database '%s'" % (column, db)
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2010-11-12 13:11:13 +03:00
|
|
|
query = rootQuery.blind.count2
|
2011-03-28 15:01:55 +04:00
|
|
|
query = query % (db, db, db, db, db, db)
|
2010-05-17 20:16:49 +04:00
|
|
|
query += " AND %s" % colQuery.replace("[DB]", db)
|
2011-01-19 02:02:11 +03:00
|
|
|
count = inject.getValue(query, inband=False, error=False, expected=EXPECTED.INT, charsetType=2)
|
2010-05-17 20:16:49 +04:00
|
|
|
|
2010-12-18 00:45:20 +03:00
|
|
|
if not isNumPosStrValue(count):
|
2010-05-17 20:16:49 +04:00
|
|
|
warnMsg = "no tables contain column"
|
|
|
|
if colConsider == "1":
|
|
|
|
warnMsg += "s like"
|
|
|
|
warnMsg += " '%s' " % column
|
|
|
|
warnMsg += "in database '%s'" % db
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
indexRange = getRange(count)
|
|
|
|
|
|
|
|
for index in indexRange:
|
2010-11-12 13:11:13 +03:00
|
|
|
query = rootQuery.blind.query2
|
2011-03-28 15:01:55 +04:00
|
|
|
query = query % (db, db, db, db, db, db)
|
2010-05-17 20:16:49 +04:00
|
|
|
query += " AND %s" % colQuery.replace("[DB]", db)
|
|
|
|
query = agent.limitQuery(index, query, colCond.replace("[DB]", db))
|
2011-01-19 02:02:11 +03:00
|
|
|
tbl = inject.getValue(query, inband=False, error=False)
|
2010-05-17 20:16:49 +04:00
|
|
|
kb.hintValue = tbl
|
|
|
|
|
2011-03-30 01:54:15 +04:00
|
|
|
tbl = safeSQLIdentificatorNaming(tbl, True)
|
2011-03-28 15:01:55 +04:00
|
|
|
|
2010-05-17 20:16:49 +04:00
|
|
|
if tbl not in dbs[db]:
|
|
|
|
dbs[db][tbl] = {}
|
|
|
|
|
|
|
|
if colConsider == "1":
|
|
|
|
conf.db = db
|
|
|
|
conf.tbl = tbl
|
|
|
|
conf.col = column
|
|
|
|
|
|
|
|
self.getColumns(onlyColNames=True)
|
|
|
|
|
2011-06-16 18:27:44 +04:00
|
|
|
if db in kb.data.cachedColumns and tbl in kb.data.cachedColumns[db]:
|
|
|
|
dbs[db][tbl].update(kb.data.cachedColumns[db][tbl])
|
2010-05-17 20:16:49 +04:00
|
|
|
kb.data.cachedColumns = {}
|
|
|
|
else:
|
|
|
|
dbs[db][tbl][column] = None
|
|
|
|
|
|
|
|
foundCols[column][db].append(tbl)
|
|
|
|
|
|
|
|
self.dumpFoundColumn(dbs, foundCols, colConsider)
|