getting rid of obsolete getCompiledRegex (in newer versions of Python regexes are already cached)

This commit is contained in:
Miroslav Stampar 2012-04-03 14:34:15 +00:00
parent 556b349be3
commit b0787f193c
8 changed files with 31 additions and 55 deletions

View File

@ -12,7 +12,6 @@ import re
from xml.etree import ElementTree as ET from xml.etree import ElementTree as ET
from lib.core.common import Backend from lib.core.common import Backend
from lib.core.common import getCompiledRegex
from lib.core.common import isDBMSVersionAtLeast from lib.core.common import isDBMSVersionAtLeast
from lib.core.common import isTechniqueAvailable from lib.core.common import isTechniqueAvailable
from lib.core.common import randomInt from lib.core.common import randomInt
@ -379,14 +378,14 @@ class Agent:
""" """
prefixRegex = "(?:\s+(?:FIRST|SKIP)\s+\d+)*" prefixRegex = "(?:\s+(?:FIRST|SKIP)\s+\d+)*"
fieldsSelectTop = getCompiledRegex("\ASELECT\s+TOP\s+[\d]+\s+(.+?)\s+FROM", re.I).search(query) fieldsSelectTop = re.search("\ASELECT\s+TOP\s+[\d]+\s+(.+?)\s+FROM", query, re.I)
fieldsSelectDistinct = getCompiledRegex("\ASELECT%s\s+DISTINCT\((.+?)\)\s+FROM" % prefixRegex, re.I).search(query) fieldsSelectDistinct = re.search("\ASELECT%s\s+DISTINCT\((.+?)\)\s+FROM" % prefixRegex, query, re.I)
fieldsSelectCase = getCompiledRegex("\ASELECT%s\s+(\(CASE WHEN\s+.+\s+END\))" % prefixRegex, re.I).search(query) fieldsSelectCase = re.search("\ASELECT%s\s+(\(CASE WHEN\s+.+\s+END\))" % prefixRegex, query, re.I)
fieldsSelectFrom = getCompiledRegex("\ASELECT%s\s+(.+?)\s+FROM\s+" % prefixRegex, re.I).search(query) fieldsSelectFrom = re.search("\ASELECT%s\s+(.+?)\s+FROM\s+" % prefixRegex, query, re.I)
fieldsExists = getCompiledRegex("EXISTS(.*)", re.I).search(query) fieldsExists = re.search("EXISTS(.*)", query, re.I)
fieldsSelect = getCompiledRegex("\ASELECT%s\s+(.*)" % prefixRegex, re.I).search(query) fieldsSelect = re.search("\ASELECT%s\s+(.*)" % prefixRegex, query, re.I)
fieldsSubstr = getCompiledRegex("\A(SUBSTR|MID\()", re.I).search(query) fieldsSubstr = re.search("\A(SUBSTR|MID\()", query, re.I)
fieldsMinMaxstr = getCompiledRegex("(?:MIN|MAX)\(([^\(\)]+)\)", re.I).search(query) fieldsMinMaxstr = re.search("(?:MIN|MAX)\(([^\(\)]+)\)", query, re.I)
fieldsNoSelect = query fieldsNoSelect = query
if fieldsSubstr: if fieldsSubstr:
@ -799,8 +798,7 @@ class Agent:
retVal = None retVal = None
if inpStr: if inpStr:
regObj = getCompiledRegex("%s(?P<result>.*?)%s" % (PAYLOAD_DELIMITER, PAYLOAD_DELIMITER)) match = re.search("%s(?P<result>.*?)%s" % (PAYLOAD_DELIMITER, PAYLOAD_DELIMITER), inpStr)
match = regObj.search(inpStr)
if match: if match:
retVal = match.group("result") retVal = match.group("result")
@ -814,8 +812,7 @@ class Agent:
retVal = inpStr retVal = inpStr
if inpStr: if inpStr:
regObj = getCompiledRegex("(%s.*?%s)" % (PAYLOAD_DELIMITER, PAYLOAD_DELIMITER)) retVal = re.sub("(%s.*?%s)" % (PAYLOAD_DELIMITER, PAYLOAD_DELIMITER), "%s%s%s" % (PAYLOAD_DELIMITER, payload, PAYLOAD_DELIMITER), inpStr)
retVal = regObj.sub("%s%s%s" % (PAYLOAD_DELIMITER, payload, PAYLOAD_DELIMITER), inpStr)
return retVal return retVal

