From ebfcf05512b8b3e049c809471fc3b1d0ec9f49f4 Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Sat, 25 May 2019 00:22:27 +0200 Subject: [PATCH] Improvement for #3453 --- lib/core/convert.py | 94 ++++++++++++++++++++++---------------------- lib/core/settings.py | 2 +- 2 files changed, 49 insertions(+), 47 deletions(-) diff --git a/lib/core/convert.py b/lib/core/convert.py index 54aa42471..42ef24843 100644 --- a/lib/core/convert.py +++ b/lib/core/convert.py @@ -105,52 +105,6 @@ def isListLike(value): # Cross-referenced function def shellExec(cmd): # Cross-referenced function raise NotImplementedError -def stdoutEncode(value): - value = value or "" - - if IS_WIN and IS_TTY and kb.get("codePage", -1) is None: - output = shellExec("chcp") - match = re.search(r": (\d{3,})", output or "") - - if match: - try: - candidate = "cp%s" % match.group(1) - codecs.lookup(candidate) - except LookupError: - pass - else: - kb.codePage = candidate - - kb.codePage = kb.codePage or "" - - if isinstance(value, six.text_type) and PYVERSION < "3.6": - encoding = kb.get("codePage") or sys.stdout.encoding or UNICODE_ENCODING - - 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 += "(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) - - if six.PY3: - retVal = getUnicode(retVal, encoding) - - else: - retVal = value - - return retVal - def jsonize(data): """ Returns JSON serialized data @@ -365,3 +319,51 @@ def getText(value): pass return retVal + +def stdoutEncode(value): + """ + Returns binary representation of a given Unicode value safe for writing to stdout + """ + + value = value or "" + + if IS_WIN and IS_TTY and kb.get("codePage", -1) is None: + output = shellExec("chcp") + match = re.search(r": (\d{3,})", output or "") + + if match: + try: + candidate = "cp%s" % match.group(1) + codecs.lookup(candidate) + except LookupError: + pass + else: + kb.codePage = candidate + + kb.codePage = kb.codePage or "" + + if isinstance(value, six.text_type): + encoding = kb.get("codePage") or sys.stdout.encoding or UNICODE_ENCODING + + while True: + try: + retVal = value.encode(encoding) + break + except UnicodeEncodeError as ex: + value = value[:ex.start] + "?" * (ex.end - ex.start) + value[ex.end:] + + warnMsg = "cannot properly display (some) Unicode characters " + warnMsg += "inside your terminal ('%s') environment. All " % encoding + warnMsg += "unhandled occurrences will result in " + warnMsg += "replacement with '?' character. Please, find " + warnMsg += "proper character representation inside " + warnMsg += "corresponding output files" + singleTimeWarnMessage(warnMsg) + + if six.PY3: + retVal = getUnicode(retVal, encoding) + + else: + retVal = value + + return retVal diff --git a/lib/core/settings.py b/lib/core/settings.py index b3a28d9cc..28861c037 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.138" +VERSION = "1.3.5.139" 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)