sqlmap/lib/parse/handler.py

81 lines
2.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python
"""
$Id$
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)
2011-04-30 17:20:05 +04:00
self.__banner = sanitizeStr(banner)
self.__regexp = None
self.__match = None
self.__dbmsVersion = None
self.__techVersion = None
2011-04-30 17:20:05 +04:00
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":
self.__info[key] = value
else:
if key not in self.__info.keys():
self.__info[key] = set()
2011-12-22 02:09:21 +04:00
for _ in value.split("|"):
self.__info[key].add(_)
def startElement(self, name, attrs):
if name == "regexp":
self.__regexp = sanitizeStr(attrs.get("value"))
2011-11-22 16:40:12 +04:00
_ = 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
2011-11-22 16:40:12 +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)
else:
self.__match = None
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"))
self.__dbmsVersion = sanitizeStr(attrs.get("dbms_version"))
self.__techVersion = sanitizeStr(attrs.get("tech_version"))
2011-04-30 17:20:05 +04:00
self.__sp = sanitizeStr(attrs.get("sp"))
if self.__dbmsVersion.isdigit():
self.__feedInfo("dbmsVersion", self.__match.group(int(self.__dbmsVersion)))
if self.__techVersion.isdigit():
self.__feedInfo("technology", "%s %s" % (attrs.get("technology"), self.__match.group(int(self.__techVersion))))
else:
self.__feedInfo("technology", attrs.get("technology"))
if self.__sp.isdigit():
self.__feedInfo("sp", "Service Pack %s" % self.__match.group(int(self.__sp)))
2011-04-30 17:20:05 +04:00
self.__regexp = None
self.__match = None
self.__dbmsVersion = None
self.__techVersion = None