2008-11-18 20:42:46 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
$Id$
|
|
|
|
|
2011-07-08 00:10:03 +04:00
|
|
|
Copyright (c) 2006-2011 sqlmap developers (http://www.sqlmap.org/)
|
2010-10-15 03:18:29 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
2008-11-18 20:42:46 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
import re
|
|
|
|
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
|
2008-11-18 20:42:46 +03:00
|
|
|
self.__dbmsVersion = None
|
|
|
|
self.__techVersion = None
|
2011-04-30 17:20:05 +04:00
|
|
|
self.__info = info
|
2008-11-18 20:42:46 +03:00
|
|
|
|
|
|
|
def __feedInfo(self, key, value):
|
|
|
|
value = sanitizeStr(value)
|
|
|
|
|
|
|
|
if value in ( None, "None" ):
|
|
|
|
return
|
|
|
|
|
|
|
|
if key in ( "dbmsVersion" ):
|
|
|
|
self.__info[key] = value
|
|
|
|
else:
|
|
|
|
if key not in self.__info.keys():
|
|
|
|
self.__info[key] = set()
|
|
|
|
|
2008-11-19 03:36:44 +03:00
|
|
|
for v in value.split("|"):
|
|
|
|
self.__info[key].add(v)
|
2008-11-18 20:42:46 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
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
|
2008-11-18 20:42:46 +03: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"))
|
|
|
|
|
|
|
|
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"))
|
2008-11-18 20:42:46 +03:00
|
|
|
|
|
|
|
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
|
2008-11-18 20:42:46 +03:00
|
|
|
self.__dbmsVersion = None
|
|
|
|
self.__techVersion = None
|