File: fail2ban

package info (click to toggle)
logwatch 7.4.3%2Bgit20161207-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,064 kB
  • ctags: 474
  • sloc: perl: 9,204; sh: 231; makefile: 38
file content (254 lines) | stat: -rw-r--r-- 10,208 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
##########################################################################
# $Id$
##########################################################################
# $Log: fail2ban,v $
# Revision 1.5  2008/08/18 16:07:46  mike
# Patches from Paul Gear <paul at libertysys.com> -mgt
#
# Revision 1.4  2008/06/30 23:07:51  kirk
# fixed copyright holders for files where I know who they should be
#
# Revision 1.3  2008/03/24 23:31:26  kirk
# added copyright/license notice to each script
#
# Revision 1.2  2006/12/15 04:53:59  bjorn
# Additional filtering, by Willi Mann.
#
# Revision 1.1  2006/05/30 19:04:26  bjorn
# Added fail2ban service, written by Yaroslav Halchenko.
#
# Written by Yaroslav Halchenko <debian@onerussian.com> for fail2ban
#
##########################################################################

########################################################
## Copyright (c) 2008  Yaroslav Halchenko
## Covered under the included MIT/X-Consortium License:
##    http://www.opensource.org/licenses/mit-license.php
## All modifications and contributions by other persons to
## this script are assumed to have been donated to the
## Logwatch project and thus assume the above copyright
## and licensing terms.  If you want to make contributions
## under your own copyright or a different license this
## must be explicitly stated in the contribution an the
## Logwatch project reserves the right to not accept such
## contributions.  If you have made significant
## contributions to this script and want to claim
## copyright please contact logwatch-devel@lists.sourceforge.net.
#########################################################

use strict;
use Logwatch ':all';

my $Debug = $ENV{'LOGWATCH_DEBUG'} || 0;
my $Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'} || 0;
my $IgnoreHost = $ENV{'sshd_ignore_host'} || "";
my $DebugCounter = 0;
my $ReInitializations = 0;
my @IptablesErrors = ();
my @ActionErrors = ();
my $NotValidIP = 0;             # reported invalid IPs number
my %ErrorList = ();
my %WarningList = ();
my %InfoList = ();
my %NoticeList = ();
my %OtherList = ();

my %ServicesBans = ();
my %ServicesFound = ();
my %ServicesIgnored = ();

if ( $Debug >= 5 ) {
        print STDERR "\n\nDEBUG: Inside Fail2Ban Filter \n\n";
        $DebugCounter = 1;
}

