Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a138dbe

Browse files
committed
Minor bug fixes and code refactoring
1 parent 919a834 commit a138dbe

3 files changed

Lines changed: 51 additions & 56 deletions

File tree

lib/core/common.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import posixpath
3838
import subprocess
3939

40+
from ConfigParser import DEFAULTSECT
41+
from ConfigParser import RawConfigParser
4042
from StringIO import StringIO
4143
from subprocess import PIPE
4244
from subprocess import Popen as execute
@@ -1365,3 +1367,22 @@ def getBruteUnicode(string):
13651367
for char in string:
13661368
retVal += unichr(ord(char))
13671369
return retVal
1370+
1371+
class UnicodeRawConfigParser(RawConfigParser):
1372+
def write(self, fp):
1373+
"""Write an .ini-format representation of the configuration state."""
1374+
if self._defaults:
1375+
fp.write("[%s]\n" % DEFAULTSECT)
1376+
for (key, value) in self._defaults.items():
1377+
fp.write("%s = %s\n" % (key, unicode(value).replace('\n', '\n\t')))
1378+
fp.write("\n")
1379+
for section in self._sections:
1380+
fp.write("[%s]\n" % section)
1381+
for (key, value) in self._sections[section].items():
1382+
if key != "__name__":
1383+
if value is None:
1384+
fp.write("%s\n" % (key))
1385+
else:
1386+
fp.write("%s = %s\n" %
1387+
(key, unicode(value).replace('\n', '\n\t')))
1388+
fp.write("\n")

lib/core/option.py

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@
3333
import urllib2
3434
import urlparse
3535

36-
from ConfigParser import DEFAULTSECT
37-
from ConfigParser import RawConfigParser
38-
3936
from lib.core.common import getConsoleWidth
4037
from lib.core.common import getFileType
4138
from lib.core.common import normalizePath
@@ -45,6 +42,7 @@
4542
from lib.core.common import paths
4643
from lib.core.common import randomRange
4744
from lib.core.common import sanitizeStr
45+
from lib.core.common import UnicodeRawConfigParser
4846
from lib.core.data import conf
4947
from lib.core.data import kb
5048
from lib.core.data import logger
@@ -77,26 +75,6 @@
7775
proxyHandler = urllib2.BaseHandler()
7876
redirectHandler = SmartRedirectHandler()
7977

80-
81-
class UnicodeRawConfigParser(RawConfigParser):
82-
def write(self, fp):
83-
"""Write an .ini-format representation of the configuration state."""
84-
if self._defaults:
85-
fp.write("[%s]\n" % DEFAULTSECT)
86-
for (key, value) in self._defaults.items():
87-
fp.write("%s = %s\n" % (key, unicode(value).replace('\n', '\n\t')))
88-
fp.write("\n")
89-
for section in self._sections:
90-
fp.write("[%s]\n" % section)
91-
for (key, value) in self._sections[section].items():
92-
if key != "__name__":
93-
if value is None:
94-
fp.write("%s\n" % (key))
95-
else:
96-
fp.write("%s = %s\n" %
97-
(key, unicode(value).replace('\n', '\n\t')))
98-
fp.write("\n")
99-
10078
def __urllib2Opener():
10179
"""
10280
This function creates the urllib2 OpenerDirector.
@@ -118,7 +96,7 @@ def __urllib2Opener():
11896
urllib2.install_opener(opener)
11997

12098
def __feedTargetsDict(reqFile, addedTargetUrls):
121-
fp = codecs.open(reqFile, "r", conf.dataEncoding)
99+
fp = codecs.open(reqFile, "rb", conf.dataEncoding)
122100

123101
fread = fp.read()
124102
fread = fread.replace("\r", "")
@@ -856,6 +834,13 @@ def __cleanupOptions():
856834
debugMsg = "cleaning up configuration parameters"
857835
logger.debug(debugMsg)
858836

837+
width = getConsoleWidth()
838+
839+
if conf.eta:
840+
conf.progressWidth = width-26
841+
else:
842+
conf.progressWidth = width-46
843+
859844
if conf.testParameter:
860845
conf.testParameter = conf.testParameter.replace(" ", "")
861846
conf.testParameter = conf.testParameter.split(",")
@@ -932,13 +917,6 @@ def __setConfAttributes():
932917
conf.threadException = False
933918
conf.wFileType = None
934919

935-
width = getConsoleWidth()
936-
937-
if conf.eta:
938-
conf.progressWidth = width-26
939-
else:
940-
conf.progressWidth = width-46
941-
942920
def __setKnowledgeBaseAttributes():
943921
"""
944922
This function set some needed attributes into the knowledge base
@@ -989,7 +967,6 @@ def __setKnowledgeBaseAttributes():
989967
kb.unionNegative = False
990968
kb.unionFalseCond = False
991969

