BanScript in Python

    Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

    • BanScript in Python

      Hi :)

      Weil ich grad nicht wirklich weiss wo hin damit, post ichs hier mal rein.
      Das Script ist wie der Name schon sagt ein kleines Script was in Python realisiert und auf Linux ausgeführt einen "IP-Ban" tätigt.

      Das ganze ist recht einfach aufgebaut:
      - Iptables flushen um mehrfachadds zu verhindern
      - verbindung zu MySQL-Datenbank aufbauen
      - Die zu bannenden IPs holen
      - einen IPtables-Eintrag hinzufügen, der eine Weiterverarbeitung der ankommenden Pakete der IP verhindert

      Vorrausetzungen:
      - MySQL
      - Python
      - Iptables
      - Python-MySQL Library

      Die DB-Structure

      Quellcode

      1. CREATE TABLE `BanList` (
      2. `IP` text NOT NULL,
      3. PRIMARY KEY (`IP`(30))
      4. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
      Das Script

      Quellcode

      1. #! /usr/bin/env python
      2. # -*- coding: utf-8 -*-
      3. import MySQLdb
      4. import os
      5. print "AutoBan Script V0.1"
      6. print "\n"
      7. print "Open Connection to DB"
      8. mysql = MySQLdb.connect(
      9. host="127.0.0.1",
      10. db="DATENBANK_NAME",
      11. user="USERNAME", passwd="PASSWORD"
      12. )
      13. cursor = mysql.cursor()
      14. cursor.execute("SELECT * FROM `BanList`")
      15. IP = cursor.fetchall()
      16. numrows = int(cursor.rowcount)
      17. #Flush the Tables, prevents for doubleadd
      18. os.system("/sbin/iptables --flush")
      19. #Count down and write down
      20. i = 0
      21. while i < numrows:
      22. temp=str(IP[i])
      23. le = len(temp)-3
      24. defstr= ("/sbin/iptables -A INPUT -s "+ temp[2:le] +" -j DROP")
      25. print (defstr)
      26. os.system(defstr)
      27. #outfile.write (defstr)
      28. i = i + 1
      Alles anzeigen
      Selbstverständlich kann man das Script auch auf mehreren Computern ausführen und dabei eine einzige DB auf einem Zentralroot haben.
      Spart arbeit, denn nun kann man Zentral auf eine DB zu greifen und muss nicht auf jeden root die Iptables seperat setzen.

      LG GiR-Blunti

      PS: Es hilft bei Synfloods nicht, aber die Jungs mit ihren kleinen Scantools für Apache, Console, usw kann man so schön sauber rauswerfen.
      Es werden halt von allen IPs die in dem Table sind die eingehenden Pakete verworfen.

      Dazu passend gleich noch ein kleines PythonScript um die IPs per Console in die DB eintragen zu können:

      writeip.py

      Quellcode

      1. #!/usr/bin/python
      2. import sys
      3. import MySQLdb
      4. print "Open Connection to DB"
      5. mysql = MySQLdb.connect(
      6. host="127.0.0.1",
      7. db="DATENBANK_NAME",
      8. user="USERNAME", passwd="PASSWORD"
      9. )
      10. if (len(sys.argv) > 1):
      11. cursor = mysql.cursor()
      12. cursor.execute("INSERT INTO BanList VALUES (%s)", sys.argv[1])
      13. defstr= ("/sbin/iptables -A INPUT -s "+ sys.argv[1] +" -j DROP")
      14. #print (defstr)
      15. os.system(defstr)
      16. else:
      17. print "Usage:"
      18. print "writeip.py <IP-to-BAN>"
      Alles anzeigen
      Die Benutzung ist recht easy:
      $writeip.py 127.0.0.1

      Das ganze kann man dazu verwenden um mit Fail2Ban die gesperrten IPs per CustomAction in die DB zu verfrachten um den anderen Roots eventuelle "angiffe" zu ersparen.

      So ein Fail2Ban-Acion kann so aussehen:

      /etc/fail2ban/action.d/write-to-mysql.conf

      Quellcode

      1. # $Revision: 658 $
      2. #
      3. [Definition]
      4. # Option: actionstart
      5. # Notes.: command executed once at the start of Fail2Ban.
      6. # Values: CMD
      7. #
      8. actionstart =
      9. # Option: actionstop
      10. # Notes.: command executed once at the end of Fail2Ban
      11. # Values: CMD
      12. #
      13. actionstop =
      14. # Option: actioncheck
      15. # Notes.: command executed once before each actionban command
      16. # Values: CMD
      17. #
      18. actioncheck =
      19. # Option: actionban
      20. # Notes.: command executed when banning an IP. Take care that the
      21. # command is executed with Fail2Ban user rights.
      22. # Tags: <ip> IP address
      23. # <failures> number of failures
      24. # <time> unix timestamp of the ban time
      25. # Values: CMD
      26. #
      27. actionban = python /root/scripts/writeip.py <ip>
      28. # Option: actionunban
      29. # Notes.: command executed when unbanning an IP. Take care that the
      30. # command is executed with Fail2Ban user rights.
      31. # Tags: <ip> IP address
      32. # <failures> number of failures
      33. # <time> unix timestamp of the ban time
      34. # Values: CMD
      35. #
      36. actionunban =
      37. [Init]
      38. # Defaut name of the chain
      39. #
      40. name = default
      41. # Option: port
      42. # Notes.: specifies port to monitor
      43. # Values: [ NUM | STRING ] Default:
      44. #
      45. port = ssh
      46. # Option: protocol
      47. # Notes.: internally used by config reader for interpolations.
      48. # Values: [ tcp | udp | icmp | all ] Default: tcp
      49. #
      50. protocol = tcp
      Alles anzeigen
      Viel Spaß damit.

      LG GiR-Blunti
    • Werbung zur Unterstützung des Forums ( Bitte AddBlocker deaktivieren )