refactoring

This commit is contained in:
Miroslav Stampar
2011-01-30 11:36:03 +00:00
parent 3060c369a5
commit ddf23ba7cc
17 changed files with 87 additions and 42 deletions

View File

@@ -59,6 +59,7 @@ from lib.core.exception import sqlmapMissingDependence
from lib.core.exception import sqlmapSyntaxException
from lib.core.optiondict import optDict
from lib.core.settings import INFERENCE_UNKNOWN_CHAR
from lib.core.settings import UNICODE_ENCODING
from lib.core.settings import DESCRIPTION
from lib.core.settings import IS_WIN
from lib.core.settings import PLATFORM
@@ -99,7 +100,7 @@ class UnicodeRawConfigParser(RawConfigParser):
fp.write("[%s]\n" % DEFAULTSECT)
for (key, value) in self._defaults.items():
fp.write("%s = %s\n" % (key, getUnicode(value, conf.dataEncoding).replace('\n', '\n\t')))
fp.write("%s = %s\n" % (key, getUnicode(value, UNICODE_ENCODING).replace('\n', '\n\t')))
fp.write("\n")
@@ -111,7 +112,7 @@ class UnicodeRawConfigParser(RawConfigParser):
if value is None:
fp.write("%s\n" % (key))
else:
fp.write("%s = %s\n" % (key, getUnicode(value, conf.dataEncoding).replace('\n', '\n\t')))
fp.write("%s = %s\n" % (key, getUnicode(value, UNICODE_ENCODING).replace('\n', '\n\t')))
fp.write("\n")
@@ -584,9 +585,9 @@ def dataToStdout(data, forceOutput=False):
if not ('threadException' in kb and kb.threadException):
if forceOutput or (conf.verbose > 0) and not getCurrentThreadData().disableStdOut:
try:
sys.stdout.write(data)
except UnicodeEncodeError:
sys.stdout.write(data.encode(conf.dataEncoding))
sys.stdout.write(data.encode(sys.stdout.encoding))
except:
sys.stdout.write(data.encode(UNICODE_ENCODING, errors="replace"))
finally:
sys.stdout.flush()
@@ -660,7 +661,7 @@ def readInput(message, default=None):
message += " "
if conf.batch and default:
infoMsg = "%s%s" % (message, getUnicode(default, conf.dataEncoding))
infoMsg = "%s%s" % (message, getUnicode(default, UNICODE_ENCODING))
logger.info(infoMsg)
debugMsg = "used the default behaviour, running in batch mode"
@@ -668,7 +669,7 @@ def readInput(message, default=None):
data = default
else:
data = raw_input(message.encode(sys.stdout.encoding or conf.dataEncoding))
data = raw_input(message.encode(sys.stdout.encoding or UNICODE_ENCODING))
if not data:
data = default
@@ -1438,7 +1439,7 @@ def readCachedFileContent(filename, mode='rb'):
if filename not in kb.cache.content:
checkFile(filename)
xfile = codecs.open(filename, mode, conf.dataEncoding)
xfile = codecs.open(filename, mode, UNICODE_ENCODING)
content = xfile.read()
kb.cache.content[filename] = content
xfile.close()
@@ -1450,7 +1451,7 @@ def readCachedFileContent(filename, mode='rb'):
def readXmlFile(xmlFile):
checkFile(xmlFile)
xfile = codecs.open(xmlFile, 'r', conf.dataEncoding)
xfile = codecs.open(xmlFile, 'r', UNICODE_ENCODING)
retVal = minidom.parse(xfile).documentElement
xfile.close()
@@ -1502,7 +1503,7 @@ def initCommonOutputs():
kb.commonOutputs = {}
key = None
cfile = codecs.open(paths.COMMON_OUTPUTS, 'r', conf.dataEncoding)
cfile = codecs.open(paths.COMMON_OUTPUTS, 'r', UNICODE_ENCODING)
for line in cfile.readlines(): # xreadlines doesn't return unicode strings when codec.open() is used
if line.find('#') != -1:
@@ -1528,7 +1529,7 @@ def getFileItems(filename, commentPrefix='#', unicode_=True, lowercase=False, un
checkFile(filename)
if unicode_:
ifile = codecs.open(filename, 'r', conf.dataEncoding)
ifile = codecs.open(filename, 'r', UNICODE_ENCODING)
else:
ifile = open(filename, 'r')
@@ -1683,7 +1684,7 @@ def getUnicode(value, encoding=None):
if isinstance(value, unicode):
return value
elif isinstance(value, basestring):
return unicode(value, encoding or conf.dataEncoding, errors="replace")
return unicode(value, encoding or UNICODE_ENCODING, errors="replace")
else:
return unicode(value) # encoding ignored for non-basestring instances
@@ -2260,7 +2261,7 @@ def openFile(filename, mode='r'):
"""
try:
return codecs.open(filename, mode, conf.dataEncoding)
return codecs.open(filename, mode, UNICODE_ENCODING)
except IOError:
errMsg = "there has been a file opening error for filename '%s'. " % filename
errMsg += "Please check %s permissions on a file " % ("write" if \

View File

@@ -20,6 +20,7 @@ from lib.core.data import conf
from lib.core.data import kb
from lib.core.data import logger
from lib.core.replication import Replication
from lib.core.settings import UNICODE_ENCODING
class Dump:
"""
@@ -46,7 +47,7 @@ class Dump:
def setOutputFile(self):
self.__outputFile = "%s%slog" % (conf.outputPath, os.sep)
self.__outputFP = codecs.open(self.__outputFile, "ab", conf.dataEncoding)
self.__outputFP = codecs.open(self.__outputFile, "ab", UNICODE_ENCODING)
def getOutputFile(self):
return self.__outputFile

View File

@@ -1137,7 +1137,6 @@ def __setConfAttributes():
conf.boundaries = []
conf.cj = None
conf.dataEncoding = "utf-8"
conf.dbmsConnector = None
conf.dbmsHandler = None
conf.dumpPath = None

View File

@@ -12,9 +12,9 @@ import os
import cProfile
from lib.core.common import getUnicode
from lib.core.data import conf
from lib.core.data import logger
from lib.core.data import paths
from lib.core.settings import UNICODE_ENCODING
def profile(profileOutputFile=None, dotOutputFile=None, imageOutputFile=None):
"""
@@ -27,7 +27,7 @@ def profile(profileOutputFile=None, dotOutputFile=None, imageOutputFile=None):
import gtk
import pydot
except ImportError, e:
errMsg = "profiling requires third-party libraries (%s)" % getUnicode(e, conf.dataEncoding)
errMsg = "profiling requires third-party libraries (%s)" % getUnicode(e, UNICODE_ENCODING)
logger.error(errMsg)
return
@@ -60,7 +60,7 @@ def profile(profileOutputFile=None, dotOutputFile=None, imageOutputFile=None):
# Create dot file by using extra/gprof2dot/gprof2dot.py
# http://code.google.com/p/jrfonseca/wiki/Gprof2Dot
dotFilePointer = codecs.open(dotOutputFile, 'wt', conf.dataEncoding)
dotFilePointer = codecs.open(dotOutputFile, 'wt', UNICODE_ENCODING)
parser = gprof2dot.PstatsParser(profileOutputFile)
profile = parser.parse()
profile.prune(0.5/100.0, 0.1/100.0)

View File

@@ -208,3 +208,6 @@ BURP_SPLITTER = "======================================================"
# Do the url-encoding based on parameter place
URL_ENCODE_PAYLOAD = { PLACE.GET: True, PLACE.POST: True, PLACE.COOKIE: False, PLACE.UA: True, PLACE.URI: False }
# Encoding used for Unicode data
UNICODE_ENCODING = "utf8"

View File

@@ -31,6 +31,7 @@ from lib.core.exception import sqlmapSyntaxException
from lib.core.option import __setDBMS
from lib.core.option import __setKnowledgeBaseAttributes
from lib.core.session import resumeConfKb
from lib.core.settings import UNICODE_ENCODING
from lib.core.xmldump import dumper as xmldumper
from lib.request.connect import Connect as Request
@@ -139,7 +140,7 @@ def __setOutputResume():
if os.path.exists(conf.sessionFile):
if not conf.flushSession:
readSessionFP = codecs.open(conf.sessionFile, "r", conf.dataEncoding, 'replace')
readSessionFP = codecs.open(conf.sessionFile, "r", UNICODE_ENCODING, 'replace')
__url_cache = set()
__expression_cache = {}
@@ -190,7 +191,7 @@ def __setOutputResume():
raise sqlmapFilePathException, errMsg
try:
conf.sessionFP = codecs.open(conf.sessionFile, "a", conf.dataEncoding)
conf.sessionFP = codecs.open(conf.sessionFile, "a", UNICODE_ENCODING)
dataToSessionFile("\n[%s]\n" % time.strftime("%X %x"))
except IOError:
errMsg = "unable to write on the session file specified"

View File

@@ -31,6 +31,7 @@ from lib.core.data import paths
from lib.core.exception import sqlmapConnectionException
from lib.core.exception import sqlmapFilePathException
from lib.core.settings import MSSQL_VERSIONS_URL
from lib.core.settings import UNICODE_ENCODING
from lib.core.subprocessng import pollProcess
from lib.request.connect import Connect as Request
@@ -130,7 +131,7 @@ def __updateMSSQLXML():
servicepackElement.appendChild(servicepackText)
# Get the XML old file content to a local variable
mssqlXml = codecs.open(paths.MSSQL_XML, "r", conf.dataEncoding)
mssqlXml = codecs.open(paths.MSSQL_XML, "r", UNICODE_ENCODING)
oldMssqlXml = mssqlXml.read()
oldMssqlXmlSignatures = oldMssqlXml.count("<signature>")
oldMssqlXmlList = oldMssqlXml.splitlines(1)
@@ -140,12 +141,12 @@ def __updateMSSQLXML():
shutil.copy(paths.MSSQL_XML, "%s.bak" % paths.MSSQL_XML)
# Save our newly created XML to the signatures file
mssqlXml = codecs.open(paths.MSSQL_XML, "w", conf.dataEncoding)
mssqlXml = codecs.open(paths.MSSQL_XML, "w", UNICODE_ENCODING)
doc.writexml(writer=mssqlXml, addindent=" ", newl="\n")
mssqlXml.close()
# Get the XML new file content to a local variable
mssqlXml = codecs.open(paths.MSSQL_XML, "r", conf.dataEncoding)
mssqlXml = codecs.open(paths.MSSQL_XML, "r", UNICODE_ENCODING)
newMssqlXml = mssqlXml.read()
newMssqlXmlSignatures = newMssqlXml.count("<signature>")
newMssqlXmlList = newMssqlXml.splitlines(1)
@@ -199,7 +200,7 @@ def __updateSqlmap():
logger.debug(debugMsg)
def notify(event_dict):
action = getUnicode(event_dict['action'], conf.dataEncoding)
action = getUnicode(event_dict['action'])
index = action.find('_')
prefix = action[index + 1].upper() if index != -1 else action.capitalize()
@@ -209,7 +210,7 @@ def __updateSqlmap():
if action.find('_completed') == -1:
dataToStdout("%s\t%s\n" % (prefix, event_dict['path']))
else:
revision = getUnicode(event_dict['revision'], conf.dataEncoding)
revision = getUnicode(event_dict['revision'], UNICODE_ENCODING)
index = revision.find('number ')
if index != -1:

View File

@@ -16,6 +16,7 @@ from lib.core.common import restoreDumpMarkedChars
from lib.core.data import conf
from lib.core.data import logger
from lib.core.exception import sqlmapFilePathException
from lib.core.settings import UNICODE_ENCODING
TECHNIC_ELEM_NAME = "Technic"
TECHNICS_ELEM_NAME = "Technics"
@@ -489,7 +490,7 @@ class XMLDump:
except ExpatError:
self.__doc = Document()
self.__outputFP = codecs.open(self.__outputFile, "w+", conf.dataEncoding)
self.__outputFP = codecs.open(self.__outputFile, "w+", UNICODE_ENCODING)
if self.__root is None:
self.__root = self.__doc.createElementNS(NAME_SPACE_ATTR, RESULTS_ELEM_NAME)
@@ -525,7 +526,7 @@ class XMLDump:
statusElem.appendChild(errorElem)
self.__addToRoot(statusElem)
self.__write(prettyprint.formatXML(self.__doc, encoding=conf.dataEncoding))
self.__write(prettyprint.formatXML(self.__doc, encoding=UNICODE_ENCODING))
self.__outputFP.close()
def closeDumper(status, msg=""):