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

Skip to content

Commit bb7bd51

Browse files
committed
Some more DREI stuff
1 parent da15701 commit bb7bd51

15 files changed

Lines changed: 94 additions & 71 deletions

File tree

extra/safe2bin/safe2bin.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
if sys.version_info >= (3, 0):
2222
xrange = range
23+
text_type = str
24+
else:
25+
text_type = unicode
2326

2427
# Regex used for recognition of hex encoded characters
2528
HEX_ENCODED_CHAR_REGEX = r"(?P<result>\\x[0-9A-Fa-f]{2})"
@@ -52,14 +55,14 @@ def safecharencode(value):
5255
retVal = value
5356

5457
if isinstance(value, basestring):
55-
if any([_ not in SAFE_CHARS for _ in value]):
58+
if any(_ not in SAFE_CHARS for _ in value):
5659
retVal = retVal.replace(HEX_ENCODED_PREFIX, HEX_ENCODED_PREFIX_MARKER)
5760
retVal = retVal.replace('\\', SLASH_MARKER)
5861

5962
for char in SAFE_ENCODE_SLASH_REPLACEMENTS:
6063
retVal = retVal.replace(char, repr(char).strip('\''))
6164

62-
retVal = reduce(lambda x, y: x + (y if (y in string.printable or isinstance(value, unicode) and ord(y) >= 160) else '\\x%02x' % ord(y)), retVal, (unicode if isinstance(value, unicode) else str)())
65+
retVal = reduce(lambda x, y: x + (y if (y in string.printable or isinstance(value, text_type) and ord(y) >= 160) else '\\x%02x' % ord(y)), retVal, type(value)())
6366

6467
retVal = retVal.replace(SLASH_MARKER, "\\\\")
6568
retVal = retVal.replace(HEX_ENCODED_PREFIX_MARKER, HEX_ENCODED_PREFIX)
@@ -81,7 +84,7 @@ def safechardecode(value, binary=False):
8184
while True:
8285
match = re.search(HEX_ENCODED_CHAR_REGEX, retVal)
8386
if match:
84-
retVal = retVal.replace(match.group("result"), (unichr if isinstance(value, unicode) else chr)(ord(binascii.unhexlify(match.group("result").lstrip("\\x")))))
87+
retVal = retVal.replace(match.group("result"), (unichr if isinstance(value, text_type) else chr)(ord(binascii.unhexlify(match.group("result").lstrip("\\x")))))
8588
else:
8689
break
8790

@@ -91,7 +94,7 @@ def safechardecode(value, binary=False):
9194
retVal = retVal.replace(SLASH_MARKER, '\\')
9295

9396
if binary:
94-
if isinstance(retVal, unicode):
97+
if isinstance(retVal, text_type):
9598
retVal = retVal.encode("utf8")
9699

97100
elif isinstance(value, (list, tuple)):

extra/shutils/pyflakes.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
# See the file 'LICENSE' for copying permission
55

66
# Runs pyflakes on all python files (prerequisite: apt-get install pyflakes)
7-
find . -wholename "./thirdparty" -prune -o -type f -iname "*.py" -exec pyflakes '{}' \;
7+
find . -wholename "./thirdparty" -prune -o -type f -iname "*.py" -exec pyflakes '{}' \; | grep -v "redefines '_'"

lib/controller/controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def start():
333333

334334
testSqlInj = False
335335

336-
if PLACE.GET in conf.parameters and not any([conf.data, conf.testParameter]):
336+
if PLACE.GET in conf.parameters and not any((conf.data, conf.testParameter)):
337337
for parameter in re.findall(r"([^=]+)=([^%s]+%s?|\Z)" % (re.escape(conf.paramDel or "") or DEFAULT_GET_POST_DELIMITER, re.escape(conf.paramDel or "") or DEFAULT_GET_POST_DELIMITER), conf.parameters[PLACE.GET]):
338338
paramKey = (conf.hostname, conf.path, PLACE.GET, parameter[0])
339339

lib/core/common.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,16 @@ def singleTimeLogMessage(message, level=logging.INFO, flag=None):
882882
logger.log(level, message)
883883

884884
def boldifyMessage(message):
885+
"""
886+
Sets ANSI bold marking on entire message if parts found in predefined BOLD_PATTERNS
887+
888+
>>> boldifyMessage("Hello World")
889+
'Hello World'
890+
891+
>>> boldifyMessage("GET parameter id is not injectable")
892+
'\\x1b[1mGET parameter id is not injectable\\x1b[0m'
893+
"""
894+
885895
retVal = message
886896

887897
if any(_ in message for _ in BOLD_PATTERNS):
@@ -890,6 +900,13 @@ def boldifyMessage(message):
890900
return retVal
891901

