2005-07-01 09:30:52 +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
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
# Author: Cyril Jaquier
|
|
|
|
#
|
|
|
|
# $Revision$
|
|
|
|
|
|
|
|
__author__ = "Cyril Jaquier"
|
|
|
|
__version__ = "$Revision$"
|
|
|
|
__date__ = "$Date$"
|
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
__license__ = "GPL"
|
|
|
|
|
2005-08-01 16:35:18 +00:00
|
|
|
import time, os, logging, re
|
2005-07-05 12:57:20 +00:00
|
|
|
|
2005-07-07 16:52:54 +00:00
|
|
|
from utils.process import executeCmd
|
2005-07-12 13:08:24 +00:00
|
|
|
from utils.strings import replaceTag
|
2005-07-07 16:52:54 +00:00
|
|
|
|
2005-08-01 16:35:18 +00:00
|
|
|
# Gets the instance of the logger.
|
|
|
|
logSys = logging.getLogger("fail2ban")
|
2005-07-01 09:30:52 +00:00
|
|
|
|
|
|
|
class Firewall:
|
|
|
|
""" Manages the ban list and executes the command that ban
|
|
|
|
the IP.
|
|
|
|
"""
|
|
|
|
|
2005-07-07 16:52:54 +00:00
|
|
|
def __init__(self, banRule, unBanRule, banTime):
|
2005-07-05 12:57:20 +00:00
|
|
|
self.banRule = banRule
|
|
|
|
self.unBanRule = unBanRule
|
2005-07-01 09:30:52 +00:00
|
|
|
self.banTime = banTime
|
2005-07-15 14:07:08 +00:00
|
|
|
self.banList = dict()
|
2005-07-01 09:30:52 +00:00
|
|
|
|
2005-07-09 15:11:48 +00:00
|
|
|
def addBanIP(self, aInfo, debug):
|
2005-07-01 09:30:52 +00:00
|
|
|
""" Bans an IP.
|
|
|
|
"""
|
2005-07-09 15:11:48 +00:00
|
|
|
ip = aInfo["ip"]
|
2005-07-01 09:30:52 +00:00
|
|
|
if not self.inBanList(ip):
|
2005-07-09 15:11:48 +00:00
|
|
|
crtTime = time.time()
|
|
|
|
logSys.warn("Ban " + ip)
|
|
|
|
self.banList[ip] = crtTime
|
|
|
|
aInfo["bantime"] = crtTime
|
|
|
|
executeCmd(self.banIP(aInfo), debug)
|
2005-07-01 09:30:52 +00:00
|
|
|
else:
|
2005-07-05 12:57:20 +00:00
|
|
|
logSys.error(ip+" already in ban list")
|
2005-07-01 09:30:52 +00:00
|
|
|
|
2005-07-09 15:11:48 +00:00
|
|
|
def delBanIP(self, aInfo, debug):
|
2005-07-01 09:30:52 +00:00
|
|
|
""" Unban an IP.
|
|
|
|
"""
|
2005-07-09 15:11:48 +00:00
|
|
|
ip = aInfo["ip"]
|
2005-07-01 09:30:52 +00:00
|
|
|
if self.inBanList(ip):
|
2005-07-05 12:57:20 +00:00
|
|
|
logSys.warn("Unban "+ip)
|
2005-07-01 09:30:52 +00:00
|
|
|
del self.banList[ip]
|
2005-07-09 15:11:48 +00:00
|
|
|
executeCmd(self.unBanIP(aInfo), debug)
|
2005-07-01 09:30:52 +00:00
|
|
|
else:
|
2005-07-05 12:57:20 +00:00
|
|
|
logSys.error(ip+" not in ban list")
|
2005-07-01 09:30:52 +00:00
|
|
|
|
|
|
|
def inBanList(self, ip):
|
|
|
|
""" Checks if IP is in ban list.
|
|
|
|
"""
|
|
|
|
return self.banList.has_key(ip)
|
|
|
|
|
|
|
|
def checkForUnBan(self, debug):
|
|
|
|
""" Check for IP to remove from ban list.
|
|
|
|
"""
|
|
|
|
banListTemp = self.banList.copy()
|
|
|
|
for element in banListTemp.iteritems():
|
|
|
|
btime = element[1]
|
|
|
|
if btime < time.time()-self.banTime:
|
2005-07-09 15:11:48 +00:00
|
|
|
aInfo = {"ip": element[0],
|
|
|
|
"bantime": btime,
|
|
|
|
"unbantime": time.time()}
|
|
|
|
self.delBanIP(aInfo, debug)
|
2005-07-01 09:30:52 +00:00
|
|
|
|
|
|
|
def flushBanList(self, debug):
|
|
|
|
""" Flushes the ban list and of course the firewall rules.
|
|
|
|
Called when fail2ban exits.
|
|
|
|
"""
|
|
|
|
banListTemp = self.banList.copy()
|
|
|
|
for element in banListTemp.iteritems():
|
2005-07-09 15:11:48 +00:00
|
|
|
aInfo = {"ip": element[0],
|
|
|
|
"bantime": element[1],
|
|
|
|
"unbantime": time.time()}
|
|
|
|
self.delBanIP(aInfo, debug)
|
2005-07-05 12:57:20 +00:00
|
|
|
|
2005-07-09 15:11:48 +00:00
|
|
|
def banIP(self, aInfo):
|
2005-07-05 12:57:20 +00:00
|
|
|
""" Returns query to ban IP.
|
|
|
|
"""
|
2005-07-12 13:08:24 +00:00
|
|
|
query = replaceTag(self.banRule, aInfo)
|
2005-07-05 12:57:20 +00:00
|
|
|
return query
|
|
|
|
|
2005-07-09 15:11:48 +00:00
|
|
|
def unBanIP(self, aInfo):
|
2005-07-05 12:57:20 +00:00
|
|
|
""" Returns query to unban IP.
|
|
|
|
"""
|
2005-07-12 13:08:24 +00:00
|
|
|
query = replaceTag(self.unBanRule, aInfo)
|
2005-07-05 12:57:20 +00:00
|
|
|
return query
|
|
|
|
|
2005-07-01 09:30:52 +00:00
|
|
|
def viewBanList(self):
|
|
|
|
""" Prints the ban list on screen. Usefull for debugging.
|
|
|
|
"""
|
|
|
|
for element in self.banList.iteritems():
|
|
|
|
print element
|