sqlmap/lib/core/session.py

187 lines
6.2 KiB
Python
Raw Normal View History

2008-10-15 19:38:22 +04:00
#!/usr/bin/env python
"""
2008-10-15 19:56:32 +04:00
$Id$
2008-10-15 19:38:22 +04:00
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
2008-10-15 19:38:22 +04:00
"""
import re
from lib.core.common import Backend
from lib.core.common import Format
2008-10-15 19:38:22 +04:00
from lib.core.common import dataToSessionFile
from lib.core.common import intersect
2008-10-15 19:38:22 +04:00
from lib.core.common import readInput
2011-07-08 13:32:58 +04:00
from lib.core.common import singleTimeWarnMessage
from lib.core.convert import base64pickle
from lib.core.convert import base64unpickle
2008-10-15 19:38:22 +04:00
from lib.core.data import conf
from lib.core.data import kb
from lib.core.data import logger
from lib.core.enums import OS
from lib.core.settings import SUPPORTED_DBMS
from lib.core.settings import UNKNOWN_DBMS_VERSION
2008-10-15 19:38:22 +04:00
2010-10-11 00:51:11 +04:00
def safeFormatString(value):
retVal = value
if retVal:
retVal = retVal.replace("[", "__LEFT_SQUARE_BRACKET__").replace("]", "__RIGHT_SQUARE_BRACKET__")
return retVal
def unSafeFormatString(value):
retVal = value
if retVal:
retVal = retVal.replace("__LEFT_SQUARE_BRACKET__", "[").replace("__RIGHT_SQUARE_BRACKET__", "]")
return retVal
2008-10-15 19:38:22 +04:00
def setDbms(dbms):
"""
@param dbms: database management system to be set into the knowledge
base as fingerprint.
@type dbms: C{str}
"""
condition = (
not kb.resumedQueries
2008-10-15 19:38:22 +04:00
or ( kb.resumedQueries.has_key(conf.url) and
not kb.resumedQueries[conf.url].has_key("DBMS") )
2008-10-15 19:38:22 +04:00
)
if condition:
dataToSessionFile("[%s][%s][%s][DBMS][%s]\n" % (conf.url, kb.injection.place, safeFormatString(conf.parameters[kb.injection.place]), safeFormatString(dbms)))
2008-10-15 19:38:22 +04:00
firstRegExp = "(%s)" % ("|".join([alias for alias in SUPPORTED_DBMS]))
2008-10-15 19:38:22 +04:00
dbmsRegExp = re.search("^%s" % firstRegExp, dbms, re.I)
if dbmsRegExp:
dbms = dbmsRegExp.group(1)
Backend.setDbms(dbms)
2008-10-15 19:38:22 +04:00
logger.info("the back-end DBMS is %s" % Backend.getDbms())
2008-12-31 00:24:01 +03:00
def setOs():
"""
Example of kb.bannerFp dictionary:
{
'sp': set(['Service Pack 4']),
'dbmsVersion': '8.00.194',
'dbmsServicePack': '0',
'distrib': set(['2000']),
'dbmsRelease': '2000',
'type': set(['Windows'])
}
"""
2011-04-30 17:20:05 +04:00
infoMsg = ""
condition = (
not kb.resumedQueries
or ( kb.resumedQueries.has_key(conf.url) and
not kb.resumedQueries[conf.url].has_key("OS") )
)
if not kb.bannerFp:
return
if "type" in kb.bannerFp:
Backend.setOs(Format.humanize(kb.bannerFp["type"]))
infoMsg = "the back-end DBMS operating system is %s" % Backend.getOs()
if "distrib" in kb.bannerFp:
kb.osVersion = Format.humanize(kb.bannerFp["distrib"])
infoMsg += " %s" % kb.osVersion
if "sp" in kb.bannerFp:
kb.osSP = int(Format.humanize(kb.bannerFp["sp"]).replace("Service Pack ", ""))
elif "sp" not in kb.bannerFp and Backend.isOs(OS.WINDOWS):
kb.osSP = 0
if Backend.getOs() and kb.osVersion and kb.osSP:
infoMsg += " Service Pack %d" % kb.osSP
if infoMsg:
logger.info(infoMsg)
if condition:
dataToSessionFile("[%s][%s][%s][OS][%s]\n" % (conf.url, kb.injection.place, safeFormatString(conf.parameters[kb.injection.place]), Backend.getOs()))
2012-02-27 17:44:07 +04:00
def resumeConfKb(expression, url, value):
if expression == "Dynamic markings" and url == conf.url:
kb.dynamicMarkings = base64unpickle(value[:-1])
2011-04-30 19:29:59 +04:00
infoMsg = "resuming dynamic markings from session file"
logger.info(infoMsg)
2008-10-15 19:38:22 +04:00
elif expression == "DBMS" and url == conf.url:
2011-04-30 17:20:05 +04:00
dbms = unSafeFormatString(value[:-1])
dbms = dbms.lower()
dbmsVersion = [UNKNOWN_DBMS_VERSION]
2008-10-15 19:38:22 +04:00
2011-04-30 19:29:59 +04:00
infoMsg = "resuming back-end DBMS '%s' " % dbms
infoMsg += "from session file"
logger.info(infoMsg)
2008-10-15 19:38:22 +04:00
firstRegExp = "(%s)" % ("|".join([alias for alias in SUPPORTED_DBMS]))
2008-10-15 19:38:22 +04:00
dbmsRegExp = re.search("%s ([\d\.]+)" % firstRegExp, dbms)
if dbmsRegExp:
2011-04-30 17:20:05 +04:00
dbms = dbmsRegExp.group(1)
dbmsVersion = [ dbmsRegExp.group(2) ]
2008-10-15 19:38:22 +04:00
if conf.dbms and conf.dbms.lower() != dbms:
2011-04-30 17:20:05 +04:00
message = "you provided '%s' as back-end DBMS, " % conf.dbms
2008-10-15 19:38:22 +04:00
message += "but from a past scan information on the target URL "
message += "sqlmap assumes the back-end DBMS is %s. " % dbms
message += "Do you really want to force the back-end "
message += "DBMS value? [y/N] "
test = readInput(message, default="N")
if not test or test[0] in ("n", "N"):
conf.dbms = None
Backend.setDbms(dbms)
Backend.setVersionList(dbmsVersion)
2008-10-15 19:38:22 +04:00
else:
Backend.setDbms(dbms)
Backend.setVersionList(dbmsVersion)
2008-10-15 19:38:22 +04:00
elif expression == "OS" and url == conf.url:
2010-10-11 00:51:11 +04:00
os = unSafeFormatString(value[:-1])
if os and os != 'None':
2011-04-30 19:29:59 +04:00
infoMsg = "resuming back-end DBMS operating system '%s' " % os
infoMsg += "from session file"
logger.info(infoMsg)
if conf.os and conf.os.lower() != os.lower():
2011-04-30 17:20:05 +04:00
message = "you provided '%s' as back-end DBMS operating " % conf.os
message += "system, but from a past scan information on the "
message += "target URL sqlmap assumes the back-end DBMS "
message += "operating system is %s. " % os
message += "Do you really want to force the back-end DBMS "
message += "OS value? [y/N] "
test = readInput(message, default="N")
if not test or test[0] in ("n", "N"):
conf.os = os
else:
conf.os = os
Backend.setOs(conf.os)
elif expression == "Remote temp path" and url == conf.url and conf.tmpPath is None:
2011-01-16 02:11:36 +03:00
conf.tmpPath = unSafeFormatString(value[:-1])
2011-04-30 19:29:59 +04:00
infoMsg = "resuming remote absolute path of temporary "
infoMsg += "files directory '%s' from session file" % conf.tmpPath
logger.info(infoMsg)
2011-01-16 02:11:36 +03:00
2011-06-28 01:48:26 +04:00
elif conf.freshQueries:
pass
elif expression == "xp_cmdshell availability" and url == conf.url:
kb.xpCmdshellAvailable = True if unSafeFormatString(value[:-1]).lower() == "true" else False
2011-04-30 19:29:59 +04:00
infoMsg = "resuming xp_cmdshell availability"
logger.info(infoMsg)