Perl新手,写下来练练手。
1.利用crontab每分钟执行一次,或者时间稍长一些
*/1 * * * *
2.找出/var/log/secure中IP地址,统计次数,如果次数大于10,那么写入/etc/hosts.deny(之前还要判断该IP是否已经在/etc/hosts.deny当中)
3.效果像这样:/etc/hosts.deny的内容
- ##########2012-08-06##########
- sshd:218.94.106.239
- ##########2012-08-07##########
- ##########2012-08-08##########
- sshd:61.167.33.222
- sshd:111.1.3.69
- ##########2012-08-09##########
- sshd:218.75.128.43
- ##########2012-08-10##########
- sshd:222.197.192.251
- ##########2012-08-11##########
- sshd:118.129.139.68
- sshd:183.60.150.200
- sshd:118.129.166.59
- ###########2012-08-12###########
- sshd:58.49.59.212
- sshd:114.80.215.250
- sshd:216.24.204.220
4.虚拟机测试的话,得手工添加数据,像这样:
- 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!
- Aug 12 15:03:19 server sshd[5197]: Failed password for root from ::ffff:216.24.204.220 port 37212 ssh2
- 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!
- Aug 12 15:03:24 server sshd[5199]: Failed password for root from ::ffff:216.24.204.220 port 37587 ssh2
- 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!
- Aug 12 15:03:28 server sshd[5203]: Failed password for root from ::ffff:216.24.204.220 port 37983 ssh2
- 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!
- Aug 12 15:03:33 server sshd[5206]: Failed password for root from ::ffff:216.24.204.220 port 38391 ssh2
- 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!
- Aug 12 15:03:38 server sshd[5209]: Failed password for root from ::ffff:216.24.204.220 port 38799 ssh2
- Aug 12 15:03:40 server sshd[5212]: Invalid user git from ::ffff:216.24.204.220
- 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!
- Aug 12 15:03:43 server sshd[5212]: Failed password for invalid user git from ::ffff:216.24.204.220 port 39195 ssh2
- Aug 12 15:03:45 server sshd[5215]: Invalid user cron from ::ffff:216.24.204.220
- 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!
- Aug 12 15:03:47 server sshd[5215]: Failed password for invalid user cron from ::ffff:216.24.204.220 port 39585 ssh2
- Aug 12 15:03:50 server sshd[5218]: Invalid user git from ::ffff:216.24.204.220
- 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!
- Aug 12 15:03:52 server sshd[5218]: Failed password for invalid user git from ::ffff:216.24.204.220 port 39987 ssh2
- Aug 12 15:03:55 server sshd[5221]: Invalid user git from ::ffff:216.24.204.220
- 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!
- Aug 12 15:03:57 server sshd[5221]: Failed password for invalid user git from ::ffff:216.24.204.220 port 40414 ssh2
- Aug 12 15:03:59 server sshd[5224]: Invalid user git from ::ffff:216.24.204.220
- 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.脚本内容如下:
- #!/usr/bin/perl -w
- #Author:Leo Email:chanyipiaomiao@163.com
- my $secrue = "/var/log/secure";
- my $hosts = "/etc/hosts.deny" ;
- my $count = 10;
- #打开读secure文件句柄,然后计算每个IP地址Failed或Invalid出现的次数
- open SECRUEFILE, "<", $secrue or die "Can't open $secrue: $!";
- foreach (<SECRUEFILE> ){
- $ip{$1}++ if (/(?:Failed|Invalid).*:(\d+\.\d+\.\d+\.\d+)/gi );
- }
- close SECRUEFILE;
- #打开读hosts文件句柄,读入hosts文件内容
- open HOSTSREAD, "<", $hosts or die "Can't read $hosts: $!";
- $tempstrings = join '',<HOSTSREAD>;
- close HOSTSREAD;
- #打开写hosts文件句柄
- open HOSTSWRITE, ">>", $hosts or die "Can't create $hosts: $!";
- #获取系统日期,判断当天日期是否在hosts文件中,不在则写入hosts文件
- my ($mday,$mon,$year) = (localtime)[3..5];
- ($mday,$mon,$year) = (
- sprintf("%02d", $mday),
- sprintf("%02d", $mon + 1),
- $year + 1900
- );
- $date = $year."-".$mon."-".$mday;
- unless ((grep /$date/,$tempstrings)){
- print HOSTSWRITE "\n";
- print HOSTSWRITE "###########$date###########\n";
- }
- #定义子程序,如果hosts已经有了的IP地址,则不写入hosts文件。
- sub ip_noexists_write {
- my $temp_ip = shift @_;
- if (0 == @tempstrings){
- print HOSTSWRITE "sshd:$temp_ip\n";
- }else {
- print HOSTSWRITE "sshd:$temp_ip\n" unless (grep /$temp_ip/,$tempstrings);
- }
- }
- #如果次数大于$count的值,就写入到hosts文件
- foreach (keys %ip){
- #print "$_ = $ip{$_} \n";
- &ip_noexists_write($_) if ($ip{$_} >= $count);
- }
- close HOSTSWRITE;