sqlmap/plugins/dbms/maxdb/enumeration.py

239 lines
8.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python
"""
2017-01-02 16:19:18 +03:00
Copyright (c) 2006-2017 sqlmap developers (http://sqlmap.org/)
2010-10-15 03:18:29 +04:00
See the file 'doc/COPYING' for copying permission
"""
2011-02-21 01:07:12 +03:00
from lib.core.common import Backend
from lib.core.common import randomStr
2015-12-29 15:19:25 +03:00
from lib.core.common import readInput
from lib.core.common import safeSQLIdentificatorNaming
from lib.core.common import unsafeSQLIdentificatorNaming
2011-02-21 01:07:12 +03:00
from lib.core.data import conf
from lib.core.data import kb
from lib.core.data import logger
2015-12-29 15:19:25 +03:00
from lib.core.data import paths
2011-02-21 01:07:12 +03:00
from lib.core.data import queries
2016-12-21 12:33:35 +03:00
from lib.core.enums import DBMS
from lib.core.exception import SqlmapMissingMandatoryOptionException
from lib.core.exception import SqlmapNoneDataException
2015-12-29 15:19:25 +03:00
from lib.core.exception import SqlmapUserQuitException
2012-02-16 18:42:28 +04:00
from lib.core.settings import CURRENT_DB
2012-09-10 21:23:24 +04:00
from lib.utils.pivotdumptable import pivotDumpTable
2015-12-29 15:19:25 +03:00
from lib.techniques.brute.use import columnExists
from plugins.generic.enumeration import Enumeration as GenericEnumeration
class Enumeration(GenericEnumeration):
def __init__(self):
GenericEnumeration.__init__(self)
kb.data.processChar = lambda x: x.replace('_', ' ') if x else x
def getPasswordHashes(self):
warnMsg = "on SAP MaxDB it is not possible to enumerate the user password hashes"
logger.warn(warnMsg)
return {}
def getDbs(self):
if len(kb.data.cachedDbs) > 0:
return kb.data.cachedDbs
2011-02-21 01:07:12 +03:00
infoMsg = "fetching database names"
2011-02-21 01:07:12 +03:00
logger.info(infoMsg)
2016-12-21 12:33:35 +03:00
rootQuery = queries[DBMS.MAXDB].dbs
2011-02-21 01:41:42 +03:00
randStr = randomStr()
query = rootQuery.inband.query
2012-09-10 21:23:24 +04:00
retVal = pivotDumpTable("(%s) AS %s" % (query, randStr), ['%s.schemaname' % randStr], blind=True)
2011-02-21 01:41:42 +03:00
if retVal:
kb.data.cachedDbs = retVal[0].values()[0]
2011-02-21 01:41:42 +03:00
2011-10-28 17:16:22 +04:00
if kb.data.cachedDbs:
kb.data.cachedDbs.sort()
return kb.data.cachedDbs
2011-02-21 01:41:42 +03:00
def getTables(self, bruteForce=None):
if len(kb.data.cachedTables) > 0:
return kb.data.cachedTables
2011-02-21 01:41:42 +03:00
self.forceDbmsEnum()
2012-02-16 18:42:28 +04:00
if conf.db == CURRENT_DB:
conf.db = self.getCurrentDb()
2011-02-21 01:41:42 +03:00
if conf.db:
dbs = conf.db.split(",")
else:
dbs = self.getDbs()
for db in filter(None, dbs):
dbs[dbs.index(db)] = safeSQLIdentificatorNaming(db)
infoMsg = "fetching tables for database"
2012-01-14 01:46:21 +04:00
infoMsg += "%s: %s" % ("s" if len(dbs) > 1 else "", ", ".join(db if isinstance(db, basestring) else db[0] for db in sorted(dbs)))
2011-02-21 01:41:42 +03:00
logger.info(infoMsg)
2016-12-21 12:33:35 +03:00
rootQuery = queries[DBMS.MAXDB].tables
2011-02-21 01:41:42 +03:00
for db in dbs:
2011-02-21 01:07:12 +03:00
randStr = randomStr()
2011-02-21 01:41:42 +03:00
query = rootQuery.inband.query % (("'%s'" % db) if db != "USER" else 'USER')
2012-09-10 21:23:24 +04:00
retVal = pivotDumpTable("(%s) AS %s" % (query, randStr), ['%s.tablename' % randStr], blind=True)
2011-02-21 01:07:12 +03:00
if retVal:
2011-02-21 01:41:42 +03:00
for table in retVal[0].values()[0]:
if db not in kb.data.cachedTables:
2011-02-21 01:41:42 +03:00
kb.data.cachedTables[db] = [table]
else:
kb.data.cachedTables[db].append(table)
2011-02-21 01:07:12 +03:00
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-21 01:41:42 +03:00
return kb.data.cachedTables
2011-02-21 01:07:12 +03:00
2015-12-29 15:19:25 +03:00
def getColumns(self, onlyColNames=False, colTuple=None, bruteForce=None, dumpMode=False):
self.forceDbmsEnum()
2011-02-21 01:07:12 +03:00
2012-02-16 18:42:28 +04:00
if conf.db is None or conf.db == CURRENT_DB:
if conf.db is None:
2012-10-04 20:28:36 +04:00
warnMsg = "missing database parameter. sqlmap is going "
warnMsg += "to use the current database to enumerate "
warnMsg += "table(s) columns"
logger.warn(warnMsg)
2011-02-21 01:07:12 +03:00
conf.db = self.getCurrentDb()
2011-02-21 01:41:42 +03:00
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)
2011-02-21 01:41:42 +03:00
conf.db = safeSQLIdentificatorNaming(conf.db)
2011-02-21 01:41:42 +03:00
2015-12-29 15:19:25 +03:00
if conf.col:
colList = conf.col.split(",")
else:
colList = []
if conf.excludeCol:
colList = [_ for _ in colList if _ not in conf.excludeCol.split(',')]
for col in colList:
colList[colList.index(col)] = safeSQLIdentificatorNaming(col)
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:
2012-02-01 13:17:38 +04:00
errMsg = "unable to retrieve the tables "
2012-03-01 15:52:30 +04:00
errMsg += "on database '%s'" % unsafeSQLIdentificatorNaming(conf.db)
raise SqlmapNoneDataException(errMsg)
for tbl in tblList:
2011-06-16 17:41:02 +04:00
tblList[tblList.index(tbl)] = safeSQLIdentificatorNaming(tbl, True)
2015-12-29 15:19:25 +03:00
if bruteForce:
resumeAvailable = False
for tbl in tblList:
for db, table, colName, colType in kb.brute.columns:
if db == conf.db and table == tbl:
resumeAvailable = True
break
if resumeAvailable and not conf.freshQueries or colList:
columns = {}
for column in colList:
columns[column] = None
for tbl in tblList:
for db, table, colName, colType in kb.brute.columns:
if db == conf.db and table == tbl:
columns[colName] = colType
if conf.db in kb.data.cachedColumns:
kb.data.cachedColumns[safeSQLIdentificatorNaming(conf.db)][safeSQLIdentificatorNaming(tbl, True)] = columns
else:
kb.data.cachedColumns[safeSQLIdentificatorNaming(conf.db)] = {safeSQLIdentificatorNaming(tbl, True): columns}
return kb.data.cachedColumns
message = "do you want to use common column existence check? [y/N/q] "
test = readInput(message, default="Y" if "Y" in message else "N")
if test[0] in ("n", "N"):
return
elif test[0] in ("q", "Q"):
raise SqlmapUserQuitException
else:
return columnExists(paths.COMMON_COLUMNS)
2016-12-21 12:33:35 +03:00
rootQuery = queries[DBMS.MAXDB].columns
for tbl in tblList:
if conf.db is not None and len(kb.data.cachedColumns) > 0 \
2011-06-16 18:27:44 +04:00
and conf.db in kb.data.cachedColumns and tbl in \
kb.data.cachedColumns[conf.db]:
infoMsg = "fetched tables' columns on "
2012-03-01 15:52:30 +04:00
infoMsg += "database '%s'" % unsafeSQLIdentificatorNaming(conf.db)
logger.info(infoMsg)
2012-07-13 14:22:37 +04:00
return {conf.db: kb.data.cachedColumns[conf.db]}
2015-12-29 15:19:25 +03:00
if dumpMode and colList:
table = {}
table[safeSQLIdentificatorNaming(tbl)] = dict((_, None) for _ in colList)
kb.data.cachedColumns[safeSQLIdentificatorNaming(conf.db)] = table
continue
infoMsg = "fetching columns "
2012-03-01 15:52:30 +04:00
infoMsg += "for table '%s' " % unsafeSQLIdentificatorNaming(tbl)
infoMsg += "on database '%s'" % unsafeSQLIdentificatorNaming(conf.db)
logger.info(infoMsg)
randStr = randomStr()
query = rootQuery.inband.query % (unsafeSQLIdentificatorNaming(tbl), ("'%s'" % unsafeSQLIdentificatorNaming(conf.db)) if unsafeSQLIdentificatorNaming(conf.db) != "USER" else 'USER')
2013-01-10 16:18:44 +04:00
retVal = pivotDumpTable("(%s) AS %s" % (query, randStr), ['%s.columnname' % randStr, '%s.datatype' % randStr, '%s.len' % randStr], blind=True)
if retVal:
table = {}
columns = {}
for columnname, datatype, length in zip(retVal[0]["%s.columnname" % randStr], retVal[0]["%s.datatype" % randStr], retVal[0]["%s.len" % randStr]):
2011-06-16 17:41:02 +04:00
columns[safeSQLIdentificatorNaming(columnname)] = "%s(%s)" % (datatype, length)
table[tbl] = columns
kb.data.cachedColumns[conf.db] = table
return kb.data.cachedColumns
2012-12-11 15:14:33 +04:00
def getPrivileges(self, *args):
warnMsg = "on SAP MaxDB it is not possible to enumerate the user privileges"
logger.warn(warnMsg)
return {}
def searchDb(self):
warnMsg = "on SAP MaxDB it is not possible to search databases"
logger.warn(warnMsg)
return []
2012-10-14 19:53:55 +04:00
def getHostname(self):
warnMsg = "on SAP MaxDB it is not possible to enumerate the hostname"
logger.warn(warnMsg)