Skip to content

Commit ddf23ba

Browse files
committed
refactoring
1 parent 3060c36 commit ddf23ba

17 files changed

Lines changed: 87 additions & 42 deletions

File tree

lib/core/common.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
from lib.core.exception import sqlmapSyntaxException
6060
from lib.core.optiondict import optDict
6161
from lib.core.settings import INFERENCE_UNKNOWN_CHAR
62+
from lib.core.settings import UNICODE_ENCODING
6263
from lib.core.settings import DESCRIPTION
6364
from lib.core.settings import IS_WIN
6465
from lib.core.settings import PLATFORM
@@ -99,7 +100,7 @@ def write(self, fp):
99100
fp.write("[%s]\n" % DEFAULTSECT)
100101

101102
for (key, value) in self._defaults.items():
102-
fp.write("%s = %s\n" % (key, getUnicode(value, conf.dataEncoding).replace('\n', '\n\t')))
103+
fp.write("%s = %s\n" % (key, getUnicode(value, UNICODE_ENCODING).replace('\n', '\n\t')))
103104

104105
fp.write("\n")
105106

@@ -111,7 +112,7 @@ def write(self, fp):
111112
if value is None:
112113
fp.write("%s\n" % (key))
113114
else:
114-
fp.write("%s = %s\n" % (key, getUnicode(value, conf.dataEncoding).replace('\n', '\n\t')))
115+
fp.write("%s = %s\n" % (key, getUnicode(value, UNICODE_ENCODING).replace('\n', '\n\t')))
115116

116117
fp.write("\n")
117118

@@ -584,9 +585,9 @@ def dataToStdout(data, forceOutput=False):
584585
if not ('threadException' in kb and kb.threadException):
585586
if forceOutput or (conf.verbose > 0) and not getCurrentThreadData().disableStdOut:
586587
try:
587-
sys.stdout.write(data)
588-
except UnicodeEncodeError:
589-
sys.stdout.write(data.encode(conf.dataEncoding))
588+
sys.stdout.write(data.encode(sys.stdout.encoding))
589+
except:
590+
sys.stdout.write(data.encode(UNICODE_ENCODING, errors="replace"))
590591
finally:
591592
sys.stdout.flush()
592593

@@ -660,15 +661,15 @@ def readInput(message, default=None):
660661
message += " "
661662

662663
if conf.batch and default:
663-
infoMsg = "%s%s" % (message, getUnicode(default, conf.dataEncoding))
664+
infoMsg = "%s%s" % (message, getUnicode(default, UNICODE_ENCODING))
664665
logger.info(infoMsg)
665666

666667
debugMsg = "used the default behaviour, running in batch mode"
667668
logger.debug(debugMsg)
668669

669670
data = default
670671
else:
671-
data = raw_input(message.encode(sys.stdout.encoding or conf.dataEncoding))
672+
data = raw_input(message.encode(sys.stdout.encoding or UNICODE_ENCODING))
672673

673674
if not data:
674675
data = default
@@ -1438,7 +1439,7 @@ def readCachedFileContent(filename, mode='rb'):
14381439

14391440
if filename not in kb.cache.content:
14401441
checkFile(filename)
1441-
xfile = codecs.open(filename, mode, conf.dataEncoding)
1442+
xfile = codecs.open(filename, mode, UNICODE_ENCODING)
14421443
content = xfile.read()
14431444
kb.cache.content[filename] = content
14441445
xfile.close()
@@ -1450,7 +1451,7 @@ def readCachedFileContent(filename, mode='rb'):
14501451
def readXmlFile(xmlFile):
14511452
checkFile(xmlFile)
14521453

1453-
xfile = codecs.open(xmlFile, 'r', conf.dataEncoding)
1454+
xfile = codecs.open(xmlFile, 'r', UNICODE_ENCODING)
14541455
retVal = minidom.parse(xfile).documentElement
14551456

14561457
xfile.close()
@@ -1502,7 +1503,7 @@ def initCommonOutputs():
15021503
kb.commonOutputs = {}
15031504
key = None
15041505

