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

View File

@ -1821,22 +1821,6 @@ def goGoodSamaritan(prevValue, originalCharset):
else:
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():
"""
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
# calling this function
for i in xrange(0, len(stack) - 1):
for regex in (getCompiledRegex('self\.(get[^(]+)\(\)'), getCompiledRegex('conf\.dbmsHandler\.([^(]+)\(\)')):
match = regex.search(stack[i])
for regex in (r"self\.(get[^(]+)\(\)", r"conf\.dbmsHandler\.([^(]+)\(\)"):
match = re.search(regex, stack[i])
if match:
# This is the calling conf.dbmsHandler or self method
@ -2158,7 +2142,7 @@ def extractRegexResult(regex, content, flags=0):
retVal = None
if regex and content and '?P<result>' in regex:
match = getCompiledRegex(regex, flags).search(content)
match = re.search(regex, content, flags)
if match:
retVal = match.group("result")
@ -2257,11 +2241,11 @@ def removeDynamicContent(page):
if prefix is None and suffix is None:
continue
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:
page = getCompiledRegex('(?s)%s.+$' % prefix).sub(prefix, page)
page = re.sub(r'(?s)%s.+$' % prefix, prefix, page)
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
@ -2327,7 +2311,7 @@ def parseSqliteTableSchema(value):
table = {}
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)
table[conf.tbl] = columns
@ -2473,7 +2457,7 @@ def filterListValue(value, 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:
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 clearConsoleLine
from lib.core.common import dataToStdout
from lib.core.common import getCompiledRegex
from lib.core.common import readXmlFile
from lib.core.data import conf
from lib.core.data import logger
@ -231,7 +230,7 @@ def runCase(switches=None, log=None, session=None):
def replaceVars(item, vars_):
retVal = item
if item and vars_:
for var in re.findall(getCompiledRegex("\$\{([^}]+)\}"), item):
for var in re.findall("\$\{([^}]+)\}", item):
if var in vars_:
retVal = retVal.replace("${%s}" % var, vars_[var])
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
"""
import re
from xml.sax.handler import ContentHandler
from lib.core.common import checkFile
from lib.core.common import getCompiledRegex
from lib.core.common import Backend
from lib.core.common import parseXmlFile
from lib.core.common import sanitizeStr
@ -64,8 +65,7 @@ class MSSQLBannerHandler(ContentHandler):
def endElement(self, name):
if name == "signature":
for version in (self._version, self._versionAlt):
regObj = getCompiledRegex(" %s[\.\ ]+" % version)
if version and regObj.search(self._banner):
if version and re.search(r" %s[\.\ ]+" % version, self._banner):
self._feedInfo("dbmsRelease", self._release)
self._feedInfo("dbmsVersion", self._version)
self._feedInfo("dbmsServicePack", self._servicePack)
@ -79,8 +79,7 @@ class MSSQLBannerHandler(ContentHandler):
self._inVersion = False
self._version = self._version.replace(" ", "")
regObj = getCompiledRegex(r"\A(?P<major>\d+)\.00\.(?P<build>\d+)\Z")
match = regObj.search(self._version)
match = re.search(r"\A(?P<major>\d+)\.00\.(?P<build>\d+)\Z", self._version)
self._versionAlt = "%s.0.%s.0" % (match.group('major'), match.group('build')) if match else None
elif name == "servicepack":

View File

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

View File

@ -23,6 +23,7 @@ except (ImportError, OSError):
else:
_multiprocessing = multiprocessing
import re
import time
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 clearConsoleLine
from lib.core.common import dataToStdout
from lib.core.common import getCompiledRegex
from lib.core.common import getFileItems
from lib.core.common import getPublicTypeMembers
from lib.core.common import hashDBRetrieve
@ -404,7 +404,7 @@ def hashRecognition(value):
elif regex == HASH.CRYPT_GENERIC:
if any((value.lower() == value, value.upper() == value)):
continue
elif getCompiledRegex(regex).match(value):
elif re.match(regex, value):
retVal = regex
break
@ -574,7 +574,7 @@ def dictionaryAttack(attack_dict):
hash_ = hash_.split()[0]
if getCompiledRegex(hash_regex).match(hash_):
if re.match(hash_regex, hash_):
item = None
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 dataToStdout
from lib.core.common import Backend
from lib.core.common import getCompiledRegex
from lib.core.common import safeStringFormat
from lib.core.common import randomStr
from lib.core.common import replaceNewlineTabs
@ -133,7 +132,7 @@ def resume(expression, payload):
return None
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)

View File

@ -7,7 +7,8 @@ Copyright (c) 2006-2012 sqlmap developers (http://www.sqlmap.org/)
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 hashDBWrite
from lib.core.common import isTechniqueAvailable
@ -52,7 +53,7 @@ class Miscellaneous:
else:
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)
conf.tmpPath = normalizePath(conf.tmpPath)