while (defined(my $ThisLine = <STDIN>)) {
    if ( $Debug >= 5 ) {
        print STDERR "DEBUG($DebugCounter): $ThisLine";
        $DebugCounter++;
    }
    chomp($ThisLine);
    if ( ($ThisLine =~ /..,... DEBUG: /) or
         ($ThisLine =~ /..,... \S*\s*: DEBUG /) or # syntax of 0.7.? fail2ban
         ($ThisLine =~ /..,... INFO: (Fail2Ban v.* is running|Exiting|Enabled sections:)/) or
         ($ThisLine =~ /INFO\s+Log rotation detected for/) or
         ($ThisLine =~ /INFO\s+Jail.+(?:stopped|started|uses )/) or
         ($ThisLine =~ /INFO\s+Changed logging target to/) or
         ($ThisLine =~ /INFO\s+Creating new jail/) or
         ($ThisLine =~ /INFO\s+(Set |Socket|Exiting|Gamin|Created|Added|Using|Connected to |rollover performed)/) or # syntax of 0.7.? fail2ban
         ($ThisLine =~ /..,... WARNING: Verbose level is /) or
         ($ThisLine =~ /..,... WARNING: Restoring firewall rules/) or
         ($ThisLine =~ /WARNING Determined IP using DNS Lookup: [^ ]+ = \['[^']+'\]/) or
         ($ThisLine =~ /INFO\s+(Stopping all jails|Exiting Fail2ban)/) or
         ($ThisLine =~ /INFO\s+Initiated '.*' backend/) or
         ($ThisLine =~ /INFO\s+(Added logfile = .*|Set maxRetry = \d+|Set findtime = \d+|Set banTime = \d+)/) or
         ($ThisLine =~ /Unable to find a corresponding IP address for .*: \[Errno -2\] Name or service not known/)
       )
    {
        if ( $Debug >= 6 ) {
            print STDERR "DEBUG($DebugCounter): line ignored\n";
        }
    } elsif ( my ($Service,$Action,$Host) = ($ThisLine =~ m/(?:WARNING|NOTICE):?\s+\[?(.*?)[]:]?\s(Ban|Unban)[^\.]* (\S+)/)) {
        if ( $Debug >= 6 ) {
            print STDERR "DEBUG($DebugCounter): Found $Action for $Service from $Host\n";
        }
        $ServicesBans{$Service}{$Host}{$Action}++;
        $ServicesBans{$Service}{"(all)"}{$Action}++;
    } elsif ( my ($Service,$Host,$NumFailures) = ($ThisLine =~ m/INFO: (\S+): (.+) has (\d+) login failure\(s\). Banned./)) {
        if ($Debug >= 4) {
            print STDERR "DEBUG: Found host $Host trying to access $Service - failed $NumFailures times\n";
        }
        push @{$ServicesBans{$Service}{$Host}{'Failures'}}, $NumFailures;
    } elsif ( my ($Service,$Host) = ($ThisLine =~ m/ ERROR:\s(.*):\s(\S+)\salready in ban list/)) {
        $ServicesBans{$Service}{$Host}{'AlreadyInTheList'}++;
    } elsif ( my ($Service,$Host) = ($ThisLine =~ m/(?:INFO|WARNING|NOTICE)\s*\[(.*)\]\s*(\S+)\s*already banned/)) {
        $ServicesBans{$Service}{$Host}{'AlreadyInTheList'}++;
    } elsif ( my ($Service,$Host) = ($ThisLine =~ m/ WARNING:\s(.*):\sReBan (\S+)/)) {
        $ServicesBans{$Service}{$Host}{'ReBan'}++;
    } elsif ($ThisLine =~ / ERROR:?\s*(Execution of command )?\'?iptables/) {
        push @IptablesErrors, "$ThisLine\n";
    } elsif ($ThisLine =~ /ERROR.*returned \d+$/) {
        push @ActionErrors, "$ThisLine\n";
    } elsif (($ThisLine =~ /..,... WARNING: \#\S+ reinitialization of firewalls/) or
            ($ThisLine =~ / ERROR\s*Invariant check failed. Trying to restore a sane environment/)) {
        $ReInitializations++;
    } elsif ($ThisLine =~ /..,... WARNING:  is not a valid IP address/) {
        # just ignore - this will be fixed within fail2ban and is harmless warning
    } elsif ( my ($Service,$Host) = ($ThisLine =~ /INFO\s+\[(.*)\] Found (.*)/)) {
        $ServicesFound{$Service}{$Host}++;
    } elsif ( my ($Service,$Host) = ($ThisLine =~ /INFO\s+\[(.*)\] Ignore (.*)/)) {
        $ServicesIgnored{$Service}{$Host}++;
    # Generice messages
    } elsif ( my ($Message) = ($ThisLine =~ / ERROR (.*)$/)) {
        $ErrorList{$Message}++;
    } elsif ( my ($Message) = ($ThisLine =~ / WARNING (.*)$/)) {
        $WarningList{$Message}++;
    } elsif ( my ($Message) = ($ThisLine =~ / INFO (.*)$/)) {
        $InfoList{$Message}++;
    } elsif ( my ($Message) = ($ThisLine =~ / NOTICE (.*)$/)) {
        $NoticeList{$Message}++;
    } else {
        # Report any unmatched entries...
        $OtherList{$ThisLine}++;
    }
}

###########################################################


if (keys %ServicesBans) {
    printf("\nBanned services with Fail2Ban:                             Bans:Unbans\n");
    foreach my $service (sort {$a cmp $b} keys %ServicesBans) {
        printf("   %-55s [%3d:%-3d]\n", "$service:",
               $ServicesBans{$service}{'(all)'}{'Ban'},
               $ServicesBans{$service}{'(all)'}{'Unban'});
        delete $ServicesBans{$service}{'(all)'};
        my $totalSort = TotalCountOrder(%{$ServicesBans{$service}}, \&SortIP);
        if ($Detail >= 5) {
            foreach my $ip (sort $totalSort keys %{$ServicesBans{$service}}) {
                my $name = LookupIP($ip);
                printf("      %-53s %3d:%-3d\n",
                       $name,
                       $ServicesBans{$service}{$ip}{'Ban'},
                       $ServicesBans{$service}{$ip}{'Unban'});
                if (($Detail >= 10) and ($ServicesBans{$service}{$ip}{'Failures'}>0)) {
                    print "      Failed ";
                    foreach my $fails (@{$ServicesBans{$service}{$ip}{'Failures'}}) {
                        print " $fails";
                    }
                    print " times";
                    printf("\n     %d Duplicate Ban attempts", $ServicesBans{$service}{$ip}{'AlreadyInTheList'}) ;
                    printf("\n     %d ReBans due to rules reinitilizations", $ServicesBans{$service}{$ip}{'ReBan'}) ;
                    print "\n";
                }
            }
        }
    }
}

if (keys %ServicesFound and $Detail>5) {
    printf("\nFail2Ban hosts found:\n");
    foreach my $service (sort {$a cmp $b} keys %ServicesFound) {
        print("    $service:\n");
        foreach my $ip (sort {$a cmp $b} keys %{$ServicesFound{$service}}) {
            printf("       %-15s (%3d Times)\n", "$ip",
                   $ServicesFound{$service}{$ip});
        }
    }
}

if (keys %ServicesIgnored and $Detail>5) {
    printf("\nFail2Ban hosts ignored:\n");
    foreach my $service (sort {$a cmp $b} keys %ServicesIgnored) {
        print("    $service:\n");
        foreach my $ip (sort {$a cmp $b} keys %{$ServicesIgnored{$service}}) {
            printf("       %-15s (%3d Times)\n", "$ip",
                   $ServicesIgnored{$service}{$ip});
        }
    }
}

if (keys(%ErrorList)) {
    print "\n** ERRORS **\n";
    foreach my $line (sort {$a cmp $b} keys %ErrorList) {
        print "   $line: $ErrorList{$line} Time(s)\n";
    }
}

if (keys(%WarningList)) {
    print "\n** WARNINGS **\n";
    foreach my $line (sort {$a cmp $b} keys %WarningList) {
        print "   $line: $WarningList{$line} Time(s)\n";
    }
}

if (keys(%InfoList) && $Detail>5) {
    print "\nInformational Messages:\n";
    foreach my $line (sort {$a cmp $b} keys %InfoList) {
        print "   $line: $InfoList{$line} Time(s)\n";
    }
}

if (keys(%NoticeList) && $Detail>7) {
    print "\nNotices:\n";
    foreach my $line (sort {$a cmp $b} keys %OtherList) {
        print "   $line: $OtherList{$line} Time(s)\n";
    }
}

if ($Detail>0) {
    if ($#IptablesErrors > 0) {
        printf("\n%d faulty iptables invocation(s)", $#IptablesErrors);
        if ($Detail > 5) {
            print ":\n";
            print @IptablesErrors ;
        }
    }
    if ($#ActionErrors > 0) {
        printf("\n%d error(s) returned from actions", $#ActionErrors);
        if ($Detail > 5) {
            print ":\n";
            print @ActionErrors ;
        }
    }
    if ($ReInitializations > 0) {
        printf("\n%d fail2ban rules reinitialization(s)", $ReInitializations);
    }
}

if (keys(%OtherList)) {
    print "\n**Unmatched Entries**\n";
    foreach my $line (sort {$a cmp $b} keys %OtherList) {
        print "   $line: $OtherList{$line} Time(s)\n";
    }
}

exit(0);

# vi: shiftwidth=3 tabstop=3 syntax=perl et
# Local Variables:
# mode: perl
# perl-indent-level: 3
# indent-tabs-mode: nil
# End: