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
|
########################################################
# Please file all bug reports, patches, and feature
# requests under:
# https://sourceforge.net/p/logwatch/_list/tickets
# Help requests and discusion can be filed under:
# https://sourceforge.net/p/logwatch/discussion/
########################################################
#######################################################
## Copyright (c) 2021-2022 Orion Poplawski
## 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 warnings;
use strict;
use Logwatch ':all';
my $Debug = $ENV{'LOGWATCH_DEBUG'} || 0;
my $Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'} || 0;
my $IgnoreHost = $ENV{'ignore_host'} || "";
my $IllegalUsersThreshold = $ENV{'illegal_users_threshold'} || 0;
$main::DoLookup = $ENV{'sshd_ip_lookup'};
my $DebugCounter = 0;
#Init String Containers
my (
$Host, $Key, $Method,
$Port, $User, $user,
);
my %Users = ();
my %NonexistentUsers = ();
my %BadPassword = ();
my %OtherList = ();
my $NetworkErrors = 0;
my $Kills = 0;
while (defined(my $ThisLine = <STDIN>)) {
chomp($ThisLine);
if (
($ThisLine =~ /Connection reset by peer/) or
($ThisLine =~ /^Child connection from/) or
($ThisLine =~ /Disconnect received/) or
($ThisLine =~ /Exited normally/) or
($ThisLine =~ /Not backgrounding/) or
0 # This line prevents blame shifting as lines are added above
) {
# Ignore these
} elsif ( ($Method,$User,$Host,$Port) = ($ThisLine =~ /^(\S+) auth succeeded for '(.*)' from ([\d\.:a-f]+)(?:%\w+)?:(\d+)/) ) {
if ($Detail >= 20) {
$Users{$User}{$Host}{$Method}++;
} else {
if ( $Host !~ /$IgnoreHost/ ) {
$Users{$User}{$Host}{"(all)"}++;
}
}
} elsif ( ($Method,$User,$Key,$Host,$Port) = ($ThisLine =~ /^(\S+) auth succeeded for '(.*)' with.* key (.*) from ([\d\.:a-f]+)(?:%\w+)?:(\d+)/) ) {
if ($Detail >= 20) {
$Users{$User}{$Host}{$Method . " ($Key)"}++;
} else {
if ( $Host !~ /$IgnoreHost/ ) {
$Users{$User}{$Host}{"(all)"}++;
}
}
} elsif (
($ThisLine =~ m/^Error reading/ ) or
0
) {
$NetworkErrors++;
} elsif ( ($User,$Host) = ($ThisLine =~ /^Bad password attempt for '(.*)' from ([^ ]*):/)) {
$BadPassword{$Host}{$User}++;
} elsif ( ($Host) = ($ThisLine =~ /^Login attempt for nonexistent user from ([^ ]*):/)) {
$NonexistentUsers{$Host}++;
} elsif ( $ThisLine =~ /^Login attempt for nonexistent user/) {
$NonexistentUsers{"unknown"}++;
} elsif ( $ThisLine =~ m/Terminated by signal/) {
$Kills++;
} else {
$OtherList{$ThisLine} += 1;
}
}
###########################################################
sub timesplural {
my ($count) = @_;
my $plural = ($count > 1) ? "s" : "";
return "$count Time$plural\n";
}
if ($NetworkErrors) {
print "\nNetwork Read Write Errors: " . $NetworkErrors . "\n";
}
if ($Kills) {
print "\nKilled: " . timesplural($Kills);
}
if (keys %BadPassword) {
print "\nBad password attempts:\n";
foreach my $ip (sort SortIP keys %BadPassword) {
my $name = LookupIP($ip);
my $totcount = 0;
foreach my $user (keys %{$BadPassword{$ip}}) {
$totcount += $BadPassword{$ip}{$user};
}
print " $name: ". timesplural($totcount);
if ($Detail >= 5) {
my $sort = CountOrder(%{$BadPassword{$ip}});
foreach my $user (sort $sort keys %{$BadPassword{$ip}}) {
my $val = $BadPassword{$ip}{$user};
print " $user: " . timesplural($val);
}
}
}
}
if (keys %NonexistentUsers) {
print "\nNon-existent users from";
if ($IllegalUsersThreshold) {
print " (with threshold >= $IllegalUsersThreshold)";
}
print ":\n";
foreach my $ip (sort SortIP keys %NonexistentUsers) {
my $name = LookupIP($ip);
print " $name: " . timesplural($NonexistentUsers{$ip});
}
}
if (keys %Users) {
print "\nUsers logging in through sshd:\n";
foreach my $user (sort {$a cmp $b} keys %Users) {
print " $user:\n";
if ($Detail < 20) {
my $totalSort = TotalCountOrder(%{$Users{$user}}, \&SortIP);
foreach my $ip (sort $totalSort keys %{$Users{$user}}) {
my $name = LookupIP($ip);
my $val = (values %{$Users{$user}{$ip}})[0];
print " $name: " . timesplural($val);
}
} else {
my %Methods = ();
foreach my $ip (keys %{$Users{$user}}) {
foreach my $method (keys %{$Users{$user}{$ip}}) {
$Methods{$method}{$ip} = $Users{$user}{$ip}{$method};
}
}
if (scalar keys %{$Users{$user}} < scalar %Methods) {
my $totalSort = TotalCountOrder(%{$Users{$user}}, \&SortIP);
foreach my $ip (sort $totalSort keys %{$Users{$user}}) {
my $name = LookupIP($ip);
print " $name:\n";
my $sort = CountOrder(%{$Users{$user}{$ip}});
foreach my $method (sort $sort keys %{$Users{$user}{$ip}}) {
my $val = $Users{$user}{$ip}{$method};
print " $method: " . timesplural($val);
}
}
} else {
my $totalSort = TotalCountOrder(%Methods);
foreach my $method (sort $totalSort keys %Methods) {
print " $method:\n";
my $sort = CountOrder(%{$Methods{$method}});
foreach my $ip (sort $sort keys %{$Methods{$method}}) {
my $name = LookupIP($ip);
my $val = (values %{$Users{$user}{$ip}})[0];
print " $name: " . timesplural($val);
}
}
}
}
}
}
if (keys %OtherList) {
print "\n**Unmatched Entries**\n";
print "$_ : " . timesplural($OtherList{$_}) foreach sort keys %OtherList;
}
exit(0);
# vi: shiftwidth=3 tabstop=3 syntax=perl et
# Local Variables:
# mode: perl
# perl-indent-level: 3
# indent-tabs-mode: nil
# End:
|