2019-05-08 13:47:52 +03:00
|
|
|
#!/usr/bin/env python
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
"""
|
2020-01-01 15:25:15 +03:00
|
|
|
Copyright (c) 2006-2020 sqlmap developers (http://sqlmap.org/)
|
2017-10-11 15:50:46 +03:00
|
|
|
See the file 'LICENSE' for copying permission
|
2010-03-23 01:57:57 +03:00
|
|
|
"""
|
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
from lib.core.common import Backend
|
|
|
|
from lib.core.common import Format
|
2019-05-06 01:54:21 +03:00
|
|
|
from lib.core.convert 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 MSSQL_ALIASES
|
|
|
|
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.MSSQL)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
def getFingerprint(self):
|
|
|
|
value = ""
|
2011-01-28 19:36:09 +03:00
|
|
|
wsOsFp = Format.getOs("web server", kb.headersFp)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
if wsOsFp:
|
|
|
|
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
|
|
|
|
|
|
|
if dbmsOsFp:
|
|
|
|
value += "%s\n" % dbmsOsFp
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
blank = " " * 15
|
|
|
|
value += "active fingerprint: %s" % actVer
|
|
|
|
|
|
|
|
if kb.bannerFp:
|
2018-08-25 23:57:49 +03:00
|
|
|
release = kb.bannerFp.get("dbmsRelease")
|
|
|
|
version = kb.bannerFp.get("dbmsVersion")
|
|
|
|
servicepack = kb.bannerFp.get("dbmsServicePack")
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
if release and version and servicepack:
|
2011-01-20 02:06:15 +03:00
|
|
|
banVer = "%s %s " % (DBMS.MSSQL, release)
|
2010-03-23 01:57:57 +03:00
|
|
|
banVer += "Service Pack %s " % servicepack
|
|
|
|
banVer += "version %s" % version
|
|
|
|
|
|
|
|
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):
|
2017-03-06 14:53:04 +03:00
|
|
|
if not conf.extensiveFp and Backend.isDbmsWithin(MSSQL_ALIASES):
|
2011-01-28 19:36:09 +03:00
|
|
|
setDbms("%s %s" % (DBMS.MSSQL, Backend.getVersion()))
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
self.getBanner()
|
|
|
|
|
2011-04-23 20:25:09 +04:00
|
|
|
Backend.setOs(OS.WINDOWS)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
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.MSSQL
|
2010-03-23 01:57:57 +03:00
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2010-12-21 12:23:00 +03:00
|
|
|
# NOTE: SELECT LEN(@@VERSION)=LEN(@@VERSION) FROM DUAL does not
|
|
|
|
# work connecting directly to the Microsoft SQL Server database
|
2010-03-31 14:50:47 +04:00
|
|
|
if conf.direct:
|
|
|
|
result = True
|
|
|
|
else:
|
2017-07-29 04:18:49 +03:00
|
|
|
result = inject.checkBooleanExpression("UNICODE(SQUARE(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.MSSQL
|
2010-03-23 01:57:57 +03:00
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2018-03-13 15:45:42 +03:00
|
|
|
for version, check in (
|
2020-01-25 19:15:11 +03:00
|
|
|
("2019", "CHARINDEX('15.0.',@@VERSION)>0"),
|
2020-01-25 18:49:32 +03:00
|
|
|
("Azure", "@@VERSION LIKE '%Azure%'"),
|
2020-01-25 19:15:11 +03:00
|
|
|
("2017", "TRIM(NULL) IS NULL"),
|
|
|
|
("2016", "ISJSON(NULL) IS NULL"),
|
|
|
|
("2014", "CHARINDEX('12.0.',@@VERSION)>0"),
|
|
|
|
("2012", "CONCAT(NULL,NULL)=CONCAT(NULL,NULL)"),
|
|
|
|
("2008", "SYSDATETIME()=SYSDATETIME()"),
|
|
|
|
("2005", "XACT_STATE()=XACT_STATE()"),
|
|
|
|
("2000", "HOST_NAME()=HOST_NAME()"),
|
2018-03-13 15:45:42 +03:00
|
|
|
):
|
2010-12-06 21:20:57 +03:00
|
|
|
result = inject.checkBooleanExpression(check)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
if result:
|
2011-01-28 19:36:09 +03:00
|
|
|
Backend.setVersion(version)
|
2020-01-25 19:15:11 +03:00
|
|
|
break
|
2010-12-18 00:01:14 +03:00
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
if Backend.getVersion():
|
|
|
|
setDbms("%s %s" % (DBMS.MSSQL, Backend.getVersion()))
|
2010-03-23 01:57:57 +03:00
|
|
|
else:
|
2010-11-02 14:59:24 +03:00
|
|
|
setDbms(DBMS.MSSQL)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
self.getBanner()
|
|
|
|
|
2011-04-23 20:25:09 +04:00
|
|
|
Backend.setOs(OS.WINDOWS)
|
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.MSSQL
|
2010-03-23 01:57:57 +03:00
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def checkDbmsOs(self, detailed=False):
|
2012-01-13 20:49:52 +04:00
|
|
|
if Backend.getOs() and Backend.getOsVersion() and Backend.getOsServicePack():
|
2010-03-23 01:57:57 +03:00
|
|
|
return
|
|
|
|
|
2011-04-23 20:25:09 +04:00
|
|
|
if not Backend.getOs():
|
|
|
|
Backend.setOs(OS.WINDOWS)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
if not detailed:
|
|
|
|
return
|
|
|
|
|
2011-04-30 17:20:05 +04:00
|
|
|
infoMsg = "fingerprinting the back-end DBMS operating system "
|
2010-03-23 01:57:57 +03:00
|
|
|
infoMsg += "version and service pack"
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2011-04-23 20:25:09 +04:00
|
|
|
infoMsg = "the back-end DBMS operating system is %s" % Backend.getOs()
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
self.createSupportTbl(self.fileTblName, self.tblField, "varchar(1000)")
|
|
|
|
inject.goStacked("INSERT INTO %s(%s) VALUES (%s)" % (self.fileTblName, self.tblField, "@@VERSION"))
|
|
|
|
|
2018-05-08 15:06:34 +03:00
|
|
|
# Reference: https://en.wikipedia.org/wiki/Comparison_of_Microsoft_Windows_versions
|
|
|
|
# https://en.wikipedia.org/wiki/Windows_NT#Releases
|
2018-03-13 15:45:42 +03:00
|
|
|
versions = {
|
|
|
|
"NT": ("4.0", (6, 5, 4, 3, 2, 1)),
|
|
|
|
"2000": ("5.0", (4, 3, 2, 1)),
|
|
|
|
"XP": ("5.1", (3, 2, 1)),
|
|
|
|
"2003": ("5.2", (2, 1)),
|
|
|
|
"Vista or 2008": ("6.0", (2, 1)),
|
|
|
|
"7 or 2008 R2": ("6.1", (1, 0)),
|
|
|
|
"8 or 2012": ("6.2", (0,)),
|
|
|
|
"8.1 or 2012 R2": ("6.3", (0,))
|
|
|
|
}
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
# Get back-end DBMS underlying operating system version
|
|
|
|
for version, data in versions.items():
|
2014-05-15 21:39:18 +04:00
|
|
|
query = "EXISTS(SELECT %s FROM %s WHERE %s " % (self.tblField, self.fileTblName, self.tblField)
|
|
|
|
query += "LIKE '%Windows NT " + data[0] + "%')"
|
2012-02-03 19:42:36 +04:00
|
|
|
result = inject.checkBooleanExpression(query)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2012-02-03 19:42:36 +04:00
|
|
|
if result:
|
2012-01-13 20:49:52 +04:00
|
|
|
Backend.setOsVersion(version)
|
|
|
|
infoMsg += " %s" % Backend.getOsVersion()
|
2010-03-23 01:57:57 +03:00
|
|
|
break
|
|
|
|
|
2012-01-13 20:49:52 +04:00
|
|
|
if not Backend.getOsVersion():
|
|
|
|
Backend.setOsVersion("2003")
|
|
|
|
Backend.setOsServicePack(2)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2010-12-14 00:33:42 +03:00
|
|
|
warnMsg = "unable to fingerprint the underlying operating "
|
2010-03-23 01:57:57 +03:00
|
|
|
warnMsg += "system version, assuming it is Windows "
|
2012-01-13 20:49:52 +04:00
|
|
|
warnMsg += "%s Service Pack %d" % (Backend.getOsVersion(), Backend.getOsServicePack())
|
2010-03-23 01:57:57 +03:00
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
self.cleanup(onlyFileTbl=True)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
# Get back-end DBMS underlying operating system service pack
|
2012-01-13 20:49:52 +04:00
|
|
|
sps = versions[Backend.getOsVersion()][1]
|
2010-03-23 01:57:57 +03:00
|
|
|
for sp in sps:
|
2014-05-15 21:39:18 +04:00
|
|
|
query = "EXISTS(SELECT %s FROM %s WHERE %s " % (self.tblField, self.fileTblName, self.tblField)
|
|
|
|
query += "LIKE '%Service Pack " + getUnicode(sp) + "%')"
|
|
|
|
result = inject.checkBooleanExpression(query)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2014-05-15 21:39:18 +04:00
|
|
|
if result:
|
2012-01-13 20:49:52 +04:00
|
|
|
Backend.setOsServicePack(sp)
|
2010-03-23 01:57:57 +03:00
|
|
|
break
|
|
|
|
|
2012-01-13 20:49:52 +04:00
|
|
|
if not Backend.getOsServicePack():
|
2010-03-23 01:57:57 +03:00
|
|
|
debugMsg = "assuming the operating system has no service pack"
|
|
|
|
logger.debug(debugMsg)
|
|
|
|
|
2012-01-13 20:49:52 +04:00
|
|
|
Backend.setOsServicePack(0)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2012-01-13 20:49:52 +04:00
|
|
|
if Backend.getOsVersion():
|
|
|
|
infoMsg += " Service Pack %d" % Backend.getOsServicePack()
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
self.cleanup(onlyFileTbl=True)
|