From 605b126758b43d4811e058778981629bdb64b99a Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Wed, 26 Nov 2014 13:38:21 +0100 Subject: [PATCH] Patch for an Issue #976 --- lib/core/common.py | 12 ++++++------ lib/core/dump.py | 2 +- lib/parse/configfile.py | 3 ++- lib/utils/crawler.py | 3 ++- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/core/common.py b/lib/core/common.py index 53b425af4..f954af023 100755 --- a/lib/core/common.py +++ b/lib/core/common.py @@ -861,7 +861,7 @@ def dataToOutFile(filename, data): retVal = os.path.join(conf.filePath, filePathToSafeString(filename)) try: - with codecs.open(retVal, "wb", UNICODE_ENCODING) as f: + with openFile(retVal, "wb") as f: f.write(data) except IOError, ex: errMsg = "something went wrong while trying to write " @@ -1813,7 +1813,7 @@ def readCachedFileContent(filename, mode='rb'): with kb.locks.cache: if filename not in kb.cache.content: checkFile(filename) - with codecs.open(filename, mode, UNICODE_ENCODING) as f: + with openFile(filename, mode) as f: kb.cache.content[filename] = f.read() return kb.cache.content[filename] @@ -1878,7 +1878,7 @@ def initCommonOutputs(): kb.commonOutputs = {} key = None - with codecs.open(paths.COMMON_OUTPUTS, 'r', UNICODE_ENCODING) as f: + with openFile(paths.COMMON_OUTPUTS, 'r') as f: for line in f.readlines(): # xreadlines doesn't return unicode strings when codec.open() is used if line.find('#') != -1: line = line[:line.find('#')] @@ -1905,7 +1905,7 @@ def getFileItems(filename, commentPrefix='#', unicode_=True, lowercase=False, un checkFile(filename) try: - with codecs.open(filename, 'r', UNICODE_ENCODING, errors="ignore") if unicode_ else open(filename, 'r') as f: + with openFile(filename, 'r', errors="ignore") if unicode_ else open(filename, 'r') as f: for line in (f.readlines() if unicode_ else f.xreadlines()): # xreadlines doesn't return unicode strings when codec.open() is used if commentPrefix: if line.find(commentPrefix) != -1: @@ -2822,13 +2822,13 @@ def showHttpErrorCodes(): for code, count in kb.httpErrorCodes.items()) logger.warn(warnMsg) -def openFile(filename, mode='r'): +def openFile(filename, mode='r', encoding=UNICODE_ENCODING, errors="replace"): """ Returns file handle of a given filename """ try: - return codecs.open(filename, mode, UNICODE_ENCODING, "replace") + return codecs.open(filename, mode, encoding, errors) except IOError: errMsg = "there has been a file opening error for filename '%s'. " % filename errMsg += "Please check %s permissions on a file " % ("write" if \ diff --git a/lib/core/dump.py b/lib/core/dump.py index 43a86c740..4d048b2f7 100644 --- a/lib/core/dump.py +++ b/lib/core/dump.py @@ -87,7 +87,7 @@ class Dump(object): def setOutputFile(self): self._outputFile = os.path.join(conf.outputPath, "log") try: - self._outputFP = codecs.open(self._outputFile, "ab" if not conf.flushSession else "wb", UNICODE_ENCODING) + self._outputFP = openFile(self._outputFile, "ab" if not conf.flushSession else "wb") except IOError, ex: errMsg = "error occurred while opening log file ('%s')" % ex raise SqlmapGenericException(errMsg) diff --git a/lib/parse/configfile.py b/lib/parse/configfile.py index ec8f72100..7e1b07ad9 100644 --- a/lib/parse/configfile.py +++ b/lib/parse/configfile.py @@ -11,6 +11,7 @@ from ConfigParser import MissingSectionHeaderError from ConfigParser import ParsingError from lib.core.common import checkFile +from lib.core.common import openFile from lib.core.common import unArrayizeValue from lib.core.common import UnicodeRawConfigParser from lib.core.data import conf @@ -65,7 +66,7 @@ def configFileParser(configFile): logger.debug(debugMsg) checkFile(configFile) - configFP = codecs.open(configFile, "rb", UNICODE_ENCODING) + configFP = openFile(configFile, "rb") try: config = UnicodeRawConfigParser() diff --git a/lib/utils/crawler.py b/lib/utils/crawler.py index 9554a76ca..cead9875d 100644 --- a/lib/utils/crawler.py +++ b/lib/utils/crawler.py @@ -16,6 +16,7 @@ import time from lib.core.common import clearConsoleLine from lib.core.common import dataToStdout from lib.core.common import findPageForms +from lib.core.common import openFile from lib.core.common import readInput from lib.core.common import safeCSValue from lib.core.common import singleTimeWarnMessage @@ -170,7 +171,7 @@ def storeResultsToFile(results): infoMsg = "writing crawling results to a temporary file '%s' " % filename logger.info(infoMsg) - with codecs.open(filename, "w+b", UNICODE_ENCODING) as f: + with openFile(filename, "w+b") as f: if conf.forms: f.write("URL,POST\n")