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

Skip to content

Commit 1f05e85

Browse files
committed
Some more drei updates
1 parent 291b491 commit 1f05e85

21 files changed

Lines changed: 142 additions & 53 deletions

File tree

extra/safe2bin/safe2bin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
xrange = range
2323
text_type = str
2424
string_types = (str,)
25+
unichr = chr
2526
else:
2627
text_type = unicode
2728
string_types = (basestring,)
@@ -88,7 +89,7 @@ def safechardecode(value, binary=False):
8889
while True:
8990
match = re.search(HEX_ENCODED_CHAR_REGEX, retVal)
9091
if match:
91-
retVal = retVal.replace(match.group("result"), (unichr if isinstance(value, text_type) else chr)(ord(binascii.unhexlify(match.group("result").lstrip("\\x")))))
92+
retVal = retVal.replace(match.group("result"), unichr(ord(binascii.unhexlify(match.group("result").lstrip("\\x")))))
9293
else:
9394
break
9495

extra/shutils/drei.sh

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

66
# Stress test against Python3
77

8-
export SQLMAP_DREI=1
9-
for i in $(find . -iname "*.py" | grep -v __init__); do python3 -c 'import '`echo $i | cut -d '.' -f 2 | cut -d '/' -f 2- | sed 's/\//./g'`''; done
10-
unset SQLMAP_DREI
11-
source `dirname "$0"`"/junk.sh"
8+
# export SQLMAP_DREI=1
9+
# for i in $(find . -iname "*.py" | grep -v __init__); do python3 -c 'import '`echo $i | cut -d '.' -f 2 | cut -d '/' -f 2- | sed 's/\//./g'`''; done
10+
# unset SQLMAP_DREI
11+
# source `dirname "$0"`"/junk.sh"
12+
13+
for i in $(find . -iname "*.py" | grep -v __init__); do pylint --py3k $i; done

lib/core/bigarray.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def pop(self):
9090
self.chunks[-1] = pickle.loads(bz2.decompress(f.read()))
9191
except IOError as ex:
9292
errMsg = "exception occurred while retrieving data "
93-
errMsg += "from a temporary file ('%s')" % ex.message
93+
errMsg += "from a temporary file ('%s')" % ex
9494
raise SqlmapSystemException(errMsg)
9595

9696
return self.chunks[-1].pop()
@@ -112,7 +112,7 @@ def _dump(self, chunk):
112112
return filename
113113
except (OSError, IOError) as ex:
114114
errMsg = "exception occurred while storing data "
115-
errMsg += "to a temporary file ('%s'). Please " % ex.message
115+
errMsg += "to a temporary file ('%s'). Please " % ex
116116
errMsg += "make sure that there is enough disk space left. If problem persists, "
117117
errMsg += "try to set environment variable 'TEMP' to a location "
118118
errMsg += "writeable by the current user"
@@ -129,7 +129,7 @@ def _checkcache(self, index):
129129
self.cache = Cache(index, pickle.loads(bz2.decompress(f.read())), False)
130130
except Exception as ex:
131131
errMsg = "exception occurred while retrieving data "
132-
errMsg += "from a temporary file ('%s')" % ex.message
132+
errMsg += "from a temporary file ('%s')" % ex
133133
raise SqlmapSystemException(errMsg)
134134

135135
def __getstate__(self):

