From a138dbe5f6600080b1c9afc8ae4ba2f2f0c5c2f4 Mon Sep 17 00:00:00 2001 From: Bernardo Damele Date: Fri, 28 May 2010 15:57:43 +0000 Subject: [PATCH] Minor bug fixes and code refactoring --- lib/core/common.py | 21 ++++++++++++++++ lib/core/option.py | 54 ++++++++++++----------------------------- lib/parse/configfile.py | 32 +++++++++++------------- 3 files changed, 51 insertions(+), 56 deletions(-) diff --git a/lib/core/common.py b/lib/core/common.py index 4a213a4d2..be30a2597 100644 --- a/lib/core/common.py +++ b/lib/core/common.py @@ -37,6 +37,8 @@ import ntpath import posixpath import subprocess +from ConfigParser import DEFAULTSECT +from ConfigParser import RawConfigParser from StringIO import StringIO from subprocess import PIPE from subprocess import Popen as execute @@ -1365,3 +1367,22 @@ def getBruteUnicode(string): for char in string: retVal += unichr(ord(char)) return retVal + +class UnicodeRawConfigParser(RawConfigParser): + def write(self, fp): + """Write an .ini-format representation of the configuration state.""" + if self._defaults: + fp.write("[%s]\n" % DEFAULTSECT) + for (key, value) in self._defaults.items(): + fp.write("%s = %s\n" % (key, unicode(value).replace('\n', '\n\t'))) + fp.write("\n") + for section in self._sections: + fp.write("[%s]\n" % section) + for (key, value) in self._sections[section].items(): + if key != "__name__": + if value is None: + fp.write("%s\n" % (key)) + else: + fp.write("%s = %s\n" % + (key, unicode(value).replace('\n', '\n\t'))) + fp.write("\n") diff --git a/lib/core/option.py b/lib/core/option.py index f4707cacf..5c0c97a6b 100644 --- a/lib/core/option.py +++ b/lib/core/option.py @@ -33,9 +33,6 @@ import socket import urllib2 import urlparse -from ConfigParser import DEFAULTSECT -from ConfigParser import RawConfigParser - from lib.core.common import getConsoleWidth from lib.core.common import getFileType from lib.core.common import normalizePath @@ -45,6 +42,7 @@ from lib.core.common import parseTargetUrl from lib.core.common import paths from lib.core.common import randomRange from lib.core.common import sanitizeStr +from lib.core.common import UnicodeRawConfigParser from lib.core.data import conf from lib.core.data import kb from lib.core.data import logger @@ -77,26 +75,6 @@ authHandler = urllib2.BaseHandler() proxyHandler = urllib2.BaseHandler() redirectHandler = SmartRedirectHandler() - -class UnicodeRawConfigParser(RawConfigParser): - def write(self, fp): - """Write an .ini-format representation of the configuration state.""" - if self._defaults: - fp.write("[%s]\n" % DEFAULTSECT) - for (key, value) in self._defaults.items(): - fp.write("%s = %s\n" % (key, unicode(value).replace('\n', '\n\t'))) - fp.write("\n") - for section in self._sections: - fp.write("[%s]\n" % section) - for (key, value) in self._sections[section].items(): - if key != "__name__": - if value is None: - fp.write("%s\n" % (key)) - else: - fp.write("%s = %s\n" % - (key, unicode(value).replace('\n', '\n\t'))) - fp.write("\n") - def __urllib2Opener(): """ This function creates the urllib2 OpenerDirector. @@ -118,7 +96,7 @@ def __urllib2Opener(): urllib2.install_opener(opener) def __feedTargetsDict(reqFile, addedTargetUrls): - fp = codecs.open(reqFile, "r", conf.dataEncoding) + fp = codecs.open(reqFile, "rb", conf.dataEncoding) fread = fp.read() fread = fread.replace("\r", "") @@ -856,6 +834,13 @@ def __cleanupOptions(): debugMsg = "cleaning up configuration parameters" logger.debug(debugMsg) + width = getConsoleWidth() + + if conf.eta: + conf.progressWidth = width-26 + else: + conf.progressWidth = width-46 + if conf.testParameter: conf.testParameter = conf.testParameter.replace(" ", "") conf.testParameter = conf.testParameter.split(",") @@ -932,13 +917,6 @@ def __setConfAttributes(): conf.threadException = False conf.wFileType = None - width = getConsoleWidth() - - if conf.eta: - conf.progressWidth = width-26 - else: - conf.progressWidth = width-46 - def __setKnowledgeBaseAttributes(): """ This function set some needed attributes into the knowledge base @@ -989,7 +967,6 @@ def __setKnowledgeBaseAttributes(): kb.unionNegative = False kb.unionFalseCond = False - def __saveCmdline(): """ Saves the command line options on a sqlmap configuration INI file @@ -1002,7 +979,7 @@ def __saveCmdline(): debugMsg = "saving command line options on a sqlmap configuration INI file" logger.debug(debugMsg) - config = UnicodeRawConfigParser() + config = UnicodeRawConfigParser() userOpts = {} for family in optDict.keys(): @@ -1019,6 +996,9 @@ def __saveCmdline(): optionData.sort() for option, value, datatype in optionData: + if isinstance(datatype, (list, tuple, set)): + datatype = datatype[0] + if value is None: if datatype == "boolean": value = "False" @@ -1037,10 +1017,8 @@ def __saveCmdline(): config.set(family, option, value) - print 11111 - confFP = codecs.open(paths.SQLMAP_CONFIG, "wb", "UTF8") + confFP = codecs.open(paths.SQLMAP_CONFIG, "wb", conf.dataEncoding) config.write(confFP) - print 22222 infoMsg = "saved command line options on '%s' configuration file" % paths.SQLMAP_CONFIG logger.info(infoMsg) @@ -1112,11 +1090,11 @@ def init(inputOptions=advancedDict()): based upon command line and configuration file options. """ + __setConfAttributes() + __setKnowledgeBaseAttributes() __mergeOptions(inputOptions) __setVerbosity() __saveCmdline() - __setConfAttributes() - __setKnowledgeBaseAttributes() __cleanupOptions() __basicOptionValidation() __setRequestFromFile() diff --git a/lib/parse/configfile.py b/lib/parse/configfile.py index 3d07d5543..7c1320840 100644 --- a/lib/parse/configfile.py +++ b/lib/parse/configfile.py @@ -24,9 +24,9 @@ Franklin St, Fifth Floor, Boston, MA 02110-1301 USA import codecs from ConfigParser import NoSectionError -from ConfigParser import RawConfigParser from lib.core.common import checkFile +from lib.core.common import UnicodeRawConfigParser from lib.core.data import conf from lib.core.data import logger from lib.core.exception import sqlmapMissingMandatoryOptionException @@ -43,16 +43,12 @@ def configFileProxy(section, option, boolean=False, integer=False): global config if config.has_option(section, option): - value = config.get(section, option) - - if not value: - value = None - elif value.isdigit(): - value = int(value) - elif value in ("false", "False"): - value = False - elif value in ("true", "True"): - value = True + if boolean: + value = config.getboolean(section, option) + elif integer: + value = config.getint(section, option) + else: + value = config.get(section, option) if value: conf[option] = value @@ -76,8 +72,8 @@ def configFileParser(configFile): logger.debug(debugMsg) checkFile(configFile) - config = RawConfigParser() - config.readfp(codecs.open(configFile, "r", "UTF8")) + config = UnicodeRawConfigParser() + config.readfp(codecs.open(configFile, "rb", conf.dataEncoding)) if not config.has_section("Target"): raise NoSectionError, "Target in the configuration file is mandatory" @@ -92,16 +88,16 @@ def configFileParser(configFile): raise sqlmapMissingMandatoryOptionException, errMsg for family, optionData in optDict.items(): - for option, data in optionData.items(): + for option, datatype in optionData.items(): boolean = False integer = False - if isinstance(data, (tuple, dict, set)): - data = data[0] + if isinstance(datatype, (list, tuple, set)): + datatype = datatype[0] - if data == "boolean": + if datatype == "boolean": boolean = True - elif data == "integer": + elif datatype == "integer": integer = True configFileProxy(family, option, boolean, integer)