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

Skip to content

Commit dbd93e2

Browse files
committed
Minor refactoring (drei stuff)
1 parent 89d13aa commit dbd93e2

14 files changed

Lines changed: 60 additions & 35 deletions

File tree

extra/dbgtool/dbgtool.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import os
1313
import sys
14-
import struct
1514

1615
from optparse import OptionError
1716
from optparse import OptionParser

lib/controller/checks.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from lib.core.common import Backend
2222
from lib.core.common import extractRegexResult
2323
from lib.core.common import extractTextTagContent
24+
from lib.core.common import filterNone
2425
from lib.core.common import findDynamicContent
2526
from lib.core.common import Format
2627
from lib.core.common import getFilteredPageContent
@@ -581,7 +582,7 @@ def genCmpPayload():
581582
else:
582583
errorSet = set()
583584

584-
candidates = filter(None, (_.strip() if _.strip() in trueRawResponse and _.strip() not in falseRawResponse else None for _ in (trueSet - falseSet - errorSet)))
585+
candidates = filterNone(_.strip() if _.strip() in trueRawResponse and _.strip() not in falseRawResponse else None for _ in (trueSet - falseSet - errorSet))
585586

586587
if candidates:
587588
candidates = sorted(candidates, key=lambda _: len(_))
@@ -595,7 +596,7 @@ def genCmpPayload():
595596
logger.info(infoMsg)
596597

597598
if not any((conf.string, conf.notString)):
598-
candidates = filter(None, (_.strip() if _.strip() in falseRawResponse and _.strip() not in trueRawResponse else None for _ in (falseSet - trueSet)))
599+
candidates = filterNone(_.strip() if _.strip() in falseRawResponse and _.strip() not in trueRawResponse else None for _ in (falseSet - trueSet))
599600

600601
if candidates:
601602
candidates = sorted(candidates, key=lambda _: len(_))

