<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.itcollege.ee/index.php?action=history&amp;feed=atom&amp;title=Skriptimiskeeled_aine_aruanded_2010_s%C3%BCgis%2FSigmarMuuga%2FPython</id>
	<title>Skriptimiskeeled aine aruanded 2010 sügis/SigmarMuuga/Python - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.itcollege.ee/index.php?action=history&amp;feed=atom&amp;title=Skriptimiskeeled_aine_aruanded_2010_s%C3%BCgis%2FSigmarMuuga%2FPython"/>
	<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Skriptimiskeeled_aine_aruanded_2010_s%C3%BCgis/SigmarMuuga/Python&amp;action=history"/>
	<updated>2026-05-06T07:14:36Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Skriptimiskeeled_aine_aruanded_2010_s%C3%BCgis/SigmarMuuga/Python&amp;diff=22896&amp;oldid=prev</id>
		<title>Smuuga: Created page with &#039;== Sigmar Muuga Python == ===Kodutöö===  Skript, mis vaatab /var/log/auth.log faili ja võtab sealt enim ebaõnnestunud IP aadressid, mis üritasid teha sisse logimist SSH kaud…&#039;</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Skriptimiskeeled_aine_aruanded_2010_s%C3%BCgis/SigmarMuuga/Python&amp;diff=22896&amp;oldid=prev"/>
		<updated>2011-01-28T09:44:57Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;#039;== Sigmar Muuga Python == ===Kodutöö===  Skript, mis vaatab /var/log/auth.log faili ja võtab sealt enim ebaõnnestunud IP aadressid, mis üritasid teha sisse logimist SSH kaud…&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Sigmar Muuga Python ==&lt;br /&gt;
===Kodutöö===&lt;br /&gt;
&lt;br /&gt;
Skript, mis vaatab /var/log/auth.log faili ja võtab sealt enim ebaõnnestunud IP aadressid, mis üritasid teha sisse logimist SSH kaudu(lävend võiks näiteks olla 10 ebaõnnestunud katset) ning saadab need kas e-mailile või lisab tulemüüri reeglitesse(IPTABLES näiteks). Jah, ma tean, et on ka paremaid viise hoste blokeerida, näiteks kasutada hosts-allow parameetrit ssh konfiguratsioonis.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
# -*- coding: latin-1 -*-&lt;br /&gt;
 &lt;br /&gt;
import sys&lt;br /&gt;
import getopt&lt;br /&gt;
import os&lt;br /&gt;
import re&lt;br /&gt;
 &lt;br /&gt;
# Skript, mis loeb läbi UNIX-i auth.log faili ja koostab IPTables block reeglid IP aadressidele,&lt;br /&gt;
# mille pealt on autentimine ebaõnnestunud vähemalt etteantud arv kordi&lt;br /&gt;
 &lt;br /&gt;
# käivitamise näide:&lt;br /&gt;
# py check_ip.py --logfile=/path/to/logfile --iptablesfile=/path/to/iptablesfile&lt;br /&gt;
 &lt;br /&gt;
# Autor: Sigmar Muuga, DK31&lt;br /&gt;
 &lt;br /&gt;
