sqlmap/lib/parse/banner.py

116 lines
3.5 KiB
Python
Raw Normal View History

2019-05-08 13:47:52 +03:00
#!/usr/bin/env python
2008-10-15 19:38:22 +04: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
2008-10-15 19:38:22 +04:00
"""
import re
2008-10-15 19:38:22 +04:00
from xml.sax.handler import ContentHandler
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
from lib.core.data import kb
from lib.core.data import paths
from lib.core.enums import DBMS
from lib.parse.handler import FingerprintHandler
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
"""
def __init__(self, banner, info):
2011-01-15 15:53:40 +03:00
ContentHandler.__init__(self)
2012-02-16 17:46:01 +04:00
self._banner = sanitizeStr(banner)
self._inVersion = False
self._inServicePack = False
self._release = None
self._version = ""
self._versionAlt = None
self._servicePack = ""
self._info = info
def _feedInfo(self, key, value):
value = sanitizeStr(value)
if value in (None, "None"):
return
2012-02-16 17:46:01 +04:00
self._info[key] = value
2008-10-15 19:38:22 +04:00
def startElement(self, name, attrs):
if name == "signatures":
2012-02-16 17:46:01 +04:00
self._release = sanitizeStr(attrs.get("release"))
2008-10-15 19:38:22 +04:00
elif name == "version":
2012-02-16 17:46:01 +04:00
self._inVersion = True
2008-10-15 19:38:22 +04:00
elif name == "servicepack":
2012-02-16 17:46:01 +04:00
self._inServicePack = True
2008-10-15 19:38:22 +04:00
2019-05-30 23:40:51 +03:00
def characters(self, content):
2012-02-16 17:46:01 +04:00
if self._inVersion:
2019-05-30 23:40:51 +03:00
self._version += sanitizeStr(content)
2012-02-16 17:46:01 +04:00
elif self._inServicePack:
2019-05-30 23:40:51 +03:00
self._servicePack += sanitizeStr(content)
2008-10-15 19:38:22 +04:00
def endElement(self, name):
if name == "signature":
2012-02-16 17:46:01 +04:00
for version in (self._version, self._versionAlt):
if version and re.search(r" %s[\.\ ]+" % re.escape(version), self._banner):
2012-02-16 17:46:01 +04: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
2012-02-16 17:46:01 +04:00
self._version = ""
self._versionAlt = None
self._servicePack = ""
2008-10-15 19:38:22 +04:00
elif name == "version":
2012-02-16 17:46:01 +04:00
self._inVersion = False
self._version = self._version.replace(" ", "")
2010-05-21 18:42:59 +04:00
match = re.search(r"\A(?P<major>\d+)\.00\.(?P<build>\d+)\Z", self._version)
2012-02-16 17:46:01 +04: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":
2012-02-16 17:46:01 +04:00
self._inServicePack = False
self._servicePack = self._servicePack.replace(" ", "")
2008-10-15 19:38:22 +04: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
"""
xmlfile = None
if Backend.isDbms(DBMS.MSSQL):
xmlfile = paths.MSSQL_XML
elif Backend.isDbms(DBMS.MYSQL):
xmlfile = paths.MYSQL_XML
elif Backend.isDbms(DBMS.ORACLE):
xmlfile = paths.ORACLE_XML
elif Backend.isDbms(DBMS.PGSQL):
xmlfile = paths.PGSQL_XML
if not xmlfile:
return
if Backend.isDbms(DBMS.MSSQL):
handler = MSSQLBannerHandler(banner, kb.bannerFp)
2010-04-16 23:57:00 +04:00
parseXmlFile(xmlfile, handler)
handler = FingerprintHandler(banner, kb.bannerFp)
2010-04-16 23:57:00 +04:00
parseXmlFile(paths.GENERIC_XML, handler)
else:
handler = FingerprintHandler(banner, kb.bannerFp)
2010-04-16 23:57:00 +04:00
parseXmlFile(xmlfile, handler)
parseXmlFile(paths.GENERIC_XML, handler)