View File

@ -1821,22 +1821,6 @@ def goGoodSamaritan(prevValue, originalCharset):
else: else:
return None, None, None, originalCharset return None, None, None, originalCharset
def getCompiledRegex(regex, flags=0):
"""
Returns compiled regular expression and stores it in cache for further
usage (deprecated as newer versions of Python do this automatically)
>>> getCompiledRegex('test') # doctest: +ELLIPSIS
<_sre.SRE_Pattern object at...
"""
if (regex, flags) in kb.cache.regex:
retVal = kb.cache.regex[(regex, flags)]
else:
retVal = re.compile(regex, flags)
kb.cache.regex[(regex, flags)] = retVal
return retVal
def getPartRun(): def getPartRun():
""" """
Goes through call stack and finds constructs matching conf.dbmsHandler.*. Goes through call stack and finds constructs matching conf.dbmsHandler.*.
@ -1852,8 +1836,8 @@ def getPartRun():
# Goes backwards through the stack to find the conf.dbmsHandler method # Goes backwards through the stack to find the conf.dbmsHandler method
# calling this function # calling this function
for i in xrange(0, len(stack) - 1): for i in xrange(0, len(stack) - 1):
for regex in (getCompiledRegex('self\.(get[^(]+)\(\)'), getCompiledRegex('conf\.dbmsHandler\.([^(]+)\(\)')): for regex in (r"self\.(get[^(]+)\(\)", r"conf\.dbmsHandler\.([^(]+)\(\)"):
match = regex.search(stack[i]) match = re.search(regex, stack[i])
if match: if match:
# This is the calling conf.dbmsHandler or self method # This is the calling conf.dbmsHandler or self method
@ -2158,7 +2142,7 @@ def extractRegexResult(regex, content, flags=0):
retVal = None retVal = None
if regex and content and '?P<result>' in regex: if regex and content and '?P<result>' in regex:
match = getCompiledRegex(regex, flags).search(content) match = re.search(regex, content, flags)
if match: if match:
retVal = match.group("result") retVal = match.group("result")
@ -2257,11 +2241,11 @@ def removeDynamicContent(page):
if prefix is None and suffix is None: if prefix is None and suffix is None:
continue continue
elif prefix is None: elif prefix is None:
page = getCompiledRegex('(?s)^.+%s' % suffix).sub(suffix, page) page = re.sub(r'(?s)^.+%s' % suffix, suffix, page)
elif suffix is None: elif suffix is None:
page = getCompiledRegex('(?s)%s.+$' % prefix).sub(prefix, page) page = re.sub(r'(?s)%s.+$' % prefix, prefix, page)
else: else:
page = getCompiledRegex('(?s)%s.+%s' % (prefix, suffix)).sub('%s%s' % (prefix, suffix), page) page = re.sub(r'(?s)%s.+%s' % (prefix, suffix), '%s%s' % (prefix, suffix), page)
return page return page
@ -2327,7 +2311,7 @@ def parseSqliteTableSchema(value):
table = {} table = {}
columns = {} columns = {}
for match in re.finditer(getCompiledRegex(r"(\w+)\s+(TEXT|NUMERIC|INTEGER|REAL|NONE)"), value): for match in re.finditer(r"(\w+)\s+(TEXT|NUMERIC|INTEGER|REAL|NONE)", value):
columns[match.group(1)] = match.group(2) columns[match.group(1)] = match.group(2)
table[conf.tbl] = columns table[conf.tbl] = columns
@ -2473,7 +2457,7 @@ def filterListValue(value, regex):
""" """
if isinstance(value, list) and regex: if isinstance(value, list) and regex:
retVal = filter(lambda x: getCompiledRegex(regex, re.I).search(x), value) retVal = filter(lambda _: re.search(regex, _, re.I), value)
else: else:
retVal = value retVal = value