def main():&lt;br /&gt;
    # mitme ebaõnnestumise korral me IP blokeerime&lt;br /&gt;
    BLOCK_LEVEL=5&lt;br /&gt;
 &lt;br /&gt;
    # kuvab kasutamise info&lt;br /&gt;
    def usage():&lt;br /&gt;
        print &amp;#039;Programmi kasutamine:&amp;#039;&lt;br /&gt;
        print &amp;#039;py check_ip.py --logfile=/path/to/logfile --iptablesfile=/path/to/iptablesfile&amp;#039;&lt;br /&gt;
 &lt;br /&gt;
    # loendab stringi esinemised etteantud failis&lt;br /&gt;
    def matchCountInFile(str, filename):&lt;br /&gt;
        log_file = open(filename, &amp;#039;r&amp;#039;)&lt;br /&gt;
        count = 0&lt;br /&gt;
        for line in log_file:&lt;br /&gt;
            if re.search(str, line):&lt;br /&gt;
                count=count+1&lt;br /&gt;
        return count&lt;br /&gt;
 &lt;br /&gt;
    try:&lt;br /&gt;
        # loeme ja kontrollime getopti abil argumendid&lt;br /&gt;
        opts, args = getopt.getopt(sys.argv[1:], &amp;quot;li:v&amp;quot;, [&amp;quot;logfile=&amp;quot;, &amp;quot;iptablesfile=&amp;quot;])&lt;br /&gt;
        AUTH_LOG_FILE = &amp;#039;&amp;#039;&lt;br /&gt;
        IP_TABLES_FILE = &amp;#039;&amp;#039;&lt;br /&gt;
        for opt, arg in opts:&lt;br /&gt;
            if opt == &amp;#039;--logfile&amp;#039;:&lt;br /&gt;
                AUTH_LOG_FILE = arg&lt;br /&gt;
            elif opt == &amp;#039;--iptablesfile&amp;#039;:&lt;br /&gt;
                IP_TABLES_FILE = arg&lt;br /&gt;
 &lt;br /&gt;
        print &amp;#039;AUTH_LOG_FILE=&amp;#039; + AUTH_LOG_FILE&lt;br /&gt;
        print &amp;#039;IP_TABLES_FILE=&amp;#039; + IP_TABLES_FILE&lt;br /&gt;
 &lt;br /&gt;
        if AUTH_LOG_FILE.__len__() &amp;lt; 1:&lt;br /&gt;
            raise Exception(&amp;#039;Invalid auth log filename&amp;#039;)&lt;br /&gt;
        if IP_TABLES_FILE.__len__() &amp;lt; 1:&lt;br /&gt;
            raise Exception(&amp;#039;Invalid iptables filename&amp;#039;)&lt;br /&gt;
 &lt;br /&gt;
        if not os.path.isfile(AUTH_LOG_FILE):&lt;br /&gt;
            raise Exception(&amp;#039;Auth log file does not exist&amp;#039;)&lt;br /&gt;
 &lt;br /&gt;
        ip_pattern = re.compile(&amp;#039;([0-9]{1,3}\.){3}[0-9]{1,3}&amp;#039;)&lt;br /&gt;
 &lt;br /&gt;
        # valmistame logifaili lugemiseks ette&lt;br /&gt;
        log_file = open(AUTH_LOG_FILE, &amp;#039;r&amp;#039;)&lt;br /&gt;
        ip_address_array = []&lt;br /&gt;
        ip_tables_filehandle = None&lt;br /&gt;
        # itereerime yle logiridade&lt;br /&gt;
        for log_line in log_file:&lt;br /&gt;
            # read, mis on veaga&lt;br /&gt;
            if (re.search(&amp;#039;error&amp;#039;, log_line) != None or re.search(&amp;#039;illegal&amp;#039;,  log_line) != None or re.search(&amp;#039;not allowed&amp;#039;, log_line) != None):&lt;br /&gt;
                ip_address_match = re.search(ip_pattern, log_line)&lt;br /&gt;
                if (ip_address_match == None):&lt;br /&gt;
                    continue&lt;br /&gt;
                # leiame rea pealt IP aadressi&lt;br /&gt;
                ip_address = ip_address_match.group(0)&lt;br /&gt;
                occurences = matchCountInFile(ip_address, AUTH_LOG_FILE)&lt;br /&gt;
                # vajadusel blacklistime&lt;br /&gt;
                if occurences &amp;gt;= BLOCK_LEVEL and not ip_address in ip_address_array:&lt;br /&gt;
                    ip_address_array.append(ip_address)&lt;br /&gt;
                    print &amp;quot;Blacklisting &amp;quot; + ip_address + &amp;quot; with &amp;quot; + str(occurences) + &amp;quot; occurences&amp;quot;&lt;br /&gt;
                    if ip_tables_filehandle == None:&lt;br /&gt;
                        ip_tables_filehandle = open(IP_TABLES_FILE, &amp;#039;w&amp;#039;)&lt;br /&gt;
                    ip_tables_filehandle.write(&amp;quot;iptables -A INPUT -s &amp;quot; + ip_address + &amp;quot; -j DROP\n&amp;quot;)&lt;br /&gt;
        log_file.close()&lt;br /&gt;
 &lt;br /&gt;
        if (ip_tables_filehandle != None):&lt;br /&gt;
            ip_tables_filehandle.close()&lt;br /&gt;
 &lt;br /&gt;
    except getopt.GetoptError, err:&lt;br /&gt;
        print str(err)&lt;br /&gt;
        usage()&lt;br /&gt;
        sys.exit(2)&lt;br /&gt;
 &lt;br /&gt;
# k2ivitame meetodi&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Smuuga</name></author>
	</entry>
</feed>