Skip to content

Commit b0787f1

Browse files
committed
getting rid of obsolete getCompiledRegex (in newer versions of Python regexes are already cached)
1 parent 556b349 commit b0787f1

8 files changed

Lines changed: 31 additions & 55 deletions

File tree

lib/core/agent.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from xml.etree import ElementTree as ET
1313

1414
from lib.core.common import Backend
15-
from lib.core.common import getCompiledRegex
1615
from lib.core.common import isDBMSVersionAtLeast
1716
from lib.core.common import isTechniqueAvailable
1817
from lib.core.common import randomInt
@@ -379,14 +378,14 @@ def getFields(self, query):
379378
"""
380379

381380
prefixRegex = "(?:\s+(?:FIRST|SKIP)\s+\d+)*"
382-
fieldsSelectTop = getCompiledRegex("\ASELECT\s+TOP\s+[\d]+\s+(.+?)\s+FROM", re.I).search(query)
383-
fieldsSelectDistinct = getCompiledRegex("\ASELECT%s\s+DISTINCT\((.+?)\)\s+FROM" % prefixRegex, re.I).search(query)
384-
fieldsSelectCase = getCompiledRegex("\ASELECT%s\s+(\(CASE WHEN\s+.+\s+END\))" % prefixRegex, re.I).search(query)
385-
fieldsSelectFrom = getCompiledRegex("\ASELECT%s\s+(.+?)\s+FROM\s+" % prefixRegex, re.I).search(query)
386-
fieldsExists = getCompiledRegex("EXISTS(.*)", re.I).search(query)
387-
fieldsSelect = getCompiledRegex("\ASELECT%s\s+(.*)" % prefixRegex, re.I).search(query)
388-
fieldsSubstr = getCompiledRegex("\A(SUBSTR|MID\()", re.I).search(query)
389-
fieldsMinMaxstr = getCompiledRegex("(?:MIN|MAX)\(([^\(\)]+)\)", re.I).search(query)
381+
fieldsSelectTop = re.search("\ASELECT\s+TOP\s+[\d]+\s+(.+?)\s+FROM", query, re.I)
382+
fieldsSelectDistinct = re.search("\ASELECT%s\s+DISTINCT\((.+?)\)\s+FROM" % prefixRegex, query, re.I)
383+
fieldsSelectCase = re.search("\ASELECT%s\s+(\(CASE WHEN\s+.+\s+END\))" % prefixRegex, query, re.I)
384+
fieldsSelectFrom = re.search("\ASELECT%s\s+(.+?)\s+FROM\s+" % prefixRegex, query, re.I)
385+
fieldsExists = re.search("EXISTS(.*)", query, re.I)
386+
fieldsSelect = re.search("\ASELECT%s\s+(.*)" % prefixRegex, query, re.I)
387+
fieldsSubstr = re.search("\A(SUBSTR|MID\()", query, re.I)
388+
fieldsMinMaxstr = re.search("(?:MIN|MAX)\(([^\(\)]+)\)", query, re.I)
390389
fieldsNoSelect = query
391390

392391
if fieldsSubstr:
@@ -799,8 +798,7 @@ def extractPayload(self, inpStr):
799798
retVal = None
800799

801800
if inpStr:
802-
regObj = getCompiledRegex("%s(?P<result>.*?)%s" % (PAYLOAD_DELIMITER, PAYLOAD_DELIMITER))
803-
match = regObj.search(inpStr)
801+
match = re.search("%s(?P<result>.*?)%s" % (PAYLOAD_DELIMITER, PAYLOAD_DELIMITER), inpStr)
804802

805803
if match:
806804
retVal = match.group("result")
@@ -814,8 +812,7 @@ def replacePayload(self, inpStr, payload):
814812
retVal = inpStr
815813

816814
if inpStr:
817-
regObj = getCompiledRegex("(%s.*?%s)" % (PAYLOAD_DELIMITER, PAYLOAD_DELIMITER))
818-
retVal = regObj.sub("%s%s%s" % (PAYLOAD_DELIMITER, payload, PAYLOAD_DELIMITER), inpStr)
815+
retVal = re.sub("(%s.*?%s)" % (PAYLOAD_DELIMITER, PAYLOAD_DELIMITER), "%s%s%s" % (PAYLOAD_DELIMITER, payload, PAYLOAD_DELIMITER), inpStr)
819816

820817
return retVal
821818

lib/core/common.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,22 +1821,6 @@ def goGoodSamaritan(prevValue, originalCharset):
18211821
else:
18221822
return None, None, None, originalCharset
18231823

1824-
def getCompiledRegex(regex, flags=0):
1825-
"""
1826-
Returns compiled regular expression and stores it in cache for further
1827-
usage (deprecated as newer versions of Python do this automatically)
1828-
1829-
>>> getCompiledRegex('test') # doctest: +ELLIPSIS
1830-
<_sre.SRE_Pattern object at...
1831-
"""
1832-
1833-
if (regex, flags) in kb.cache.regex:
1834-
retVal = kb.cache.regex[(regex, flags)]
1835-
else:
1836-
retVal = re.compile(regex, flags)
1837-
kb.cache.regex[(regex, flags)] = retVal
1838-
return retVal
1839-
18401824
def getPartRun():
18411825
"""
18421826
Goes through call stack and finds constructs matching conf.dbmsHandler.*.
@@ -1852,8 +1836,8 @@ def getPartRun():
18521836
# Goes backwards through the stack to find the conf.dbmsHandler method
18531837
# calling this function
18541838
for i in xrange(0, len(stack) - 1):
1855-
for regex in (getCompiledRegex('self\.(get[^(]+)\(\)'), getCompiledRegex('conf\.dbmsHandler\.([^(]+)\(\)')):
1856-
match = regex.search(stack[i])
1839+
for regex in (r"self\.(get[^(]+)\(\)", r"conf\.dbmsHandler\.([^(]+)\(\)"):
1840+
match = re.search(regex, stack[i])
18571841

