2010-03-23 01:57:57 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
$Id$
|
|
|
|
|
2010-10-14 18:41:14 +04:00
|
|
|
Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/)
|
2010-10-15 03:18:29 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
2010-03-23 01:57:57 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
from lib.core.agent import agent
|
|
|
|
from lib.core.common import formatDBMSfp
|
|
|
|
from lib.core.common import formatFingerprint
|
2011-01-02 19:51:21 +03:00
|
|
|
from lib.core.common import getErrorParsedDBMSesFormatted
|
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.common import randomInt
|
|
|
|
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
|
2010-03-23 01:57:57 +03:00
|
|
|
from lib.core.session import setDbms
|
|
|
|
from lib.core.settings import MSSQL_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 lib.request.connect import Connect as Request
|
|
|
|
|
|
|
|
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 = ""
|
|
|
|
wsOsFp = formatFingerprint("web server", kb.headersFp)
|
|
|
|
|
|
|
|
if wsOsFp:
|
|
|
|
value += "%s\n" % wsOsFp
|
|
|
|
|
|
|
|
if kb.data.banner:
|
|
|
|
dbmsOsFp = formatFingerprint("back-end DBMS", kb.bannerFp)
|
|
|
|
|
|
|
|
if dbmsOsFp:
|
|
|
|
value += "%s\n" % dbmsOsFp
|
|
|
|
|
|
|
|
value += "back-end DBMS: "
|
|
|
|
actVer = formatDBMSfp()
|
|
|
|
|
|
|
|
if not conf.extensiveFp:
|
|
|
|
value += actVer
|
|
|
|
return value
|
|
|
|
|
|
|
|
blank = " " * 15
|
|
|
|
value += "active fingerprint: %s" % actVer
|
|
|
|
|
|
|
|
if kb.bannerFp:
|
|
|
|
release = kb.bannerFp["dbmsRelease"] if 'dbmsRelease' in kb.bannerFp else None
|
|
|
|
version = kb.bannerFp["dbmsVersion"] if 'dbmsVersion' in kb.bannerFp else None
|
|
|
|
servicepack = kb.bannerFp["dbmsServicePack"] if 'dbmsServicePack' in kb.bannerFp else None
|
|
|
|
|
|
|
|
if release and version and servicepack:
|
|
|
|
banVer = "Microsoft SQL Server %s " % release
|
|
|
|
banVer += "Service Pack %s " % servicepack
|
|
|
|
banVer += "version %s" % version
|
|
|
|
|
|
|
|
value += "\n%sbanner parsing fingerprint: %s" % (blank, banVer)
|
|
|
|
|
2011-01-02 19:51:21 +03:00
|
|
|
htmlErrorFp = getErrorParsedDBMSesFormatted()
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
if htmlErrorFp:
|
|
|
|
value += "\n%shtml error message fingerprint: %s" % (blank, htmlErrorFp)
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
def checkDbms(self):
|
2011-01-14 15:47:07 +03:00
|
|
|
if not conf.extensiveFp and ((kb.dbms is not None and kb.dbms.lower() in MSSQL_ALIASES) \
|
2010-12-01 01:40:25 +03:00
|
|
|
or conf.dbms in MSSQL_ALIASES) and kb.dbmsVersion and \
|
|
|
|
kb.dbmsVersion[0].isdigit():
|
2010-11-02 14:59:24 +03:00
|
|
|
setDbms("%s %s" % (DBMS.MSSQL, kb.dbmsVersion[0]))
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
self.getBanner()
|
|
|
|
|
2011-01-14 15:47:07 +03:00
|
|
|
kb.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
|
|
|
|
|
|
|
infoMsg = "testing Microsoft SQL Server"
|
|
|
|
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:
|
2010-10-12 23:05:12 +04:00
|
|
|
randInt = randomInt()
|
2010-12-14 00:33:42 +03:00
|
|
|
result = inject.checkBooleanExpression("BINARY_CHECKSUM(%d)=BINARY_CHECKSUM(%d)" % (randInt, randInt))
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
if result:
|
|
|
|
infoMsg = "confirming Microsoft SQL Server"
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2010-12-21 02:27:04 +03:00
|
|
|
for version, check in [\
|
|
|
|
("2000", "HOST_NAME()=HOST_NAME()"),\
|
|
|
|
("2005", "XACT_STATE()=XACT_STATE()"),\
|
2011-01-02 21:12:18 +03:00
|
|
|
("2008", "SYSDATETIME()=SYSDATETIME()") ]:
|
2010-12-06 21:20:57 +03:00
|
|
|
result = inject.checkBooleanExpression(check)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
if result:
|
2010-12-21 02:27:04 +03:00
|
|
|
kb.dbmsVersion = [version]
|
2010-12-18 00:01:14 +03:00
|
|
|
|
2010-03-23 01:57:57 +03:00
|
|
|
if kb.dbmsVersion:
|
2010-11-02 14:59:24 +03:00
|
|
|
setDbms("%s %s" % (DBMS.MSSQL, kb.dbmsVersion[0]))
|
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()
|
|
|
|
|
|
|
|
kb.os = "Windows"
|
|
|
|
|
|
|
|
return True
|
|
|
|
else:
|
2010-11-12 13:02:02 +03:00
|
|
|
warnMsg = "the back-end DBMS is not Microsoft SQL Server"
|
2010-03-23 01:57:57 +03:00
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def checkDbmsOs(self, detailed=False):
|
|
|
|
if kb.os and kb.osVersion and kb.osSP:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not kb.os:
|
|
|
|
kb.os = "Windows"
|
|
|
|
|
|
|
|
if not detailed:
|
|
|
|
return
|
|
|
|
|
|
|
|
infoMsg = "fingerprinting the back-end DBMS operating system "
|
|
|
|
infoMsg += "version and service pack"
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
infoMsg = "the back-end DBMS operating system is %s" % kb.os
|
|
|
|
|
|
|
|
self.createSupportTbl(self.fileTblName, self.tblField, "varchar(1000)")
|
|
|
|
inject.goStacked("INSERT INTO %s(%s) VALUES (%s)" % (self.fileTblName, self.tblField, "@@VERSION"))
|
|
|
|
|
|
|
|
versions = { "2003": ("5.2", (2, 1)),
|
|
|
|
#"2003": ("6.0", (2, 1)),
|
|
|
|
"2008": ("7.0", (1,)),
|
|
|
|
"2000": ("5.0", (4, 3, 2, 1)),
|
|
|
|
"XP": ("5.1", (2, 1)),
|
|
|
|
"NT": ("4.0", (6, 5, 4, 3, 2, 1)) }
|
|
|
|
|
|
|
|
# Get back-end DBMS underlying operating system version
|
|
|
|
for version, data in versions.items():
|
2010-12-14 00:33:42 +03:00
|
|
|
query = "(SELECT LEN(%s) FROM %s WHERE %s " % (self.tblField, self.fileTblName, self.tblField)
|
2010-03-23 01:57:57 +03:00
|
|
|
query += "LIKE '%Windows NT " + data[0] + "%')>0"
|
|
|
|
|
2010-12-14 00:33:42 +03:00
|
|
|
if inject.checkBooleanExpression(query):
|
|
|
|
infoMsg += " %s" % kb.osVersion
|
2010-03-23 01:57:57 +03:00
|
|
|
kb.osVersion = version
|
|
|
|
break
|
|
|
|
|
|
|
|
if not kb.osVersion:
|
|
|
|
kb.osVersion = "2003"
|
2010-12-14 00:33:42 +03:00
|
|
|
kb.osSP = 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 "
|
|
|
|
warnMsg += "%s Service Pack %d" % (kb.osVersion, kb.osSP)
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
self.cleanup(onlyFileTbl=True)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
# Get back-end DBMS underlying operating system service pack
|
|
|
|
sps = versions[kb.osVersion][1]
|
|
|
|
|
|
|
|
for sp in sps:
|
2010-12-14 00:33:42 +03:00
|
|
|
query = "(SELECT LEN(%s) FROM %s WHERE %s " % (self.tblField, self.fileTblName, self.tblField)
|
2010-06-02 16:45:40 +04:00
|
|
|
query += "LIKE '%Service Pack " + getUnicode(sp) + "%')>0"
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2010-12-14 00:33:42 +03:00
|
|
|
if inject.checkBooleanExpression(query):
|
2010-03-23 01:57:57 +03:00
|
|
|
kb.osSP = sp
|
|
|
|
break
|
|
|
|
|
|
|
|
if not kb.osSP:
|
|
|
|
debugMsg = "assuming the operating system has no service pack"
|
|
|
|
logger.debug(debugMsg)
|
|
|
|
|
|
|
|
kb.osSP = 0
|
|
|
|
|
|
|
|
if kb.osVersion:
|
|
|
|
infoMsg += " Service Pack %d" % kb.osSP
|
|
|
|
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
self.cleanup(onlyFileTbl=True)
|