Perl新手,写下来练练手。

1.利用crontab每分钟执行一次,或者时间稍长一些

*/1 * * * *

2.找出/var/log/secure中IP地址,统计次数,如果次数大于10,那么写入/etc/hosts.deny(之前还要判断该IP是否已经在/etc/hosts.deny当中)

3.效果像这样:/etc/hosts.deny的内容

 
  
  1. ##########2012-08-06########## 
  2. sshd:218.94.106.239 
  3.  
  4. ##########2012-08-07########## 
  5.  
  6. ##########2012-08-08########## 
  7. sshd:61.167.33.222 
  8. sshd:111.1.3.69 
  9.  
  10. ##########2012-08-09########## 
  11. sshd:218.75.128.43 
  12.  
  13. ##########2012-08-10########## 
  14. sshd:222.197.192.251 
  15.  
  16. ##########2012-08-11########## 
  17. sshd:118.129.139.68 
  18. sshd:183.60.150.200 
  19. sshd:118.129.166.59 
  20.  
  21. ###########2012-08-12########### 
  22. sshd:58.49.59.212 
  23. sshd:114.80.215.250 
  24. sshd:216.24.204.220 

 4.虚拟机测试的话,得手工添加数据,像这样:

 
  
 
  
  1. Aug 12 15:03:17 server sshd[5197]: Address 216.24.204.220 maps to unassigned.psychz.net, but this does not map back to the address - POSSIBLE BREAKIN ATTEMPT! 
  2. Aug 12 15:03:19 server sshd[5197]: Failed password for root from ::ffff:216.24.204.220 port 37212 ssh2 
  3. Aug 12 15:03:21 server sshd[5199]: Address 216.24.204.220 maps to unassigned.psychz.net, but this does not map back to the address - POSSIBLE BREAKIN ATTEMPT! 
  4. Aug 12 15:03:24 server sshd[5199]: Failed password for root from ::ffff:216.24.204.220 port 37587 ssh2 
  5. Aug 12 15:03:26 server sshd[5203]: Address 216.24.204.220 maps to unassigned.psychz.net, but this does not map back to the address - POSSIBLE BREAKIN ATTEMPT! 
  6. Aug 12 15:03:28 server sshd[5203]: Failed password for root from ::ffff:216.24.204.220 port 37983 ssh2 
  7. Aug 12 15:03:31 server sshd[5206]: Address 216.24.204.220 maps to unassigned.psychz.net, but this does not map back to the address - POSSIBLE BREAKIN ATTEMPT! 
  8. Aug 12 15:03:33 server sshd[5206]: Failed password for root from ::ffff:216.24.204.220 port 38391 ssh2 
  9. Aug 12 15:03:36 server sshd[5209]: Address 216.24.204.220 maps to unassigned.psychz.net, but this does not map back to the address - POSSIBLE BREAKIN ATTEMPT! 
  10. Aug 12 15:03:38 server sshd[5209]: Failed password for root from ::ffff:216.24.204.220 port 38799 ssh2 
  11. Aug 12 15:03:40 server sshd[5212]: Invalid user git from ::ffff:216.24.204.220 
  12. Aug 12 15:03:40 server sshd[5212]: Address 216.24.204.220 maps to unassigned.psychz.net, but this does not map back to the address - POSSIBLE BREAKIN ATTEMPT! 
  13. Aug 12 15:03:43 server sshd[5212]: Failed password for invalid user git from ::ffff:216.24.204.220 port 39195 ssh2 
  14. Aug 12 15:03:45 server sshd[5215]: Invalid user cron from ::ffff:216.24.204.220 
  15. Aug 12 15:03:45 server sshd[5215]: Address 216.24.204.220 maps to unassigned.psychz.net, but this does not map back to the address - POSSIBLE BREAKIN ATTEMPT! 
  16. Aug 12 15:03:47 server sshd[5215]: Failed password for invalid user cron from ::ffff:216.24.204.220 port 39585 ssh2 
  17. Aug 12 15:03:50 server sshd[5218]: Invalid user git from ::ffff:216.24.204.220 
  18. Aug 12 15:03:50 server sshd[5218]: Address 216.24.204.220 maps to unassigned.psychz.net, but this does not map back to the address - POSSIBLE BREAKIN ATTEMPT! 
  19. Aug 12 15:03:52 server sshd[5218]: Failed password for invalid user git from ::ffff:216.24.204.220 port 39987 ssh2 
  20. Aug 12 15:03:55 server sshd[5221]: Invalid user git from ::ffff:216.24.204.220 
  21. Aug 12 15:03:55 server sshd[5221]: Address 216.24.204.220 maps to unassigned.psychz.net, but this does not map back to the address - POSSIBLE BREAKIN ATTEMPT! 
  22. Aug 12 15:03:57 server sshd[5221]: Failed password for invalid user git from ::ffff:216.24.204.220 port 40414 ssh2 
  23. Aug 12 15:03:59 server sshd[5224]: Invalid user git from ::ffff:216.24.204.220 
  24. Aug 12 15:03:59 server sshd[5224]: Address 216.24.204.220 maps to unassigned.psychz.net, but this does not map back to the address - POSSIBLE BREAKIN ATTEMPT! 

