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

Skip to content

Commit 7074365

Browse files
committed
More refactoring like the last couple of commits
1 parent fd705c3 commit 7074365

22 files changed

Lines changed: 144 additions & 137 deletions

File tree

extra/wafdetectify/wafdetectify.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ def main():
9595
if filename[:-3] in sys.modules:
9696
del sys.modules[filename[:-3]]
9797
module = __import__(filename[:-3].encode(sys.getfilesystemencoding() or "utf8"))
98-
except ImportError, msg:
99-
exit(colorize("[x] cannot import WAF script '%s' (%s)" % (filename[:-3], msg)))
98+
except ImportError as ex:
99+
exit(colorize("[x] cannot import WAF script '%s' (%s)" % (filename[:-3], ex)))
100100

101101
_ = dict(inspect.getmembers(module))
102102
if "detect" not in _:

lib/core/common.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4409,9 +4409,9 @@ def resetCookieJar(cookieJar):
44094409
errMsg = "no valid cookies found"
44104410
raise SqlmapGenericException(errMsg)
44114411

4412-
except cookielib.LoadError, msg:
4412+
except cookielib.LoadError as ex:
44134413
errMsg = "there was a problem loading "
4414-
errMsg += "cookies file ('%s')" % re.sub(r"(cookies) file '[^']+'", r"\g<1>", str(msg))
4414+
errMsg += "cookies file ('%s')" % re.sub(r"(cookies) file '[^']+'", r"\g<1>", getSafeExString(ex))
44154415
raise SqlmapGenericException(errMsg)
44164416