892902
def setColor(message, color=None, bold=False, level=None):
903+
"""
904+
Sets ANSI color codes
905+
906+
>>> setColor("Hello World", "red")
907+
'\\x1b[31mHello World\\x1b[0m'
908+
"""
909+
893910
retVal = message
894911
level = level or extractRegexResult(r"\[(?P<result>%s)\]" % '|'.join(_[0] for _ in getPublicTypeMembers(LOGGING_LEVELS)), message)
895912

@@ -933,7 +950,7 @@ def dataToStdout(data, forceOutput=False, bold=False, content_type=None, status=
933950
if multiThreadMode:
934951
logging._acquireLock()
935952

936-
if isinstance(data, unicode):
953+
if isinstance(data, six.text_type):
937954
message = stdoutencode(data)
938955
else:
939956
message = data
@@ -1840,7 +1857,7 @@ def safeFilepathEncode(filepath):
18401857

18411858
retVal = filepath
18421859

1843-
if filepath and isinstance(filepath, unicode):
1860+
if filepath and isinstance(filepath, six.text_type):
18441861
retVal = filepath.encode(sys.getfilesystemencoding() or UNICODE_ENCODING)
18451862

18461863
return retVal
@@ -1927,7 +1944,7 @@ def getFilteredPageContent(page, onlyText=True, split=" "):
19271944
retVal = page
19281945

19291946
# only if the page's charset has been successfully identified
1930-
if isinstance(page, unicode):
1947+
if isinstance(page, six.text_type):
19311948
retVal = re.sub(r"(?si)<script.+?</script>|<!--.+?-->|<style.+?</style>%s" % (r"|<[^>]+>|\t|\n|\r" if onlyText else ""), split, page)
19321949
retVal = re.sub(r"%s{2,}" % split, split, retVal)
19331950
retVal = htmlunescape(retVal.strip().strip(split))
@@ -1945,7 +1962,7 @@ def getPageWordSet(page):
19451962
retVal = set()
19461963

19471964
# only if the page's charset has been successfully identified
1948-
if isinstance(page, unicode):
1965+
if isinstance(page, six.text_type):
19491966
retVal = set(_.group(0) for _ in re.finditer(r"\w+", getFilteredPageContent(page)))
19501967

19511968
return retVal
@@ -2430,7 +2447,7 @@ def getUnicode(value, encoding=None, noneToNull=False):
24302447
except UnicodeDecodeError:
24312448
return six.text_type(str(value), errors="ignore") # encoding ignored for non-basestring instances
24322449

2433-
def getBytes(value, encoding=UNICODE_ENCODING):
2450+
def getBytes(value, encoding=UNICODE_ENCODING, errors="strict"):
24342451
"""
24352452
Returns byte representation of provided Unicode value
24362453
@@ -2445,11 +2462,11 @@ def getBytes(value, encoding=UNICODE_ENCODING):
24452462
for char in xrange(0xF0000, 0xF00FF + 1):
24462463
value = value.replace(unichr(char), "%s%02x" % (SAFE_HEX_MARKER, char - 0xF0000))
24472464

2448-
retVal = value.encode(encoding)
2465+
retVal = value.encode(encoding, errors)
24492466

24502467
retVal = re.sub(r"%s([0-9a-f]{2})" % SAFE_HEX_MARKER, lambda _: _.group(1).decode("hex"), retVal)
24512468
else:
2452-
retVal = value.encode(encoding)
2469+
retVal = value.encode(encoding, errors)
24532470
retVal = re.sub(r"\\x([0-9a-f]{2})", lambda _: _.group(1).decode("hex"), retVal)
24542471

24552472
return retVal
@@ -3694,7 +3711,7 @@ def removeReflectiveValues(content, payload, suppressWarning=False):
36943711
retVal = content
36953712

36963713
try:
3697-
if all((content, payload)) and isinstance(content, unicode) and kb.reflectiveMechanism and not kb.heuristicMode:
3714+
if all((content, payload)) and isinstance(content, six.text_type) and kb.reflectiveMechanism and not kb.heuristicMode:
36983715
def _(value):
36993716
while 2 * REFLECTED_REPLACEMENT_REGEX in value:
37003717
value = value.replace(2 * REFLECTED_REPLACEMENT_REGEX, REFLECTED_REPLACEMENT_REGEX)
@@ -3786,7 +3803,7 @@ def normalizeUnicode(value):
37863803
'sucuraj'
37873804
"""
37883805

3789-
return unicodedata.normalize("NFKD", value).encode("ascii", "ignore") if isinstance(value, unicode) else value
3806+
return unicodedata.normalize("NFKD", value).encode("ascii", "ignore") if isinstance(value, six.text_type) else value
37903807

37913808
def safeSQLIdentificatorNaming(name, isTable=False):
37923809
"""
@@ -4105,7 +4122,7 @@ def quote(s, safe):
41054122
# _urllib.parse.quote(s.replace('%', '')) != s.replace('%', '')
41064123
# which would trigger on all %-characters, e.g. "&".
41074124
if getUnicode(s).encode("ascii", "replace") != s or forceQuote:
4108-
return _urllib.parse.quote(s.encode(UNICODE_ENCODING) if isinstance(s, unicode) else s, safe=safe)
4125+
return _urllib.parse.quote(s.encode(UNICODE_ENCODING) if isinstance(s, six.text_type) else s, safe=safe)
41094126
return s
41104127

41114128
username = quote(parts.username, '')
@@ -4459,8 +4476,8 @@ def _(value):
44594476
retVal = retVal.decode("utf-16-be")
44604477
except UnicodeDecodeError:
44614478
pass
4462-
if not isinstance(retVal, unicode):
4463-
retVal = getUnicode(retVal, conf.encoding or "utf8")
4479+
if not isinstance(retVal, six.text_type):
4480+
retVal = getUnicode(retVal, conf.encoding or UNICODE_ENCODING)
44644481

44654482
return retVal
44664483

lib/core/dump.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def dbTables(self, dbTables):
242242
if table and isListLike(table):
243243
table = table[0]
244244

245-
maxlength = max(maxlength, len(unsafeSQLIdentificatorNaming(normalizeUnicode(table) or unicode(table))))
245+
maxlength = max(maxlength, len(unsafeSQLIdentificatorNaming(normalizeUnicode(table) or getUnicode(table))))
246246

247247
lines = "-" * (int(maxlength) + 2)
248248

@@ -263,7 +263,7 @@ def dbTables(self, dbTables):
263263
table = table[0]
264264

265265
table = unsafeSQLIdentificatorNaming(table)
266-
blank = " " * (maxlength - len(normalizeUnicode(table) or unicode(table)))
266+
blank = " " * (maxlength - len(normalizeUnicode(table) or getUnicode(table)))
267267
self._write("| %s%s |" % (table, blank))
268268

269269
self._write("+%s+\n" % lines)
@@ -358,7 +358,7 @@ def dbTablesCount(self, dbTables):
358358
for ctables in dbTables.values():
359359
for tables in ctables.values():
360360
for table in tables:
361-
maxlength1 = max(maxlength1, len(normalizeUnicode(table) or unicode(table)))
361+
maxlength1 = max(maxlength1, len(normalizeUnicode(table) or getUnicode(table)))
362362

363363
for db, counts in dbTables.items():
364364
self._write("Database: %s" % unsafeSQLIdentificatorNaming(db) if db else "Current database")
@@ -384,7 +384,7 @@ def dbTablesCount(self, dbTables):
384384
tables.sort(key=lambda _: _.lower() if hasattr(_, "lower") else _)
385385

386386
for table in tables:
387-
blank1 = " " * (maxlength1 - len(normalizeUnicode(table) or unicode(table)))
387+
blank1 = " " * (maxlength1 - len(normalizeUnicode(table) or getUnicode(table)))
388388
blank2 = " " * (maxlength2 - len(str(count)))
389389
self._write("| %s%s | %d%s |" % (table, blank1, count, blank2))
390390

lib/core/option.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1716,7 +1716,7 @@ def _cleanupOptions():
17161716
except re.error:
17171717
conf.csrfToken = re.escape(conf.csrfToken)
17181718
finally:
1719-
class _(unicode):
1719+
class _(six.text_type):
17201720
pass
17211721
conf.csrfToken = _(conf.csrfToken)
17221722
conf.csrfToken._original = original

lib/core/settings.py

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

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

lib/core/target.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
from lib.core.settings import USER_AGENT_ALIASES
7474
from lib.core.settings import XML_RECOGNITION_REGEX
7575
from lib.utils.hashdb import HashDB
76+
from thirdparty import six
7677
from thirdparty.odict import OrderedDict
7778
from thirdparty.six.moves import urllib as _urllib
7879

@@ -409,7 +410,7 @@ def process(match, repl):
409410
message += "Do you want sqlmap to automatically update it in further requests? [y/N] "
410411

411412
if readInput(message, default='N', boolean=True):
412-
class _(unicode):
413+
class _(six.text_type):
413414
pass
414415
conf.csrfToken = _(re.escape(getUnicode(parameter)))
415416
conf.csrfToken._original = getUnicode(parameter)
@@ -712,7 +713,7 @@ def initTargetEnv():
712713
_setDBMS()
713714

714715
if conf.data:
715-
class _(unicode):
716+
class _(six.text_type):
716717
pass
717718

718719
kb.postUrlEncode = True

lib/request/basic.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from lib.core.common import extractErrorMessage
1818
from lib.core.common import extractRegexResult
1919
from lib.core.common import filterNone
20+
from lib.core.common import getBytes
2021
from lib.core.common import getPublicTypeMembers
2122
from lib.core.common import getSafeExString
2223
from lib.core.common import getUnicode
@@ -42,11 +43,11 @@
4243
from lib.core.settings import META_CHARSET_REGEX
4344
from lib.core.settings import PARSE_HEADERS_LIMIT
4445
from lib.core.settings import SELECT_FROM_TABLE_REGEX
45-
from lib.core.settings import UNICODE_ENCODING
4646
from lib.core.settings import VIEWSTATE_REGEX
4747
from lib.parse.headers import headersParser
4848
from lib.parse.html import htmlParser
4949
from lib.utils.htmlentities import htmlEntities
50+
from thirdparty import six
5051
from thirdparty.chardet import detect
5152
from thirdparty.odict import OrderedDict
5253

@@ -219,13 +220,13 @@ def checkCharEncoding(encoding, warn=True):
219220
# Reference: http://www.iana.org/assignments/character-sets
220221
# Reference: http://docs.python.org/library/codecs.html
221222
try:
222-
codecs.lookup(encoding.encode(UNICODE_ENCODING) if isinstance(encoding, unicode) else encoding)
223-
except (LookupError, ValueError):
223+
codecs.lookup(encoding)
224+
except:
224225
encoding = None
225226

226227
if encoding:
227228
try:
228-
unicode(randomStr(), encoding)
229+
six.text_type(getBytes(randomStr()), encoding)
229230
except:
230231
if warn:
231232
warnMsg = "invalid web page charset '%s'" % encoding
@@ -313,7 +314,7 @@ def decodePage(page, contentEncoding, contentType):
313314
kb.pageEncoding = conf.encoding
314315

315316
# can't do for all responses because we need to support binary files too
316-
if not isinstance(page, unicode) and "text/" in contentType:
317+
if isinstance(page, six.binary_type) and "text/" in contentType:
317318
# e.g. &#x9;&#195;&#235;&#224;&#226;&#224;
318319
if "&#" in page:
319320
page = re.sub(r"&#x([0-9a-f]{1,2});", lambda _: (_.group(1) if len(_.group(1)) == 2 else "0%s" % _.group(1)).decode("hex"), page)

lib/request/comparison.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import re
99

1010
from lib.core.common import extractRegexResult
11+
from lib.core.common import getBytes
1112
from lib.core.common import getFilteredPageContent
1213
from lib.core.common import listToStrValue
1314
from lib.core.common import removeDynamicContent
@@ -28,6 +29,7 @@
2829
from lib.core.settings import UPPER_RATIO_BOUND
2930
from lib.core.settings import URI_HTTP_HEADER
3031
from lib.core.threads import getCurrentThreadData
32+
from thirdparty import six
3133

3234
def comparison(page, headers, code=None, getRatioValue=False, pageLength=None):
3335
_ = _adjust(_comparison(page, headers, code, getRatioValue, pageLength), getRatioValue)
@@ -105,10 +107,10 @@ def _comparison(page, headers, code, getRatioValue, pageLength):
105107
else:
106108
# Preventing "Unicode equal comparison failed to convert both arguments to Unicode"
107109
# (e.g. if one page is PDF and the other is HTML)
108-
if isinstance(seqMatcher.a, str) and isinstance(page, unicode):
109-
page = page.encode(kb.pageEncoding or DEFAULT_PAGE_ENCODING, "ignore")
110-
elif isinstance(seqMatcher.a, unicode) and isinstance(page, str):
111-
seqMatcher.a = seqMatcher.a.encode(kb.pageEncoding or DEFAULT_PAGE_ENCODING, "ignore")
110+
if isinstance(seqMatcher.a, six.binary_type) and isinstance(page, six.text_type):
111+
page = getBytes(page, kb.pageEncoding or DEFAULT_PAGE_ENCODING, "ignore")
112+
elif isinstance(seqMatcher.a, six.text_type) and isinstance(page, six.binary_type):
113+
seqMatcher.a = getBytes(seqMatcher.a, kb.pageEncoding or DEFAULT_PAGE_ENCODING, "ignore")
112114

113115
if any(_ is None for _ in (page, seqMatcher.a)):
114116
return None

0 commit comments

Comments
 (0)