1505-
cfile = codecs.open(paths.COMMON_OUTPUTS, 'r', conf.dataEncoding)
1506+
cfile = codecs.open(paths.COMMON_OUTPUTS, 'r', UNICODE_ENCODING)
15061507

15071508
for line in cfile.readlines(): # xreadlines doesn't return unicode strings when codec.open() is used
15081509
if line.find('#') != -1:
@@ -1528,7 +1529,7 @@ def getFileItems(filename, commentPrefix='#', unicode_=True, lowercase=False, un
15281529
checkFile(filename)
15291530

15301531
if unicode_:
1531-
ifile = codecs.open(filename, 'r', conf.dataEncoding)
1532+
ifile = codecs.open(filename, 'r', UNICODE_ENCODING)
15321533
else:
15331534
ifile = open(filename, 'r')
15341535

@@ -1683,7 +1684,7 @@ def getUnicode(value, encoding=None):
16831684
if isinstance(value, unicode):
16841685
return value
16851686
elif isinstance(value, basestring):
1686-
return unicode(value, encoding or conf.dataEncoding, errors="replace")
1687+
return unicode(value, encoding or UNICODE_ENCODING, errors="replace")
16871688
else:
16881689
return unicode(value) # encoding ignored for non-basestring instances
16891690

@@ -2260,7 +2261,7 @@ def openFile(filename, mode='r'):
22602261
"""
22612262

22622263
try:
2263-
return codecs.open(filename, mode, conf.dataEncoding)
2264+
return codecs.open(filename, mode, UNICODE_ENCODING)
22642265
except IOError:
22652266
errMsg = "there has been a file opening error for filename '%s'. " % filename
22662267
errMsg += "Please check %s permissions on a file " % ("write" if \

lib/core/dump.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from lib.core.data import kb
2121
from lib.core.data import logger
2222
from lib.core.replication import Replication
23+
from lib.core.settings import UNICODE_ENCODING
2324

2425
class Dump:
2526
"""
@@ -46,7 +47,7 @@ def __formatString(self, inpStr):
4647

4748
def setOutputFile(self):
4849
self.__outputFile = "%s%slog" % (conf.outputPath, os.sep)
49-
self.__outputFP = codecs.open(self.__outputFile, "ab", conf.dataEncoding)
50+
self.__outputFP = codecs.open(self.__outputFile, "ab", UNICODE_ENCODING)
5051

5152
def getOutputFile(self):
5253
return self.__outputFile

lib/core/option.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,6 @@ def __setConfAttributes():
11371137

11381138
conf.boundaries = []
11391139
conf.cj = None
1140-
conf.dataEncoding = "utf-8"
11411140
conf.dbmsConnector = None
11421141
conf.dbmsHandler = None
11431142
conf.dumpPath = None

lib/core/profiling.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
import cProfile
1313

1414
from lib.core.common import getUnicode
15-
from lib.core.data import conf
1615
from lib.core.data import logger
1716
from lib.core.data import paths
17+
from lib.core.settings import UNICODE_ENCODING
1818

1919
def profile(profileOutputFile=None, dotOutputFile=None, imageOutputFile=None):
2020
"""
@@ -27,7 +27,7 @@ def profile(profileOutputFile=None, dotOutputFile=None, imageOutputFile=None):
2727
import gtk
2828
import pydot
2929
except ImportError, e:
30-
errMsg = "profiling requires third-party libraries (%s)" % getUnicode(e, conf.dataEncoding)
30+
errMsg = "profiling requires third-party libraries (%s)" % getUnicode(e, UNICODE_ENCODING)
3131
logger.error(errMsg)
3232
return
3333

@@ -60,7 +60,7 @@ def profile(profileOutputFile=None, dotOutputFile=None, imageOutputFile=None):
6060

6161
# Create dot file by using extra/gprof2dot/gprof2dot.py
6262
# http://code.google.com/p/jrfonseca/wiki/Gprof2Dot
63-
dotFilePointer = codecs.open(dotOutputFile, 'wt', conf.dataEncoding)
63+
dotFilePointer = codecs.open(dotOutputFile, 'wt', UNICODE_ENCODING)
6464
parser = gprof2dot.PstatsParser(profileOutputFile)
6565
profile = parser.parse()
6666
profile.prune(0.5/100.0, 0.1/100.0)

lib/core/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,6 @@
208208

209209
# Do the url-encoding based on parameter place
210210
URL_ENCODE_PAYLOAD = { PLACE.GET: True, PLACE.POST: True, PLACE.COOKIE: False, PLACE.UA: True, PLACE.URI: False }
211+
212+
# Encoding used for Unicode data
213+
UNICODE_ENCODING = "utf8"

lib/core/target.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from lib.core.option import __setDBMS
3232
from lib.core.option import __setKnowledgeBaseAttributes
3333
from lib.core.session import resumeConfKb
34+
from lib.core.settings import UNICODE_ENCODING
3435
from lib.core.xmldump import dumper as xmldumper
3536
from lib.request.connect import Connect as Request
3637

@@ -139,7 +140,7 @@ def __setOutputResume():
139140

140141
if os.path.exists(conf.sessionFile):
141142
if not conf.flushSession:
142-
readSessionFP = codecs.open(conf.sessionFile, "r", conf.dataEncoding, 'replace')
143+
readSessionFP = codecs.open(conf.sessionFile, "r", UNICODE_ENCODING, 'replace')
143144
__url_cache = set()
144145
__expression_cache = {}
145146

@@ -190,7 +191,7 @@ def __setOutputResume():
190191
raise sqlmapFilePathException, errMsg
191192

192193
try:
193-
conf.sessionFP = codecs.open(conf.sessionFile, "a", conf.dataEncoding)
194+
conf.sessionFP = codecs.open(conf.sessionFile, "a", UNICODE_ENCODING)
194195
dataToSessionFile("\n[%s]\n" % time.strftime("%X %x"))
195196
except IOError:
196197
errMsg = "unable to write on the session file specified"

lib/core/update.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from lib.core.exception import sqlmapConnectionException
3232
from lib.core.exception import sqlmapFilePathException
3333
from lib.core.settings import MSSQL_VERSIONS_URL
34+
from lib.core.settings import UNICODE_ENCODING
3435
from lib.core.subprocessng import pollProcess
3536
from lib.request.connect import Connect as Request
3637

@@ -130,7 +131,7 @@ def __updateMSSQLXML():
130131
servicepackElement.appendChild(servicepackText)
131132

132133
# Get the XML old file content to a local variable
133-
mssqlXml = codecs.open(paths.MSSQL_XML, "r", conf.dataEncoding)
134+
mssqlXml = codecs.open(paths.MSSQL_XML, "r", UNICODE_ENCODING)
134135
oldMssqlXml = mssqlXml.read()
135136
oldMssqlXmlSignatures = oldMssqlXml.count("<signature>")
136137
oldMssqlXmlList = oldMssqlXml.splitlines(1)
@@ -140,12 +141,12 @@ def __updateMSSQLXML():
140141
shutil.copy(paths.MSSQL_XML, "%s.bak" % paths.MSSQL_XML)
141142

142143
# Save our newly created XML to the signatures file
143-
mssqlXml = codecs.open(paths.MSSQL_XML, "w", conf.dataEncoding)
144+
mssqlXml = codecs.open(paths.MSSQL_XML, "w", UNICODE_ENCODING)
144145
doc.writexml(writer=mssqlXml, addindent=" ", newl="\n")
145146
mssqlXml.close()
146147

147148
# Get the XML new file content to a local variable
148-
mssqlXml = codecs.open(paths.MSSQL_XML, "r", conf.dataEncoding)
149+
mssqlXml = codecs.open(paths.MSSQL_XML, "r", UNICODE_ENCODING)
149150
newMssqlXml = mssqlXml.read()
150151
newMssqlXmlSignatures = newMssqlXml.count("<signature>")
151152
newMssqlXmlList = newMssqlXml.splitlines(1)
@@ -199,7 +200,7 @@ def __updateSqlmap():
199200
logger.debug(debugMsg)
200201

201202
def notify(event_dict):
202-
action = getUnicode(event_dict['action'], conf.dataEncoding)
203+
action = getUnicode(event_dict['action'])
203204
index = action.find('_')
204205
prefix = action[index + 1].upper() if index != -1 else action.capitalize()
205206

@@ -209,7 +210,7 @@ def notify(event_dict):
209210
if action.find('_completed') == -1:
210211
dataToStdout("%s\t%s\n" % (prefix, event_dict['path']))
211212
else:
212-
revision = getUnicode(event_dict['revision'], conf.dataEncoding)
213+
revision = getUnicode(event_dict['revision'], UNICODE_ENCODING)
213214
index = revision.find('number ')
214215

215216
if index != -1:

lib/core/xmldump.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from lib.core.data import conf
1717
from lib.core.data import logger
1818
from lib.core.exception import sqlmapFilePathException
19+
from lib.core.settings import UNICODE_ENCODING
1920

2021
TECHNIC_ELEM_NAME = "Technic"
2122
TECHNICS_ELEM_NAME = "Technics"
@@ -489,7 +490,7 @@ def setOutputFile(self):
489490
except ExpatError:
490491
self.__doc = Document()
491492

492-
self.__outputFP = codecs.open(self.__outputFile, "w+", conf.dataEncoding)
493+
self.__outputFP = codecs.open(self.__outputFile, "w+", UNICODE_ENCODING)
493494

494495
if self.__root is None:
495496
self.__root = self.__doc.createElementNS(NAME_SPACE_ATTR, RESULTS_ELEM_NAME)
@@ -525,7 +526,7 @@ def finish(self, resultStatus, resultMsg=""):
525526
statusElem.appendChild(errorElem)
526527

527528
self.__addToRoot(statusElem)
528-
self.__write(prettyprint.formatXML(self.__doc, encoding=conf.dataEncoding))
529+
self.__write(prettyprint.formatXML(self.__doc, encoding=UNICODE_ENCODING))
529530
self.__outputFP.close()
530531

531532
def closeDumper(status, msg=""):

lib/parse/cmdline.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from lib.core.data import logger
1919
from lib.core.settings import TIME_DEFAULT_DELAY
2020
from lib.core.settings import VERSION_STRING
21+
from lib.core.settings import UNICODE_ENCODING
2122

2223
def cmdLineParser():
2324
"""
@@ -545,11 +546,12 @@ def cmdLineParser():
545546
parser.add_option_group(miscellaneous)
546547

547548
args = []
549+
from lib.core.common import dataToStdout
548550
for arg in sys.argv:
549551
try:
550-
args.append(getUnicode(arg, sys.stdin.encoding or sys.getfilesystemencoding()))
552+
args.append(getUnicode(arg, sys.getfilesystemencoding() or sys.stdin.encoding))
551553
except:
552-
args.append(getUnicode(arg, "utf8"))
554+
args.append(getUnicode(arg, UNICODE_ENCODING))
553555
(args, _) = parser.parse_args(args)
554556

555557
if not args.direct and not args.url and not args.list and not args.googleDork and not args.configFile\

lib/parse/configfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
from lib.core.common import checkFile
1515
from lib.core.common import UnicodeRawConfigParser
16-
from lib.core.data import conf
1716
from lib.core.data import logger
1817
from lib.core.exception import sqlmapMissingMandatoryOptionException
1918
from lib.core.optiondict import optDict
19+
from lib.core.settings import UNICODE_ENCODING
2020

2121
config = None
2222

@@ -58,7 +58,7 @@ def configFileParser(configFile):
5858
logger.debug(debugMsg)
5959

6060
checkFile(configFile)
61-
configFP = codecs.open(configFile, "rb", conf.dataEncoding)
61+
configFP = codecs.open(configFile, "rb", UNICODE_ENCODING)
6262
config = UnicodeRawConfigParser()
6363
config.readfp(configFP)
6464

0 commit comments

Comments
 (0)