2009-04-22 15:48:07 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
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
|
2009-04-22 15:48:07 +04:00
|
|
|
"""
|
|
|
|
|
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
|
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
|
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
|
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.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:
|
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
|
|
|
|
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)
|
|
|
|
|
2011-04-30 18:54:29 +04:00
|
|
|
if Backend.isDbms(DBMS.MYSQL):
|
2010-03-23 01:57:57 +03:00
|
|
|
first, last = 1, 6
|
|
|
|
|
2011-04-30 18:54:29 +04:00
|
|
|
elif Backend.isDbms(DBMS.PGSQL):
|
2010-03-23 01:57:57 +03:00
|
|
|
first, last = 12, 6
|
|
|
|
|
2011-04-30 18:54:29 +04:00
|
|
|
elif Backend.isDbms(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)
|
2012-02-09 13:48:47 +04:00
|
|
|
kb.bannerFp["dbmsVersion"] = (kb.bannerFp["dbmsVersion"] or "").replace(",", "").replace("-", "").replace(" ", "")
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2012-07-11 16:39:33 +04:00
|
|
|
def delRemoteFile(self, filename):
|
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)
|
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
|
|
|
|
|
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):
|
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
|
2011-04-30 17:20:05 +04:00
|
|
|
output = readInput(message, default="Y")
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
if not output or output in ("y", "Y"):
|
|
|
|
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
|
2011-07-04 19:23:05 +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-06-28 20:47:55 +04:00
|
|
|
choice = readInput(message, default='1') if not conf.disableLike else '2'
|
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"
|
|
|
|
raise sqlmapNoneDataException, errMsg
|
|
|
|
|
|
|
|
return choice, condParam
|