sqlmap/lib/parse/handler.py

79 lines
2.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python
"""
2012-01-11 18:59:46 +04:00
Copyright (c) 2006-2012 sqlmap developers (http://www.sqlmap.org/)
2010-10-15 03:18:29 +04:00
See the file 'doc/COPYING' for copying permission
"""
import re
2011-12-22 02:09:21 +04:00
from xml.sax.handler import ContentHandler
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)
2012-02-16 17:46:01 +04:00
self._banner = sanitizeStr(banner)
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:
2012-02-16 17:46:01 +04:00
if key not in self._info.keys():
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"))
_ = re.match("\A[A-Za-z0-9]+", self._regexp) # minor trick avoiding compiling of large amount of regexes
2011-12-22 02:09:21 +04:00
2012-02-16 17:46:01 +04:00
if _ and _.group(0).lower() in self._banner.lower() or not _:
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():
self._feedInfo("sp", "Service Pack %s" % self._match.group(int(self._sp)))
2012-02-16 17:46:01 +04:00
self._regexp = None
self._match = None
self._dbmsVersion = None
self._techVersion = None