mirror of https://github.com/fail2ban/fail2ban
- a few changes and corrections
git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@9 a942ae1a-1317-0410-a47c-b1dcaea8d6050.6
parent
0de46864a1
commit
68ab4b0b26
|
@ -24,24 +24,54 @@ __date__ = "$Date$"
|
||||||
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
||||||
__license__ = "GPL"
|
__license__ = "GPL"
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
class Firewall:
|
class Firewall:
|
||||||
|
|
||||||
banList = dict()
|
banList = dict()
|
||||||
|
|
||||||
def addBanIP(self, ip, time):
|
def __init__(self, banTime):
|
||||||
self.banList[ip] = time
|
self.banTime = banTime
|
||||||
|
|
||||||
|
def addBanIP(self, ip):
|
||||||
|
if not self.inBanList(ip):
|
||||||
|
self.banList[ip] = time.time()
|
||||||
|
self.executeCmd(self.banIP(ip))
|
||||||
|
else:
|
||||||
|
print ip, "already in ban list"
|
||||||
|
|
||||||
def delBanIP(self, ip):
|
def delBanIP(self, ip):
|
||||||
del self.banList[ip]
|
if self.inBanList(ip):
|
||||||
|
del self.banList[ip]
|
||||||
|
self.executeCmd(self.unBanIP(ip))
|
||||||
|
else:
|
||||||
|
print ip, "not in ban list"
|
||||||
|
|
||||||
|
def inBanList(self, ip):
|
||||||
|
return self.banList.has_key(ip)
|
||||||
|
|
||||||
|
def checkForUnBan(self):
|
||||||
|
""" Check for user to remove from ban list.
|
||||||
|
"""
|
||||||
|
banListTemp = self.banList.copy()
|
||||||
|
iterBanList = banListTemp.iteritems()
|
||||||
|
for i in range(len(self.banList)):
|
||||||
|
element = iterBanList.next()
|
||||||
|
ip = element[0]
|
||||||
|
btime = element[1]
|
||||||
|
if btime < time.time()-self.banTime:
|
||||||
|
self.delBanIP(ip)
|
||||||
|
print '`->', time.time()
|
||||||
|
|
||||||
def flushBanList(self):
|
def flushBanList(self):
|
||||||
iterBanList = self.banList.iteritems()
|
iterBanList = self.banList.iteritems()
|
||||||
for i in range(len(self.banList)):
|
for i in range(len(self.banList)):
|
||||||
element = iterBanList.next()
|
element = iterBanList.next()
|
||||||
ip = element[0]
|
ip = element[0]
|
||||||
self.unBanIP(ip)
|
self.delBanIP(ip)
|
||||||
|
|
||||||
def executeCmd(self, cmd):
|
def executeCmd(self, cmd):
|
||||||
|
print cmd
|
||||||
return #os.system(cmd)
|
return #os.system(cmd)
|
||||||
|
|
||||||
def viewBanList(self):
|
def viewBanList(self):
|
||||||
|
|
|
@ -28,14 +28,10 @@ from firewall import Firewall
|
||||||
|
|
||||||
class Iptables(Firewall):
|
class Iptables(Firewall):
|
||||||
|
|
||||||
def banIP(self, ip, time):
|
def banIP(self, ip):
|
||||||
query = 'iptables -I INPUT 1 -i eth0 -s '+str(ip)+' -j DROP'
|
query = 'iptables -I INPUT 1 -i eth0 -s '+ip+' -j DROP'
|
||||||
self.addBanIP(ip, time)
|
return query
|
||||||
self.executeCmd(query)
|
|
||||||
print query
|
|
||||||
|
|
||||||
def unBanIP(self, ip):
|
def unBanIP(self, ip):
|
||||||
query = 'iptables -D INPUT -i eth0 -s '+str(ip)+' -j DROP'
|
query = 'iptables -D INPUT -i eth0 -s '+ip+' -j DROP'
|
||||||
self.delBanIP(ip)
|
return query
|
||||||
self.executeCmd(query)
|
|
||||||
print query
|
|
||||||
|
|
Loading…
Reference in New Issue