Advertisement
Mozai

secure2scoundrels.pl

May 26th, 2015
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.98 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # -- config
  3. my @FILES=qw(/var/log/auth.log.1.gz /var/log/auth.log);
  4. my $regex=qr!^(\S+\s+\S+).+?sshd\[[0-9]+\]:.+?ailed password.+?from (\S+)!;
  5. my $regex2=qr!message repeated (\d+) times: \[ Failed password for \S+ from (\S+)!;
  6.  
  7. # -- init
  8. use strict;
  9. use IO::Uncompress::Gunzip;
  10. use NetAddr::IP;
  11. use Net::Whois::Raw;
  12. $Net::Whois::Raw::OMIT_MSG = 1;
  13. $Net::Whois::Raw::CHECK_FAIL = 1;
  14.  
  15.  
  16. use vars qw($whoisServer %whois_cache $whoisServer);
  17. $whoisServer = 'whois.arin.net';
  18. sub whois_cached($) {
  19.   my $ip = $_[0];
  20.   if (! $whois_cache{$ip}) {
  21.     my ($w,$x);
  22.     eval {$w = whois($ip,$whoisServer);};
  23.     if ($w =~ m/inetnum:\s+(\d+.*$)/m) {
  24.       $whois_cache{$ip} = $1;
  25.       $x = NetAddr::IP->new($1);
  26.       if ($x) {
  27.         $whois_cache{$ip} = $x;      
  28.       }
  29.     }
  30.   }
  31.   return $whois_cache{$ip};
  32. }
  33.  
  34. # -- main
  35. use vars qw(%iptally %sntally %sn2ips);
  36. foreach my $file (@FILES) {
  37.   my $fh;
  38.   if($file=~m/\.gz$/){
  39.     $fh=new IO::Uncompress::Gunzip $file;
  40.     #open($fh,"/bin/zcat \"$file\" |");
  41.   }
  42.   else {
  43.     open($fh,$file);
  44.   }
  45.   unless($fh){ warn "$file: $!\n"; next; }
  46.   while(<$fh>) {
  47.     if (m!$regex!) {
  48.       $iptally{sprintf("%16s", $2)} += 1;
  49.     }
  50.     elsif (m!$regex2!) {
  51.       $iptally{sprintf("%16s", $2)} += $1;
  52.     }
  53.   }
  54.   close($fh);
  55. }
  56. foreach my $ip (keys %iptally) {
  57.   my $sn = (&whois_cached($ip) or "$ip/32");
  58.   $sntally{$sn} += $iptally{$ip};
  59.   if (not exists($sn2ips{$sn})) { $sn2ips{$sn} = []; }
  60.   push(@{$sn2ips{$sn}}, $ip);
  61. }
  62. foreach my $sn (sort keys %sntally) {
  63.   my $v = $sntally{$sn};
  64.   if ($v > 10) {
  65.     #$n = gethostbyaddr(pack('C4',split('\.',$ip)),2);
  66.     #printf("%s : %s password attempts : %s : %s\n",$ip,$v,$w,$n);
  67.     my $iplist = join(', ', sort(@{$sn2ips{$sn}}));
  68.     $iplist =~ s/  +/ /g;
  69.     if (length($sn) < 21) {
  70.       printf("%5d password attempts : %20s : %s\n", $v, $sn, $iplist);
  71.     }
  72.     else {
  73.       printf("%5d password attempts : %35s : %s\n", $v, $sn, $iplist);
  74.     }
  75.   }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement