2009-04-22 15:48:07 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
This file is part of the sqlmap project, http://sqlmap.sourceforge.net.
|
|
|
|
|
2010-03-03 18:26:27 +03:00
|
|
|
Copyright (c) 2007-2010 Bernardo Damele A. G. <bernardo.damele@gmail.com>
|
2009-04-22 15:48:07 +04:00
|
|
|
Copyright (c) 2006 Daniele Bellucci <daniele.bellucci@gmail.com>
|
|
|
|
|
|
|
|
sqlmap is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation version 2 of the License.
|
|
|
|
|
|
|
|
sqlmap is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with sqlmap; if not, write to the Free Software Foundation, Inc., 51
|
|
|
|
Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
"""
|
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
import re
|
2009-04-22 15:48:07 +04:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
from lib.techniques.outband.stacked import stackedTest
|
|
|
|
|
|
|
|
|
|
|
|
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":
|
|
|
|
# NOTES:
|
|
|
|
#
|
2009-09-26 03:03:45 +04:00
|
|
|
# * The system-wide temporary files directory is
|
|
|
|
# C:\WINDOWS\Temp
|
|
|
|
#
|
|
|
|
# * MySQL runs by default as SYSTEM
|
2009-04-22 15:48:07 +04:00
|
|
|
#
|
|
|
|
# * PostgreSQL runs by default as postgres user and the
|
|
|
|
# temporary files directory is C:\Documents and Settings\postgres\Local Settings\Temp,
|
2009-09-26 03:03:45 +04:00
|
|
|
# however the system-wide folder is writable too
|
|
|
|
#
|
2009-04-22 15:48:07 +04:00
|
|
|
#infoMsg = "retrieving remote absolute path of temporary files "
|
|
|
|
#infoMsg += "directory"
|
|
|
|
#logger.info(infoMsg)
|
|
|
|
#
|
|
|
|
#conf.tmpPath = self.evalCmd("echo %TEMP%")
|
|
|
|
conf.tmpPath = "C:/WINDOWS/Temp"
|
|
|
|
else:
|
|
|
|
conf.tmpPath = "/tmp"
|
|
|
|
|
2010-02-26 15:00:47 +03:00
|
|
|
if re.search("\A[\w]:[\/\\\\]+", conf.tmpPath, re.I):
|
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)
|
|
|
|
|
|
|
|
if kb.dbms == "MySQL":
|
|
|
|
first, last = 1, 6
|
|
|
|
|
|
|
|
elif kb.dbms == "PostgreSQL":
|
|
|
|
first, last = 12, 6
|
|
|
|
|
|
|
|
elif kb.dbms == "Microsoft SQL Server":
|
|
|
|
first, last = 29, 9
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise sqlmapUnsupportedFeatureException, "unsupported DBMS"
|
|
|
|
|
|
|
|
query = queries[kb.dbms].substring % (queries[kb.dbms].banner, first, last)
|
|
|
|
|
2010-03-27 02:23:25 +03:00
|
|
|
if conf.direct:
|
|
|
|
query = "SELECT %s" % query
|
|
|
|
|
2010-03-23 01:57:57 +03:00
|
|
|
kb.bannerFp["dbmsVersion"] = inject.getValue(query, unpack=False)
|
|
|
|
kb.bannerFp["dbmsVersion"] = kb.bannerFp["dbmsVersion"].replace(",", "").replace("-", "").replace(" ", "")
|
|
|
|
|
2009-12-18 01:04:01 +03:00
|
|
|
def delRemoteFile(self, tempFile, doubleslash=False):
|
2009-09-26 03:03:45 +04:00
|
|
|
self.checkDbmsOs()
|
|
|
|
|
|
|
|
if kb.os == "Windows":
|
2010-01-02 05:02:12 +03:00
|
|
|
if doubleslash:
|
2009-09-26 03:03:45 +04:00
|
|
|
tempFile = tempFile.replace("/", "\\\\")
|
|
|
|
else:
|
2010-02-04 17:37:00 +03:00
|
|
|
tempFile = posixToNtSlashes(tempFile)
|
2009-09-26 03:03:45 +04:00
|
|
|
|
|
|
|
cmd = "del /F /Q %s" % tempFile
|
|
|
|
else:
|
|
|
|
cmd = "rm -f %s" % tempFile
|
|
|
|
|
|
|
|
self.execCmd(cmd, forgeCmd=True)
|
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
def createSupportTbl(self, tblName, tblField, tblType):
|
|
|
|
inject.goStacked("DROP TABLE %s" % tblName)
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
|
|
|
stackedTest()
|
|
|
|
|
2010-03-27 02:23:25 +03:00
|
|
|
if not kb.stackedTest 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")
|
|
|
|
inject.goStacked("DROP TABLE %s" % self.fileTblName)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
if not onlyFileTbl:
|
|
|
|
inject.goStacked("DROP TABLE %s" % self.cmdTblName)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
if kb.dbms == "Microsoft SQL Server":
|
|
|
|
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
|
|
|
|
|
|
|
|
if kb.dbms == "PostgreSQL":
|
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)
|
|
|
|
inject.goStacked(dropStr)
|
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
|
|
|
|
message += "[1] as LIKE column names (default)\n"
|
|
|
|
message += "[2] as exact column names"
|
|
|
|
|
|
|
|
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
|