lib/core/agent.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from lib.core.common import Backend
1111
from lib.core.common import extractRegexResult
12+
from lib.core.common import filterNone
1213
from lib.core.common import getSQLSnippet
1314
from lib.core.common import getUnicode
1415
from lib.core.common import isDBMSVersionAtLeast
@@ -106,7 +107,7 @@ def payload(self, place=None, parameter=None, value=None, newValue=None, where=N
106107
if place == PLACE.URI:
107108
origValue = origValue.split(kb.customInjectionMark)[0]
108109
else:
109-
origValue = filter(None, (re.search(_, origValue.split(BOUNDED_INJECTION_MARKER)[0]) for _ in (r"\w+\Z", r"[^\"'><]+\Z", r"[^ ]+\Z")))[0].group(0)
110+
origValue = filterNone(re.search(_, origValue.split(BOUNDED_INJECTION_MARKER)[0]) for _ in (r"\w+\Z", r"[^\"'><]+\Z", r"[^ ]+\Z"))[0].group(0)
110111
origValue = origValue[origValue.rfind('/') + 1:]
111112
for char in ('?', '=', ':', ',', '&'):
112113
if char in origValue:

lib/core/common.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import binascii
99
import codecs
10+
import collections
1011
import contextlib
1112
import copy
1213
import distutils
@@ -228,7 +229,7 @@ def getDbms(versions=None):
228229
if versions is None and Backend.getVersionList():
229230
versions = Backend.getVersionList()
230231

231-
return Backend.getDbms() if versions is None else "%s %s" % (Backend.getDbms(), " and ".join(filter(None, versions)))
232+
return Backend.getDbms() if versions is None else "%s %s" % (Backend.getDbms(), " and ".join(filterNone(versions)))
232233

233234
@staticmethod
234235
def getErrorParsedDBMSes():
@@ -501,15 +502,15 @@ def getIdentifiedDbms():
501502

502503
@staticmethod
503504
def getVersion():
504-
versions = filter(None, flattenValue(kb.dbmsVersion))
505+
versions = filterNone(flattenValue(kb.dbmsVersion))
505506
if not isNoneValue(versions):
506507
return versions[0]
507508
else:
508509
return None
509510

510511
@staticmethod
511512
def getVersionList():
512-
versions = filter(None, flattenValue(kb.dbmsVersion))
513+
versions = filterNone(flattenValue(kb.dbmsVersion))
513514
if not isNoneValue(versions):
514515
return versions
515516
else:
@@ -787,7 +788,7 @@ def getManualDirectories():
787788
else:
788789
targets.add('.'.join(_[:-1]))
789790

790-
targets = filter(None, targets)
791+
targets = filterNone(targets)
791792

792793
for prefix in BRUTE_DOC_ROOT_PREFIXES.get(Backend.getOs(), DEFAULT_DOC_ROOTS[OS.LINUX]):
793794
if BRUTE_DOC_ROOT_TARGET_MARK in prefix and re.match(IP_ADDRESS_REGEX, conf.hostname):
@@ -1473,7 +1474,7 @@ def parseTargetUrl():
14731474
errMsg += "in the hostname part"
14741475
raise SqlmapGenericException(errMsg)
14751476

1476-
hostnamePort = urlSplit.netloc.split(":") if not re.search(r"\[.+\]", urlSplit.netloc) else filter(None, (re.search(r"\[.+\]", urlSplit.netloc).group(0), re.search(r"\](:(?P<port>\d+))?", urlSplit.netloc).group("port")))
1477+
hostnamePort = urlSplit.netloc.split(":") if not re.search(r"\[.+\]", urlSplit.netloc) else filterNone((re.search(r"\[.+\]", urlSplit.netloc).group(0), re.search(r"\](:(?P<port>\d+))?", urlSplit.netloc).group("port")))
14771478

14781479
conf.scheme = (urlSplit.scheme.strip().lower() or "http") if not conf.forceSSL else "https"
14791480
conf.path = urlSplit.path.strip()
@@ -2389,13 +2390,13 @@ def getUnicode(value, encoding=None, noneToNull=False):
23892390
return value
23902391
elif isinstance(value, six.binary_type):
23912392
# Heuristics (if encoding not explicitly specified)
2392-
candidates = filter(None, (encoding, kb.get("pageEncoding") if kb.get("originalPage") else None, conf.get("encoding"), UNICODE_ENCODING, sys.getfilesystemencoding()))
2393+
candidates = filterNone((encoding, kb.get("pageEncoding") if kb.get("originalPage") else None, conf.get("encoding"), UNICODE_ENCODING, sys.getfilesystemencoding()))
23932394
if all(_ in value for _ in ('<', '>')):
23942395
pass
23952396
elif any(_ in value for _ in (":\\", '/', '.')) and '\n' not in value:
2396-
candidates = filter(None, (encoding, sys.getfilesystemencoding(), kb.get("pageEncoding") if kb.get("originalPage") else None, UNICODE_ENCODING, conf.get("encoding")))
2397+
candidates = filterNone((encoding, sys.getfilesystemencoding(), kb.get("pageEncoding") if kb.get("originalPage") else None, UNICODE_ENCODING, conf.get("encoding")))
23972398
elif conf.get("encoding") and '\n' not in value:
2398-
candidates = filter(None, (encoding, conf.get("encoding"), kb.get("pageEncoding") if kb.get("originalPage") else None, sys.getfilesystemencoding(), UNICODE_ENCODING))
2399+
candidates = filterNone((encoding, conf.get("encoding"), kb.get("pageEncoding") if kb.get("originalPage") else None, sys.getfilesystemencoding(), UNICODE_ENCODING))
23992400

24002401
for candidate in candidates:
24012402
try:
@@ -2837,7 +2838,7 @@ def extractTextTagContent(page):
28372838
except MemoryError:
28382839
page = page.replace(REFLECTED_VALUE_MARKER, "")
28392840

2840-
return filter(None, (_.group("result").strip() for _ in re.finditer(TEXT_TAG_REGEX, page)))
2841+
return filterNone(_.group("result").strip() for _ in re.finditer(TEXT_TAG_REGEX, page))
28412842

28422843
def trimAlphaNum(value):
28432844
"""
@@ -2996,6 +2997,21 @@ def filterControlChars(value, replacement=' '):
29962997

29972998
return filterStringValue(value, PRINTABLE_CHAR_REGEX, replacement)
29982999

3000+
def filterNone(values):
3001+
"""
3002+
Emulates filterNone([...]) functionality
3003+
3004+
>>> filterNone([1, 2, "", None, 3])
3005+
[1, 2, 3]
3006+
"""
3007+
3008+
retVal = values
3009+
3010+
if isinstance(values, collections.Iterable):
3011+
retVal = [_ for _ in values if _]
3012+
3013+
return retVal
3014+
29993015
def isDBMSVersionAtLeast(version):
30003016
"""
30013017
Checks if the recognized DBMS version is at least the version
@@ -3537,7 +3553,7 @@ def maskSensitiveData(msg):
35373553

35383554
retVal = getUnicode(msg)
35393555

3540-
for item in filter(None, (conf.get(_) for _ in SENSITIVE_OPTIONS)):
3556+
for item in filterNone(conf.get(_) for _ in SENSITIVE_OPTIONS):
35413557
regex = SENSITIVE_DATA_REGEX % re.sub(r"(\W)", r"\\\1", getUnicode(item))
35423558
while extractRegexResult(regex, retVal):
35433559
value = extractRegexResult(regex, retVal)
@@ -3640,14 +3656,14 @@ def _(value):
36403656
regex = _(filterStringValue(payload, r"[A-Za-z0-9]", REFLECTED_REPLACEMENT_REGEX.encode("string_escape")))
36413657

36423658
if regex != payload:
3643-
if all(part.lower() in content.lower() for part in filter(None, regex.split(REFLECTED_REPLACEMENT_REGEX))[1:]): # fast optimization check
3659+
if all(part.lower() in content.lower() for part in filterNone(regex.split(REFLECTED_REPLACEMENT_REGEX))[1:]): # fast optimization check
36443660
parts = regex.split(REFLECTED_REPLACEMENT_REGEX)
36453661
retVal = content.replace(payload, REFLECTED_VALUE_MARKER) # dummy approach
36463662

36473663
if len(parts) > REFLECTED_MAX_REGEX_PARTS: # preventing CPU hogs
36483664
regex = _("%s%s%s" % (REFLECTED_REPLACEMENT_REGEX.join(parts[:REFLECTED_MAX_REGEX_PARTS // 2]), REFLECTED_REPLACEMENT_REGEX, REFLECTED_REPLACEMENT_REGEX.join(parts[-REFLECTED_MAX_REGEX_PARTS // 2:])))
36493665

3650-
parts = filter(None, regex.split(REFLECTED_REPLACEMENT_REGEX))
3666+
parts = filterNone(regex.split(REFLECTED_REPLACEMENT_REGEX))
36513667

36523668
if regex.startswith(REFLECTED_REPLACEMENT_REGEX):
36533669
regex = r"%s%s" % (REFLECTED_BORDER_REGEX, regex[len(REFLECTED_REPLACEMENT_REGEX):])
@@ -4482,7 +4498,7 @@ def resetCookieJar(cookieJar):
44824498
logger.info(infoMsg)
44834499

44844500
content = readCachedFileContent(conf.loadCookies)
4485-
lines = filter(None, (line.strip() for line in content.split("\n") if not line.startswith('#')))
4501+
lines = filterNone(line.strip() for line in content.split("\n") if not line.startswith('#'))
44864502
handle, filename = tempfile.mkstemp(prefix=MKSTEMP_PREFIX.COOKIE_JAR)
44874503
os.close(handle)
44884504

lib/core/option.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from lib.core.common import getPublicTypeMembers
3434
from lib.core.common import getSafeExString
3535
from lib.core.common import getUnicode
36+
from lib.core.common import filterNone
3637
from lib.core.common import findLocalPort
3738
from lib.core.common import findPageForms
3839
from lib.core.common import getConsoleWidth
@@ -784,7 +785,7 @@ def _setTamperingFunctions():
784785
if name == "tamper" and inspect.getargspec(function).args and inspect.getargspec(function).keywords == "kwargs":
785786
found = True
786787
kb.tamperFunctions.append(function)
787-
function.func_name = module.__name__
788+
function.__name__ = module.__name__
788789

789790
if check_priority and priority > last_priority:
790791
message = "it appears that you might have mixed "
@@ -880,7 +881,7 @@ def _setPreprocessFunctions():
880881
found = True
881882

882883
kb.preprocessFunctions.append(function)
883-
function.func_name = module.__name__
884+
function.__name__ = module.__name__
884885

885886
break
886887

@@ -1113,7 +1114,7 @@ def _setHTTPHandlers():
11131114
debugMsg = "creating HTTP requests opener object"
11141115
logger.debug(debugMsg)
11151116

1116-
handlers = filter(None, [multipartPostHandler, proxyHandler if proxyHandler.proxies else None, authHandler, redirectHandler, rangeHandler, chunkedHandler if conf.chunked else None, httpsHandler])
1117+
handlers = filterNone([multipartPostHandler, proxyHandler if proxyHandler.proxies else None, authHandler, redirectHandler, rangeHandler, chunkedHandler if conf.chunked else None, httpsHandler])
11171118

11181119
if not conf.dropSetCookie:
11191120
if not conf.loadCookies:

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.3.77"
20+
VERSION = "1.3.3.78"
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/request/basic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from lib.core.common import Backend
1717
from lib.core.common import extractErrorMessage
1818
from lib.core.common import extractRegexResult
19+
from lib.core.common import filterNone
1920
from lib.core.common import getPublicTypeMembers
2021
from lib.core.common import getSafeExString
2122
from lib.core.common import getUnicode
@@ -100,7 +101,7 @@ def title(self):
100101

101102
if ("%s=" % getUnicode(cookie.name)) in getUnicode(headers[HTTP_HEADER.COOKIE]):
102103
if conf.loadCookies:
103-
conf.httpHeaders = filter(None, ((item if item[0] != HTTP_HEADER.COOKIE else None) for item in conf.httpHeaders))
104+
conf.httpHeaders = filterNone((item if item[0] != HTTP_HEADER.COOKIE else None) for item in conf.httpHeaders)
104105
elif kb.mergeCookies is None:
105106
message = "you provided a HTTP %s header value. " % HTTP_HEADER.COOKIE
106107
message += "The target URL provided its own cookies within "

lib/request/connect.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class WebSocketException(Exception):
3232
from lib.core.common import escapeJsonValue
3333
from lib.core.common import evaluateCode
3434
from lib.core.common import extractRegexResult
35+
from lib.core.common import filterNone
3536
from lib.core.common import findMultipartPostBoundary
3637
from lib.core.common import getCurrentThreadData
3738
from lib.core.common import getHeader
@@ -600,7 +601,7 @@ class _(dict):
600601
except:
601602
pass
602603
finally:
603-
page = page if isinstance(page, unicode) else getUnicode(page)
604+
page = getUnicode(page)
604605

605606
code = ex.code
606607
status = getSafeExString(ex)
@@ -758,7 +759,7 @@ class _(dict):
758759
page, responseHeaders, code = function(page, responseHeaders, code)
759760
except Exception as ex:
760761
errMsg = "error occurred while running preprocess "
761-
errMsg += "function '%s' ('%s')" % (function.func_name, getSafeExString(ex))
762+
errMsg += "function '%s' ('%s')" % (function.__name__, getSafeExString(ex))
762763
raise SqlmapGenericException(errMsg)
763764

764765
threadData.lastPage = page
@@ -857,11 +858,11 @@ def queryPage(value=None, place=None, content=False, getRatioValue=False, silent
857858
payload = function(payload=payload, headers=auxHeaders, delimiter=delimiter, hints=hints)
858859
except Exception as ex:
859860
errMsg = "error occurred while running tamper "
860-
errMsg += "function '%s' ('%s')" % (function.func_name, getSafeExString(ex))
861+
errMsg += "function '%s' ('%s')" % (function.__name__, getSafeExString(ex))
861862
raise SqlmapGenericException(errMsg)
862863

863864
if not isinstance(payload, six.string_types):
864-
errMsg = "tamper function '%s' returns " % function.func_name
865+
errMsg = "tamper function '%s' returns " % function.__name__
865866
errMsg += "invalid payload type ('%s')" % type(payload)
866867
raise SqlmapValueException(errMsg)
867868

@@ -1095,7 +1096,7 @@ def _randomizeParameter(paramString, randomParameter):
10951096
else:
10961097
query = None
10971098

1098-
for item in filter(None, (get, post if not kb.postHint else None, query)):
1099+
for item in filterNone((get, post if not kb.postHint else None, query)):
10991100
for part in item.split(delimiter):
11001101
if '=' in part:
11011102
name, value = part.split('=', 1)

lib/request/httpshandler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import re
1010
import socket
1111

12+
from lib.core.common import filterNone
1213
from lib.core.common import getSafeExString
1314
from lib.core.data import conf
1415
from lib.core.data import kb
@@ -25,7 +26,7 @@
2526
except ImportError:
2627
pass
2728

28-
_protocols = filter(None, (getattr(ssl, _, None) for _ in ("PROTOCOL_TLSv1_2", "PROTOCOL_TLSv1_1", "PROTOCOL_TLSv1", "PROTOCOL_SSLv3", "PROTOCOL_SSLv23", "PROTOCOL_SSLv2")))
29+
_protocols = filterNone(getattr(ssl, _, None) for _ in ("PROTOCOL_TLSv1_2", "PROTOCOL_TLSv1_1", "PROTOCOL_TLSv1", "PROTOCOL_SSLv3", "PROTOCOL_SSLv23", "PROTOCOL_SSLv2"))
2930

3031
class HTTPSConnection(_http_client.HTTPSConnection):
3132
"""

lib/request/inject.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from lib.core.common import cleanQuery
1818
from lib.core.common import expandAsteriskForColumns
1919
from lib.core.common import extractExpectedValue
20+
from lib.core.common import filterNone
2021
from lib.core.common import getPublicTypeMembers
2122
from lib.core.common import getTechniqueData
2223
from lib.core.common import hashDBRetrieve
@@ -431,7 +432,7 @@ def getValue(expression, blind=True, union=True, error=True, time=True, fromUser
431432
found = (value is not None) or (value is None and expectingNone) or count >= MAX_TECHNIQUES_PER_VALUE
432433

433434
if found and conf.dnsDomain:
434-
_ = "".join(filter(None, (key if isTechniqueAvailable(value) else None for key, value in {'E': PAYLOAD.TECHNIQUE.ERROR, 'Q': PAYLOAD.TECHNIQUE.QUERY, 'U': PAYLOAD.TECHNIQUE.UNION}.items())))
435+
_ = "".join(filterNone(key if isTechniqueAvailable(value) else None for key, value in {'E': PAYLOAD.TECHNIQUE.ERROR, 'Q': PAYLOAD.TECHNIQUE.QUERY, 'U': PAYLOAD.TECHNIQUE.UNION}.items()))
435436
warnMsg = "option '--dns-domain' will be ignored "
436437
warnMsg += "as faster techniques are usable "
437438
warnMsg += "(%s) " % _

0 commit comments

Comments
 (0)