2019-05-08 13:47:52 +03:00
|
|
|
#!/usr/bin/env python
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
"""
|
2020-12-31 13:46:27 +03:00
|
|
|
Copyright (c) 2006-2021 sqlmap developers (http://sqlmap.org/)
|
2017-10-11 15:50:46 +03:00
|
|
|
See the file 'LICENSE' for copying permission
|
2009-04-22 15:48:07 +04:00
|
|
|
"""
|
|
|
|
|
2013-01-30 17:38:21 +04:00
|
|
|
import ntpath
|
2012-04-03 18:34:15 +04:00
|
|
|
import re
|
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
from lib.core.common import Backend
|
2012-02-25 14:43:10 +04:00
|
|
|
from lib.core.common import hashDBWrite
|
2013-02-13 12:57:16 +04:00
|
|
|
from lib.core.common import isStackingAvailable
|
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
|
2013-02-14 17:18:15 +04:00
|
|
|
from lib.core.common import singleTimeDebugMessage
|
2012-10-25 11:42:43 +04:00
|
|
|
from lib.core.common import unArrayizeValue
|
2009-04-22 15:48:07 +04:00
|
|
|
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
|
2012-02-25 14:43:10 +04:00
|
|
|
from lib.core.enums import HASHDB_KEYS
|
2011-04-23 20:25:09 +04:00
|
|
|
from lib.core.enums import OS
|
2012-12-06 17:14:19 +04:00
|
|
|
from lib.core.exception import SqlmapNoneDataException
|
2009-04-22 15:48:07 +04:00
|
|
|
from lib.request import inject
|
|
|
|
|
2019-05-29 17:42:04 +03:00
|
|
|
class Miscellaneous(object):
|
2009-04-22 15:48:07 +04:00
|
|
|
"""
|
|
|
|
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):
|
2013-01-30 17:38:21 +04:00
|
|
|
if not conf.tmpPath and Backend.isDbms(DBMS.MSSQL):
|
2013-02-13 16:46:45 +04: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 17:07:47 +04:00
|
|
|
logger.debug(debugMsg)
|
2013-02-13 16:46:45 +04:00
|
|
|
|
2013-01-30 17:38:21 +04:00
|
|
|
_ = unArrayizeValue(inject.getValue("SELECT SERVERPROPERTY('ErrorLogFileName')", safeCharEncode=False))
|
2013-02-13 16:46:45 +04:00
|
|
|
|
2013-01-30 17:38:21 +04:00
|
|
|
if _:
|
|
|
|
conf.tmpPath = ntpath.dirname(_)
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
if not conf.tmpPath:
|
2011-04-23 20:25:09 +04:00
|
|
|
if Backend.isOs(OS.WINDOWS):
|
2012-02-20 15:06:55 +04:00
|
|
|
if conf.direct:
|
|
|
|
conf.tmpPath = "%TEMP%"
|
2012-02-03 19:28:11 +04:00
|
|
|
else:
|
2012-02-20 15:06:55 +04:00
|
|
|
self.checkDbmsOs(detailed=True)
|
|
|
|
|
|
|
|
if Backend.getOsVersion() in ("2000", "NT"):
|
|
|
|
conf.tmpPath = "C:/WINNT/Temp"
|
2012-03-15 02:39:46 +04:00
|
|
|
elif Backend.isOs("XP"):
|
2012-03-15 03:44:29 +04:00
|
|
|
conf.tmpPath = "C:/Documents and Settings/All Users/Application Data/Temp"
|
2012-02-20 15:06:55 +04:00
|
|
|
else:
|
2012-03-15 02:39:46 +04:00
|
|
|
conf.tmpPath = "C:/Windows/Temp"
|
2009-04-22 15:48:07 +04:00
|
|
|
else:
|
|
|
|
conf.tmpPath = "/tmp"
|
|
|
|
|
2012-04-03 18:34:15 +04:00
|
|
|
if re.search(r"\A[\w]:[\/\\]+", conf.tmpPath, re.I):
|
2011-04-23 20:25:09 +04:00
|
|
|
Backend.setOs(OS.WINDOWS)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
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
|
|
|
|
2016-01-04 14:08:56 +03:00
|
|
|
singleTimeDebugMessage("going to use '%s' as temporary files directory" % conf.tmpPath)
|
2013-02-13 16:46:45 +04:00
|
|
|
|
2012-02-25 14:43:10 +04:00
|
|
|
hashDBWrite(HASHDB_KEYS.CONF_TMP_PATH, conf.tmpPath)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2012-03-09 18:21:41 +04:00
|
|
|
return conf.tmpPath
|
|
|
|
|
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)
|
|
|
|
|
2021-01-19 14:25:03 +03:00
|
|
|
query = queries[Backend.getIdentifiedDbms()].banner.query
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2010-03-27 02:23:25 +03:00
|
|
|
if conf.direct:
|
|
|
|
query = "SELECT %s" % query
|
|
|
|
|
2021-01-19 14:25:03 +03: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-23 01:57:57 +03:00
|
|
|
|
2012-07-11 16:39:33 +04:00
|
|
|
def delRemoteFile(self, filename):
|
2012-07-13 16:25:39 +04:00
|
|
|
if not filename:
|
|
|
|
return
|
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
self.checkDbmsOs()
|
|
|
|
|
2011-04-23 20:25:09 +04:00
|
|
|
if Backend.isOs(OS.WINDOWS):
|
2012-07-11 16:39:33 +04:00
|
|
|
filename = posixToNtSlashes(filename)
|
|
|
|
cmd = "del /F /Q %s" % filename
|
2009-09-26 03:03:45 +04:00
|
|
|
else:
|
2012-07-11 16:39:33 +04:00
|
|
|
cmd = "rm -f %s" % filename
|
2009-09-26 03:03:45 +04:00
|
|
|
|
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)
|
2013-02-14 16:39:17 +04: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 15:48:07 +04:00
|
|
|
|
2012-07-11 17:07:20 +04:00
|
|
|
def cleanup(self, onlyFileTbl=False, udfDict=None, web=False):
|
2009-04-22 15:48:07 +04:00
|
|
|
"""
|
2012-07-11 17:07:20 +04:00
|
|
|
Cleanup file system and database from sqlmap create files, tables
|
|
|
|
and functions
|
2009-04-22 15:48:07 +04:00
|
|
|
"""
|
|
|
|
|
2012-07-16 20:58:30 +04:00
|
|
|
if web and self.webBackdoorFilePath:
|
2012-07-11 17:07:20 +04:00
|
|
|
logger.info("cleaning up the web files uploaded")
|
|
|
|
|
|
|
|
self.delRemoteFile(self.webStagerFilePath)
|
|
|
|
self.delRemoteFile(self.webBackdoorFilePath)
|
|
|
|
|
2019-10-17 16:16:21 +03:00
|
|
|
if (not isStackingAvailable() or kb.udfFail) and not conf.direct:
|
2009-04-22 15:48:07 +04:00
|
|
|
return
|
|
|
|
|
2019-03-22 15:49:52 +03:00
|
|
|
if any((conf.osCmd, conf.osShell)) and Backend.isDbms(DBMS.PGSQL) and kb.copyExecTest:
|
|
|
|
return
|
|
|
|
|
2011-04-23 20:25:09 +04:00
|
|
|
if Backend.isOs(OS.WINDOWS):
|
2009-04-22 15:48:07 +04:00
|
|
|
libtype = "dynamic-link library"
|
|
|
|
|
2011-04-23 20:25:09 +04:00
|
|
|
elif Backend.isOs(OS.LINUX):
|
2009-04-22 15:48:07 +04:00
|
|
|
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-04-30 18:54:29 +04:00
|
|
|
if Backend.isDbms(DBMS.MSSQL):
|
2019-06-03 11:41:51 +03:00
|
|
|
udfDict = {"master..new_xp_cmdshell": {}}
|
2009-04-22 15:48:07 +04:00
|
|
|
|
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
|
|
|
|
2017-04-18 16:48:05 +03:00
|
|
|
if readInput(message, default='Y', boolean=True):
|
2009-04-22 15:48:07 +04:00
|
|
|
dropStr = "DROP FUNCTION %s" % udf
|
|
|
|
|
2011-04-30 18:54:29 +04:00
|
|
|
if Backend.isDbms(DBMS.PGSQL):
|
2011-04-30 17:20:05 +04:00
|
|
|
inp = ", ".join(i for i in inpRet["input"])
|
2009-09-26 03:03:45 +04:00
|
|
|
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
|
2012-12-18 17:55:26 +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
|
|
|
|
2012-12-18 17:55:26 +04:00
|
|
|
choice = readInput(message, default='1')
|
2010-05-07 17:40:57 +04:00
|
|
|
|
2012-06-04 13:24:46 +04:00
|
|
|
if not choice or choice == '1':
|
|
|
|
choice = '1'
|
2011-07-04 19:23:05 +04:00
|
|
|
condParam = " LIKE '%%%s%%'"
|
2012-06-04 13:24:46 +04:00
|
|
|
elif choice == '2':
|
2010-05-07 17:40:57 +04:00
|
|
|
condParam = "='%s'"
|
|
|
|
else:
|
|
|
|
errMsg = "invalid value"
|
2013-01-04 02:20:55 +04:00
|
|
|
raise SqlmapNoneDataException(errMsg)
|
2010-05-07 17:40:57 +04:00
|
|
|
|
|
|
|
return choice, condParam
|