From 4cc13d3c1ef542f10748d0fc91a32509937d9ac3 Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Mon, 20 May 2019 11:21:31 +0200 Subject: [PATCH] Fixes #3680 --- lib/core/common.py | 6 +++--- lib/core/convert.py | 32 ++++++++++++++++++++------------ lib/core/patch.py | 4 ++-- lib/core/settings.py | 2 +- thirdparty/ansistrm/ansistrm.py | 4 ++-- 5 files changed, 28 insertions(+), 20 deletions(-) mode change 100644 => 100755 lib/core/convert.py diff --git a/lib/core/common.py b/lib/core/common.py index 037b6be5c..1df5843f8 100644 --- a/lib/core/common.py +++ b/lib/core/common.py @@ -58,7 +58,7 @@ from lib.core.convert import getBytes from lib.core.convert import getText from lib.core.convert import getUnicode from lib.core.convert import htmlunescape -from lib.core.convert import stdoutencode +from lib.core.convert import stdoutEncode from lib.core.data import conf from lib.core.data import kb from lib.core.data import logger @@ -968,9 +968,9 @@ def dataToStdout(data, forceOutput=False, bold=False, content_type=None, status= try: if conf.get("api"): - sys.stdout.write(stdoutencode(clearColors(data)), status, content_type) + sys.stdout.write(stdoutEncode(clearColors(data)), status, content_type) else: - sys.stdout.write(stdoutencode(setColor(data, bold=bold))) + sys.stdout.write(stdoutEncode(setColor(data, bold=bold))) sys.stdout.flush() except IOError: diff --git a/lib/core/convert.py b/lib/core/convert.py old mode 100644 new mode 100755 index e2b4d9a88..97d97473d --- a/lib/core/convert.py +++ b/lib/core/convert.py @@ -23,6 +23,7 @@ from lib.core.settings import INVALID_UNICODE_PRIVATE_AREA from lib.core.settings import IS_WIN from lib.core.settings import NULL from lib.core.settings import PICKLE_PROTOCOL +from lib.core.settings import PYVERSION from lib.core.settings import SAFE_HEX_MARKER from lib.core.settings import UNICODE_ENCODING from thirdparty import six @@ -100,27 +101,34 @@ def filterNone(values): # Cross-referenced function def isListLike(value): # Cross-referenced function raise NotImplementedError -def stdoutencode(data): - retVal = data +def stdoutEncode(value): + value = value or "" - if six.PY2: - try: - retVal = getBytes(data or "", sys.stdout.encoding, unsafe=False) + if isinstance(value, six.text_type) and PYVERSION < "3.6": + encoding = sys.stdout.encoding or UNICODE_ENCODING - # Reference: http://bugs.python.org/issue1602 - if IS_WIN: - if '?' in retVal and '?' not in retVal: - warnMsg = "cannot properly display Unicode characters " + while True: + try: + retVal = value.encode(encoding) + break + except UnicodeEncodeError as ex: + value = value[:ex.start] + "?" + value[ex.end:] + + if IS_WIN and PYVERSION < "3.6": + warnMsg = "cannot properly display (some) Unicode characters " warnMsg += "inside Windows OS command prompt " - warnMsg += "(http://bugs.python.org/issue1602). All " + warnMsg += "(https://bugs.python.org/issue1602). All " warnMsg += "unhandled occurrences will result in " warnMsg += "replacement with '?' character. Please, find " warnMsg += "proper character representation inside " warnMsg += "corresponding output files. " singleTimeWarnMessage(warnMsg) - except: - retVal = getBytes(data or "", unsafe=False) + if six.PY3: + retVal = getUnicode(retVal, encoding) + + else: + retVal = value return retVal diff --git a/lib/core/patch.py b/lib/core/patch.py index e5bf06f19..18a68a6c7 100644 --- a/lib/core/patch.py +++ b/lib/core/patch.py @@ -24,7 +24,7 @@ from lib.core.common import getSafeExString from lib.core.common import isListLike from lib.core.common import singleTimeWarnMessage from lib.core.common import readInput -from lib.core.convert import stdoutencode +from lib.core.convert import stdoutEncode from lib.core.option import _setHTTPHandlers from lib.core.option import setVerbosity from lib.core.option import _setWafFunctions @@ -70,7 +70,7 @@ def resolveCrossReferences(): lib.controller.checks.setVerbosity = setVerbosity lib.controller.checks.setWafFunctions = _setWafFunctions lib.utils.sqlalchemy.getSafeExString = getSafeExString - thirdparty.ansistrm.ansistrm.stdoutencode = stdoutencode + thirdparty.ansistrm.ansistrm.stdoutEncode = stdoutEncode def pympTempLeakPatch(tempDir): """ diff --git a/lib/core/settings.py b/lib/core/settings.py index 379f07a17..433a9dc0f 100644 --- a/lib/core/settings.py +++ b/lib/core/settings.py @@ -18,7 +18,7 @@ from lib.core.enums import OS from thirdparty.six import unichr as _unichr # sqlmap version (...) -VERSION = "1.3.5.109" +VERSION = "1.3.5.110" 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/thirdparty/ansistrm/ansistrm.py b/thirdparty/ansistrm/ansistrm.py index 41e6646f7..ecb478fd6 100644 --- a/thirdparty/ansistrm/ansistrm.py +++ b/thirdparty/ansistrm/ansistrm.py @@ -20,7 +20,7 @@ if IS_WIN: ctypes.windll.kernel32.SetConsoleTextAttribute.argtypes = [ctypes.wintypes.HANDLE, ctypes.wintypes.WORD] ctypes.windll.kernel32.SetConsoleTextAttribute.restype = ctypes.wintypes.BOOL -def stdoutencode(data): # Cross-referenced function +def stdoutEncode(data): # Cross-referenced function raise NotImplementedError class ColorizingStreamHandler(logging.StreamHandler): @@ -56,7 +56,7 @@ class ColorizingStreamHandler(logging.StreamHandler): def emit(self, record): try: - message = stdoutencode(self.format(record)) + message = stdoutEncode(self.format(record)) stream = self.stream if not self.is_tty: