2010-10-13 22:55:17 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
2010-10-14 18:41:14 +04:00
|
|
|
$Id$
|
2010-10-13 22:55:17 +04:00
|
|
|
|
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-10-13 22:55:17 +04:00
|
|
|
"""
|
|
|
|
|
2011-02-19 21:04:27 +03:00
|
|
|
from lib.core.common import Backend
|
2011-08-09 18:20:25 +04:00
|
|
|
from lib.core.common import filterPairValues
|
2011-02-19 21:04:27 +03:00
|
|
|
from lib.core.common import isTechniqueAvailable
|
|
|
|
from lib.core.common import randomStr
|
2011-05-01 02:11:36 +04:00
|
|
|
from lib.core.common import safeSQLIdentificatorNaming
|
|
|
|
from lib.core.common import unsafeSQLIdentificatorNaming
|
2011-02-19 21:04:27 +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
|
|
|
|
from lib.core.dicts import sybaseTypes
|
|
|
|
from lib.core.enums import PAYLOAD
|
2011-02-21 01:45:23 +03:00
|
|
|
from lib.core.exception import sqlmapMissingMandatoryOptionException
|
2011-05-01 02:11:36 +04:00
|
|
|
from lib.core.exception import sqlmapNoneDataException
|
2010-10-13 22:55:17 +04:00
|
|
|
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)
|
2011-02-19 21:04:27 +03:00
|
|
|
|
|
|
|
def getUsers(self):
|
|
|
|
infoMsg = "fetching database users"
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
rootQuery = queries[Backend.getIdentifiedDbms()].users
|
|
|
|
|
|
|
|
randStr = randomStr()
|
|
|
|
query = rootQuery.inband.query
|
|
|
|
|
|
|
|
if isTechniqueAvailable(PAYLOAD.TECHNIQUE.UNION) or isTechniqueAvailable(PAYLOAD.TECHNIQUE.ERROR) or conf.direct:
|
|
|
|
blinds = [False, True]
|
|
|
|
else:
|
|
|
|
blinds = [True]
|
|
|
|
|
|
|
|
for blind in blinds:
|
|
|
|
retVal = self.__pivotDumpTable("(%s) AS %s" % (query, randStr), ['%s.name' % randStr], blind=blind)
|
|
|
|
|
|
|
|
if retVal:
|
|
|
|
kb.data.cachedUsers = retVal[0].values()[0]
|
|
|
|
break
|
|
|
|
|
|
|
|
return kb.data.cachedUsers
|
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
def getPrivileges(self, *args):
|
|
|
|
warnMsg = "on Sybase it is not possible to fetch "
|
|
|
|
warnMsg += "database users privileges, sqlmap will check whether "
|
|
|
|
warnMsg += "or not the database users are database administrators"
|
|
|
|
logger.warn(warnMsg)
|
2011-04-29 04:02:28 +04:00
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
users = []
|
|
|
|
areAdmins = set()
|
2011-02-21 01:45:23 +03:00
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
if conf.user:
|
|
|
|
users = [ conf.user ]
|
|
|
|
elif not len(kb.data.cachedUsers):
|
|
|
|
users = self.getUsers()
|
|
|
|
else:
|
|
|
|
users = kb.data.cachedUsers
|
2011-02-19 21:04:27 +03:00
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
for user in users:
|
|
|
|
if user is None:
|
|
|
|
continue
|
2011-02-19 21:04:27 +03:00
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
isDba = self.isDba(user)
|
2011-02-19 21:04:27 +03:00
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
if isDba is True:
|
|
|
|
areAdmins.add(user)
|
|
|
|
|
|
|
|
kb.data.cachedUsersPrivileges[user] = None
|
|
|
|
|
|
|
|
return ( kb.data.cachedUsersPrivileges, areAdmins )
|
2011-02-19 21:04:27 +03:00
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
def getDbs(self):
|
|
|
|
if len(kb.data.cachedDbs) > 0:
|
|
|
|
return kb.data.cachedDbs
|
|
|
|
|
|
|
|
infoMsg = "fetching database names"
|
2011-02-19 21:04:27 +03:00
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
rootQuery = queries[Backend.getIdentifiedDbms()].dbs
|
|
|
|
randStr = randomStr()
|
|
|
|
query = rootQuery.inband.query
|
|
|
|
|
2011-02-19 21:04:27 +03:00
|
|
|
if isTechniqueAvailable(PAYLOAD.TECHNIQUE.UNION) or isTechniqueAvailable(PAYLOAD.TECHNIQUE.ERROR) or conf.direct:
|
|
|
|
blinds = [False, True]
|
|
|
|
else:
|
|
|
|
blinds = [True]
|
|
|
|
|
|
|
|
for blind in blinds:
|
2011-05-01 02:11:36 +04:00
|
|
|
retVal = self.__pivotDumpTable("(%s) AS %s" % (query, randStr), ['%s.name' % randStr], blind=blind)
|
2011-02-19 21:04:27 +03:00
|
|
|
|
|
|
|
if retVal:
|
2011-05-01 02:11:36 +04:00
|
|
|
kb.data.cachedDbs = retVal[0].values()[0]
|
2011-02-19 21:04:27 +03:00
|
|
|
break
|
|
|
|
|
2011-10-28 17:16:22 +04:00
|
|
|
if kb.data.cachedDbs:
|
|
|
|
kb.data.cachedDbs.sort()
|
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
return kb.data.cachedDbs
|
2011-02-19 21:36:26 +03:00
|
|
|
|
|
|
|
def getTables(self, bruteForce=None):
|
2011-05-01 02:11:36 +04:00
|
|
|
if len(kb.data.cachedTables) > 0:
|
|
|
|
return kb.data.cachedTables
|
2011-02-19 21:36:26 +03:00
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
self.forceDbmsEnum()
|
2011-02-19 21:36:26 +03:00
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
if conf.db == "CD":
|
|
|
|
conf.db = self.getCurrentDb()
|
2011-02-19 21:36:26 +03:00
|
|
|
|
|
|
|
if conf.db:
|
2011-05-01 02:11:36 +04:00
|
|
|
dbs = conf.db.split(",")
|
2011-02-19 21:36:26 +03:00
|
|
|
else:
|
2011-05-01 02:11:36 +04:00
|
|
|
dbs = self.getDbs()
|
|
|
|
|
|
|
|
for db in dbs:
|
|
|
|
dbs[dbs.index(db)] = safeSQLIdentificatorNaming(db)
|
|
|
|
|
2011-07-30 17:01:37 +04:00
|
|
|
dbs = filter(None, dbs)
|
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
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)))
|
2011-05-01 02:11:36 +04:00
|
|
|
logger.info(infoMsg)
|
2011-02-19 21:36:26 +03:00
|
|
|
|
|
|
|
if isTechniqueAvailable(PAYLOAD.TECHNIQUE.UNION) or isTechniqueAvailable(PAYLOAD.TECHNIQUE.ERROR) or conf.direct:
|
|
|
|
blinds = [False, True]
|
|
|
|
else:
|
|
|
|
blinds = [True]
|
|
|
|
|
2011-05-09 20:09:18 +04:00
|
|
|
rootQuery = queries[Backend.getIdentifiedDbms()].tables
|
|
|
|
|
2011-02-19 21:36:26 +03:00
|
|
|
for db in dbs:
|
|
|
|
for blind in blinds:
|
|
|
|
randStr = randomStr()
|
|
|
|
query = rootQuery.inband.query % db
|
|
|
|
retVal = self.__pivotDumpTable("(%s) AS %s" % (query, randStr), ['%s.name' % randStr], blind=blind)
|
|
|
|
|
|
|
|
if retVal:
|
|
|
|
for table in retVal[0].values()[0]:
|
|
|
|
if not kb.data.cachedTables.has_key(db):
|
|
|
|
kb.data.cachedTables[db] = [table]
|
|
|
|
else:
|
|
|
|
kb.data.cachedTables[db].append(table)
|
|
|
|
break
|
|
|
|
|
2011-10-28 17:07:23 +04:00
|
|
|
for db, tables in kb.data.cachedTables.items():
|
|
|
|
kb.data.cachedTables[db] = sorted(tables) if tables else tables
|
|
|
|
|
2011-02-19 21:36:26 +03:00
|
|
|
return kb.data.cachedTables
|
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
def getColumns(self, onlyColNames=False):
|
|
|
|
self.forceDbmsEnum()
|
2011-02-19 21:36:26 +03:00
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
if conf.db is None or conf.db == "CD":
|
|
|
|
if conf.db is None:
|
|
|
|
warnMsg = "missing database parameter, sqlmap is going "
|
|
|
|
warnMsg += "to use the current database to enumerate "
|
|
|
|
warnMsg += "table(s) columns"
|
|
|
|
logger.warn(warnMsg)
|
2011-02-19 21:36:26 +03:00
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
conf.db = self.getCurrentDb()
|
|
|
|
|
|
|
|
elif conf.db is not None:
|
|
|
|
if ',' in conf.db:
|
|
|
|
errMsg = "only one database name is allowed when enumerating "
|
|
|
|
errMsg += "the tables' columns"
|
|
|
|
raise sqlmapMissingMandatoryOptionException, errMsg
|
|
|
|
|
|
|
|
conf.db = safeSQLIdentificatorNaming(conf.db)
|
|
|
|
|
2011-06-15 22:49:35 +04:00
|
|
|
if conf.col:
|
|
|
|
colList = conf.col.split(",")
|
|
|
|
else:
|
|
|
|
colList = []
|
|
|
|
|
|
|
|
for col in colList:
|
|
|
|
colList[colList.index(col)] = safeSQLIdentificatorNaming(col)
|
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
if conf.tbl:
|
|
|
|
tblList = conf.tbl.split(",")
|
|
|
|
else:
|
|
|
|
self.getTables()
|
|
|
|
|
|
|
|
if len(kb.data.cachedTables) > 0:
|
|
|
|
tblList = kb.data.cachedTables.values()
|
|
|
|
|
|
|
|
if isinstance(tblList[0], (set, tuple, list)):
|
|
|
|
tblList = tblList[0]
|
|
|
|
else:
|
|
|
|
errMsg = "unable to retrieve the tables"
|
|
|
|
errMsg += "on database '%s'" % conf.db
|
|
|
|
raise sqlmapNoneDataException, errMsg
|
|
|
|
|
|
|
|
for tbl in tblList:
|
|
|
|
tblList[tblList.index(tbl)] = safeSQLIdentificatorNaming(tbl)
|
|
|
|
|
|
|
|
rootQuery = queries[Backend.getIdentifiedDbms()].columns
|
2011-02-19 21:36:26 +03:00
|
|
|
|
|
|
|
if isTechniqueAvailable(PAYLOAD.TECHNIQUE.UNION) or isTechniqueAvailable(PAYLOAD.TECHNIQUE.ERROR) or conf.direct:
|
|
|
|
blinds = [False, True]
|
|
|
|
else:
|
|
|
|
blinds = [True]
|
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
for tbl in tblList:
|
|
|
|
if conf.db is not None and len(kb.data.cachedColumns) > 0 \
|
|
|
|
and conf.db in kb.data.cachedColumns and tbl in \
|
|
|
|
kb.data.cachedColumns[conf.db]:
|
|
|
|
infoMsg = "fetched tables' columns on "
|
|
|
|
infoMsg += "database '%s'" % conf.db
|
|
|
|
logger.info(infoMsg)
|
2011-02-20 19:28:48 +03:00
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
return { conf.db: kb.data.cachedColumns[conf.db]}
|
2011-02-20 19:28:48 +03:00
|
|
|
|
2011-06-15 22:49:35 +04:00
|
|
|
if colList:
|
|
|
|
table = {}
|
2011-06-16 17:41:02 +04:00
|
|
|
table[safeSQLIdentificatorNaming(tbl)] = dict(map(lambda x: (x, None), colList))
|
|
|
|
kb.data.cachedColumns[safeSQLIdentificatorNaming(conf.db)] = table
|
2011-06-15 22:49:35 +04:00
|
|
|
continue
|
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
infoMsg = "fetching columns "
|
|
|
|
infoMsg += "for table '%s' " % tbl
|
|
|
|
infoMsg += "on database '%s'" % conf.db
|
|
|
|
logger.info(infoMsg)
|
2011-02-20 19:28:48 +03:00
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
for blind in blinds:
|
|
|
|
randStr = randomStr()
|
|
|
|
query = rootQuery.inband.query % (conf.db, conf.db, conf.db, conf.db, conf.db, conf.db, conf.db, unsafeSQLIdentificatorNaming(tbl))
|
|
|
|
retVal = self.__pivotDumpTable("(%s) AS %s" % (query, randStr), ['%s.name' % randStr,'%s.usertype' % randStr], blind=blind)
|
2011-02-20 19:28:48 +03:00
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
if retVal:
|
|
|
|
table = {}
|
|
|
|
columns = {}
|
2011-02-20 19:28:48 +03:00
|
|
|
|
2011-08-09 18:20:25 +04:00
|
|
|
for name, type_ in filterPairValues(zip(retVal[0]["%s.name" % randStr], retVal[0]["%s.usertype" % randStr])):
|
2011-06-14 12:40:25 +04:00
|
|
|
columns[name] = sybaseTypes.get(type_, type_)
|
2011-02-20 19:28:48 +03:00
|
|
|
|
2011-06-16 17:41:02 +04:00
|
|
|
table[safeSQLIdentificatorNaming(tbl)] = columns
|
|
|
|
kb.data.cachedColumns[safeSQLIdentificatorNaming(conf.db)] = table
|
2011-02-20 19:28:48 +03:00
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
break
|
2011-02-20 19:28:48 +03:00
|
|
|
|
2011-05-01 02:11:36 +04:00
|
|
|
return kb.data.cachedColumns
|
2011-02-20 19:28:48 +03:00
|
|
|
|
|
|
|
def searchDb(self):
|
|
|
|
warnMsg = "on Sybase searching of databases is not implemented"
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
def searchTable(self):
|
|
|
|
warnMsg = "on Sybase searching of tables is not implemented"
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
def searchColumn(self):
|
|
|
|
warnMsg = "on Sybase searching of columns is not implemented"
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
return []
|