2019-05-08 12:47:52 +02:00
|
|
|
#!/usr/bin/env python
|
2009-04-22 11:48:07 +00:00
|
|
|
|
|
|
|
|
"""
|
2024-01-03 23:11:52 +01:00
|
|
|
Copyright (c) 2006-2024 sqlmap developers (https://sqlmap.org/)
|
2017-10-11 14:50:46 +02:00
|
|
|
See the file 'LICENSE' for copying permission
|
2009-04-22 11:48:07 +00:00
|
|
|
"""
|
|
|
|
|
|
2013-01-30 14:38:21 +01:00
|
|
|
import ntpath
|
2012-04-03 14:34:15 +00:00
|
|
|
import re
|
|
|
|
|
|
2011-01-28 16:36:09 +00:00
|
|
|
from lib.core.common import Backend
|
2012-02-25 10:43:10 +00:00
|
|
|
from lib.core.common import hashDBWrite
|
2013-02-13 09:57:16 +01:00
|
|
|
from lib.core.common import isStackingAvailable
|
2010-02-04 14:50:54 +00:00
|
|
|
from lib.core.common import normalizePath
|
2010-02-04 14:37:00 +00:00
|
|
|
from lib.core.common import ntToPosixSlashes
|
|
|
|
|
from lib.core.common import posixToNtSlashes
|
2009-04-22 11:48:07 +00:00
|
|
|
from lib.core.common import readInput
|
2013-02-14 13:18:15 +00:00
|
|
|
from lib.core.common import singleTimeDebugMessage
|
2012-10-25 09:42:43 +02:00
|
|
|
from lib.core.common import unArrayizeValue
|
2009-04-22 11:48:07 +00:00
|
|
|
from lib.core.data import conf
|
|
|
|
|
from lib.core.data import kb
|
|
|
|
|
from lib.core.data import logger
|
2010-03-22 22:57:57 +00:00
|
|
|
from lib.core.data import queries
|
2010-11-08 09:20:02 +00:00
|
|
|
from lib.core.enums import DBMS
|
2012-02-25 10:43:10 +00:00
|
|
|
from lib.core.enums import HASHDB_KEYS
|
2011-04-23 16:25:09 +00:00
|
|
|
from lib.core.enums import OS
|
2012-12-06 14:14:19 +01:00
|
|
|
from lib.core.exception import SqlmapNoneDataException
|
2009-04-22 11:48:07 +00:00
|
|
|
from lib.request import inject
|
|
|
|
|
|
2019-05-29 16:42:04 +02:00
|
|
|
class Miscellaneous(object):
|
2009-04-22 11:48:07 +00:00
|
|
|
"""
|
|
|
|
|
This class defines miscellaneous functionalities for plugins.
|
|
|
|
|
"""
|
|
|
|
|
|
2010-03-22 22:57:57 +00:00
|
|
|
def __init__(self):
|
|
|
|
|
pass
|
|
|
|
|
|
2009-04-22 11:48:07 +00:00
|
|
|
def getRemoteTempPath(self):
|
2013-01-30 14:38:21 +01:00
|
|
|
if not conf.tmpPath and Backend.isDbms(DBMS.MSSQL):
|
2013-02-13 12:46:45 +00:00
|
|
|
debugMsg = "identifying Microsoft SQL Server error log directory "
|
|
|
|
|
debugMsg += "that sqlmap will use to store temporary files with "
|
|
|
|
|
debugMsg += "commands' output"
|
2013-02-13 13:07:47 +00:00
|
|
|
logger.debug(debugMsg)
|
2013-02-13 12:46:45 +00:00
|
|
|
|
2013-01-30 14:38:21 +01:00
|
|
|
_ = unArrayizeValue(inject.getValue("SELECT SERVERPROPERTY('ErrorLogFileName')", safeCharEncode=False))
|
2013-02-13 12:46:45 +00:00
|
|
|
|
2013-01-30 14:38:21 +01:00
|
|
|
if _:
|
|
|
|
|
conf.tmpPath = ntpath.dirname(_)
|
|
|
|
|
|
2009-04-22 11:48:07 +00:00
|
|
|
if not conf.tmpPath:
|
2011-04-23 16:25:09 +00:00
|
|
|
if Backend.isOs(OS.WINDOWS):
|
2012-02-20 11:06:55 +00:00
|
|
|
if conf.direct:
|
|
|
|
|
conf.tmpPath = "%TEMP%"
|
2012-02-03 15:28:11 +00:00
|
|
|
else:
|
2012-02-20 11:06:55 +00:00
|
|
|
self.checkDbmsOs(detailed=True)
|
|
|
|
|
|
|
|
|
|
if Backend.getOsVersion() in ("2000", "NT"):
|
|
|
|
|
conf.tmpPath = "C:/WINNT/Temp"
|
2012-03-14 22:39:46 +00:00
|
|
|
elif Backend.isOs("XP"):
|
2012-03-14 23:44:29 +00:00
|
|
|
conf.tmpPath = "C:/Documents and Settings/All Users/Application Data/Temp"
|
2012-02-20 11:06:55 +00:00
|
|
|
else:
|
2012-03-14 22:39:46 +00:00
|
|
|
conf.tmpPath = "C:/Windows/Temp"
|
2009-04-22 11:48:07 +00:00
|
|
|
else:
|
|
|
|
|
conf.tmpPath = "/tmp"
|
|
|
|
|
|
2012-04-03 14:34:15 +00:00
|
|
|
if re.search(r"\A[\w]:[\/\\]+", conf.tmpPath, re.I):
|
2011-04-23 16:25:09 +00:00
|
|
|
Backend.setOs(OS.WINDOWS)
|
2009-04-22 11:48:07 +00:00
|
|
|
|
2010-02-04 14:50:54 +00:00
|
|
|
conf.tmpPath = normalizePath(conf.tmpPath)
|
2010-04-23 16:34:20 +00:00
|
|
|
conf.tmpPath = ntToPosixSlashes(conf.tmpPath)
|
2009-04-22 11:48:07 +00:00
|
|
|
|
2016-01-04 12:08:56 +01:00
|
|
|
singleTimeDebugMessage("going to use '%s' as temporary files directory" % conf.tmpPath)
|
2013-02-13 12:46:45 +00:00
|
|
|
|
2012-02-25 10:43:10 +00:00
|
|
|
hashDBWrite(HASHDB_KEYS.CONF_TMP_PATH, conf.tmpPath)
|
2009-04-22 11:48:07 +00:00
|
|
|
|
2012-03-09 14:21:41 +00:00
|
|
|
return conf.tmpPath
|
|
|
|
|
|
2010-03-22 22:57:57 +00:00
|
|
|
def getVersionFromBanner(self):
|
|
|
|
|
if "dbmsVersion" in kb.bannerFp:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
infoMsg = "detecting back-end DBMS version from its banner"
|
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
2021-01-19 12:25:03 +01:00
|
|
|
query = queries[Backend.getIdentifiedDbms()].banner.query
|
2010-03-22 22:57:57 +00:00
|
|
|
|
2010-03-26 23:23:25 +00:00
|
|
|
if conf.direct:
|
|
|
|
|
query = "SELECT %s" % query
|
|
|
|
|
|
2021-01-19 12:25:03 +01:00
|
|
|
kb.bannerFp["dbmsVersion"] = unArrayizeValue(inject.getValue(query)) or ""
|
|
|
|
|
|
|
|
|
|
match = re.search(r"\d[\d.-]*", kb.bannerFp["dbmsVersion"])
|
|
|
|
|
if match:
|
|
|
|
|
kb.bannerFp["dbmsVersion"] = match.group(0)
|
2010-03-22 22:57:57 +00:00
|
|
|
|
2012-07-11 13:39:33 +01:00
|
|
|
def delRemoteFile(self, filename):
|
2012-07-13 14:25:39 +02:00
|
|
|
if not filename:
|
|
|
|
|
return
|
|
|
|
|
|
2009-09-25 23:03:45 +00:00
|
|
|
self.checkDbmsOs()
|
|
|
|
|
|
2011-04-23 16:25:09 +00:00
|
|
|
if Backend.isOs(OS.WINDOWS):
|
2012-07-11 13:39:33 +01:00
|
|
|
filename = posixToNtSlashes(filename)
|
|
|
|
|
cmd = "del /F /Q %s" % filename
|
2009-09-25 23:03:45 +00:00
|
|
|
else:
|
2012-07-11 13:39:33 +01:00
|
|
|
cmd = "rm -f %s" % filename
|
2009-09-25 23:03:45 +00:00
|
|
|
|
2010-10-29 13:09:53 +00:00
|
|
|
self.execCmd(cmd, silent=True)
|
2009-09-25 23:03:45 +00:00
|
|
|
|
2010-01-02 02:02:12 +00:00
|
|
|
def createSupportTbl(self, tblName, tblField, tblType):
|
2010-10-29 13:09:53 +00:00
|
|
|
inject.goStacked("DROP TABLE %s" % tblName, silent=True)
|
2013-02-14 12:39:17 +00:00
|
|
|
|
|
|
|
|
if Backend.isDbms(DBMS.MSSQL) and tblName == self.cmdTblName:
|
|
|
|
|
inject.goStacked("CREATE TABLE %s(id INT PRIMARY KEY IDENTITY, %s %s)" % (tblName, tblField, tblType))
|
|
|
|
|
else:
|
|
|
|
|
inject.goStacked("CREATE TABLE %s(%s %s)" % (tblName, tblField, tblType))
|
2009-04-22 11:48:07 +00:00
|
|
|
|
2012-07-11 14:07:20 +01:00
|
|
|
def cleanup(self, onlyFileTbl=False, udfDict=None, web=False):
|
2009-04-22 11:48:07 +00:00
|
|
|
"""
|
2012-07-11 14:07:20 +01:00
|
|
|
Cleanup file system and database from sqlmap create files, tables
|
|
|
|
|
and functions
|
2009-04-22 11:48:07 +00:00
|
|
|
"""
|
|
|
|
|
|
2012-07-16 17:58:30 +01:00
|
|
|
if web and self.webBackdoorFilePath:
|
2012-07-11 14:07:20 +01:00
|
|
|
logger.info("cleaning up the web files uploaded")
|
|
|
|
|
|
|
|
|
|
self.delRemoteFile(self.webStagerFilePath)
|
|
|
|
|
self.delRemoteFile(self.webBackdoorFilePath)
|
|
|
|
|
|
2019-10-17 15:16:21 +02:00
|
|
|
if (not isStackingAvailable() or kb.udfFail) and not conf.direct:
|
2009-04-22 11:48:07 +00:00
|
|
|
return
|
|
|
|
|
|
2019-03-22 13:49:52 +01:00
|
|
|
if any((conf.osCmd, conf.osShell)) and Backend.isDbms(DBMS.PGSQL) and kb.copyExecTest:
|
|
|
|
|
return
|
|
|
|
|
|
2011-04-23 16:25:09 +00:00
|
|
|
if Backend.isOs(OS.WINDOWS):
|
2009-04-22 11:48:07 +00:00
|
|
|
libtype = "dynamic-link library"
|
|
|
|
|
|
2011-04-23 16:25:09 +00:00
|
|
|
elif Backend.isOs(OS.LINUX):
|
2009-04-22 11:48:07 +00:00
|
|
|
libtype = "shared object"
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
libtype = "shared library"
|
|
|
|
|
|
2010-01-02 02:02:12 +00:00
|
|
|
if onlyFileTbl:
|
2009-04-22 11:48:07 +00:00
|
|
|
logger.debug("cleaning up the database management system")
|
|
|
|
|
else:
|
|
|
|
|
logger.info("cleaning up the database management system")
|
|
|
|
|
|
2010-01-02 02:02:12 +00:00
|
|
|
logger.debug("removing support tables")
|
2010-10-29 13:09:53 +00:00
|
|
|
inject.goStacked("DROP TABLE %s" % self.fileTblName, silent=True)
|
2011-04-10 13:48:29 +00:00
|
|
|
inject.goStacked("DROP TABLE %shex" % self.fileTblName, silent=True)
|
2009-04-22 11:48:07 +00:00
|
|
|
|
2010-01-02 02:02:12 +00:00
|
|
|
if not onlyFileTbl:
|
2010-10-29 13:09:53 +00:00
|
|
|
inject.goStacked("DROP TABLE %s" % self.cmdTblName, silent=True)
|
2009-04-22 11:48:07 +00:00
|
|
|
|
2011-04-30 14:54:29 +00:00
|
|
|
if Backend.isDbms(DBMS.MSSQL):
|
2019-06-03 10:41:51 +02:00
|
|
|
udfDict = {"master..new_xp_cmdshell": {}}
|
2009-04-22 11:48:07 +00:00
|
|
|
|
2009-09-25 23:03:45 +00:00
|
|
|
if udfDict is None:
|
2021-07-28 00:34:10 +02:00
|
|
|
udfDict = getattr(self, "sysUdfs", {})
|
2009-09-25 23:03:45 +00:00
|
|
|
|
|
|
|
|
for udf, inpRet in udfDict.items():
|
2010-01-02 02:02:12 +00:00
|
|
|
message = "do you want to remove UDF '%s'? [Y/n] " % udf
|
2009-04-22 11:48:07 +00:00
|
|
|
|
2017-04-18 15:48:05 +02:00
|
|
|
if readInput(message, default='Y', boolean=True):
|
2009-04-22 11:48:07 +00:00
|
|
|
dropStr = "DROP FUNCTION %s" % udf
|
|
|
|
|
|
2011-04-30 14:54:29 +00:00
|
|
|
if Backend.isDbms(DBMS.PGSQL):
|
2011-04-30 13:20:05 +00:00
|
|
|
inp = ", ".join(i for i in inpRet["input"])
|
2009-09-25 23:03:45 +00:00
|
|
|
dropStr += "(%s)" % inp
|
2009-04-22 11:48:07 +00:00
|
|
|
|
2010-01-02 02:02:12 +00:00
|
|
|
logger.debug("removing UDF '%s'" % udf)
|
2010-10-29 13:09:53 +00:00
|
|
|
inject.goStacked(dropStr, silent=True)
|
2009-04-22 11:48:07 +00: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 02:02:12 +00:00
|
|
|
warnMsg += "manually"
|
2022-06-22 12:04:34 +02:00
|
|
|
logger.warning(warnMsg)
|
2010-05-07 13:40:57 +00:00
|
|
|
|
|
|
|
|
def likeOrExact(self, what):
|
|
|
|
|
message = "do you want sqlmap to consider provided %s(s):\n" % what
|
2012-12-18 13:55:26 +00:00
|
|
|
message += "[1] as LIKE %s names (default)\n" % what
|
|
|
|
|
message += "[2] as exact %s names" % what
|
2010-05-07 13:40:57 +00:00
|
|
|
|
2012-12-18 13:55:26 +00:00
|
|
|
choice = readInput(message, default='1')
|
2010-05-07 13:40:57 +00:00
|
|
|
|
2012-06-04 09:24:46 +00:00
|
|
|
if not choice or choice == '1':
|
|
|
|
|
choice = '1'
|
2011-07-04 15:23:05 +00:00
|
|
|
condParam = " LIKE '%%%s%%'"
|
2012-06-04 09:24:46 +00:00
|
|
|
elif choice == '2':
|
2010-05-07 13:40:57 +00:00
|
|
|
condParam = "='%s'"
|
|
|
|
|
else:
|
|
|
|
|
errMsg = "invalid value"
|
2013-01-03 23:20:55 +01:00
|
|
|
raise SqlmapNoneDataException(errMsg)
|
2010-05-07 13:40:57 +00:00
|
|
|
|
|
|
|
|
return choice, condParam
|