1111import contextlib
1212import copy
1313import distutils
14+ import functools
1415import getpass
1516import hashlib
1617import inspect
@@ -1849,7 +1850,7 @@ def safeFilepathEncode(filepath):
18491850
18501851 retVal = filepath
18511852
1852- if filepath and isinstance (filepath , six .text_type ):
1853+ if filepath and six . PY2 and isinstance (filepath , six .text_type ):
18531854 retVal = filepath .encode (sys .getfilesystemencoding () or UNICODE_ENCODING )
18541855
18551856 return retVal
@@ -1929,8 +1930,8 @@ def getFilteredPageContent(page, onlyText=True, split=" "):
19291930 Returns filtered page content without script, style and/or comments
19301931 or all HTML tags
19311932
1932- >>> getFilteredPageContent(u'<html><title>foobar</title><body>test</body></html>')
1933- u'foobar test'
1933+ >>> getFilteredPageContent(u'<html><title>foobar</title><body>test</body></html>') == "foobar test"
1934+ True
19341935 """
19351936
19361937 retVal = page
@@ -1947,8 +1948,8 @@ def getPageWordSet(page):
19471948 """
19481949 Returns word set used in page content
19491950
1950- >>> sorted(getPageWordSet(u'<html><title>foobar</title><body>test</body></html>'))
1951- [u'foobar', u'test']
1951+ >>> sorted(getPageWordSet(u'<html><title>foobar</title><body>test</body></html>')) == [u'foobar', u'test']
1952+ True
19521953 """
19531954
19541955 retVal = set ()
@@ -2459,13 +2460,13 @@ def decodeHex(value):
24592460 True
24602461 """
24612462
2462- return bytes .fromhex (value ) if hasattr (bytes , "fromhex" ) else value .decode ("hex" )
2463+ return bytes .fromhex (getUnicode ( value ) ) if hasattr (bytes , "fromhex" ) else value .decode ("hex" )
24632464
24642465def getBytes (value , encoding = UNICODE_ENCODING , errors = "strict" ):
24652466 """
24662467 Returns byte representation of provided Unicode value
24672468
2468- >>> getBytes(getUnicode("foo\x01 \x83 \xff bar")) == b"foo\x01 \x83 \xff bar"
2469+ >>> getBytes(getUnicode(b "foo\\ x01\\ x83\\ xffbar")) == b"foo\\ x01\\ x83\ \ xffbar"
24692470 True
24702471 """
24712472
@@ -2488,9 +2489,9 @@ def getOrds(value):
24882489 """
24892490 Returns ORD(...) representation of provided string value
24902491
2491- >>> getOrds(u'fo\xf6 bar')
2492+ >>> getOrds(u'fo\\ xf6bar')
24922493 [102, 111, 246, 98, 97, 114]
2493- >>> getOrds(b"fo\xc3 \xb6 bar")
2494+ >>> getOrds(b"fo\\ xc3\ \ xb6bar")
24942495 [102, 111, 195, 182, 98, 97, 114]
24952496 """
24962497
@@ -2642,8 +2643,8 @@ def extractErrorMessage(page):
26422643 """
26432644 Returns reported error message from page if it founds one
26442645
2645- >>> extractErrorMessage(u'<html><title>Test</title>\\ n<b>Warning</b>: oci_parse() [function.oci-parse]: ORA-01756: quoted string not properly terminated<br><p>Only a test page</p></html>')
2646- u'oci_parse() [function.oci-parse]: ORA-01756: quoted string not properly terminated'
2646+ >>> extractErrorMessage(u'<html><title>Test</title>\\ n<b>Warning</b>: oci_parse() [function.oci-parse]: ORA-01756: quoted string not properly terminated<br><p>Only a test page</p></html>') == u'oci_parse() [function.oci-parse]: ORA-01756: quoted string not properly terminated'
2647+ True
26472648 """
26482649
26492650 retVal = None
@@ -2716,10 +2717,10 @@ def urldecode(value, encoding=None, unsafe="%%&=;+%s" % CUSTOM_INJECTION_MARK_CH
27162717 """
27172718 URL decodes given value
27182719
2719- >>> urldecode('AND%201%3E%282%2B3%29%23', convall=True)
2720- u'AND 1>(2+3)#'
2721- >>> urldecode('AND%201%3E%282%2B3%29%23', convall=False)
2722- u'AND 1>(2%2B3)#'
2720+ >>> urldecode('AND%201%3E%282%2B3%29%23', convall=True) == 'AND 1>(2+3)#'
2721+ True
2722+ >>> urldecode('AND%201%3E%282%2B3%29%23', convall=False) == 'AND 1>(2%2B3)#'
2723+ True
27232724 """
27242725
27252726 result = value
@@ -2738,7 +2739,7 @@ def urldecode(value, encoding=None, unsafe="%%&=;+%s" % CUSTOM_INJECTION_MARK_CH
27382739 charset = set (string .printable ) - set (unsafe )
27392740
27402741 def _ (match ):
2741- char = chr ( ord (match .group (1 ). decode ( "hex" )))
2742+ char = getUnicode ( decodeHex (match .group (1 )))
27422743 return char if char in charset else match .group (0 )
27432744
27442745 if spaceplus :
@@ -3020,13 +3021,15 @@ def findDynamicContent(firstPage, secondPage):
30203021 prefix = prefix [- DYNAMICITY_BOUNDARY_LENGTH :]
30213022 suffix = suffix [:DYNAMICITY_BOUNDARY_LENGTH ]
30223023
3023- infix = max (re .search (r"(?s)%s(.+)%s" % (re .escape (prefix ), re .escape (suffix )), _ ) for _ in (firstPage , secondPage )).group (1 )
3024-
3025- if infix [0 ].isalnum ():
3026- prefix = trimAlphaNum (prefix )
3027-
3028- if infix [- 1 ].isalnum ():
3029- suffix = trimAlphaNum (suffix )
3024+ for _ in (firstPage , secondPage ):
3025+ match = re .search (r"(?s)%s(.+)%s" % (re .escape (prefix ), re .escape (suffix )), _ )
3026+ if match :
3027+ infix = match .group (1 )
3028+ if infix [0 ].isalnum ():
3029+ prefix = trimAlphaNum (prefix )
3030+ if infix [- 1 ].isalnum ():
3031+ suffix = trimAlphaNum (suffix )
3032+ break
30303033
30313034 kb .dynamicMarkings .append ((prefix if prefix else None , suffix if suffix else None ))
30323035
@@ -3557,7 +3560,7 @@ def getLatestRevision():
35573560 req = _urllib .request .Request (url = "https://raw.githubusercontent.com/sqlmapproject/sqlmap/master/lib/core/settings.py" )
35583561
35593562 try :
3560- content = _urllib .request .urlopen (req ).read ()
3563+ content = getUnicode ( _urllib .request .urlopen (req ).read () )
35613564 retVal = extractRegexResult (r"VERSION\s*=\s*[\"'](?P<result>[\d.]+)" , content )
35623565 except :
35633566 pass
@@ -4423,12 +4426,8 @@ def serializeObject(object_):
44234426 """
44244427 Serializes given object
44254428
4426- >>> serializeObject([1, 2, 3, ('a', 'b')])
4427- 'gAJdcQEoSwFLAksDVQFhVQFihnECZS4='
4428- >>> serializeObject(None)
4429- 'gAJOLg=='
4430- >>> serializeObject('foobar')
4431- 'gAJVBmZvb2JhcnEBLg=='
4429+ >>> type(serializeObject([1, 2, 3, ('a', 'b')])) == six.binary_type
4430+ True
44324431 """
44334432
44344433 return base64pickle (object_ )
@@ -4668,7 +4667,10 @@ def prioritySortColumns(columns):
46684667 def _ (column ):
46694668 return column and "id" in column .lower ()
46704669
4671- return sorted (sorted (columns , key = len ), lambda x , y : - 1 if _ (x ) and not _ (y ) else 1 if not _ (x ) and _ (y ) else 0 )
4670+ if six .PY2 :
4671+ return sorted (sorted (columns , key = len ), lambda x , y : - 1 if _ (x ) and not _ (y ) else 1 if not _ (x ) and _ (y ) else 0 )
4672+ else :
4673+ return sorted (sorted (columns , key = len ), key = functools .cmp_to_key (lambda x , y : - 1 if _ (x ) and not _ (y ) else 1 if not _ (x ) and _ (y ) else 0 ))
46724674
46734675def getRequestHeader (request , name ):
46744676 """
@@ -4975,25 +4977,25 @@ def safeVariableNaming(value):
49754977 """
49764978 Returns escaped safe-representation of a given variable name that can be used in Python evaluated code
49774979
4978- >>> safeVariableNaming("class.id")
4979- 'EVAL_636c6173732e6964'
4980+ >>> safeVariableNaming("class.id") == "EVAL_636c6173732e6964"
4981+ True
49804982 """
49814983
49824984 if value in keyword .kwlist or re .search (r"\A[^a-zA-Z]|[^\w]" , value ):
4983- value = "%s%s" % (EVALCODE_ENCODED_PREFIX , value . encode ( UNICODE_ENCODING ). encode ( "hex" ))
4985+ value = "%s%s" % (EVALCODE_ENCODED_PREFIX , getUnicode ( binascii . hexlify ( getBytes ( value )) ))
49844986
49854987 return value
49864988
49874989def unsafeVariableNaming (value ):
49884990 """
49894991 Returns unescaped safe-representation of a given variable name
49904992
4991- >>> unsafeVariableNaming("EVAL_636c6173732e6964")
4992- u'class.id'
4993+ >>> unsafeVariableNaming("EVAL_636c6173732e6964") == "class.id"
4994+ True
49934995 """
49944996
49954997 if value .startswith (EVALCODE_ENCODED_PREFIX ):
4996- value = value [len (EVALCODE_ENCODED_PREFIX ):]. decode ( "hex" ). decode ( UNICODE_ENCODING )
4998+ value = getUnicode ( decodeHex ( value [len (EVALCODE_ENCODED_PREFIX ):]) )
49974999
49985000 return value
49995001
0 commit comments