lib/core/common.py

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
See the file 'LICENSE' for copying permission
66
"""
77

8+
import base64
89
import binascii
910
import codecs
1011
import collections
@@ -47,6 +48,8 @@
4748
from extra.cloak.cloak import decloak
4849
from extra.safe2bin.safe2bin import safecharencode
4950
from lib.core.bigarray import BigArray
51+
from lib.core.compat import cmp
52+
from lib.core.compat import round
5053
from lib.core.compat import xrange
5154
from lib.core.convert import base64pickle
5255
from lib.core.convert import base64unpickle
@@ -179,7 +182,9 @@
179182
from thirdparty.six.moves import configparser as _configparser
180183
from thirdparty.six.moves import http_client as _http_client
181184
from thirdparty.six.moves import input as _input
185+
from thirdparty.six.moves import reload_module as _reload_module
182186
from thirdparty.six.moves import urllib as _urllib
187+
from thirdparty.six.moves import zip as _zip
183188
from thirdparty.termcolor.termcolor import colored
184189

185190
class UnicodeRawConfigParser(_configparser.RawConfigParser):
@@ -610,7 +615,7 @@ def paramToDict(place, parameters=None):
610615
if parameter in (conf.base64Parameter or []):
611616
try:
612617
oldValue = value
613-
value = value.decode("base64")
618+
value = decodeBase64(value, binary=False)
614619
parameters = re.sub(r"\b%s\b" % re.escape(oldValue), value, parameters)
615620
except:
616621
errMsg = "parameter '%s' does not contain " % parameter
@@ -2278,7 +2283,7 @@ def getFileItems(filename, commentPrefix='#', unicoded=True, lowercase=False, un
22782283

22792284
try:
22802285
with openFile(filename, 'r', errors="ignore") if unicoded else open(filename, 'r') as f:
2281-
for line in (f.readlines() if unicoded else f.xreadlines()): # xreadlines doesn't return unicode strings when codec.open() is used
2286+
for line in f:
22822287
if commentPrefix:
22832288
if line.find(commentPrefix) != -1:
22842289
line = line[:line.find(commentPrefix)]
@@ -2452,15 +2457,39 @@ def getUnicode(value, encoding=None, noneToNull=False):
24522457
except UnicodeDecodeError:
24532458
return six.text_type(str(value), errors="ignore") # encoding ignored for non-basestring instances
24542459

2455-
def decodeHex(value):
2460+
def decodeHex(value, binary=True):
24562461
"""
2457-
Returns byte representation of provided hexadecimal value
2462+
Returns a decoded representation of provided hexadecimal value
24582463
24592464
>>> decodeHex("313233") == b"123"
24602465
True
2466+
>>> decodeHex("313233", binary=False) == u"123"
2467+
True
2468+
"""
2469+
2470+
retVal = codecs.decode(value, "hex")
2471+
2472+
if not binary:
2473+
retVal = getUnicode(retVal)
2474+
2475+
return retVal
2476+
2477+
def decodeBase64(value, binary=True):
2478+
"""
2479+
Returns a decoded representation of provided Base64 value
2480+
2481+
>>> decodeBase64("MTIz") == b"123"
2482+
True
2483+
>>> decodeBase64("MTIz", binary=False) == u"123"
2484+
True
24612485
"""
24622486

2463-
return bytes.fromhex(getUnicode(value)) if hasattr(bytes, "fromhex") else value.decode("hex")
2487+
retVal = base64.b64decode(value)
2488+
2489+
if not binary:
2490+
retVal = getUnicode(retVal)
2491+
2492+
return retVal
24642493

24652494
def getBytes(value, encoding=UNICODE_ENCODING, errors="strict"):
24662495
"""
@@ -2475,7 +2504,7 @@ def getBytes(value, encoding=UNICODE_ENCODING, errors="strict"):
24752504
if isinstance(value, six.text_type):
24762505
if INVALID_UNICODE_PRIVATE_AREA:
24772506
for char in xrange(0xF0000, 0xF00FF + 1):
2478-
value = value.replace(unichr(char), "%s%02x" % (SAFE_HEX_MARKER, char - 0xF0000))
2507+
value = value.replace(six.unichr(char), "%s%02x" % (SAFE_HEX_MARKER, char - 0xF0000))
24792508

24802509
retVal = value.encode(encoding, errors)
24812510
retVal = re.sub(r"%s([0-9a-f]{2})" % SAFE_HEX_MARKER, lambda _: decodeHex(_.group(1)), retVal)
@@ -2525,7 +2554,13 @@ def longestCommonPrefix(*sequences):
25252554
return sequences[0]
25262555

25272556
def commonFinderOnly(initial, sequence):
2528-
return longestCommonPrefix(*filter(lambda _: _.startswith(initial), sequence))
2557+
"""
2558+
Returns parts of sequence which start with the given initial string
2559+
2560+
>>> commonFinderOnly("abcd", ["abcdefg", "foobar", "abcde"])
2561+
['abcdefg', 'abcde']
2562+
"""
2563+
return longestCommonPrefix([_ for _ in sequence if _.startswith(initial)])
25292564

25302565
def pushValue(value):
25312566
"""
@@ -2811,13 +2846,13 @@ def runningAsAdmin():
28112846
if PLATFORM in ("posix", "mac"):
28122847
_ = os.geteuid()
28132848

2814-
isAdmin = isinstance(_, (int, float, long)) and _ == 0
2849+
isAdmin = isinstance(_, (float, six.integer_types)) and _ == 0
28152850
elif IS_WIN:
28162851
import ctypes
28172852

28182853
_ = ctypes.windll.shell32.IsUserAnAdmin()
28192854

2820-
isAdmin = isinstance(_, (int, float, long)) and _ == 1
2855+
isAdmin = isinstance(_, (float, six.integer_types)) and _ == 1
28212856
else:
28222857
errMsg = "sqlmap is not able to check if you are running it "
28232858
errMsg += "as an administrator account on this platform. "
@@ -3318,6 +3353,8 @@ def unArrayizeValue(value):
33183353
33193354
>>> unArrayizeValue(['1'])
33203355
'1'
3356+
>>> unArrayizeValue(['1', '2'])
3357+
'1'
33213358
"""
33223359