992-
993970
def __saveCmdline():
994971
"""
995972
Saves the command line options on a sqlmap configuration INI file
@@ -1002,7 +979,7 @@ def __saveCmdline():
1002979
debugMsg = "saving command line options on a sqlmap configuration INI file"
1003980
logger.debug(debugMsg)
1004981

1005-
config = UnicodeRawConfigParser()
982+
config = UnicodeRawConfigParser()
1006983
userOpts = {}
1007984

1008985
for family in optDict.keys():
@@ -1019,6 +996,9 @@ def __saveCmdline():
1019996
optionData.sort()
1020997

1021998
for option, value, datatype in optionData:
999+
if isinstance(datatype, (list, tuple, set)):
1000+
datatype = datatype[0]
1001+
10221002
if value is None:
10231003
if datatype == "boolean":
10241004
value = "False"
@@ -1037,10 +1017,8 @@ def __saveCmdline():
10371017

10381018
config.set(family, option, value)
10391019

1040-
print 11111
1041-
confFP = codecs.open(paths.SQLMAP_CONFIG, "wb", "UTF8")
1020+
confFP = codecs.open(paths.SQLMAP_CONFIG, "wb", conf.dataEncoding)
10421021
config.write(confFP)
1043-
print 22222
10441022

10451023
infoMsg = "saved command line options on '%s' configuration file" % paths.SQLMAP_CONFIG
10461024
logger.info(infoMsg)
@@ -1112,11 +1090,11 @@ def init(inputOptions=advancedDict()):
11121090
based upon command line and configuration file options.
11131091
"""
11141092

1093+
__setConfAttributes()
1094+
__setKnowledgeBaseAttributes()
11151095
__mergeOptions(inputOptions)
11161096
__setVerbosity()
11171097
__saveCmdline()
1118-
__setConfAttributes()
1119-
__setKnowledgeBaseAttributes()
11201098
__cleanupOptions()
11211099
__basicOptionValidation()
11221100
__setRequestFromFile()

lib/parse/configfile.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import codecs
2525

2626
from ConfigParser import NoSectionError
27-
from ConfigParser import RawConfigParser
2827

2928
from lib.core.common import checkFile
29+
from lib.core.common import UnicodeRawConfigParser
3030
from lib.core.data import conf
3131
from lib.core.data import logger
3232
from lib.core.exception import sqlmapMissingMandatoryOptionException
@@ -43,16 +43,12 @@ def configFileProxy(section, option, boolean=False, integer=False):
4343
global config
4444

4545
if config.has_option(section, option):
46-
value = config.get(section, option)
47-
48-
if not value:
49-
value = None
50-
elif value.isdigit():
51-
value = int(value)
52-
elif value in ("false", "False"):
53-
value = False
54-
elif value in ("true", "True"):
55-
value = True
46+
if boolean:
47+
value = config.getboolean(section, option)
48+
elif integer:
49+
value = config.getint(section, option)
50+
else:
51+
value = config.get(section, option)
5652

5753
if value:
5854
conf[option] = value
@@ -76,8 +72,8 @@ def configFileParser(configFile):
7672
logger.debug(debugMsg)
7773

7874
checkFile(configFile)
79-
config = RawConfigParser()
80-
config.readfp(codecs.open(configFile, "r", "UTF8"))
75+
config = UnicodeRawConfigParser()
76+
config.readfp(codecs.open(configFile, "rb", conf.dataEncoding))
8177

8278
if not config.has_section("Target"):
8379
raise NoSectionError, "Target in the configuration file is mandatory"
@@ -92,16 +88,16 @@ def configFileParser(configFile):
9288
raise sqlmapMissingMandatoryOptionException, errMsg
9389

9490
for family, optionData in optDict.items():
95-
for option, data in optionData.items():
91+
for option, datatype in optionData.items():
9692
boolean = False
9793
integer = False
9894

99-
if isinstance(data, (tuple, dict, set)):
100-
data = data[0]
95+
if isinstance(datatype, (list, tuple, set)):
96+
datatype = datatype[0]
10197

102-
if data == "boolean":
98+
if datatype == "boolean":
10399
boolean = True
104-
elif data == "integer":
100+
elif datatype == "integer":
105101
integer = True
106102

107103
configFileProxy(family, option, boolean, integer)

0 commit comments

Comments
 (0)