diff --git a/fail2ban b/fail2ban index 2936021a..278a118f 100755 --- a/fail2ban +++ b/fail2ban @@ -20,9 +20,17 @@ # # $Revision$ +__author__ = "Cyril Jaquier" +__version__ = "$Revision$" +__date__ = "$Date$" +__copyright__ = "Copyright (c) 2004 Cyril Jaquier" +__license__ = "GPL" + import posix,sys,os import string,re,time +from firewall.iptables import Iptables + def checkForRoot(): """ Check for root user. """ @@ -32,6 +40,7 @@ def checkForRoot(): else: return False +# start: To be removed def executeCmd(cmd): return #os.system(cmd) @@ -44,6 +53,7 @@ def banIP(ip): iptables = 'iptables -I INPUT 1 -i eth0 -s '+ip+' -j DROP' executeCmd(iptables) print iptables +# end: def checkForUnBan(banList, currentTime, banTime): """ Check for user to remove from ban list. @@ -87,11 +97,21 @@ def parseLogLine(line): if __name__ == "__main__": + # start: For object oriented testing + f = Iptables() + f.banIP('11', 1231) + f.banIP('13', 1232) + f.banIP('13', 1233) + f.unBanIP('11') + f.viewBanList() + f.flushBanList() + # end: + if not checkForRoot(): print "You must be root." #sys.exit(-1) - logPath = './log/temp' + logPath = './log-test/test' banTime = 60 ignoreIPs = '127.0.0.1' diff --git a/firewall/__init__.py b/firewall/__init__.py new file mode 100644 index 00000000..76dba873 --- /dev/null +++ b/firewall/__init__.py @@ -0,0 +1,25 @@ +# 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" \ No newline at end of file diff --git a/firewall/firewall.py b/firewall/firewall.py new file mode 100644 index 00000000..0a937d77 --- /dev/null +++ b/firewall/firewall.py @@ -0,0 +1,52 @@ +# 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" + +class Firewall: + + banList = dict() + + def addBanIP(self, ip, time): + self.banList[ip] = time + + def delBanIP(self, ip): + del self.banList[ip] + + def flushBanList(self): + iterBanList = self.banList.iteritems() + for i in range(len(self.banList)): + element = iterBanList.next() + ip = element[0] + self.unBanIP(ip) + + def executeCmd(self, cmd): + return #os.system(cmd) + + def viewBanList(self): + iterBanList = self.banList.iteritems() + for i in range(len(self.banList)): + element = iterBanList.next() + print element + diff --git a/firewall/iptables.py b/firewall/iptables.py new file mode 100644 index 00000000..fcee9652 --- /dev/null +++ b/firewall/iptables.py @@ -0,0 +1,41 @@ +# 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" + +from firewall import Firewall + +class Iptables(Firewall): + + def banIP(self, ip, time): + query = 'iptables -I INPUT 1 -i eth0 -s '+str(ip)+' -j DROP' + self.addBanIP(ip, time) + self.executeCmd(query) + print query + + def unBanIP(self, ip): + query = 'iptables -D INPUT -i eth0 -s '+str(ip)+' -j DROP' + self.delBanIP(ip) + self.executeCmd(query) + print query