33233360
if isListLike(value):
@@ -3326,8 +3363,8 @@ def unArrayizeValue(value):
33263363
elif len(value) == 1 and not isListLike(value[0]):
33273364
value = value[0]
33283365
else:
3329-
_ = filter(lambda _: _ is not None, (_ for _ in flattenValue(value)))
3330-
value = _[0] if len(_) > 0 else None
3366+
value = [_ for _ in flattenValue(value) if _ is not None]
3367+
value = value[0] if len(value) > 0 else None
33313368

33323369
return value
33333370

@@ -3459,7 +3496,7 @@ def decodeIntToUnicode(value):
34593496
elif Backend.isDbms(DBMS.MSSQL):
34603497
retVal = getUnicode(raw, "UTF-16-BE")
34613498
elif Backend.getIdentifiedDbms() in (DBMS.PGSQL, DBMS.ORACLE):
3462-
retVal = unichr(value)
3499+
retVal = six.unichr(value)
34633500
else:
34643501
retVal = getUnicode(raw, conf.encoding)
34653502
else:
@@ -3600,7 +3637,7 @@ def createGithubIssue(errMsg, excMsg):
36003637
choice = None
36013638

36023639
if choice:
3603-
ex = None
3640+
_excMsg = None
36043641
errMsg = errMsg[errMsg.find("\n"):]
36053642

36063643
req = _urllib.request.Request(url="https://api.github.com/search/issues?q=%s" % _urllib.parse.quote("repo:sqlmapproject/sqlmap Unhandled exception (#%s)" % key))
@@ -3621,12 +3658,13 @@ def createGithubIssue(errMsg, excMsg):
36213658
pass
36223659

36233660
data = {"title": "Unhandled exception (#%s)" % key, "body": "```%s\n```\n```\n%s```" % (errMsg, excMsg)}
3624-
req = _urllib.request.Request(url="https://api.github.com/repos/sqlmapproject/sqlmap/issues", data=json.dumps(data), headers={"Authorization": "token %s" % GITHUB_REPORT_OAUTH_TOKEN.decode("base64")})
3661+
req = _urllib.request.Request(url="https://api.github.com/repos/sqlmapproject/sqlmap/issues", data=json.dumps(data), headers={"Authorization": "token %s" % decodeBase64(GITHUB_REPORT_OAUTH_TOKEN, binary=False)})
36253662

36263663
try:
36273664
content = _urllib.request.urlopen(req).read()
36283665
except Exception as ex:
36293666
content = None
3667+
_excMsg = getSafeExString(ex)
36303668

36313669
issueUrl = re.search(r"https://github.com/sqlmapproject/sqlmap/issues/\d+", content or "")
36323670
if issueUrl:
@@ -3640,8 +3678,8 @@ def createGithubIssue(errMsg, excMsg):
36403678
pass
36413679
else:
36423680
warnMsg = "something went wrong while creating a Github issue"
3643-
if ex:
3644-
warnMsg += " ('%s')" % getSafeExString(ex)
3681+
if _excMsg:
3682+
warnMsg += " ('%s')" % _excMsg
36453683
if "Unauthorized" in warnMsg:
36463684
warnMsg += ". Please update to the latest revision"
36473685
logger.warn(warnMsg)
@@ -4403,7 +4441,7 @@ def checkSystemEncoding():
44034441
warnMsg = "temporary switching to charset 'cp1256'"
44044442
logger.warn(warnMsg)
44054443

4406-
reload(sys)
4444+
_reload_module(sys)
44074445
sys.setdefaultencoding("cp1256")
44084446

44094447
def evaluateCode(code, variables=None):
@@ -4741,7 +4779,7 @@ def splitFields(fields, delimiter=','):
47414779
commas.extend(zeroDepthSearch(fields, ','))
47424780
commas = sorted(commas)
47434781

4744-
return [fields[x + 1:y] for (x, y) in zip(commas, commas[1:])]
4782+
return [fields[x + 1:y] for (x, y) in _zip(commas, commas[1:])]
47454783

