Skip to content

Commit d465007

Browse files
committed
More drei updates
1 parent 26c8423 commit d465007

File tree

14 files changed

+60
-39
lines changed

14 files changed

+60
-39
lines changed

extra/safe2bin/safe2bin.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
if sys.version_info >= (3, 0):
2222
xrange = range
2323
text_type = str
24+
string_types = (str,)
2425
else:
2526
text_type = unicode
27+
string_types = (basestring,)
2628

2729
# Regex used for recognition of hex encoded characters
2830
HEX_ENCODED_CHAR_REGEX = r"(?P<result>\\x[0-9A-Fa-f]{2})"
@@ -54,7 +56,7 @@ def safecharencode(value):
5456

5557
retVal = value
5658

57-
if isinstance(value, basestring):
59+
if isinstance(value, string_types):
5860
if any(_ not in SAFE_CHARS for _ in value):
5961
retVal = retVal.replace(HEX_ENCODED_PREFIX, HEX_ENCODED_PREFIX_MARKER)
6062
retVal = retVal.replace('\\', SLASH_MARKER)
@@ -78,7 +80,7 @@ def safechardecode(value, binary=False):
7880
"""
7981

8082
retVal = value
81-
if isinstance(value, basestring):
83+
if isinstance(value, string_types):
8284
retVal = retVal.replace('\\\\', SLASH_MARKER)
8385

8486
while True:

lib/controller/handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def setHandler():
7575
(DBMS.INFORMIX, INFORMIX_ALIASES, InformixMap, InformixConn),
7676
]
7777

78-
_ = max(_ if (conf.get("dbms") or Backend.getIdentifiedDbms() or kb.heuristicExtendedDbms or "").lower() in _[1] else None for _ in items)
78+
_ = max(_ if (conf.get("dbms") or Backend.getIdentifiedDbms() or kb.heuristicExtendedDbms or "").lower() in _[1] else "" for _ in items) or None
7979
if _:
8080
items.remove(_)
8181
items.insert(0, _)

lib/core/common.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
from thirdparty.odict import OrderedDict
178178
from thirdparty.six.moves import configparser as _configparser
179179
from thirdparty.six.moves import http_client as _http_client
180+
from thirdparty.six.moves import input as _input
180181
from thirdparty.six.moves import urllib as _urllib
181182
from thirdparty.termcolor.termcolor import colored
182183

@@ -942,8 +943,6 @@ def dataToStdout(data, forceOutput=False, bold=False, content_type=None, status=
942943
Writes text to the stdout (console) stream
943944
"""
944945

945-
message = ""
946-
947946
if not kb.get("threadException"):
948947
if forceOutput or not (getCurrentThreadData().disableStdOut or kb.get("wizardMode")):
949948
multiThreadMode = isMultiThreadMode()
@@ -1082,7 +1081,7 @@ def readInput(message, default=None, checkBatch=True, boolean=False):
10821081
dataToStdout("%s" % message, forceOutput=not kb.wizardMode, bold=True)
10831082
kb.prependFlag = False
10841083

1085-
retVal = raw_input().strip() or default
1084+
retVal = _input().strip() or default
10861085
retVal = getUnicode(retVal, encoding=sys.stdin.encoding) if retVal else retVal
10871086
except:
10881087
try:
@@ -2452,11 +2451,21 @@ def getUnicode(value, encoding=None, noneToNull=False):
24522451
except UnicodeDecodeError:
24532452
return six.text_type(str(value), errors="ignore") # encoding ignored for non-basestring instances
24542453

2454+
def decodeHex(value):
2455+
"""
2456+
Returns byte representation of provided hexadecimal value
2457+
2458+
>>> decodeHex("313233") == b"123"
2459+
True
2460+
"""
2461+
2462+
return bytes.fromhex(value) if hasattr(bytes, "fromhex") else value.decode("hex")
2463+
24552464
def getBytes(value, encoding=UNICODE_ENCODING, errors="strict"):
24562465
"""
24572466
Returns byte representation of provided Unicode value
24582467
2459-
>>> getBytes(getUnicode("foo\x01\x83\xffbar")) == "foo\x01\x83\xffbar"
2468+
>>> getBytes(getUnicode("foo\x01\x83\xffbar")) == b"foo\x01\x83\xffbar"
24602469
True
24612470
"""
24622471

