2011-10-07 19:47:50 +00:00
|
|
|
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: t -*-
|
|
|
|
# vi: set ft=python sts=4 ts=4 sw=4 noet :
|
|
|
|
|
2006-09-05 21:17:35 +00:00
|
|
|
# This file is part of Fail2Ban.
|
|
|
|
#
|
|
|
|
# Fail2Ban is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Fail2Ban is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Fail2Ban; if not, write to the Free Software
|
2011-11-21 12:20:20 +00:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2006-09-05 21:17:35 +00:00
|
|
|
|
|
|
|
# Author: Cyril Jaquier
|
|
|
|
#
|
2006-12-23 16:37:17 +00:00
|
|
|
# $Revision$
|
2006-09-05 21:17:35 +00:00
|
|
|
|
|
|
|
__author__ = "Cyril Jaquier"
|
2006-12-23 16:37:17 +00:00
|
|
|
__version__ = "$Revision$"
|
|
|
|
__date__ = "$Date$"
|
2006-09-05 21:17:35 +00:00
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
__license__ = "GPL"
|
|
|
|
|
2006-09-10 20:43:13 +00:00
|
|
|
import time, logging
|
2006-09-05 21:17:35 +00:00
|
|
|
|
2008-05-18 19:53:18 +00:00
|
|
|
from datetemplate import DateStrptime, DateTai64n, DateEpoch, DateISO8601
|
2006-09-10 20:43:13 +00:00
|
|
|
from threading import Lock
|
|
|
|
|
|
|
|
# Gets the instance of the logger.
|
|
|
|
logSys = logging.getLogger("fail2ban.filter.datedetector")
|
2006-09-05 21:17:35 +00:00
|
|
|
|
|
|
|
class DateDetector:
|
|
|
|
|
|
|
|
def __init__(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__lock = Lock()
|
|
|
|
self.__templates = list()
|
2006-09-05 21:17:35 +00:00
|
|
|
|
|
|
|
def addDefaultTemplate(self):
|
2008-01-16 22:55:04 +00:00
|
|
|
self.__lock.acquire()
|
2007-02-25 23:53:22 +00:00
|
|
|
try:
|
|
|
|
# standard
|
|
|
|
template = DateStrptime()
|
2008-08-12 21:42:21 +00:00
|
|
|
template.setName("MONTH Day Hour:Minute:Second")
|
2009-01-20 21:48:04 +00:00
|
|
|
template.setRegex("\S{3}\s{1,2}\d{1,2} \d{2}:\d{2}:\d{2}")
|
2007-02-25 23:53:22 +00:00
|
|
|
template.setPattern("%b %d %H:%M:%S")
|
|
|
|
self.__templates.append(template)
|
|
|
|
# asctime
|
|
|
|
template = DateStrptime()
|
2008-08-12 21:42:21 +00:00
|
|
|
template.setName("WEEKDAY MONTH Day Hour:Minute:Second Year")
|
2007-02-25 23:53:22 +00:00
|
|
|
template.setRegex("\S{3} \S{3}\s{1,2}\d{1,2} \d{2}:\d{2}:\d{2} \d{4}")
|
|
|
|
template.setPattern("%a %b %d %H:%M:%S %Y")
|
|
|
|
self.__templates.append(template)
|
2007-04-01 20:42:05 +00:00
|
|
|
# asctime without year
|
|
|
|
template = DateStrptime()
|
2008-08-12 21:42:21 +00:00
|
|
|
template.setName("WEEKDAY MONTH Day Hour:Minute:Second")
|
2007-04-01 20:42:05 +00:00
|
|
|
template.setRegex("\S{3} \S{3}\s{1,2}\d{1,2} \d{2}:\d{2}:\d{2}")
|
|
|
|
template.setPattern("%a %b %d %H:%M:%S")
|
|
|
|
self.__templates.append(template)
|
2007-02-25 23:53:22 +00:00
|
|
|
# simple date
|
|
|
|
template = DateStrptime()
|
|
|
|
template.setName("Year/Month/Day Hour:Minute:Second")
|
|
|
|
template.setRegex("\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}")
|
|
|
|
template.setPattern("%Y/%m/%d %H:%M:%S")
|
|
|
|
self.__templates.append(template)
|
2008-04-07 22:49:47 +00:00
|
|
|
# simple date too (from x11vnc)
|
|
|
|
template = DateStrptime()
|
|
|
|
template.setName("Day/Month/Year Hour:Minute:Second")
|
|
|
|
template.setRegex("\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}")
|
|
|
|
template.setPattern("%d/%m/%Y %H:%M:%S")
|
|
|
|
self.__templates.append(template)
|
2009-09-10 15:15:38 +00:00
|
|
|
# previous one but with year given by 2 digits
|
|
|
|
# (See http://bugs.debian.org/537610)
|
|
|
|
template = DateStrptime()
|
|
|
|
template.setName("Day/Month/Year Hour:Minute:Second")
|
|
|
|
template.setRegex("\d{2}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}")
|
|
|
|
template.setPattern("%d/%m/%y %H:%M:%S")
|
|
|
|
self.__templates.append(template)
|
2007-02-25 23:53:22 +00:00
|
|
|
# Apache format [31/Oct/2006:09:22:55 -0000]
|
|
|
|
template = DateStrptime()
|
2008-08-12 21:42:21 +00:00
|
|
|
template.setName("Day/MONTH/Year:Hour:Minute:Second")
|
2007-02-25 23:53:22 +00:00
|
|
|
template.setRegex("\d{2}/\S{3}/\d{4}:\d{2}:\d{2}:\d{2}")
|
|
|
|
template.setPattern("%d/%b/%Y:%H:%M:%S")
|
|
|
|
self.__templates.append(template)
|
2009-01-27 23:21:55 +00:00
|
|
|
# CPanel 05/20/2008:01:57:39
|
|
|
|
template = DateStrptime()
|
|
|
|
template.setName("Month/Day/Year:Hour:Minute:Second")
|
|
|
|
template.setRegex("\d{2}/\d{2}/\d{4}:\d{2}:\d{2}:\d{2}")
|
|
|
|
template.setPattern("%m/%d/%Y:%H:%M:%S")
|
|
|
|
self.__templates.append(template)
|
2007-02-25 23:53:22 +00:00
|
|
|
# Exim 2006-12-21 06:43:20
|
|
|
|
template = DateStrptime()
|
|
|
|
template.setName("Year-Month-Day Hour:Minute:Second")
|
|
|
|
template.setRegex("\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}")
|
|
|
|
template.setPattern("%Y-%m-%d %H:%M:%S")
|
|
|
|
self.__templates.append(template)
|
2011-11-22 01:01:15 +00:00
|
|
|
# custom for syslog-ng 2006.12.21 06:43:20
|
|
|
|
template = DateStrptime()
|
|
|
|
template.setName("Year.Month.Day Hour:Minute:Second")
|
|
|
|
template.setRegex("\d{4}.\d{2}.\d{2} \d{2}:\d{2}:\d{2}")
|
|
|
|
template.setPattern("%Y.%m.%d %H:%M:%S")
|
|
|
|
self.__templates.append(template)
|
2007-08-08 22:16:22 +00:00
|
|
|
# named 26-Jul-2007 15:20:52.252
|
|
|
|
template = DateStrptime()
|
2008-08-12 21:42:21 +00:00
|
|
|
template.setName("Day-MONTH-Year Hour:Minute:Second[.Millisecond]")
|
2007-08-08 22:16:22 +00:00
|
|
|
template.setRegex("\d{2}-\S{3}-\d{4} \d{2}:\d{2}:\d{2}")
|
|
|
|
template.setPattern("%d-%b-%Y %H:%M:%S")
|
|
|
|
self.__templates.append(template)
|
2008-08-12 21:42:21 +00:00
|
|
|
# 17-07-2008 17:23:25
|
|
|
|
template = DateStrptime()
|
|
|
|
template.setName("Day-Month-Year Hour:Minute:Second")
|
|
|
|
template.setRegex("\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}")
|
|
|
|
template.setPattern("%d-%m-%Y %H:%M:%S")
|
|
|
|
self.__templates.append(template)
|
2012-01-28 17:35:39 +00:00
|
|
|
# 01-27-2012 16:22:44.252
|
|
|
|
template = DateStrptime()
|
|
|
|
template.setName("Month-Day-Year Hour:Minute:Second[.Millisecond]")
|
|
|
|
template.setRegex("\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}")
|
|
|
|
template.setPattern("%m-%d-%Y %H:%M:%S")
|
|
|
|
self.__templates.append(template)
|
2007-02-25 23:53:22 +00:00
|
|
|
# TAI64N
|
|
|
|
template = DateTai64n()
|
|
|
|
template.setName("TAI64N")
|
|
|
|
self.__templates.append(template)
|
|
|
|
# Epoch
|
|
|
|
template = DateEpoch()
|
|
|
|
template.setName("Epoch")
|
|
|
|
self.__templates.append(template)
|
2008-05-18 19:53:18 +00:00
|
|
|
# ISO 8601
|
|
|
|
template = DateISO8601()
|
|
|
|
template.setName("ISO 8601")
|
|
|
|
self.__templates.append(template)
|
2008-08-12 20:59:17 +00:00
|
|
|
# Only time information in the log
|
|
|
|
template = DateStrptime()
|
|
|
|
template.setName("Hour:Minute:Second")
|
|
|
|
template.setRegex("^\d{2}:\d{2}:\d{2}")
|
|
|
|
template.setPattern("%H:%M:%S")
|
|
|
|
self.__templates.append(template)
|
2008-10-10 16:26:18 +00:00
|
|
|
# <09/16/08@05:03:30>
|
|
|
|
template = DateStrptime()
|
|
|
|
template.setName("<Month/Day/Year@Hour:Minute:Second>")
|
|
|
|
template.setRegex("^<\d{2}/\d{2}/\d{2}@\d{2}:\d{2}:\d{2}>")
|
|
|
|
template.setPattern("<%m/%d/%y@%H:%M:%S>")
|
|
|
|
self.__templates.append(template)
|
2007-02-25 23:53:22 +00:00
|
|
|
finally:
|
|
|
|
self.__lock.release()
|
2006-09-05 21:17:35 +00:00
|
|
|
|
2006-09-19 22:21:20 +00:00
|
|
|
def getTemplates(self):
|
|
|
|
return self.__templates
|
|
|
|
|
2006-09-05 21:17:35 +00:00
|
|
|
def matchTime(self, line):
|
2008-01-16 22:55:04 +00:00
|
|
|
self.__lock.acquire()
|
2007-12-17 20:04:29 +00:00
|
|
|
try:
|
|
|
|
for template in self.__templates:
|
|
|
|
match = template.matchDate(line)
|
|
|
|
if not match == None:
|
|
|
|
return match
|
|
|
|
return None
|
|
|
|
finally:
|
|
|
|
self.__lock.release()
|
2006-09-05 21:17:35 +00:00
|
|
|
|
|
|
|
def getTime(self, line):
|
2008-01-16 22:55:04 +00:00
|
|
|
self.__lock.acquire()
|
2007-12-17 20:04:29 +00:00
|
|
|
try:
|
|
|
|
for template in self.__templates:
|
|
|
|
try:
|
|
|
|
date = template.getDate(line)
|
|
|
|
if date == None:
|
|
|
|
continue
|
|
|
|
return date
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
return None
|
|
|
|
finally:
|
|
|
|
self.__lock.release()
|
2006-09-05 21:17:35 +00:00
|
|
|
|
|
|
|
def getUnixTime(self, line):
|
|
|
|
date = self.getTime(line)
|
|
|
|
if date == None:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return time.mktime(date)
|
|
|
|
|
2006-09-10 20:43:13 +00:00
|
|
|
##
|
|
|
|
# Sort the template lists using the hits score. This method is not called
|
|
|
|
# in this object and thus should be called from time to time.
|
|
|
|
|
2006-09-05 21:17:35 +00:00
|
|
|
def sortTemplate(self):
|
2008-01-16 22:55:04 +00:00
|
|
|
self.__lock.acquire()
|
2007-02-25 23:53:22 +00:00
|
|
|
try:
|
|
|
|
logSys.debug("Sorting the template list")
|
2007-12-16 21:38:04 +00:00
|
|
|
self.__templates.sort(lambda x, y: cmp(x.getHits(), y.getHits()))
|
|
|
|
self.__templates.reverse()
|
2007-02-25 23:53:22 +00:00
|
|
|
finally:
|
|
|
|
self.__lock.release()
|