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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
#!/usr/bin/perl -w
##########################################################################
# $Id: sshd,v 1.38 2004/06/23 15:01:17 kirk Exp $
##########################################################################
# $Log: sshd,v $
# Revision 1.38 2004/06/23 15:01:17 kirk
# - Added more patches from blues@ds.pg.gda.pl
#
# Revision 1.37 2004/02/03 19:13:14 kirk
# More Solaris patches from Sean Boran <sean@boran.com>
#
# Revision 1.36 2004/02/03 18:39:34 kirk
# Patches from [ISO-8859-2] Pawe? Go?aszewski" <blues@ds.pg.gda.pl>
#
# Revision 1.35 2004/02/03 03:52:20 kirk
# Added mailscanner filter and more Solaris support from Mike Tremaine <mgt@stellarcore.net>
#
# Revision 1.34 2004/02/03 02:45:26 kirk
# Tons of patches, and new 'oidentd' and 'shaperd' filters from
# Pawe? Go?aszewski" <blues@ds.pg.gda.pl>
#
##########################################################################
########################################################
# This was written and is maintained by:
# Kirk Bauer <kirk@kaybee.org>
#
# Please send all comments, suggestions, bug reports,
# etc, to kirk@kaybee.org.
########################################################
use Logwatch ':all';
$Debug = ValueOrDefault($ENV{'LOGWATCH_DEBUG'}, 0);
$Detail = ValueOrDefault($ENV{'LOGWATCH_DETAIL_LEVEL'}, 0);
# Avoid "Use of uninitialized value" warning messages.
sub ValueOrDefault {
my ($value, $default) = @_;
return ($value ? $value : $default);
}
# No sense in running if 'sshd' doesn't even exist on this system...
#unless (( -f "/usr/sbin/sshd" ) or ( -f "/usr/local/sbin/sshd") or ( -f "/usr/lib/ssh/sshd")) {
# exit (0);
#}
my $sftpRequests = 0;
if ( $Debug >= 5 ) {
print STDERR "\n\nDEBUG: Inside SSHD Filter \n\n";
$DebugCounter = 1;
}
while (defined($ThisLine = <STDIN>)) {
if ( $Debug >= 5 ) {
print STDERR "DEBUG($DebugCounter): $ThisLine";
$DebugCounter++;
}
chomp($ThisLine);
if (
($ThisLine =~ m/^(log: )?$/ ) or
($ThisLine =~ m/^(log: )?\^\[\[60G/ ) or
($ThisLine =~ m/^(log: )? succeeded$/ ) or
($ThisLine =~ m/^(log: )?Closing connection to/) or
($ThisLine =~ m/^(log: )?Starting sshd:/ ) or
($ThisLine =~ m/^(log: )?sshd \-TERM succeeded/ ) or
($ThisLine =~ m/^Bad protocol version identification .*:? [\d.]+/ ) or
($ThisLine =~ m/^Bad protocol version identification.*Big-Brother-Monitor/ ) or
($ThisLine =~ m/^Connection closed by/) or
($ThisLine =~ m/^Disconnecting: Command terminated on signal \d+/) or
($ThisLine =~ m/^connect from \d+\.\d+\.\d+\.\d+/) or
($ThisLine =~ m/^fatal: Timeout before authentication/ ) or
($ThisLine =~ m/Connection from .* port /) or
($ThisLine =~ m/Postponed keyboard-interactive for [^ ]+ from [^ ]+/) or
($ThisLine =~ m/Read from socket failed/) or
($ThisLine =~ m/sshd startup\s+succeeded/) or
($ThisLine =~ m/sshd shutdown\s+succeeded/) or
($ThisLine =~ m/\(pam_unix\) .*/)
) {
# Ignore these
} elsif ($ThisLine =~ /^Accepted (\S+) for (\S+) from ([\d\.:a-f]+) port (\d+)/) {
if ($Debug >= 5) {
print STDERR "DEBUG: Found -$2 logged in from $3 using $1\n";
}
if ($Detail >= 20) {
$Users{$2}{$3}{$1}++;
} else {
$Users{$2}{$3}{"(all)"}++;
}
} elsif ( $ThisLine =~ m/^Failed (\S+) for (\S+) from ([^ ]+) port (\d+)/ ) { #openssh
if ( $Debug >= 5 ) {
print STDERR "DEBUG: Found -Failed login- line\n";
}
$BadLogins{"$2/$1 from $3"}++;
} elsif ( $ThisLine =~ m/^Failed (\S+) for illegal user (.*) from ([^ ]+) port (\d+)/ ) { #openssh
$Temp = "$2/$1 from $3";
$BadLogins{$Temp}++;
$IllegalUsers{$Temp}++;
} elsif ( ($User) = ( $ThisLine =~ /Disconnecting: Too many authentication failures for ([^ ]+)/)) {
$TooManyFailures{$User}++;
} elsif ( ($User) = ($ThisLine =~ /^input_userauth_request: illegal user (.*)$/ )) {
$IllegalUsers{"$User/none from unknown"}++;
} elsif ( $ThisLine =~ m/^Illegal user (.*) from ([^ ]+)/ ) { #redhat thing
$IllegalUsers{"$1/none from $2"}++;
} elsif ( $ThisLine =~ m/^(fatal: )?Did not receive ident(ification)? string from (.+)/ ) { # ssh/openssh
$name = LookupIP($3);
$NoIdent{$name}++;
} elsif ( ($Host) = ($ThisLine =~ /Could not write ident string to ([^ ]+)$/ )) {
$name = LookupIP($Host);
$NoIdent{$name}++;
} elsif (
($ThisLine =~ m/^fatal: Connection closed by remote host\./ ) or
($ThisLine =~ m/^fatal: Read error from remote host: Connection reset by peer/ ) or
($ThisLine =~ m/^fatal: Read from socket failed: No route to host/) or
($ThisLine =~ m/^fatal: Write failed: Network is unreachable/ ) or
($ThisLine =~ m/^fatal: Write failed: Broken pipe/) or
($ThisLine =~ m/^error: chan_shutdown_read failed for .+/)
) {
$NetworkErrors++;
} elsif ( $ThisLine =~ m/^(log: )?Received (signal 15|SIG...); (terminating|restarting)\./) { #ssh/openssh
$Kills++;
if ( $Debug >= 5 ) {
print STDERR "DEBUG: Found -Signal 15 Terminating- line\n";
}
} elsif ( $ThisLine =~ m/^(log: )?Server listening on( [^ ]+)? port \d+/ ) { #ssh/openssh
$Starts++;
if ( $Debug >= 5 ) {
print STDERR "DEBUG: Found -Listening on port 22- line\n";
}
} elsif ( ($Port,$Address,$Reason) = ($ThisLine =~ /^error: Bind to port ([^ ]+) on ([^ ]+) failed: (.+).$/ )) {
$Temp = "$Address port $Port ($Reason)";
$BindFailed{$Temp}++;
} elsif ( $ThisLine =~ m/^(log: )?Generating .* \w+ key\./ ) { # ssh/openssh
# Don't care about this...
if ( $Debug >= 5 ) {
print STDERR "DEBUG: Found -Generating RSA key- line\n";
}
} elsif ( $ThisLine =~ m/^packet_set_maxsize: /) {
if ( $Debug >= 5 ) {
print STDERR "DEBUG: Found -packet_set_maxsize- line\n";
}
} elsif ( $ThisLine =~ m/^(log: )?\w+ key generation complete\./ ) { # ssh/openssh
# Don't care about this...
if ( $Debug >= 5 ) {
print STDERR "DEBUG: Found -Keygen complete- line\n";
}
} elsif ( $ThisLine =~ m/^Failed (\w+) for (\S+) from ([\d.]+) port (\d+)/ ) { #openssh
# depending on log mode, openssh may not report these in connection context.
if ( $Debug >= 5 ) {
print STDERR "DEBUG: Found -Failed login- line\n";
}
$BadLogins{"$2/$1 from $3"}++;
} elsif ($ThisLine =~ s/^(log: )?Could not reverse map address ([^ ]*).*$/$2/) {
$NoRevMap{$ThisLine}++;
} elsif ( ($Address) = ($ThisLine =~ /^reverse mapping checking getaddrinfo for ([^ ]*) failed - POSSIBLE BREAKIN ATTEMPT!/)) {
$NoRevMap{$Address}++;
} elsif ( ($IP,$Address) = ($ThisLine =~ /^Address ([^ ]*) maps to ([^ ]*), but this does not map back to the address - POSSIBLE BREAKIN ATTEMPT!/)) {
$NoRevMap{"$Address($IP)"}++;
} elsif ( (undef,$Address) = ($ThisLine =~ /^warning: ([^ ]*), line \d+: can't verify hostname: getaddrinfo\(([^ ]*), AF_INET\) failed$/)) {
$NoRevMap{$Address}++;
} elsif ( $ThisLine =~ m/subsystem request for sftp/ ) {
$sftpRequests++;
} elsif ( $ThisLine =~ m/refused connect from (.*)$/ ) {
$RefusedConnections{$1}++;
} elsif ( ($Reason) = ($ThisLine =~ /^Authentication refused: (.*)$/ ) ) {
$RefusedAuthentication{$Reason}++;
} elsif ( ($Host,$Reason) = ($ThisLine =~ /^Received disconnect from ([^ ]*): (.*)$/)) {
$DisconnectReceived{$Reason}{$Host}++;
} elsif ( ($Host) = ($ThisLine =~ /^ROOT LOGIN REFUSED FROM ([^ ]*)$/)) {
$RootLogin{$Host}++;
} elsif ( ($Error) = ($ThisLine =~ /^Cannot release PAM authentication\[\d\]: (.*)$/)) {
$PamReleaseFail{$Error}++;
} elsif ( ($Error) = ($ThisLine =~ m/^error: PAM: (.*)$/)) {
$PamError{$Error}++;
} elsif ( ($Reason) = ($ThisLine =~ /^Setting tty modes failed: (.*)$/)) {
$TTYModesFail{$Reason}++;
} elsif ( ($User,undef) = ($ThisLine =~ /^User ([^ ]*) not allowed because ([^ ]*) exists$/)) {
$LoginLock{'nologin was set'}{$User}++;
} elsif ( ($User,$Reason) = ($ThisLine =~ /^User ([^ ]*) not allowed because (.*)$/)) {
$LoginLock{$Reason}{$User}++;
} elsif ( ($Method,$User,$Host) = ($ThisLine =~ /^Postponed ([^ ]*) for (illegal user [^ ]*|[^ ]*) from ([^ ]*) port \d+ ssh/)) {
$PostPonedAuth{"$User/$Method"}{$Host}++;
$IllegalUsers{"$User/$Method"}++;
} elsif ( ($IP) = ($ThisLine =~ /^scanned from ([^ ]*)/) ) {
push @Scanned, LookupIP($IP);
} else {
# Report any unmatched entries...
unless ($ThisLine =~ /fwd X11 connect/) {
push @OtherList, "$ThisLine\n";
}
}
}
###########################################################
if ($NetworkErrors) {
print "\nNetwork Read Write Errors: " . $NetworkErrors . "\n";
}
if ($Kills) {
print "\nSSHD Killed: " . $Kills . " Time(s)\n";
}
if ($Starts) {
print "\nSSHD Started: " . $Starts . " Time(s)\n";
}
if (keys %RootLogin) {
print "\n\nWARNING!!!\n";
print "Refused ROOT login attempt from:\n";
foreach $Host (sort {$a cmp $b} keys %RootLogin) {
print " $Host : $RootLogin{$Host} Time(s)\n";
}
}
if (keys %BindFailed) {
print "\nFailed to bind:\n";
foreach $ThisOne (sort {$a cmp $b} keys %BindFailed) {
print " $ThisOne : $BindFailed{$ThisOne} Time(s)\n";
}
}
if ($Detail >= 10) {
if (keys %NoRevMap) {
print "\nCouldn't resolve these IPs:\n";
foreach $ThisOne (sort {$a cmp $b} keys %NoRevMap) {
print " $ThisOne: $NoRevMap{$ThisOne} Time(s)\n";
}
}
if (keys %NoIdent) {
print "\nDidn't receive an ident from these IPs:\n";
foreach $ThisOne (sort {$a cmp $b} keys %NoIdent) {
print " $ThisOne: $NoIdent{$ThisOne} Time(s)\n";
}
}
}
if ($#BadRSA >= 0) {
print "\nReceived a bad response to RSA challenge from these:\n";
foreach $ThisOne (@BadRSA) {
print " $ThisOne\n";
}
}
if (keys %TooManyFailures) {
print "\nDisconnecting after too many authentication failures for user:\n";
foreach $User (sort {$a cmp $b} keys %TooManyFailures) {
print " $User : $TooManyFailures{$User} Time(s)\n";
}
}
if (keys %TooManyFailures) {
print "\nDisconnecting after too many authentication failures for user:\n";
foreach $User (sort {$a cmp $b} keys %TooManyFailures) {
print " $User : $TooManyFailures{$User} Time(s)\n";
}
}
if (keys %BadLogins) {
print "\nFailed logins from these:\n";
for (sort keys %BadLogins) {
print " $_: $BadLogins{$_} Time(s)\n";
}
}
if (keys %IllegalUsers) {
print "\nIllegal users from these:\n";
for (sort keys %IllegalUsers) {
print " $_: $IllegalUsers{$_} Time(s)\n";
}
}
if ((keys %LoginLock) and ($Detail >= 5)) {
print "\nUser login attempt failed because:\n";
foreach $Reason (sort {$a cmp $b} keys %LoginLock) {
print " $Reason:\n";
foreach $User (sort {$a cmp $b} keys %{$LoginLock{$Reason}}) {
print " $User : $LoginLock{$Reason}{$User} Time(s)\n";
}
}
}
if (keys %PostPonedAuth) {
print "\nPostponed authentication:\n";
foreach $User (sort {$a cmp $b} keys %PostPonedAuth) {
print " $User:\n";
foreach $Host (sort {$a cmp $b} keys %{$PostPonedAuth{$User}}) {
print " $Host: $PostPonedAuth{$User}{$Host} Time(s)\n";
}
}
}
if (keys %Users) {
print "\nUsers logging in through sshd:\n";
foreach $user (sort {$a cmp $b} keys %Users) {
print " $user:\n";
my $totalSort = TotalCountOrder(%{$Users{$user}}, \&SortIP);
foreach my $ip (sort $totalSort keys %{$Users{$user}}) {
my $name = LookupIP($ip);
if ($Detail >= 20) {
print " $name:\n";
my $sort = CountOrder(%{$Users{$user}{$ip}});
foreach my $method (sort $sort keys %{$Users{$user}{$ip}}) {
my $val = $Users{$user}{$ip}{$method};
my $plural = ($val > 1) ? "s" : "";
print " $method: $val time$plural\n";
}
} else {
my $val = (values %{$Users{$user}{$ip}})[0];
my $plural = ($val > 1) ? "s" : "";
print " $name: $val time$plural\n";
}
}
}
}
if (keys %RefusedAuthentication) {
print "\n\nAuthentication refused:\n";
foreach $Reason (sort {$a cmp $b} keys %RefusedAuthentication) {
print " $Reason : $RefusedAuthentication{$Reason} Time(s)\n";
}
}
if (keys %DisconnectReceived) {
print "\n\nReceived disconnect:\n";
foreach $Reason (sort {$a cmp $b} keys %DisconnectReceived) {
print " $Reason\n";
foreach $Host (sort {$a cmp $b} keys %{$DisconnectReceived{$Reason}}) {
print " $Host : $DisconnectReceived{$Reason}{$Host} Time(s)\n";
}
}
}
if ($#Scanned >= 0) {
print "\nScanned from these:\n";
foreach $ThisOne (sort SortIP @Scanned) {
print " " . $ThisOne . "\n";
}
}
if (keys %RefusedConnections) {
print "\nRefused incoming connections:\n";
foreach my $badguy (sort {$a cmp $b} keys %RefusedConnections ) {
print " $badguy: " . $RefusedConnections{$badguy} . " Time(s)\n";
}
}
if (keys %PamReleaseFail) {
print "\nCannot release PAM authentication:\n";
foreach $Error (sort {$a cmp $b} keys %PamReleaseFail) {
print " $Error : $PamReleaseFail{$Error} Time(s)\n";
}
}
if (keys %PamError) {
print "\nError in PAM authentication:\n";
foreach $Error (sort {$a cmp $b} keys %PamError) {
print " $Error : $PamError{$Error} Time(s)\n";
}
}
if (keys %TTYModesFail) {
print "\nSetting tty modes failed:\n";
foreach $Reason (sort {$a cmp $b} keys %TTYModesFail) {
print " $Reason : $TTYModesFail{$Reason} Time(s)\n";
}
}
if ($sftpRequests > 0) {
print "\nSFTP subsystem requests: $sftpRequests Time(s)\n";
}
if ($#OtherList >= 0) {
print "\n**Unmatched Entries**\n";
print @OtherList;
}
exit(0);
# vi: shiftwidth=3 tabstop=3 et
|