2009-04-22 15:48:07 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
$Id$
|
|
|
|
|
2010-10-14 18:41:14 +04:00
|
|
|
Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/)
|
2010-10-15 03:18:29 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
2009-04-22 15:48:07 +04:00
|
|
|
"""
|
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
import re
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-05-21 19:03:40 +04:00
|
|
|
from lib.core.common import getCompiledRegex
|
2011-01-28 19:36:09 +03:00
|
|
|
from lib.core.common import Backend
|
2010-12-18 18:57:47 +03:00
|
|
|
from lib.core.common import isTechniqueAvailable
|
2010-02-04 17:50:54 +03:00
|
|
|
from lib.core.common import normalizePath
|
2010-02-04 17:37:00 +03:00
|
|
|
from lib.core.common import ntToPosixSlashes
|
|
|
|
from lib.core.common import posixToNtSlashes
|
2009-04-22 15:48:07 +04:00
|
|
|
from lib.core.common import readInput
|
|
|
|
from lib.core.data import conf
|
|
|
|
from lib.core.data import kb
|
|
|
|
from lib.core.data import logger
|
2010-03-23 01:57:57 +03:00
|
|
|
from lib.core.data import queries
|
2010-11-08 12:20:02 +03:00
|
|
|
from lib.core.enums import DBMS
|
2010-12-18 18:57:47 +03:00
|
|
|
from lib.core.enums import PAYLOAD
|
2010-05-17 00:46:17 +04:00
|
|
|
from lib.core.exception import sqlmapNoneDataException
|
2010-03-23 01:57:57 +03:00
|
|
|
from lib.core.exception import sqlmapUnsupportedFeatureException
|
2009-04-22 15:48:07 +04:00
|
|
|
from lib.core.session import setRemoteTempPath
|
|
|
|
from lib.request import inject
|
|
|
|
|
|
|
|
class Miscellaneous:
|
|
|
|
"""
|
|
|
|
This class defines miscellaneous functionalities for plugins.
|
|
|
|
"""
|
|
|
|
|
2010-03-23 01:57:57 +03:00
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
def getRemoteTempPath(self):
|
|
|
|
if not conf.tmpPath:
|
|
|
|
if kb.os == "Windows":
|
2010-05-29 19:27:49 +04:00
|
|
|
conf.tmpPath = "C:/WINDOWS/Temp"
|
2009-04-22 15:48:07 +04:00
|
|
|
else:
|
|
|
|
conf.tmpPath = "/tmp"
|
|
|
|
|
2010-05-21 19:03:40 +04:00
|
|
|
if getCompiledRegex("(?i)\A[\w]:[\/\\\\]+").search(conf.tmpPath):
|
2009-04-22 15:48:07 +04:00
|
|
|
kb.os = "Windows"
|
|
|
|
|
2010-02-04 17:50:54 +03:00
|
|
|
conf.tmpPath = normalizePath(conf.tmpPath)
|
2010-04-23 20:34:20 +04:00
|
|
|
conf.tmpPath = ntToPosixSlashes(conf.tmpPath)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
setRemoteTempPath()
|
|
|
|
|
2010-03-23 01:57:57 +03:00
|
|
|
def getVersionFromBanner(self):
|
|
|
|
if "dbmsVersion" in kb.bannerFp:
|
|
|
|
return
|
|
|
|
|
|
|
|
infoMsg = "detecting back-end DBMS version from its banner"
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
if Backend.getIdentifiedDbms() == DBMS.MYSQL:
|
2010-03-23 01:57:57 +03:00
|
|
|
first, last = 1, 6
|
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
elif Backend.getIdentifiedDbms() == DBMS.PGSQL:
|
2010-03-23 01:57:57 +03:00
|
|
|
first, last = 12, 6
|
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
elif Backend.getIdentifiedDbms() == DBMS.MSSQL:
|
2010-03-23 01:57:57 +03:00
|
|
|
first, last = 29, 9
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise sqlmapUnsupportedFeatureException, "unsupported DBMS"
|
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
query = queries[Backend.getIdentifiedDbms()].substring.query % (queries[Backend.getIdentifiedDbms()].banner.query, first, last)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2010-03-27 02:23:25 +03:00
|
|
|
if conf.direct:
|
|
|
|
query = "SELECT %s" % query
|
|
|
|
|
2011-02-21 19:00:56 +03:00
|
|
|
kb.bannerFp["dbmsVersion"] = inject.getValue(query)
|
2010-03-23 01:57:57 +03:00
|
|
|
kb.bannerFp["dbmsVersion"] = kb.bannerFp["dbmsVersion"].replace(",", "").replace("-", "").replace(" ", "")
|
|
|
|
|
2010-10-28 04:19:40 +04:00
|
|
|
def delRemoteFile(self, tempFile):
|
2009-09-26 03:03:45 +04:00
|
|
|
self.checkDbmsOs()
|
|
|
|
|
|
|
|
if kb.os == "Windows":
|
2010-10-28 04:19:40 +04:00
|
|
|
tempFile = posixToNtSlashes(tempFile)
|
2009-09-26 03:03:45 +04:00
|
|
|
cmd = "del /F /Q %s" % tempFile
|
|
|
|
else:
|
|
|
|
cmd = "rm -f %s" % tempFile
|
|
|
|
|
2010-10-29 17:09:53 +04:00
|
|
|
self.execCmd(cmd, silent=True)
|
2009-09-26 03:03:45 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
def createSupportTbl(self, tblName, tblField, tblType):
|
2010-10-29 17:09:53 +04:00
|
|
|
inject.goStacked("DROP TABLE %s" % tblName, silent=True)
|
2010-01-02 05:02:12 +03:00
|
|
|
inject.goStacked("CREATE TABLE %s(%s %s)" % (tblName, tblField, tblType))
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
def cleanup(self, onlyFileTbl=False, udfDict=None):
|
2009-04-22 15:48:07 +04:00
|
|
|
"""
|
|
|
|
Cleanup database from sqlmap create tables and functions
|
|
|
|
"""
|
|
|
|
|
2010-12-18 18:57:47 +03:00
|
|
|
if not isTechniqueAvailable(PAYLOAD.TECHNIQUE.STACKED) and not conf.direct:
|
2009-04-22 15:48:07 +04:00
|
|
|
return
|
|
|
|
|
|
|
|
if kb.os == "Windows":
|
|
|
|
libtype = "dynamic-link library"
|
|
|
|
|
|
|
|
elif kb.os == "Linux":
|
|
|
|
libtype = "shared object"
|
|
|
|
|
|
|
|
else:
|
|
|
|
libtype = "shared library"
|
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
if onlyFileTbl:
|
2009-04-22 15:48:07 +04:00
|
|
|
logger.debug("cleaning up the database management system")
|
|
|
|
else:
|
|
|
|
logger.info("cleaning up the database management system")
|
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
logger.debug("removing support tables")
|
2010-10-29 17:09:53 +04:00
|
|
|
inject.goStacked("DROP TABLE %s" % self.fileTblName, silent=True)
|
2011-04-10 17:48:29 +04:00
|
|
|
inject.goStacked("DROP TABLE %shex" % self.fileTblName, silent=True)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
if not onlyFileTbl:
|
2010-10-29 17:09:53 +04:00
|
|
|
inject.goStacked("DROP TABLE %s" % self.cmdTblName, silent=True)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
if Backend.getIdentifiedDbms() == DBMS.MSSQL:
|
2009-04-22 15:48:07 +04:00
|
|
|
return
|
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
if udfDict is None:
|
|
|
|
udfDict = self.sysUdfs
|
|
|
|
|
|
|
|
for udf, inpRet in udfDict.items():
|
2010-01-02 05:02:12 +03:00
|
|
|
message = "do you want to remove UDF '%s'? [Y/n] " % udf
|
2009-04-22 15:48:07 +04:00
|
|
|
output = readInput(message, default="Y")
|
|
|
|
|
|
|
|
if not output or output in ("y", "Y"):
|
|
|
|
dropStr = "DROP FUNCTION %s" % udf
|
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
if Backend.getIdentifiedDbms() == DBMS.PGSQL:
|
2009-09-26 03:03:45 +04:00
|
|
|
inp = ", ".join(i for i in inpRet["input"])
|
|
|
|
dropStr += "(%s)" % inp
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
logger.debug("removing UDF '%s'" % udf)
|
2010-10-29 17:09:53 +04:00
|
|
|
inject.goStacked(dropStr, silent=True)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
logger.info("database management system cleanup finished")
|
|
|
|
|
|
|
|
warnMsg = "remember that UDF %s files " % libtype
|
|
|
|
|
|
|
|
if conf.osPwn:
|
|
|
|
warnMsg += "and Metasploit related files in the temporary "
|
|
|
|
warnMsg += "folder "
|
|
|
|
|
|
|
|
warnMsg += "saved on the file system can only be deleted "
|
2010-01-02 05:02:12 +03:00
|
|
|
warnMsg += "manually"
|
2009-04-22 15:48:07 +04:00
|
|
|
logger.warn(warnMsg)
|
2010-05-07 17:40:57 +04:00
|
|
|
|
|
|
|
def likeOrExact(self, what):
|
|
|
|
message = "do you want sqlmap to consider provided %s(s):\n" % what
|
2010-05-13 13:51:15 +04:00
|
|
|
message += "[1] as LIKE %s names (default)\n" % what
|
|
|
|
message += "[2] as exact %s names" % what
|
2010-05-07 17:40:57 +04:00
|
|
|
|
|
|
|
choice = readInput(message, default="1")
|
|
|
|
|
|
|
|
if not choice or choice == "1":
|
|
|
|
choice = "1"
|
|
|
|
condParam = " LIKE '%%%s%%'"
|
|
|
|
elif choice.isdigit() and choice == "2":
|
|
|
|
condParam = "='%s'"
|
|
|
|
else:
|
|
|
|
errMsg = "invalid value"
|
|
|
|
raise sqlmapNoneDataException, errMsg
|
|
|
|
|
|
|
|
return choice, condParam
|