5.脚本内容如下:

 
  
  1. #!/usr/bin/perl -w 
  2. #Author:Leo Email:chanyipiaomiao@163.com 
  3. my $secrue = "/var/log/secure"
  4. my $hosts = "/etc/hosts.deny" ; 
  5. my $count = 10; 
  6.  
  7. #打开读secure文件句柄,然后计算每个IP地址Failed或Invalid出现的次数 
  8. open SECRUEFILE, "<", $secrue or die "Can't open $secrue: $!"
  9. foreach (<SECRUEFILE> ){ 
  10.       $ip{$1}++ if (/(?:Failed|Invalid).*:(\d+\.\d+\.\d+\.\d+)/gi ); 
  11. close SECRUEFILE; 
  12.  
  13. #打开读hosts文件句柄,读入hosts文件内容 
  14. open HOSTSREAD, "<", $hosts or die "Can't read $hosts: $!"
  15. $tempstrings = join '',<HOSTSREAD>;
  16. close HOSTSREAD; 
  17.  
  18. #打开写hosts文件句柄 
  19. open HOSTSWRITE, ">>", $hosts or die "Can't create $hosts: $!"
  20.  
  21. #获取系统日期,判断当天日期是否在hosts文件中,不在则写入hosts文件 
  22. my ($mday,$mon,$year) = (localtime)[3..5]; 
  23. ($mday,$mon,$year) = ( 
  24.     sprintf("%02d", $mday), 
  25.     sprintf("%02d", $mon + 1), 
  26.     $year + 1900 
  27. ); 
  28. $date = $year."-".$mon."-".$mday; 
  29. unless ((grep /$date/,$tempstrings)){ 
  30.     print HOSTSWRITE "\n"
  31.     print HOSTSWRITE "###########$date###########\n"
  32.  
  33. #定义子程序,如果hosts已经有了的IP地址,则不写入hosts文件。 
  34. sub ip_noexists_write { 
  35.       my $temp_ip = shift @_; 
  36.       if (0 == @tempstrings){ 
  37.             print HOSTSWRITE "sshd:$temp_ip\n"
  38.       }else { 
  39.             print HOSTSWRITE "sshd:$temp_ip\n" unless (grep /$temp_ip/,$tempstrings); 
  40.       } 
  41.  
  42. #如果次数大于$count的值,就写入到hosts文件 
  43. foreach (keys %ip){ 
  44.       #print "$_ = $ip{$_} \n"
  45.       &ip_noexists_write($_) if ($ip{$_} >= $count); 
  46. close HOSTSWRITE;