@@ -2468,11 +2477,10 @@ def getBytes(value, encoding=UNICODE_ENCODING, errors="strict"):
24682477
value = value.replace(unichr(char), "%s%02x" % (SAFE_HEX_MARKER, char - 0xF0000))
24692478

24702479
retVal = value.encode(encoding, errors)
2471-
2472-
retVal = re.sub(r"%s([0-9a-f]{2})" % SAFE_HEX_MARKER, lambda _: _.group(1).decode("hex"), retVal)
2480+
retVal = re.sub(r"%s([0-9a-f]{2})" % SAFE_HEX_MARKER, lambda _: decodeHex(_.group(1)), retVal)
24732481
else:
24742482
retVal = value.encode(encoding, errors)
2475-
retVal = re.sub(r"\\x([0-9a-f]{2})", lambda _: _.group(1).decode("hex"), retVal)
2483+
retVal = re.sub(b"\\\\x([0-9a-f]{2})", lambda _: decodeHex(_.group(1)), retVal)
24762484

24772485
return retVal
24782486

@@ -2876,6 +2884,9 @@ def extractRegexResult(regex, content, flags=0):
28762884
retVal = None
28772885

28782886
if regex and content and "?P<result>" in regex:
2887+
if isinstance(content, six.binary_type) and isinstance(regex, six.text_type):
2888+
regex = getBytes(regex)
2889+
28792890
match = re.search(regex, content, flags)
28802891

28812892
if match:
@@ -3812,11 +3823,11 @@ def normalizeUnicode(value):
38123823
38133824
# Reference: http://www.peterbe.com/plog/unicode-to-ascii
38143825
3815-
>>> normalizeUnicode(u'\u0161u\u0107uraj') == b'sucuraj'
3826+
>>> normalizeUnicode(u'\u0161u\u0107uraj') == u'sucuraj'
38163827
True
38173828
"""
38183829

3819-
return unicodedata.normalize("NFKD", value).encode("ascii", "ignore") if isinstance(value, six.text_type) else value
3830+
return getUnicode(unicodedata.normalize("NFKD", value).encode("ascii", "ignore")) if isinstance(value, six.text_type) else value
38203831

38213832
def safeSQLIdentificatorNaming(name, isTable=False):
38223833
"""
@@ -4656,7 +4667,7 @@ def getRequestHeader(request, name):
46564667

46574668
if request and request.headers and name:
46584669
_ = name.upper()
4659-
retVal = max(value if _ == key.upper() else None for key, value in request.header_items())
4670+
retVal = max(value if _ == key.upper() else "" for key, value in request.header_items()) or None
46604671

46614672
return retVal
46624673

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.51"
20+
VERSION = "1.3.5.0"
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/testing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def _thread():
7272
("--technique=B --hex --fresh-queries --threads=4 --sql-query='SELECT 987654321'", ("length of query output", ": '987654321'",)),
7373
("--technique=T --fresh-queries --sql-query='SELECT 1234'", (": '1234'",)),
7474
):
75-
output = shellExec("python %s -u http://%s:%d/?id=1 --batch %s" % (os.path.join(os.path.dirname(__file__), "..", "..", "sqlmap.py"), address, port, options))
75+
output = shellExec("%s %s -u http://%s:%d/?id=1 --batch %s" % (sys.executable, os.path.join(os.path.dirname(__file__), "..", "..", "sqlmap.py"), address, port, options))
7676
output = getUnicode(output)
7777

7878
if not all(check in output for check in checks):

lib/parse/cmdline.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from lib.core.shell import clearHistory
4141
from lib.core.shell import loadHistory
4242
from lib.core.shell import saveHistory
43+
from thirdparty.six.moves import input as _input
4344

