bruteforce_scanner.pl 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/perl -w
  2. # script scans system log for invalid users ssh reports ( ssh attacks ) & saves IPs of attackers in file
  3. # using: script <result IP's file> [report file]
  4. # Sergey Kibrik sakib@meta.ua 2009
  5. #
  6. use strict;
  7. use warnings;
  8. #################
  9. my $logfile="/var/log/messages";
  10. my $resultfile=shift;
  11. my $report=shift;
  12. my $max_ip_count=30;
  13. #################
  14. my $errno=0;
  15. my $report_msg="";
  16. my %ip_table;
  17. unless ($report){
  18. $report=">&STDOUT";
  19. }
  20. open(REPORT,">",$report) or die "Failed to open reporting routine: $!\n";
  21. unless ( open(LOGFILE,"<",$logfile)){
  22. $report_msg .= "Failed to open $logfile: $!\n";
  23. $errno=1;
  24. goto QUIT;
  25. }
  26. unless ( open(RESULTFILE, ">", $resultfile)){
  27. $report_msg .= "Failed to open $resultfile: $!\n";
  28. $errno=1;
  29. goto QUIT;
  30. }
  31. while (<LOGFILE>){
  32. next unless /sshd.*Invalid\s+user\s+(\w+)\s+from\s+(.+)$/;
  33. ++$ip_table{$2};
  34. }
  35. foreach (keys %ip_table){
  36. if ($ip_table{$_} >= $max_ip_count) {
  37. print RESULTFILE $_,"\n";
  38. }
  39. }
  40. close(LOGFILE);
  41. close(RESULTFILE);
  42. QUIT:
  43. print REPORT $report_msg;
  44. close(REPORT);
  45. exit $errno;