18581842
if match:
18591843
# This is the calling conf.dbmsHandler or self method
@@ -2158,7 +2142,7 @@ def extractRegexResult(regex, content, flags=0):
21582142
retVal = None
21592143

21602144
if regex and content and '?P<result>' in regex:
2161-
match = getCompiledRegex(regex, flags).search(content)
2145+
match = re.search(regex, content, flags)
21622146

21632147
if match:
21642148
retVal = match.group("result")
@@ -2257,11 +2241,11 @@ def removeDynamicContent(page):
22572241
if prefix is None and suffix is None:
22582242
continue
22592243
elif prefix is None:
2260-
page = getCompiledRegex('(?s)^.+%s' % suffix).sub(suffix, page)
2244+
page = re.sub(r'(?s)^.+%s' % suffix, suffix, page)
22612245
elif suffix is None:
2262-
page = getCompiledRegex('(?s)%s.+$' % prefix).sub(prefix, page)
2246+
page = re.sub(r'(?s)%s.+$' % prefix, prefix, page)
22632247
else:
2264-
page = getCompiledRegex('(?s)%s.+%s' % (prefix, suffix)).sub('%s%s' % (prefix, suffix), page)
2248+
page = re.sub(r'(?s)%s.+%s' % (prefix, suffix), '%s%s' % (prefix, suffix), page)
22652249

22662250
return page
22672251

@@ -2327,7 +2311,7 @@ def parseSqliteTableSchema(value):
23272311
table = {}
23282312
columns = {}
23292313

2330-
for match in re.finditer(getCompiledRegex(r"(\w+)\s+(TEXT|NUMERIC|INTEGER|REAL|NONE)"), value):
2314+
for match in re.finditer(r"(\w+)\s+(TEXT|NUMERIC|INTEGER|REAL|NONE)", value):
23312315
columns[match.group(1)] = match.group(2)
23322316

23332317
table[conf.tbl] = columns
@@ -2473,7 +2457,7 @@ def filterListValue(value, regex):
24732457
"""
24742458

24752459
if isinstance(value, list) and regex:
2476-
retVal = filter(lambda x: getCompiledRegex(regex, re.I).search(x), value)
2460+
retVal = filter(lambda _: re.search(regex, _, re.I), value)
24772461
else:
24782462
retVal = value
24792463

lib/core/testing.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from lib.core.common import beep
2020
from lib.core.common import clearConsoleLine
2121
from lib.core.common import dataToStdout
22-
from lib.core.common import getCompiledRegex
2322
from lib.core.common import readXmlFile
2423
from lib.core.data import conf
2524
from lib.core.data import logger
@@ -231,7 +230,7 @@ def runCase(switches=None, log=None, session=None):
231230
def replaceVars(item, vars_):
232231
retVal = item
233232
if item and vars_:
234-
for var in re.findall(getCompiledRegex("\$\{([^}]+)\}"), item):
233+
for var in re.findall("\$\{([^}]+)\}", item):
235234
if var in vars_:
236235
retVal = retVal.replace("${%s}" % var, vars_[var])
237236
return retVal

lib/parse/banner.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
See the file 'doc/COPYING' for copying permission
88
"""
99

10+
import re
11+
1012
from xml.sax.handler import ContentHandler
1113