4445
def cmdLineParser(argv=None):
4546
"""
@@ -54,7 +55,7 @@ def cmdLineParser(argv=None):
5455
# Reference: https://stackoverflow.com/a/4012683 (Note: previously used "...sys.getfilesystemencoding() or UNICODE_ENCODING")
5556
_ = getUnicode(os.path.basename(argv[0]), encoding=sys.stdin.encoding)
5657

57-
usage = "%s%s [options]" % ("python " if not IS_WIN else "", "\"%s\"" % _ if " " in _ else _)
58+
usage = "%s%s [options]" % ("%s " % os.path.basename(sys.executable) if not IS_WIN else "", "\"%s\"" % _ if " " in _ else _)
5859
parser = OptionParser(usage=usage)
5960

6061
try:
@@ -809,7 +810,7 @@ def _(self, *args):
809810
command = None
810811

811812
try:
812-
command = raw_input("sqlmap-shell> ").strip()
813+
command = _input("sqlmap-shell> ").strip()
813814
command = getUnicode(command, encoding=sys.stdin.encoding)
814815
except (KeyboardInterrupt, EOFError):
815816
print()
@@ -930,7 +931,7 @@ def _(self, *args):
930931
# Protection against Windows dummy double clicking
931932
if IS_WIN:
932933
dataToStdout("\nPress Enter to continue...")
933-
raw_input()
934+
_input()
934935
raise
935936

936937
debugMsg = "parsing command line"

lib/parse/headers.py

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

8-
import itertools
98
import os
109

1110
from lib.core.common import parseXmlFile
1211
from lib.core.data import kb
1312
from lib.core.data import paths
1413
from lib.parse.handler import FingerprintHandler
14+
from thirdparty.six.moves import filter as _filter
1515

1616
def headersParser(headers):
1717
"""
@@ -30,7 +30,7 @@ def headersParser(headers):
3030
"x-powered-by": os.path.join(paths.SQLMAP_XML_BANNER_PATH, "x-powered-by.xml"),
3131
}
3232

33-
for header in itertools.ifilter(lambda _: _ in kb.headerPaths, headers):
33+
for header in _filter(lambda _: _ in kb.headerPaths, headers):
3434
value = headers[header]
3535
xmlfile = kb.headerPaths[header]
3636
handler = FingerprintHandler(value, kb.headersFp)

lib/request/basic.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import zlib
1515

