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
|
|
|
|
2011-04-15 16:33:18 +04:00
|
|
|
Copyright (c) 2006-2011 sqlmap developers (http://sqlmap.sourceforge.net/)
|
2010-10-15 03:18:29 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
2008-10-15 19:38:22 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
from xml.sax.handler import ContentHandler
|
|
|
|
|
|
|
|
from lib.core.common import checkFile
|
2010-05-21 18:42:59 +04:00
|
|
|
from lib.core.common import getCompiledRegex
|
2011-01-28 19:36:09 +03:00
|
|
|
from lib.core.common import Backend
|
2010-04-16 23:57:00 +04:00
|
|
|
from lib.core.common import parseXmlFile
|
2008-10-15 19:38:22 +04:00
|
|
|
from lib.core.common import sanitizeStr
|
2008-11-16 02:41:31 +03:00
|
|
|
from lib.core.data import kb
|
|
|
|
from lib.core.data import paths
|
2010-11-08 12:20:02 +03:00
|
|
|
from lib.core.enums import DBMS
|
2008-11-18 20:42:46 +03:00
|
|
|
from lib.parse.handler import FingerprintHandler
|
2008-11-16 02:41:31 +03:00
|
|
|
|
|
|
|
class MSSQLBannerHandler(ContentHandler):
|
|
|
|
"""
|
|
|
|
This class defines methods to parse and extract information from the
|
|
|
|
given Microsoft SQL Server banner based upon the data in XML file
|
|
|
|
"""
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
def __init__(self, banner, info):
|
2011-01-15 15:53:40 +03:00
|
|
|
ContentHandler.__init__(self)
|
|
|
|
|
2011-04-30 17:20:05 +04:00
|
|
|
self.__banner = sanitizeStr(banner)
|
|
|
|
self.__inVersion = False
|
2008-10-15 19:38:22 +04:00
|
|
|
self.__inServicePack = False
|
2011-04-30 17:20:05 +04:00
|
|
|
self.__release = None
|
|
|
|
self.__version = ""
|
|
|
|
self.__versionAlt = None
|
|
|
|
self.__servicePack = ""
|
|
|
|
self.__info = info
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-11-17 20:41:02 +03:00
|
|
|
def __feedInfo(self, key, value):
|
|
|
|
value = sanitizeStr(value)
|
|
|
|
|
|
|
|
if value in ( None, "None" ):
|
|
|
|
return
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
self.__info[key] = value
|
2008-11-16 02:41:31 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
def startElement(self, name, attrs):
|
|
|
|
if name == "signatures":
|
|
|
|
self.__release = sanitizeStr(attrs.get("release"))
|
|
|
|
|
|
|
|
elif name == "version":
|
|
|
|
self.__inVersion = True
|
|
|
|
|
|
|
|
elif name == "servicepack":
|
|
|
|
self.__inServicePack = True
|
|
|
|
|
|
|
|
def characters(self, data):
|
|
|
|
if self.__inVersion:
|
|
|
|
self.__version += sanitizeStr(data)
|
|
|
|
elif self.__inServicePack:
|
|
|
|
self.__servicePack += sanitizeStr(data)
|
|
|
|
|
|
|
|
def endElement(self, name):
|
|
|
|
if name == "signature":
|
2010-03-01 13:33:36 +03:00
|
|
|
for version in (self.__version, self.__versionAlt):
|
2010-05-21 18:42:59 +04:00
|
|
|
regObj = getCompiledRegex(" %s[\.\ ]+" % version)
|
|
|
|
if version and regObj.search(self.__banner):
|
2010-03-01 13:33:36 +03:00
|
|
|
self.__feedInfo("dbmsRelease", self.__release)
|
|
|
|
self.__feedInfo("dbmsVersion", self.__version)
|
|
|
|
self.__feedInfo("dbmsServicePack", self.__servicePack)
|
|
|
|
break
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2011-04-30 17:20:05 +04:00
|
|
|
self.__version = ""
|
|
|
|
self.__versionAlt = None
|
2008-10-15 19:38:22 +04:00
|
|
|
self.__servicePack = ""
|
|
|
|
|
|
|
|
elif name == "version":
|
|
|
|
self.__inVersion = False
|
|
|
|
self.__version = self.__version.replace(" ", "")
|
2010-05-21 18:42:59 +04:00
|
|
|
|
|
|
|
regObj = getCompiledRegex(r"\A(?P<major>\d+)\.00\.(?P<build>\d+)\Z")
|
|
|
|
match = regObj.search(self.__version)
|
2010-03-01 13:33:36 +03:00
|
|
|
self.__versionAlt = "%s.0.%s.0" % (match.group('major'), match.group('build')) if match else None
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
elif name == "servicepack":
|
|
|
|
self.__inServicePack = False
|
|
|
|
self.__servicePack = self.__servicePack.replace(" ", "")
|
|
|
|
|
2008-11-16 02:41:31 +03:00
|
|
|
def bannerParser(banner):
|
2008-10-15 19:38:22 +04:00
|
|
|
"""
|
|
|
|
This function calls a class to extract information from the given
|
|
|
|
DBMS banner based upon the data in XML file
|
|
|
|
"""
|
|
|
|
|
2010-03-18 20:20:54 +03:00
|
|
|
xmlfile = None
|
2010-10-29 20:11:50 +04:00
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
if Backend.getIdentifiedDbms() == DBMS.MSSQL:
|
2008-11-16 02:41:31 +03:00
|
|
|
xmlfile = paths.MSSQL_XML
|
2011-01-28 19:36:09 +03:00
|
|
|
elif Backend.getIdentifiedDbms() == DBMS.MYSQL:
|
2008-11-16 02:41:31 +03:00
|
|
|
xmlfile = paths.MYSQL_XML
|
2011-01-28 19:36:09 +03:00
|
|
|
elif Backend.getIdentifiedDbms() == DBMS.ORACLE:
|
2008-11-16 02:41:31 +03:00
|
|
|
xmlfile = paths.ORACLE_XML
|
2011-01-28 19:36:09 +03:00
|
|
|
elif Backend.getIdentifiedDbms() == DBMS.PGSQL:
|
2008-11-16 02:41:31 +03:00
|
|
|
xmlfile = paths.PGSQL_XML
|
|
|
|
|
2010-03-18 20:20:54 +03:00
|
|
|
if not xmlfile:
|
|
|
|
return
|
2010-10-29 20:11:50 +04:00
|
|
|
|
2008-11-16 02:41:31 +03:00
|
|
|
checkFile(xmlfile)
|
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
if Backend.getIdentifiedDbms() == DBMS.MSSQL:
|
2009-04-22 15:48:07 +04:00
|
|
|
handler = MSSQLBannerHandler(banner, kb.bannerFp)
|
2010-04-16 23:57:00 +04:00
|
|
|
parseXmlFile(xmlfile, handler)
|
2008-11-18 20:42:46 +03:00
|
|
|
|
|
|
|
handler = FingerprintHandler(banner, kb.bannerFp)
|
2010-04-16 23:57:00 +04:00
|
|
|
parseXmlFile(paths.GENERIC_XML, handler)
|
2008-11-16 02:41:31 +03:00
|
|
|
else:
|
2008-11-18 20:42:46 +03:00
|
|
|
handler = FingerprintHandler(banner, kb.bannerFp)
|
2010-04-16 23:57:00 +04:00
|
|
|
parseXmlFile(xmlfile, handler)
|
|
|
|
parseXmlFile(paths.GENERIC_XML, handler)
|