1214
from lib.core.common import checkFile
13-
from lib.core.common import getCompiledRegex
1415
from lib.core.common import Backend
1516
from lib.core.common import parseXmlFile
1617
from lib.core.common import sanitizeStr
@@ -64,8 +65,7 @@ def characters(self, data):
6465
def endElement(self, name):
6566
if name == "signature":
6667
for version in (self._version, self._versionAlt):
67-
regObj = getCompiledRegex(" %s[\.\ ]+" % version)
68-
if version and regObj.search(self._banner):
68+
if version and re.search(r" %s[\.\ ]+" % version, self._banner):
6969
self._feedInfo("dbmsRelease", self._release)
7070
self._feedInfo("dbmsVersion", self._version)
7171
self._feedInfo("dbmsServicePack", self._servicePack)
@@ -79,8 +79,7 @@ def endElement(self, name):
7979
self._inVersion = False
8080
self._version = self._version.replace(" ", "")
8181

82-
regObj = getCompiledRegex(r"\A(?P<major>\d+)\.00\.(?P<build>\d+)\Z")
83-
match = regObj.search(self._version)
82+
match = re.search(r"\A(?P<major>\d+)\.00\.(?P<build>\d+)\Z", self._version)
8483
self._versionAlt = "%s.0.%s.0" % (match.group('major'), match.group('build')) if match else None
8584

8685
elif name == "servicepack":

lib/utils/checkpayload.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import re
1111

12-
from lib.core.common import getCompiledRegex
1312
from lib.core.common import readXmlFile
1413
from lib.core.convert import urldecode
1514
from lib.core.data import paths
@@ -51,9 +50,7 @@ def checkPayload(payload):
5150

5251
if payload:
5352
for rule, desc in rules:
54-
regObj = getCompiledRegex(rule)
55-
56-
if regObj.search(payload):
53+
if re.search(rule, payload):
5754
detected = True
5855
logger.warn("highly probable IDS/IPS detection: '%s: %s'" % (desc, payload))
5956

lib/utils/hash.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
else:
2424
_multiprocessing = multiprocessing
2525

26+
import re
2627
import time
2728

2829
from hashlib import md5
@@ -35,7 +36,6 @@
3536
from lib.core.common import checkFile
3637
from lib.core.common import clearConsoleLine
3738
from lib.core.common import dataToStdout
38-
from lib.core.common import getCompiledRegex
3939
from lib.core.common import getFileItems
4040
from lib.core.common import getPublicTypeMembers
4141
from lib.core.common import hashDBRetrieve
@@ -404,7 +404,7 @@ def hashRecognition(value):
404404
elif regex == HASH.CRYPT_GENERIC:
405405
if any((value.lower() == value, value.upper() == value)):
406406
continue
407-
elif getCompiledRegex(regex).match(value):
407+
elif re.match(regex, value):
408408
retVal = regex
409409
break
410410

@@ -574,7 +574,7 @@ def dictionaryAttack(attack_dict):
574574

575575
hash_ = hash_.split()[0]
576576

577-
if getCompiledRegex(hash_regex).match(hash_):
577+
if re.match(hash_regex, hash_):
578578
item = None
579579

580580
if hash_regex not in (HASH.CRYPT_GENERIC, HASH.WORDPRESS):

lib/utils/resume.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from lib.core.common import dataToSessionFile
1515
from lib.core.common import dataToStdout
1616
from lib.core.common import Backend
17-
from lib.core.common import getCompiledRegex
1817
from lib.core.common import safeStringFormat
1918
from lib.core.common import randomStr
2019
from lib.core.common import replaceNewlineTabs
@@ -133,7 +132,7 @@ def resume(expression, payload):
133132
return None
134133

135134
substringQuery = queries[Backend.getIdentifiedDbms()].substring.query
136-
select = getCompiledRegex("\ASELECT ", re.I).search(expression)
135+
select = re.search("\ASELECT ", expression, re.I)
137136

138137
_, length, regExpr = queryOutputLength(expression, payload)
139138

plugins/generic/misc.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
See the file 'doc/COPYING' for copying permission
88
"""
99

10-
from lib.core.common import getCompiledRegex
10+
import re
11+
1112
from lib.core.common import Backend
1213
from lib.core.common import hashDBWrite
1314
from lib.core.common import isTechniqueAvailable
@@ -52,7 +53,7 @@ def getRemoteTempPath(self):
5253
else:
5354
conf.tmpPath = "/tmp"
5455

55-
if getCompiledRegex("(?i)\A[\w]:[\/\\\\]+").search(conf.tmpPath):
56+
if re.search(r"\A[\w]:[\/\\]+", conf.tmpPath, re.I):
5657
Backend.setOs(OS.WINDOWS)
5758

5859
conf.tmpPath = normalizePath(conf.tmpPath)

0 commit comments

Comments
 (0)