44174417
def decloakToTemp(filename):
@@ -4738,8 +4738,6 @@ def getSafeExString(ex, encoding=None):
47384738
u'foobar'
47394739
"""
47404740

4741-
retVal = ex
4742-
47434741
if getattr(ex, "message", None):
47444742
retVal = ex.message
47454743
elif getattr(ex, "msg", None):
@@ -4748,6 +4746,8 @@ def getSafeExString(ex, encoding=None):
47484746
retVal = ex[1]
47494747
elif isinstance(ex, (list, tuple)) and len(ex) > 0 and isinstance(ex[0], basestring):
47504748
retVal = ex[0]
4749+
else:
4750+
retVal = str(ex)
47514751

47524752
return getUnicode(retVal or "", encoding=encoding).strip()
47534753

lib/core/dump.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def dbTableValues(self, tableValues):
429429
except Exception as ex:
430430
try:
431431
tempDir = tempfile.mkdtemp(prefix="sqlmapdb")
432-
except IOError, _:
432+
except IOError as _:
433433
errMsg = "unable to write to the temporary directory ('%s'). " % _
434434
errMsg += "Please make sure that your disk is not full and "
435435
errMsg += "that you have sufficient write permissions to "

lib/core/option.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -846,8 +846,8 @@ def _setWafFunctions():
846846
if filename[:-3] in sys.modules:
847847
del sys.modules[filename[:-3]]
848848
module = __import__(filename[:-3].encode(sys.getfilesystemencoding() or UNICODE_ENCODING))
849-
except ImportError, msg:
850-
raise SqlmapSyntaxException("cannot import WAF script '%s' (%s)" % (filename[:-3], msg))
849+
except ImportError as ex:
850+
raise SqlmapSyntaxException("cannot import WAF script '%s' (%s)" % (filename[:-3], getSafeExString(ex)))
851851

852852
_ = dict(inspect.getmembers(module))
853853
if "detect" not in _:
@@ -1195,7 +1195,7 @@ def _setHTTPAuthentication():
11951195
elif authType == AUTH_TYPE.NTLM:
11961196
regExp = "^(.*\\\\.*):(.*?)$"
11971197
errMsg = "HTTP NTLM authentication credentials value must "
1198-
errMsg += "be in format 'DOMAIN\username:password'"
1198+
errMsg += "be in format 'DOMAIN\\username:password'"
11991199
elif authType == AUTH_TYPE.PKI:
12001200
errMsg = "HTTP PKI authentication require "
12011201
errMsg += "usage of option `--auth-pki`"
@@ -2136,9 +2136,9 @@ def _setDNSServer():
21362136
try:
21372137
conf.dnsServer = DNSServer()
21382138
conf.dnsServer.run()
2139-
except socket.error, msg:
2139+
except socket.error as ex:
21402140
errMsg = "there was an error while setting up "
2141-
errMsg += "DNS server instance ('%s')" % msg
2141+
errMsg += "DNS server instance ('%s')" % getSafeExString(ex)
21422142
raise SqlmapGenericException(errMsg)
21432143
else:
21442144
errMsg = "you need to run sqlmap as an administrator "

lib/core/profiling.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import cProfile
1111

12+
from lib.core.common import getSafeExString
1213
from lib.core.common import getUnicode
1314
from lib.core.data import logger
1415
from lib.core.data import paths
@@ -25,8 +26,8 @@ def profile(profileOutputFile=None, dotOutputFile=None, imageOutputFile=None):
2526
from thirdparty.xdot import xdot
2627
import gtk
2728
import pydot
28-
except ImportError, e:
29-
errMsg = "profiling requires third-party libraries ('%s') " % getUnicode(e, UNICODE_ENCODING)
29+
except ImportError as ex:
30+
errMsg = "profiling requires third-party libraries ('%s') " % getSafeExString(ex)
3031
errMsg += "(Hint: 'sudo apt-get install python-pydot python-pyparsing python-profiler graphviz')"
3132
logger.error(errMsg)
3233

lib/core/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from lib.core.enums import OS
2020

2121
# sqlmap version (<major>.<minor>.<month>.<monthly commit>)
22-
VERSION = "1.3.1.62"
22+
VERSION = "1.3.1.63"
2323
TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable"
2424
TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34}
2525
VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE)

lib/core/shell.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010

1111
from lib.core import readlineng as readline
12+
from lib.core.common import getSafeExString
1213
from lib.core.data import logger
1314
from lib.core.data import paths
1415
from lib.core.enums import AUTOCOMPLETE_TYPE
@@ -75,8 +76,8 @@ def saveHistory(completion=None):
7576
readline.set_history_length(MAX_HISTORY_LENGTH)
7677
try:
7778
readline.write_history_file(historyPath)
78-
except IOError, msg:
79-
warnMsg = "there was a problem writing the history file '%s' (%s)" % (historyPath, msg)
79+
except IOError as ex:
80+
warnMsg = "there was a problem writing the history file '%s' (%s)" % (historyPath, getSafeExString(ex))
8081
logger.warn(warnMsg)
8182
except KeyboardInterrupt:
8283
pass
@@ -99,8 +100,8 @@ def loadHistory(completion=None):
99100
if os.path.exists(historyPath):
100101
try:
101102
readline.read_history_file(historyPath)
102-
except IOError, msg:
103-
warnMsg = "there was a problem loading the history file '%s' (%s)" % (historyPath, msg)
103+
except IOError as ex:
104+
warnMsg = "there was a problem loading the history file '%s' (%s)" % (historyPath, getSafeExString(ex))
104105
logger.warn(warnMsg)
105106

106107
def autoCompletion(completion=None, os=None, commands=None):

lib/core/subprocessng.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def blockingReadFromFD(fd):
3131
while True:
3232
try:
3333
output += os.read(fd, 8192)
34-
except (OSError, IOError), ioe:
34+
except (OSError, IOError) as ioe:
3535
if ioe.args[0] in (errno.EAGAIN, errno.EINTR):
3636
# Uncomment the following line if the process seems to
3737
# take a huge amount of cpu time
@@ -52,7 +52,7 @@ def blockingWriteToFD(fd, data):
5252
try:
5353
data_length = len(data)
5454
wrote_data = os.write(fd, data)
55-
except (OSError, IOError), io:
55+
except (OSError, IOError) as io:
5656
if io.errno in (errno.EAGAIN, errno.EINTR):
5757
continue
5858
else:
@@ -95,8 +95,8 @@ def send(self, input):
9595
(errCode, written) = WriteFile(x, input)
9696
except ValueError:
9797
return self._close('stdin')
98-
except (subprocess.pywintypes.error, Exception), why:
99-
if why[0] in (109, errno.ESHUTDOWN):
98+
except (subprocess.pywintypes.error, Exception) as ex:
99+
if ex[0] in (109, errno.ESHUTDOWN):
100100
return self._close('stdin')
101101
raise
102102

@@ -116,8 +116,8 @@ def _recv(self, which, maxsize):
116116
(errCode, read) = ReadFile(x, nAvail, None)
117117
except (ValueError, NameError):
118118
return self._close(which)
119-
except (subprocess.pywintypes.error, Exception), why:
120-
if why[0] in (109, errno.ESHUTDOWN):
119+
except (subprocess.pywintypes.error, Exception) as ex:
120+
if ex[0] in (109, errno.ESHUTDOWN):
121121
return self._close(which)
122122
raise
123123

@@ -134,8 +134,8 @@ def send(self, input):
134134

135135
try:
136136
written = os.write(self.stdin.fileno(), input)
137-
except OSError, why:
138-
if why[0] == errno.EPIPE: # broken pipe
137+
except OSError as ex:
138+
if ex[0] == errno.EPIPE: # broken pipe
139139
return self._close('stdin')
140140
raise
141141

lib/core/target.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,8 @@ def _setHashDB():
427427
try:
428428
os.remove(conf.hashDBFile)
429429
logger.info("flushing session file")
430-
except OSError, msg:
431-
errMsg = "unable to flush the session file (%s)" % msg
430+
except OSError as ex:
431+
errMsg = "unable to flush the session file ('%s')" % getSafeExString(ex)
432432
raise SqlmapFilePathException(errMsg)
433433

434434
conf.hashDB = HashDB(conf.hashDBFile)
@@ -566,7 +566,7 @@ def _setResultsFile():
566566
conf.resultsFP = openFile(conf.resultsFilename, "w+", UNICODE_ENCODING, buffering=0)
567567
warnMsg += "Using temporary file '%s' instead" % conf.resultsFilename
568568
logger.warn(warnMsg)
569-
except IOError, _:
569+
except IOError as _:
570570
errMsg = "unable to write to the temporary directory ('%s'). " % _
571571
errMsg += "Please make sure that your disk is not full and "
572572
errMsg += "that you have sufficient write permissions to "

lib/parse/cmdline.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -897,8 +897,8 @@ def _(self, *args):
897897

898898
return args
899899

900-
except (OptionError, TypeError), e:
901-
parser.error(e)
900+
except (OptionError, TypeError) as ex:
901+
parser.error(ex)
902902

903903
except SystemExit:
904904
# Protection against Windows dummy double clicking

0 commit comments

Comments
 (0)