diff --git a/extra/cloak/cloak.py b/extra/cloak/cloak.py index 79d42dba0..4883342c8 100644 --- a/extra/cloak/cloak.py +++ b/extra/cloak/cloak.py @@ -7,6 +7,8 @@ Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/) See the file 'LICENSE' for copying permission """ +from __future__ import print_function + import os import sys import zlib @@ -38,7 +40,7 @@ def decloak(inputFile=None, data=None): try: data = zlib.decompress(hideAscii(data)) except: - print 'ERROR: the provided input file \'%s\' does not contain valid cloaked content' % inputFile + print('ERROR: the provided input file \'%s\' does not contain valid cloaked content' % inputFile) sys.exit(1) finally: f.close() @@ -63,7 +65,7 @@ def main(): parser.error(e) if not os.path.isfile(args.inputFile): - print 'ERROR: the provided input file \'%s\' is non existent' % args.inputFile + print('ERROR: the provided input file \'%s\' is non existent' % args.inputFile) sys.exit(1) if not args.decrypt: diff --git a/extra/dbgtool/dbgtool.py b/extra/dbgtool/dbgtool.py index fa65d448b..72403b2fd 100644 --- a/extra/dbgtool/dbgtool.py +++ b/extra/dbgtool/dbgtool.py @@ -7,6 +7,8 @@ Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/) See the file 'LICENSE' for copying permission """ +from __future__ import print_function + import os import sys import struct @@ -19,7 +21,7 @@ def convert(inputFile): fileSize = fileStat.st_size if fileSize > 65280: - print "ERROR: the provided input file '%s' is too big for debug.exe" % inputFile + print("ERROR: the provided input file '%s' is too big for debug.exe" % inputFile) sys.exit(1) script = "n %s\nr cx\n" % os.path.basename(inputFile.replace(".", "_")) @@ -59,7 +61,7 @@ def convert(inputFile): def main(inputFile, outputFile): if not os.path.isfile(inputFile): - print "ERROR: the provided input file '%s' is not a regular file" % inputFile + print("ERROR: the provided input file '%s' is not a regular file" % inputFile) sys.exit(1) script = convert(inputFile) @@ -70,7 +72,7 @@ def main(inputFile, outputFile): sys.stdout.write(script) sys.stdout.close() else: - print script + print(script) if __name__ == "__main__": usage = "%s -i [-o ]" % sys.argv[0] diff --git a/extra/safe2bin/safe2bin.py b/extra/safe2bin/safe2bin.py index c426c124b..6811a1e71 100644 --- a/extra/safe2bin/safe2bin.py +++ b/extra/safe2bin/safe2bin.py @@ -7,6 +7,8 @@ Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/) See the file 'LICENSE' for copying permission """ +from __future__ import print_function + import binascii import re import string @@ -112,7 +114,7 @@ def main(): parser.error(e) if not os.path.isfile(args.inputFile): - print 'ERROR: the provided input file \'%s\' is not a regular file' % args.inputFile + print('ERROR: the provided input file \'%s\' is not a regular file' % args.inputFile) sys.exit(1) f = open(args.inputFile, 'r') diff --git a/extra/shutils/duplicates.py b/extra/shutils/duplicates.py index e56c96cbe..b5431af4f 100755 --- a/extra/shutils/duplicates.py +++ b/extra/shutils/duplicates.py @@ -5,6 +5,8 @@ # Removes duplicate entries in wordlist like files +from __future__ import print_function + import sys if len(sys.argv) > 0: @@ -17,7 +19,7 @@ if len(sys.argv) > 0: str.encode(item) if item in items: if item: - print item + print(item) else: items.append(item) except: diff --git a/extra/shutils/newlines.py b/extra/shutils/newlines.py index c506e5f48..63a557a1f 100644 --- a/extra/shutils/newlines.py +++ b/extra/shutils/newlines.py @@ -3,6 +3,8 @@ # Runs pylint on all python scripts found in a directory tree # Reference: http://rowinggolfer.blogspot.com/2009/08/pylint-recursively.html +from __future__ import print_function + import os import sys @@ -12,16 +14,16 @@ def check(filepath): if "\n\n\n" in content: index = content.find("\n\n\n") - print filepath, repr(content[index - 30:index + 30]) + print(filepath, repr(content[index - 30:index + 30])) if __name__ == "__main__": try: BASE_DIRECTORY = sys.argv[1] except IndexError: - print "no directory specified, defaulting to current working directory" + print("no directory specified, defaulting to current working directory") BASE_DIRECTORY = os.getcwd() - print "looking for *.py scripts in subdirectories of ", BASE_DIRECTORY + print("looking for *.py scripts in subdirectories of '%s'" % BASE_DIRECTORY) for root, dirs, files in os.walk(BASE_DIRECTORY): if any(_ in root for _ in ("extra", "thirdparty")): continue diff --git a/extra/shutils/pylint.py b/extra/shutils/pylint.py index e6b475351..cec5321b2 100755 --- a/extra/shutils/pylint.py +++ b/extra/shutils/pylint.py @@ -3,6 +3,8 @@ # Runs pylint on all python scripts found in a directory tree # Reference: http://rowinggolfer.blogspot.com/2009/08/pylint-recursively.html +from __future__ import print_function + import os import re import sys @@ -17,26 +19,26 @@ def check(module): if module[-3:] == ".py": - print "CHECKING ", module + print("CHECKING ", module) pout = os.popen("pylint --rcfile=/dev/null %s" % module, 'r') for line in pout: if re.match(r"\AE:", line): - print line.strip() + print(line.strip()) if __RATING__ and "Your code has been rated at" in line: - print line + print(line) score = re.findall(r"\d.\d\d", line)[0] total += float(score) count += 1 if __name__ == "__main__": try: - print sys.argv + print(sys.argv) BASE_DIRECTORY = sys.argv[1] except IndexError: - print "no directory specified, defaulting to current working directory" + print("no directory specified, defaulting to current working directory") BASE_DIRECTORY = os.getcwd() - print "looking for *.py scripts in subdirectories of ", BASE_DIRECTORY + print("looking for *.py scripts in subdirectories of ", BASE_DIRECTORY) for root, dirs, files in os.walk(BASE_DIRECTORY): if any(_ in root for _ in ("extra", "thirdparty")): continue @@ -45,6 +47,6 @@ if __name__ == "__main__": check(filepath) if __RATING__: - print "==" * 50 - print "%d modules found" % count - print "AVERAGE SCORE = %.02f" % (total / count) + print("==" * 50) + print("%d modules found" % count) + print("AVERAGE SCORE = %.02f" % (total / count)) diff --git a/extra/shutils/regressiontest.py b/extra/shutils/regressiontest.py index 9a8ecde59..e4e920571 100755 --- a/extra/shutils/regressiontest.py +++ b/extra/shutils/regressiontest.py @@ -3,6 +3,8 @@ # Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/) # See the file 'LICENSE' for copying permission +from __future__ import print_function + import codecs import inspect import os @@ -56,8 +58,8 @@ def send_email(msg): s.sendmail(FROM, TO, msg.as_string()) s.quit() # Catch all for SMTP exceptions - except smtplib.SMTPException, e: - print "Failure to send email: %s" % str(e) + except smtplib.SMTPException as ex: + print("Failure to send email: '%s" % ex) def failure_email(msg): msg = prepare_email(msg) @@ -157,7 +159,7 @@ if __name__ == "__main__": try: main() - except Exception, e: + except Exception: log_fd.write("An exception has occurred:\n%s" % str(traceback.format_exc())) log_fd.write("Regression test finished at %s\n\n" % time.strftime("%H:%M:%S %d-%m-%Y", time.gmtime())) diff --git a/extra/sqlharvest/sqlharvest.py b/extra/sqlharvest/sqlharvest.py index 21ec3291c..42810fa71 100644 --- a/extra/sqlharvest/sqlharvest.py +++ b/extra/sqlharvest/sqlharvest.py @@ -5,6 +5,8 @@ Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/) See the file 'LICENSE' for copying permission """ +from __future__ import print_function + import cookielib import re import socket @@ -75,8 +77,8 @@ def main(): except KeyboardInterrupt: raise - except Exception, msg: - print msg + except Exception as ex: + print(ex) if abort: break @@ -86,7 +88,7 @@ def main(): sys.stdout.write("---------------\n") for sqlfile in files: - print sqlfile + print(sqlfile) try: req = urllib2.Request(sqlfile) @@ -118,8 +120,8 @@ def main(): except KeyboardInterrupt: raise - except Exception, msg: - print msg + except Exception as ex: + print(ex) else: i += 1 diff --git a/extra/wafdetectify/wafdetectify.py b/extra/wafdetectify/wafdetectify.py index c51b4f1cb..ba269a631 100755 --- a/extra/wafdetectify/wafdetectify.py +++ b/extra/wafdetectify/wafdetectify.py @@ -5,6 +5,8 @@ Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/) See the file 'LICENSE' for copying permission """ +from __future__ import print_function + import cookielib import glob import httplib @@ -68,7 +70,7 @@ def colorize(message): def main(): global WAF_FUNCTIONS - print colorize("%s #v%s\n by: %s\n" % (NAME, VERSION, AUTHOR)) + print(colorize("%s #v%s\n by: %s\n" % (NAME, VERSION, AUTHOR))) if len(sys.argv) < 2: exit(colorize("[x] usage: python %s " % os.path.split(__file__)[-1])) @@ -104,13 +106,13 @@ def main(): WAF_FUNCTIONS = sorted(WAF_FUNCTIONS, key=lambda _: "generic" in _[1].lower()) - print colorize("[i] checking '%s'..." % sys.argv[1]) + print(colorize("[i] checking '%s'..." % sys.argv[1])) hostname = sys.argv[1].split("//")[-1].split('/')[0] try: socket.getaddrinfo(hostname, None) except socket.gaierror: - print colorize("[x] host '%s' does not exist" % hostname) + print(colorize("[x] host '%s' does not exist" % hostname)) exit(1) found = False @@ -122,7 +124,7 @@ def main(): exit(colorize("[!] WAF/IPS identified as '%s'" % product)) if not found: - print colorize("[o] nothing found") + print(colorize("[o] nothing found")) print diff --git a/lib/controller/checks.py b/lib/controller/checks.py index da836099a..672de3e14 100644 --- a/lib/controller/checks.py +++ b/lib/controller/checks.py @@ -627,10 +627,10 @@ def checkSqlInjection(place, parameter, value): injectable = True - except SqlmapConnectionException, msg: + except SqlmapConnectionException as ex: debugMsg = "problem occurred most likely because the " debugMsg += "server hasn't recovered as expected from the " - debugMsg += "error-based payload used ('%s')" % msg + debugMsg += "error-based payload used ('%s')" % getSafeExString(ex) logger.debug(debugMsg) # In case of time-based blind or stacked queries diff --git a/lib/core/common.py b/lib/core/common.py index c6c062952..3f11d2049 100644 --- a/lib/core/common.py +++ b/lib/core/common.py @@ -4746,6 +4746,8 @@ def getSafeExString(ex, encoding=None): retVal = ex.msg elif isinstance(ex, (list, tuple)) and len(ex) > 1 and isinstance(ex[1], basestring): retVal = ex[1] + elif isinstance(ex, (list, tuple)) and len(ex) > 0 and isinstance(ex[0], basestring): + retVal = ex[0] return getUnicode(retVal or "", encoding=encoding).strip() diff --git a/lib/core/dump.py b/lib/core/dump.py index ed468b33e..f7bf54a27 100644 --- a/lib/core/dump.py +++ b/lib/core/dump.py @@ -624,8 +624,8 @@ class Dump(object): with open(filepath, "wb") as f: _ = safechardecode(value, True) f.write(_) - except magic.MagicException, err: - logger.debug(str(err)) + except magic.MagicException as ex: + logger.debug(getSafeExString(ex)) if conf.dumpFormat == DUMP_FORMAT.CSV: if field == fields: diff --git a/lib/core/settings.py b/lib/core/settings.py index 69dc0e27f..3354c5b73 100644 --- a/lib/core/settings.py +++ b/lib/core/settings.py @@ -19,7 +19,7 @@ from lib.core.enums import DBMS_DIRECTORY_NAME from lib.core.enums import OS # sqlmap version (...) -VERSION = "1.3.1.60" +VERSION = "1.3.1.61" TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable" TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34} VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE) diff --git a/lib/core/target.py b/lib/core/target.py index 5a6c47f47..91bbe754c 100644 --- a/lib/core/target.py +++ b/lib/core/target.py @@ -646,7 +646,7 @@ def _createTargetDirs(): except (OSError, IOError) as ex: try: tempDir = tempfile.mkdtemp(prefix="sqlmap%s" % context) - except Exception, _: + except Exception as _: errMsg = "unable to write to the temporary directory ('%s'). " % _ errMsg += "Please make sure that your disk is not full and " errMsg += "that you have sufficient write permissions to " @@ -668,7 +668,7 @@ def _createTargetDirs(): except (OSError, IOError, TypeError) as ex: try: tempDir = tempfile.mkdtemp(prefix="sqlmapoutput") - except Exception, _: + except Exception as _: errMsg = "unable to write to the temporary directory ('%s'). " % _ errMsg += "Please make sure that your disk is not full and " errMsg += "that you have sufficient write permissions to " @@ -767,4 +767,4 @@ def setupTargetEnv(): _resumeHashDBValues() _setResultsFile() _setAuthCred() - _setAuxOptions() \ No newline at end of file + _setAuxOptions() diff --git a/lib/core/testing.py b/lib/core/testing.py index 87ff3a673..030f58f39 100644 --- a/lib/core/testing.py +++ b/lib/core/testing.py @@ -75,10 +75,10 @@ def smokeTest(): try: __import__(path) module = sys.modules[path] - except Exception, msg: + except Exception as ex: retVal = False dataToStdout("\r") - errMsg = "smoke test failed at importing module '%s' (%s):\n%s" % (path, os.path.join(root, filename), msg) + errMsg = "smoke test failed at importing module '%s' (%s):\n%s" % (path, os.path.join(root, filename), ex) logger.error(errMsg) else: # Run doc tests @@ -275,10 +275,10 @@ def runCase(parse): result = start() except KeyboardInterrupt: pass - except SqlmapBaseException, e: - handled_exception = e - except Exception, e: - unhandled_exception = e + except SqlmapBaseException as ex: + handled_exception = ex + except Exception as ex: + unhandled_exception = ex finally: sys.stdout.seek(0) console = sys.stdout.read() diff --git a/lib/parse/cmdline.py b/lib/parse/cmdline.py index 669985bc2..256b39ab1 100644 --- a/lib/parse/cmdline.py +++ b/lib/parse/cmdline.py @@ -5,6 +5,8 @@ Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/) See the file 'LICENSE' for copying permission """ +from __future__ import print_function + import os import re import shlex @@ -842,7 +844,7 @@ def cmdLineParser(argv=None): argv[i] = argv[i][:-1] conf.skipThreadCheck = True elif argv[i] == "--version": - print VERSION_STRING.split('/')[-1] + print(VERSION_STRING.split('/')[-1]) raise SystemExit elif argv[i] in ("-h", "--help"): advancedHelp = False diff --git a/lib/request/basic.py b/lib/request/basic.py index 5452ea99c..acdd29f10 100644 --- a/lib/request/basic.py +++ b/lib/request/basic.py @@ -17,6 +17,7 @@ from lib.core.common import Backend from lib.core.common import extractErrorMessage from lib.core.common import extractRegexResult from lib.core.common import getPublicTypeMembers +from lib.core.common import getSafeExString from lib.core.common import getUnicode from lib.core.common import isListLike from lib.core.common import randomStr @@ -280,10 +281,10 @@ def decodePage(page, contentEncoding, contentType): raise Exception("size too large") page = data.read() - except Exception, msg: + except Exception as ex: if "