From e735f8f56835abeda6135c2088b54753193de7c9 Mon Sep 17 00:00:00 2001 From: sebres Date: Thu, 6 Oct 2016 15:27:49 +0200 Subject: [PATCH] default non-unicode and case-sensitive matching (by pattern templates automatically add `(?iu)` for "ignore case" and "unicode" if expected) --- fail2ban/server/datedetector.py | 47 ++++++++++++++------------ fail2ban/server/datetemplate.py | 25 ++++++++++---- fail2ban/tests/datedetectortestcase.py | 45 +++++++++++++++++++++--- 3 files changed, 85 insertions(+), 32 deletions(-) diff --git a/fail2ban/server/datedetector.py b/fail2ban/server/datedetector.py index 0aac4337..f256a425 100644 --- a/fail2ban/server/datedetector.py +++ b/fail2ban/server/datedetector.py @@ -254,27 +254,32 @@ class DateDetector(object): if i < len(self.__templates): ddtempl = self.__templates[i] template = ddtempl.template - distance, endpos = self.__lastPos[0], self.__lastEndPos[0] - if logSys.getEffectiveLevel() <= logLevel-1: - logSys.log(logLevel-1, " try to match last template #%02i (from %r to %r): ...%r==%r %s %r==%r...", - i, distance, endpos, - line[distance-1:distance], self.__lastPos[1], - line[distance:endpos], - line[endpos:endpos+1], self.__lastEndPos[1]) - # check same boundaries left/right, otherwise possible collision/pattern switch: - if (line[distance-1:distance] == self.__lastPos[1] and - line[endpos:endpos+1] == self.__lastEndPos[1] - ): - match = template.matchDate(line, distance, endpos) - if match: - distance = match.start() - endpos = match.end() - # if different position, possible collision/pattern switch: - if distance == self.__lastPos[0] and endpos == self.__lastEndPos[0]: - logSys.log(logLevel, " matched last time template #%02i", i) - else: - logSys.log(logLevel, " ** last pattern collision - pattern change, search ...") - match = None + if template.flags & DateTemplate.LINE_BEGIN: + if logSys.getEffectiveLevel() <= logLevel-1: + logSys.log(logLevel-1, " try to match last anchored template #%02i ...", i) + match = template.matchDate(line) + else: + distance, endpos = self.__lastPos[0], self.__lastEndPos[0] + if logSys.getEffectiveLevel() <= logLevel-1: + logSys.log(logLevel-1, " try to match last template #%02i (from %r to %r): ...%r==%r %s %r==%r...", + i, distance, endpos, + line[distance-1:distance], self.__lastPos[1], + line[distance:endpos], + line[endpos:endpos+1], self.__lastEndPos[1]) + # check same boundaries left/right, otherwise possible collision/pattern switch: + if (line[distance-1:distance] == self.__lastPos[1] and + line[endpos:endpos+1] == self.__lastEndPos[1] + ): + match = template.matchDate(line, distance, endpos) + if match: + distance = match.start() + endpos = match.end() + # if different position, possible collision/pattern switch: + if distance == self.__lastPos[0] and endpos == self.__lastEndPos[0]: + logSys.log(logLevel, " matched last time template #%02i", i) + else: + logSys.log(logLevel, " ** last pattern collision - pattern change, search ...") + match = None # search template and better match: if not match: self.__lastTemplIdx = 0x7fffffff diff --git a/fail2ban/server/datetemplate.py b/fail2ban/server/datetemplate.py index fcd51f99..9524e373 100644 --- a/fail2ban/server/datetemplate.py +++ b/fail2ban/server/datetemplate.py @@ -32,13 +32,18 @@ from ..helpers import getLogger logSys = getLogger(__name__) -RE_NO_WRD_BOUND_BEG = re.compile(r'^(?:\^|\*\*|\(\?:\^)') +RE_NO_WRD_BOUND_BEG = re.compile(r'^(?:\(\?\w+\))?(?:\^|\*\*|\(\?:\^)') RE_NO_WRD_BOUND_END = re.compile(r'(?