2013-02-14 15:32:17 +04:00
|
|
|
#!/usr/bin/env python
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
"""
|
2015-01-06 17:02:16 +03:00
|
|
|
Copyright (c) 2006-2015 sqlmap developers (http://sqlmap.org/)
|
2010-10-15 03:18:29 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
2010-03-23 01:57:57 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
from lib.core.common import Backend
|
|
|
|
from lib.core.common import Format
|
2010-06-02 16:45:40 +04:00
|
|
|
from lib.core.common import getUnicode
|
2010-03-23 01:57:57 +03:00
|
|
|
from lib.core.data import conf
|
|
|
|
from lib.core.data import kb
|
|
|
|
from lib.core.data import logger
|
2010-11-08 12:20:02 +03:00
|
|
|
from lib.core.enums import DBMS
|
2011-04-23 20:25:09 +04:00
|
|
|
from lib.core.enums import OS
|
2010-03-23 01:57:57 +03:00
|
|
|
from lib.core.session import setDbms
|
|
|
|
from lib.core.settings import MYSQL_ALIASES
|
2010-12-18 00:29:09 +03:00
|
|
|
from lib.core.settings import UNKNOWN_DBMS_VERSION
|
2010-03-23 01:57:57 +03:00
|
|
|
from lib.request import inject
|
|
|
|
from plugins.generic.fingerprint import Fingerprint as GenericFingerprint
|
|
|
|
|
|
|
|
class Fingerprint(GenericFingerprint):
|
|
|
|
def __init__(self):
|
2011-01-14 14:55:20 +03:00
|
|
|
GenericFingerprint.__init__(self, DBMS.MYSQL)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
def _commentCheck(self):
|
2011-01-20 02:06:15 +03:00
|
|
|
infoMsg = "executing %s comment injection fingerprint" % DBMS.MYSQL
|
2010-03-23 01:57:57 +03:00
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2013-02-04 18:12:03 +04:00
|
|
|
result = inject.checkBooleanExpression("[RANDNUM]=[RANDNUM]/* NoValue */")
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
if not result:
|
2011-01-20 02:06:15 +03:00
|
|
|
warnMsg = "unable to perform %s comment injection" % DBMS.MYSQL
|
2010-03-23 01:57:57 +03:00
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2011-04-10 17:30:27 +04:00
|
|
|
# MySQL valid versions updated on 04/2011
|
2010-03-23 01:57:57 +03:00
|
|
|
versions = (
|
2010-12-31 15:42:12 +03:00
|
|
|
(32200, 32235), # MySQL 3.22
|
|
|
|
(32300, 32359), # MySQL 3.23
|
2010-03-23 01:57:57 +03:00
|
|
|
(40000, 40032), # MySQL 4.0
|
2010-12-31 15:42:12 +03:00
|
|
|
(40100, 40131), # MySQL 4.1
|
|
|
|
(50000, 50092), # MySQL 5.0
|
2011-04-11 03:28:24 +04:00
|
|
|
(50100, 50156), # MySQL 5.1
|
2010-12-31 15:42:12 +03:00
|
|
|
(50400, 50404), # MySQL 5.4
|
2012-03-19 15:07:03 +04:00
|
|
|
(50500, 50521), # MySQL 5.5
|
|
|
|
(50600, 50604), # MySQL 5.6
|
2010-12-31 15:42:12 +03:00
|
|
|
(60000, 60014), # MySQL 6.0
|
2010-03-23 01:57:57 +03:00
|
|
|
)
|
|
|
|
|
2010-12-31 15:27:57 +03:00
|
|
|
index = -1
|
2010-12-31 15:21:02 +03:00
|
|
|
for i in xrange(len(versions)):
|
|
|
|
element = versions[i]
|
|
|
|
version = element[0]
|
|
|
|
version = getUnicode(version)
|
2013-02-04 18:12:03 +04:00
|
|
|
result = inject.checkBooleanExpression("[RANDNUM]=[RANDNUM]/*!%s AND [RANDNUM1]=[RANDNUM2]*/" % version)
|
2010-12-31 15:21:02 +03:00
|
|
|
|
|
|
|
if result:
|
|
|
|
break
|
2010-12-31 15:27:57 +03:00
|
|
|
else:
|
|
|
|
index += 1
|
2010-12-31 15:21:02 +03:00
|
|
|
|
2010-12-31 15:27:57 +03:00
|
|
|
if index >= 0:
|
2010-03-23 01:57:57 +03:00
|
|
|
prevVer = None
|
|
|
|
|
2011-10-22 02:34:27 +04:00
|
|
|
for version in xrange(versions[index][0], versions[index][1] + 1):
|
2010-06-02 16:45:40 +04:00
|
|
|
version = getUnicode(version)
|
2013-02-04 18:12:03 +04:00
|
|
|
result = inject.checkBooleanExpression("[RANDNUM]=[RANDNUM]/*!%s AND [RANDNUM1]=[RANDNUM2]*/" % version)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
if result:
|
|
|
|
if not prevVer:
|
|
|
|
prevVer = version
|
|
|
|
|
|
|
|
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):
|
2011-04-30 17:20:05 +04:00
|
|
|
value = ""
|
2011-01-28 19:36:09 +03:00
|
|
|
wsOsFp = Format.getOs("web server", kb.headersFp)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2013-02-07 04:05:26 +04:00
|
|
|
if wsOsFp and not hasattr(conf, "api"):
|
2010-03-23 01:57:57 +03:00
|
|
|
value += "%s\n" % wsOsFp
|
|
|
|
|
|
|
|
if kb.data.banner:
|
2011-01-28 19:36:09 +03:00
|
|
|
dbmsOsFp = Format.getOs("back-end DBMS", kb.bannerFp)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2013-02-07 04:05:26 +04:00
|
|
|
if dbmsOsFp and not hasattr(conf, "api"):
|
2010-03-23 01:57:57 +03:00
|
|
|
value += "%s\n" % dbmsOsFp
|
|
|
|
|
2011-01-20 02:06:15 +03:00
|
|
|
value += "back-end DBMS: "
|
2011-01-28 19:36:09 +03:00
|
|
|
actVer = Format.getDbms()
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
if not conf.extensiveFp:
|
|
|
|
value += actVer
|
|
|
|
return value
|
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
comVer = self._commentCheck()
|
2011-04-30 17:20:05 +04:00
|
|
|
blank = " " * 15
|
2010-03-23 01:57:57 +03:00
|
|
|
value += "active fingerprint: %s" % actVer
|
|
|
|
|
|
|
|
if comVer:
|
2011-01-28 19:36:09 +03:00
|
|
|
comVer = Format.getDbms([comVer])
|
2010-03-23 01:57:57 +03:00
|
|
|
value += "\n%scomment injection fingerprint: %s" % (blank, comVer)
|
|
|
|
|
|
|
|
if kb.bannerFp:
|
2013-08-18 03:24:40 +04:00
|
|
|
banVer = kb.bannerFp["dbmsVersion"] if "dbmsVersion" in kb.bannerFp else None
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2013-08-18 03:24:40 +04:00
|
|
|
if banVer and re.search("-log$", kb.data.banner):
|
2010-03-23 01:57:57 +03:00
|
|
|
banVer += ", logging enabled"
|
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
banVer = Format.getDbms([banVer] if banVer else None)
|
2010-03-23 01:57:57 +03:00
|
|
|
value += "\n%sbanner parsing fingerprint: %s" % (blank, banVer)
|
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
htmlErrorFp = Format.getErrorParsedDBMSes()
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
if htmlErrorFp:
|
|
|
|
value += "\n%shtml error message fingerprint: %s" % (blank, htmlErrorFp)
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
def checkDbms(self):
|
|
|
|
"""
|
|
|
|
References for fingerprint:
|
|
|
|
|
|
|
|
* 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)
|
|
|
|
"""
|
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
if not conf.extensiveFp and (Backend.isDbmsWithin(MYSQL_ALIASES) \
|
2014-08-30 23:53:09 +04:00
|
|
|
or (conf.dbms or "").lower() in MYSQL_ALIASES) and Backend.getVersion() and \
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.getVersion() != UNKNOWN_DBMS_VERSION:
|
|
|
|
v = Backend.getVersion().replace(">", "")
|
2011-01-20 02:06:15 +03:00
|
|
|
v = v.replace("=", "")
|
|
|
|
v = v.replace(" ", "")
|
2010-12-01 01:40:25 +03:00
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersion(v)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
setDbms("%s %s" % (DBMS.MYSQL, Backend.getVersion()))
|
2011-01-20 02:06:15 +03:00
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
if Backend.isVersionGreaterOrEqualThan("5"):
|
2010-03-23 01:57:57 +03:00
|
|
|
kb.data.has_information_schema = True
|
|
|
|
|
|
|
|
self.getBanner()
|
|
|
|
|
2011-01-14 15:47:07 +03:00
|
|
|
return True
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2011-01-20 02:06:15 +03:00
|
|
|
infoMsg = "testing %s" % DBMS.MYSQL
|
2010-03-31 14:50:47 +04:00
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2012-09-08 22:22:13 +04:00
|
|
|
result = inject.checkBooleanExpression("QUARTER(NULL) IS NULL")
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
if result:
|
2011-01-20 02:06:15 +03:00
|
|
|
infoMsg = "confirming %s" % DBMS.MYSQL
|
2010-03-23 01:57:57 +03:00
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2013-01-10 14:42:37 +04:00
|
|
|
result = inject.checkBooleanExpression("USER() LIKE USER()")
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
if not result:
|
2011-01-20 02:06:15 +03:00
|
|
|
warnMsg = "the back-end DBMS is not %s" % DBMS.MYSQL
|
2010-03-23 01:57:57 +03:00
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2015-10-09 15:17:13 +03:00
|
|
|
result = inject.checkBooleanExpression("ROUNDMAGIC(NULL) IS NULL")
|
|
|
|
|
|
|
|
if result:
|
|
|
|
warnMsg = "the back-end DBMS is not %s" % DBMS.MYSQL
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2011-05-20 03:03:00 +04:00
|
|
|
# reading information_schema on some platforms is causing annoying timeout exits
|
2011-05-20 03:28:27 +04:00
|
|
|
# Reference: http://bugs.mysql.com/bug.php?id=15855
|
|
|
|
|
|
|
|
# Determine if it is MySQL >= 5.0.0
|
2013-08-22 14:05:59 +04:00
|
|
|
if inject.checkBooleanExpression("ISNULL(TIMESTAMPADD(MINUTE,[RANDNUM],NULL))"):
|
2010-03-23 01:57:57 +03:00
|
|
|
kb.data.has_information_schema = True
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersion(">= 5.0.0")
|
2010-11-02 14:59:24 +03:00
|
|
|
setDbms("%s 5" % DBMS.MYSQL)
|
2010-03-23 01:57:57 +03:00
|
|
|
self.getBanner()
|
|
|
|
|
|
|
|
if not conf.extensiveFp:
|
|
|
|
return True
|
|
|
|
|
2011-01-20 02:06:15 +03:00
|
|
|
infoMsg = "actively fingerprinting %s" % DBMS.MYSQL
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2010-03-23 01:57:57 +03:00
|
|
|
# Check if it is MySQL >= 5.5.0
|
2010-12-14 00:33:42 +03:00
|
|
|
if inject.checkBooleanExpression("TO_SECONDS(950501)>0"):
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersion(">= 5.5.0")
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
# Check if it is MySQL >= 5.1.2 and < 5.5.0
|
2010-12-14 00:33:42 +03:00
|
|
|
elif inject.checkBooleanExpression("@@table_open_cache=@@table_open_cache"):
|
2013-02-04 18:12:03 +04:00
|
|
|
if inject.checkBooleanExpression("[RANDNUM]=(SELECT [RANDNUM] FROM information_schema.GLOBAL_STATUS LIMIT 0, 1)"):
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersionList([">= 5.1.12", "< 5.5.0"])
|
2013-02-04 18:12:03 +04:00
|
|
|
elif inject.checkBooleanExpression("[RANDNUM]=(SELECT [RANDNUM] FROM information_schema.PROCESSLIST LIMIT 0, 1)"):
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersionList([">= 5.1.7", "< 5.1.12"])
|
2013-02-04 18:12:03 +04:00
|
|
|
elif inject.checkBooleanExpression("[RANDNUM]=(SELECT [RANDNUM] FROM information_schema.PARTITIONS LIMIT 0, 1)"):
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersion("= 5.1.6")
|
2013-02-04 18:12:03 +04:00
|
|
|
elif inject.checkBooleanExpression("[RANDNUM]=(SELECT [RANDNUM] FROM information_schema.PLUGINS LIMIT 0, 1)"):
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersionList([">= 5.1.5", "< 5.1.6"])
|
2010-03-23 01:57:57 +03:00
|
|
|
else:
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersionList([">= 5.1.2", "< 5.1.5"])
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
# Check if it is MySQL >= 5.0.0 and < 5.1.2
|
2010-12-14 00:33:42 +03:00
|
|
|
elif inject.checkBooleanExpression("@@hostname=@@hostname"):
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersionList([">= 5.0.38", "< 5.1.2"])
|
2011-01-01 22:38:51 +03:00
|
|
|
elif inject.checkBooleanExpression("@@character_set_filesystem=@@character_set_filesystem"):
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersionList([">= 5.0.19", "< 5.0.38"])
|
2013-02-04 18:12:03 +04:00
|
|
|
elif not inject.checkBooleanExpression("[RANDNUM]=(SELECT [RANDNUM] FROM DUAL WHERE [RANDNUM1]!=[RANDNUM2])"):
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersionList([">= 5.0.11", "< 5.0.19"])
|
2011-01-01 22:38:51 +03:00
|
|
|
elif inject.checkBooleanExpression("@@div_precision_increment=@@div_precision_increment"):
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersionList([">= 5.0.6", "< 5.0.11"])
|
2011-01-01 22:38:51 +03:00
|
|
|
elif inject.checkBooleanExpression("@@automatic_sp_privileges=@@automatic_sp_privileges"):
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersionList([">= 5.0.3", "< 5.0.6"])
|
2010-03-23 01:57:57 +03:00
|
|
|
else:
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersionList([">= 5.0.0", "< 5.0.3"])
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2010-12-18 16:08:37 +03:00
|
|
|
elif inject.checkBooleanExpression("DATABASE() LIKE SCHEMA()"):
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersion(">= 5.0.2")
|
2010-12-18 16:08:37 +03:00
|
|
|
setDbms("%s 5" % DBMS.MYSQL)
|
|
|
|
self.getBanner()
|
|
|
|
|
2010-12-22 03:51:20 +03:00
|
|
|
elif inject.checkBooleanExpression("STRCMP(LOWER(CURRENT_USER()), UPPER(CURRENT_USER()))=0"):
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersion("< 5.0.0")
|
2010-11-02 14:59:24 +03:00
|
|
|
setDbms("%s 4" % DBMS.MYSQL)
|
2010-03-23 01:57:57 +03:00
|
|
|
self.getBanner()
|
|
|
|
|
|
|
|
if not conf.extensiveFp:
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Check which version of MySQL < 5.0.0 it is
|
2010-12-14 00:33:42 +03:00
|
|
|
if inject.checkBooleanExpression("3=(SELECT COERCIBILITY(USER()))"):
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersionList([">= 4.1.11", "< 5.0.0"])
|
2010-12-14 00:33:42 +03:00
|
|
|
elif inject.checkBooleanExpression("2=(SELECT COERCIBILITY(USER()))"):
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersionList([">= 4.1.1", "< 4.1.11"])
|
2010-12-14 00:33:42 +03:00
|
|
|
elif inject.checkBooleanExpression("CURRENT_USER()=CURRENT_USER()"):
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersionList([">= 4.0.6", "< 4.1.1"])
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2010-12-14 00:33:42 +03:00
|
|
|
if inject.checkBooleanExpression("'utf8'=(SELECT CHARSET(CURRENT_USER()))"):
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersion("= 4.1.0")
|
2010-03-23 01:57:57 +03:00
|
|
|
else:
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersionList([">= 4.0.6", "< 4.1.0"])
|
2010-12-20 14:25:18 +03:00
|
|
|
else:
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersionList([">= 4.0.0", "< 4.0.6"])
|
2010-12-20 14:23:57 +03:00
|
|
|
else:
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersion("< 4.0.0")
|
2010-12-20 14:23:57 +03:00
|
|
|
setDbms("%s 3" % DBMS.MYSQL)
|
|
|
|
self.getBanner()
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
return True
|
|
|
|
else:
|
2011-01-20 02:06:15 +03:00
|
|
|
warnMsg = "the back-end DBMS is not %s" % DBMS.MYSQL
|
2010-03-23 01:57:57 +03:00
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
return False
|
2010-11-03 13:08:27 +03:00
|
|
|
|
2010-03-23 01:57:57 +03:00
|
|
|
def checkDbmsOs(self, detailed=False):
|
2011-04-23 20:25:09 +04:00
|
|
|
if Backend.getOs():
|
2010-03-23 01:57:57 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
infoMsg = "fingerprinting the back-end DBMS operating system"
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2011-04-21 21:33:07 +04:00
|
|
|
result = inject.checkBooleanExpression("'W'=UPPER(MID(@@version_compile_os,1,1))")
|
2011-01-20 02:06:15 +03:00
|
|
|
|
|
|
|
if result:
|
2011-04-23 20:25:09 +04:00
|
|
|
Backend.setOs(OS.WINDOWS)
|
2011-04-21 21:34:26 +04:00
|
|
|
elif not result:
|
2011-04-23 20:25:09 +04:00
|
|
|
Backend.setOs(OS.LINUX)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2011-04-23 20:25:09 +04:00
|
|
|
if Backend.getOs():
|
|
|
|
infoMsg = "the back-end DBMS operating system is %s" % Backend.getOs()
|
2011-01-16 02:54:03 +03:00
|
|
|
logger.info(infoMsg)
|
|
|
|
else:
|
|
|
|
self.userChooseDbmsOs()
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
self.cleanup(onlyFileTbl=True)
|