View File

@ -19,7 +19,6 @@ from lib.controller.controller import start
from lib.core.common import beep from lib.core.common import beep
from lib.core.common import clearConsoleLine from lib.core.common import clearConsoleLine
from lib.core.common import dataToStdout from lib.core.common import dataToStdout
from lib.core.common import getCompiledRegex
from lib.core.common import readXmlFile from lib.core.common import readXmlFile
from lib.core.data import conf from lib.core.data import conf
from lib.core.data import logger from lib.core.data import logger
@ -231,7 +230,7 @@ def runCase(switches=None, log=None, session=None):
def replaceVars(item, vars_): def replaceVars(item, vars_):
retVal = item retVal = item
if item and vars_: if item and vars_:
for var in re.findall(getCompiledRegex("\$\{([^}]+)\}"), item): for var in re.findall("\$\{([^}]+)\}", item):
if var in vars_: if var in vars_:
retVal = retVal.replace("${%s}" % var, vars_[var]) retVal = retVal.replace("${%s}" % var, vars_[var])
return retVal return retVal

View File

@ -7,10 +7,11 @@ Copyright (c) 2006-2012 sqlmap developers (http://www.sqlmap.org/)
See the file 'doc/COPYING' for copying permission See the file 'doc/COPYING' for copying permission
""" """
import re
from xml.sax.handler import ContentHandler from xml.sax.handler import ContentHandler
from lib.core.common import checkFile from lib.core.common import checkFile
from lib.core.common import getCompiledRegex
from lib.core.common import Backend from lib.core.common import Backend
from lib.core.common import parseXmlFile from lib.core.common import parseXmlFile
from lib.core.common import sanitizeStr from lib.core.common import sanitizeStr
@ -64,8 +65,7 @@ class MSSQLBannerHandler(ContentHandler):
def endElement(self, name): def endElement(self, name):
if name == "signature": if name == "signature":
for version in (self._version, self._versionAlt): for version in (self._version, self._versionAlt):
regObj = getCompiledRegex(" %s[\.\ ]+" % version) if version and re.search(r" %s[\.\ ]+" % version, self._banner):
if version and regObj.search(self._banner):
self._feedInfo("dbmsRelease", self._release) self._feedInfo("dbmsRelease", self._release)
self._feedInfo("dbmsVersion", self._version) self._feedInfo("dbmsVersion", self._version)
self._feedInfo("dbmsServicePack", self._servicePack) self._feedInfo("dbmsServicePack", self._servicePack)
@ -79,8 +79,7 @@ class MSSQLBannerHandler(ContentHandler):
self._inVersion = False self._inVersion = False
self._version = self._version.replace(" ", "") self._version = self._version.replace(" ", "")
regObj = getCompiledRegex(r"\A(?P<major>\d+)\.00\.(?P<build>\d+)\Z") match = re.search(r"\A(?P<major>\d+)\.00\.(?P<build>\d+)\Z", self._version)
match = regObj.search(self._version)
self._versionAlt = "%s.0.%s.0" % (match.group('major'), match.group('build')) if match else None self._versionAlt = "%s.0.%s.0" % (match.group('major'), match.group('build')) if match else None
elif name == "servicepack": elif name == "servicepack":

View File

@ -9,7 +9,6 @@ See the file 'doc/COPYING' for copying permission
import re import re
from lib.core.common import getCompiledRegex
from lib.core.common import readXmlFile from lib.core.common import readXmlFile
from lib.core.convert import urldecode from lib.core.convert import urldecode
from lib.core.data import paths from lib.core.data import paths
@ -51,9 +50,7 @@ def checkPayload(payload):
if payload: if payload:
for rule, desc in rules: for rule, desc in rules:
regObj = getCompiledRegex(rule) if re.search(rule, payload):
if regObj.search(payload):
detected = True detected = True
logger.warn("highly probable IDS/IPS detection: '%s: %s'" % (desc, payload)) logger.warn("highly probable IDS/IPS detection: '%s: %s'" % (desc, payload))

View File

@ -23,6 +23,7 @@ except (ImportError, OSError):
else: else:
_multiprocessing = multiprocessing _multiprocessing = multiprocessing
import re
import time import time
from hashlib import md5 from hashlib import md5
@ -35,7 +36,6 @@ from lib.core.common import Backend
from lib.core.common import checkFile from lib.core.common import checkFile
from lib.core.common import clearConsoleLine from lib.core.common import clearConsoleLine
from lib.core.common import dataToStdout from lib.core.common import dataToStdout
from lib.core.common import getCompiledRegex
from lib.core.common import getFileItems from lib.core.common import getFileItems
from lib.core.common import getPublicTypeMembers from lib.core.common import getPublicTypeMembers
from lib.core.common import hashDBRetrieve from lib.core.common import hashDBRetrieve
@ -404,7 +404,7 @@ def hashRecognition(value):
elif regex == HASH.CRYPT_GENERIC: elif regex == HASH.CRYPT_GENERIC:
if any((value.lower() == value, value.upper() == value)): if any((value.lower() == value, value.upper() == value)):
continue continue
elif getCompiledRegex(regex).match(value): elif re.match(regex, value):
retVal = regex retVal = regex
break break
@ -574,7 +574,7 @@ def dictionaryAttack(attack_dict):
hash_ = hash_.split()[0] hash_ = hash_.split()[0]
if getCompiledRegex(hash_regex).match(hash_): if re.match(hash_regex, hash_):
item = None item = None
if hash_regex not in (HASH.CRYPT_GENERIC, HASH.WORDPRESS): if hash_regex not in (HASH.CRYPT_GENERIC, HASH.WORDPRESS):

View File

@ -14,7 +14,6 @@ from lib.core.common import calculateDeltaSeconds
from lib.core.common import dataToSessionFile from lib.core.common import dataToSessionFile
from lib.core.common import dataToStdout from lib.core.common import dataToStdout
from lib.core.common import Backend from lib.core.common import Backend
from lib.core.common import getCompiledRegex
from lib.core.common import safeStringFormat from lib.core.common import safeStringFormat
from lib.core.common import randomStr from lib.core.common import randomStr
from lib.core.common import replaceNewlineTabs from lib.core.common import replaceNewlineTabs
@ -133,7 +132,7 @@ def resume(expression, payload):
return None return None
substringQuery = queries[Backend.getIdentifiedDbms()].substring.query substringQuery = queries[Backend.getIdentifiedDbms()].substring.query
select = getCompiledRegex("\ASELECT ", re.I).search(expression) select = re.search("\ASELECT ", expression, re.I)
_, length, regExpr = queryOutputLength(expression, payload) _, length, regExpr = queryOutputLength(expression, payload)

View File

@ -7,7 +7,8 @@ Copyright (c) 2006-2012 sqlmap developers (http://www.sqlmap.org/)
See the file 'doc/COPYING' for copying permission See the file 'doc/COPYING' for copying permission
""" """
from lib.core.common import getCompiledRegex import re
from lib.core.common import Backend from lib.core.common import Backend
from lib.core.common import hashDBWrite from lib.core.common import hashDBWrite
from lib.core.common import isTechniqueAvailable from lib.core.common import isTechniqueAvailable
@ -52,7 +53,7 @@ class Miscellaneous:
else: else:
conf.tmpPath = "/tmp" conf.tmpPath = "/tmp"
if getCompiledRegex("(?i)\A[\w]:[\/\\\\]+").search(conf.tmpPath): if re.search(r"\A[\w]:[\/\\]+", conf.tmpPath, re.I):
Backend.setOs(OS.WINDOWS) Backend.setOs(OS.WINDOWS)
conf.tmpPath = normalizePath(conf.tmpPath) conf.tmpPath = normalizePath(conf.tmpPath)