1616
from lib.core.common import Backend
17+
from lib.core.common import decodeHex
1718
from lib.core.common import extractErrorMessage
1819
from lib.core.common import extractRegexResult
1920
from lib.core.common import filterNone
@@ -156,6 +157,9 @@ def checkCharEncoding(encoding, warn=True):
156157
'utf8'
157158
"""
158159

160+
if isinstance(encoding, six.binary_type):
161+
encoding = getUnicode(encoding)
162+
159163
if isListLike(encoding):
160164
encoding = unArrayizeValue(encoding)
161165

@@ -316,16 +320,16 @@ def decodePage(page, contentEncoding, contentType):
316320
# can't do for all responses because we need to support binary files too
317321
if isinstance(page, six.binary_type) and "text/" in contentType:
318322
# e.g. &#x9;&#195;&#235;&#224;&#226;&#224;
319-
if "&#" in page:
320-
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)
321-
page = re.sub(r"&#(\d{1,3});", lambda _: chr(int(_.group(1))) if int(_.group(1)) < 256 else _.group(0), page)
323+
if b"&#" in page:
324+
page = re.sub(b"&#x([0-9a-f]{1,2});", lambda _: decodeHex(_.group(1) if len(_.group(1)) == 2 else "0%s" % _.group(1)), page)
325+
page = re.sub(b"&#(\d{1,3});", lambda _: chr(int(_.group(1))) if int(_.group(1)) < 256 else _.group(0), page)
322326

323327
# e.g. %20%28%29
324-
if "%" in page:
325-
page = re.sub(r"%([0-9a-fA-F]{2})", lambda _: _.group(1).decode("hex"), page)
328+
if b"%" in page:
329+
page = re.sub(b"%([0-9a-fA-F]{2})", lambda _: decodeHex(_.group(1)), page)
326330

327331
# e.g. &amp;
328-
page = re.sub(r"&([^;]+);", lambda _: chr(htmlEntities[_.group(1)]) if htmlEntities.get(_.group(1), 256) < 256 else _.group(0), page)
332+
page = re.sub(b"&([^;]+);", lambda _: chr(htmlEntities[_.group(1)]) if htmlEntities.get(_.group(1), 256) < 256 else _.group(0), page)
329333

330334
kb.pageEncoding = kb.pageEncoding or checkCharEncoding(getHeuristicCharEncoding(page))
331335

lib/request/connect.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def _retryProxy(**kwargs):
193193

194194
@staticmethod
195195
def _connReadProxy(conn):
196-
retVal = ""
196+
retVal = b""
197197

198198
if not kb.dnsMode and conn:
199199
headers = conn.info()
@@ -413,13 +413,12 @@ def getPage(**kwargs):
413413
if auxHeaders:
414414
headers = forgeHeaders(auxHeaders, headers)
415415

416-
for key, value in headers.items():
416+
for key, value in list(headers.items()):
417417
del headers[key]
418418
for char in (r"\r", r"\n"):
419419
value = re.sub(r"(%s)([^ \t])" % char, r"\g<1>\t\g<2>", value)
420420
headers[getBytes(key)] = getBytes(value.strip("\r\n"))
421421

422-
url = getBytes(url)
423422
post = getBytes(post)
424423

425424
if websocket_:
@@ -797,7 +796,7 @@ class _(dict):
797796
responseMsg += "[#%d] (%s %s):\r\n" % (threadData.lastRequestUID, code, status)
798797

799798
if responseHeaders:
800-
logHeaders = getUnicode("".join(responseHeaders.headers).strip())
799+
logHeaders = getUnicode("".join(responseHeaders.headers).strip() if six.PY2 else responseHeaders.__bytes__())
801800

802801
logHTTPTraffic(requestMsg, "%s%s\r\n\r\n%s" % (responseMsg, logHeaders, (page or "")[:MAX_CONNECTION_CHUNK_SIZE]), start, time.time())
803802

@@ -851,7 +850,7 @@ def queryPage(value=None, place=None, content=False, getRatioValue=False, silent
851850

852851
if conf.httpHeaders:
853852
headers = OrderedDict(conf.httpHeaders)
854-
contentType = max(headers[_] if _.upper() == HTTP_HEADER.CONTENT_TYPE.upper() else None for _ in headers)
853+
contentType = max(headers[_] if _.upper() == HTTP_HEADER.CONTENT_TYPE.upper() else "" for _ in headers) or None
855854

856855
if (kb.postHint or conf.skipUrlEncode) and postUrlEncode:
857856
postUrlEncode = False
@@ -1266,7 +1265,7 @@ def _randomizeParameter(paramString, randomParameter):
12661265
warnMsg += "10 or more)"
12671266
logger.critical(warnMsg)
12681267

1269-
if conf.safeFreq > 0:
1268+
if (conf.safeFreq or 0) > 0:
12701269
kb.queryCounter += 1
12711270
if kb.queryCounter % conf.safeFreq == 0:
12721271
if conf.safeUrl:

lib/takeover/abstraction.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from lib.takeover.udf import UDF
2929
from lib.takeover.web import Web
3030
from lib.takeover.xp_cmdshell import XP_cmdshell
31+
from thirdparty.six.moves import input as _input
3132

3233
class Abstraction(Web, UDF, XP_cmdshell):
3334
"""
@@ -139,7 +140,7 @@ def shell(self):
139140
command = None
140141

141142
try:
142-
command = raw_input("os-shell> ")
143+
command = _input("os-shell> ")
143144
command = getUnicode(command, encoding=sys.stdin.encoding)
144145
except KeyboardInterrupt:
145146
print()

0 commit comments

Comments
 (0)