sqlmap/lib/parse/handler.py

80 lines
2.6 KiB
Python
Raw Normal View History

2019-05-08 13:47:52 +03:00
#!/usr/bin/env python
"""
2020-12-31 13:46:27 +03:00
Copyright (c) 2006-2021 sqlmap developers (http://sqlmap.org/)
2017-10-11 15:50:46 +03:00
See the file 'LICENSE' for copying permission
"""
import re
2011-12-22 02:09:21 +04:00
from xml.sax.handler import ContentHandler
2019-06-04 15:44:06 +03:00
from lib.core.common import sanitizeStr
class FingerprintHandler(ContentHandler):
"""
This class defines methods to parse and extract information from
the given DBMS banner based upon the data in XML file
"""
def __init__(self, banner, info):
2011-01-15 15:53:40 +03:00
ContentHandler.__init__(self)
2021-01-05 15:36:04 +03:00
self._banner = sanitizeStr(banner or "")
2012-02-16 17:46:01 +04:00
self._regexp = None
self._match = None
self._dbmsVersion = None
self._techVersion = None
self._info = info
def _feedInfo(self, key, value):
value = sanitizeStr(value)
if value in (None, "None"):
return
2011-12-22 02:09:21 +04:00
if key == "dbmsVersion":
2012-02-16 17:46:01 +04:00
self._info[key] = value
else:
if key not in self._info:
2012-02-16 17:46:01 +04:00
self._info[key] = set()
2011-12-22 02:09:21 +04:00
for _ in value.split("|"):
2012-02-16 17:46:01 +04:00
self._info[key].add(_)
def startElement(self, name, attrs):
if name == "regexp":
2012-02-16 17:46:01 +04:00
self._regexp = sanitizeStr(attrs.get("value"))
2017-10-31 13:38:09 +03:00
_ = re.match(r"\A[A-Za-z0-9]+", self._regexp) # minor trick avoiding compiling of large amount of regexes
2011-12-22 02:09:21 +04:00
2021-01-05 15:36:04 +03:00
if _ and self._banner and _.group(0).lower() in self._banner.lower() or not _:
2012-02-16 17:46:01 +04:00
self._match = re.search(self._regexp, self._banner, re.I | re.M)
2011-11-22 16:40:12 +04:00
else:
2012-02-16 17:46:01 +04:00
self._match = None
2012-02-16 17:46:01 +04:00
if name == "info" and self._match:
self._feedInfo("type", attrs.get("type"))
self._feedInfo("distrib", attrs.get("distrib"))
self._feedInfo("release", attrs.get("release"))
self._feedInfo("codename", attrs.get("codename"))
2012-02-16 17:46:01 +04:00
self._dbmsVersion = sanitizeStr(attrs.get("dbms_version"))
self._techVersion = sanitizeStr(attrs.get("tech_version"))
self._sp = sanitizeStr(attrs.get("sp"))
2012-02-16 17:46:01 +04:00
if self._dbmsVersion.isdigit():
self._feedInfo("dbmsVersion", self._match.group(int(self._dbmsVersion)))
2012-02-16 17:46:01 +04:00
if self._techVersion.isdigit():
self._feedInfo("technology", "%s %s" % (attrs.get("technology"), self._match.group(int(self._techVersion))))
else:
2012-02-16 17:46:01 +04:00
self._feedInfo("technology", attrs.get("technology"))
2012-02-16 17:46:01 +04:00
if self._sp.isdigit():
2013-12-02 01:25:12 +04:00
self._feedInfo("sp", "Service Pack %s" % int(self._sp))
2012-02-16 17:46:01 +04:00
self._regexp = None
self._match = None
self._dbmsVersion = None
self._techVersion = None