47464784
def pollProcess(process, suppress_errors=False):
47474785
"""
@@ -4807,7 +4845,7 @@ def _parseBurpLog(content):
48074845
for match in re.finditer(BURP_XML_HISTORY_REGEX, content, re.I | re.S):
48084846
port, request = match.groups()
48094847
try:
4810-
request = request.decode("base64")
4848+
request = decodeBase64(request, binary=False)
48114849
except binascii.Error:
48124850
continue
48134851
_ = re.search(r"%s:.+" % re.escape(HTTP_HEADER.HOST), request)

lib/core/compat.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
import binascii
9+
import math
910
import os
1011
import random
1112
import uuid
@@ -163,13 +164,44 @@ def whseed(self, a=None):
163164
self.__whseed(x, y, z)
164165

165166
def patchHeaders(headers):
166-
if not hasattr(headers, "headers"):
167+
if headers is not None and not hasattr(headers, "headers"):
167168
headers.headers = ["%s: %s\r\n" % (header, headers[header]) for header in headers]
168169

170+
def cmp(a, b):
171+
"""
172+
>>> cmp("a", "b")
173+
-1
174+
>>> cmp(2, 1)
175+
1
176+
"""
177+
178+
if a < b:
179+
return -1
180+
elif a > b:
181+
return 1
182+
else:
183+
return 0
184+
169185
# Reference: https://github.com/urllib3/urllib3/blob/master/src/urllib3/filepost.py
170186
def choose_boundary():
171187
return uuid.uuid4().hex
172188

189+
# Reference: http://python3porting.com/differences.html
190+
def round(x, d=0):
191+
"""
192+
>>> round(2.0)
193+
2.0
194+
>>> round(2.5)
195+
3.0
196+
"""
197+
198+
p = 10 ** d
199+
if x > 0:
200+
return float(math.floor((x * p) + 0.5))/p
201+
else:
202+
return float(math.ceil((x * p) - 0.5))/p
203+
204+
173205
if sys.version_info >= (3, 0):
174206
xrange = range
175207
else:

lib/core/convert.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def htmlunescape(value):
171171
retVal = retVal.replace(code, value)
172172

173173
try:
174-
retVal = re.sub(r"&#x([^ ;]+);", lambda match: unichr(int(match.group(1), 16)), retVal)
174+
retVal = re.sub(r"&#x([^ ;]+);", lambda match: six.unichr(int(match.group(1), 16)), retVal)
175175
except ValueError:
176176
pass
177177
return retVal

lib/core/option.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
from lib.core.common import setPaths
6060
from lib.core.common import singleTimeWarnMessage
6161
from lib.core.common import urldecode
62+
from lib.core.compat import round
6263
from lib.core.compat import xrange
6364
from lib.core.data import conf
6465
from lib.core.data import kb
@@ -2096,11 +2097,14 @@ def _useWizardInterface():
20962097
choice = readInput(message, default='1')
20972098

20982099
if choice == '2':
2099-
map(lambda _: conf.__setitem__(_, True), WIZARD.INTERMEDIATE)
2100+
options = WIZARD.INTERMEDIATE
21002101
elif choice == '3':
2101-
map(lambda _: conf.__setitem__(_, True), WIZARD.ALL)
2102+
options = WIZARD.ALL
21022103
else:
2103-
map(lambda _: conf.__setitem__(_, True), WIZARD.BASIC)
2104+
options = WIZARD.BASIC
2105+
2106+
for _ in options:
2107+
conf.__setitem__(_, True)
21042108

21052109
logger.debug("muting sqlmap.. it will do the magic for you")
21062110
conf.verbose = 0

lib/core/settings.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
from lib.core.enums import DBMS
1616
from lib.core.enums import DBMS_DIRECTORY_NAME
1717
from lib.core.enums import OS
18+
from thirdparty import six
1819

1920
# sqlmap version (<major>.<minor>.<month>.<monthly commit>)
20-
VERSION = "1.3.5.5"
21+
VERSION = "1.3.5.6"
2122
TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable"
2223
TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34}
2324
VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE)
@@ -839,7 +840,7 @@
839840
def _reversible(ex):
840841
if isinstance(ex, UnicodeDecodeError):
841842
if INVALID_UNICODE_PRIVATE_AREA:
842-
return (u"".join(unichr(int('000f00%2x' % (_ if isinstance(_, int) else ord(_)), 16)) for _ in ex.object[ex.start:ex.end]), ex.end)
843+
return (u"".join(six.unichr(int('000f00%2x' % (_ if isinstance(_, int) else ord(_)), 16)) for _ in ex.object[ex.start:ex.end]), ex.end)
843844
else:
844845
return (u"".join(INVALID_UNICODE_CHAR_FORMAT % (_ if isinstance(_, int) else ord(_)) for _ in ex.object[ex.start:ex.end]), ex.end)
845846

lib/core/testing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from lib.core.common import randomStr
2727
from lib.core.common import readXmlFile
2828
from lib.core.common import shellExec
29+
from lib.core.compat import round
2930
from lib.core.data import conf
3031
from lib.core.data import logger
3132
from lib.core.data import paths

0 commit comments

Comments
 (0)