遵循GUN版权
#!/bin/bash
MONITOR_FILE="/var/log/messages"
MONITOR_LOG_FILE="/var/crontab/anti_scan.log"
TABLES="/tmp/anti_scan.pid"
tmp="/tmp/anti_scan.pid.tmp"
test -e $TABLES || touch $TABLES
test -e $TABLES || touch $tmp
while read line
do
str=`echo $line | grep "authentication failure" | grep -v "grep" | awk '{for(x=1;x<=NF;x++){if(match($x,"rhost=")){rhost=substr($x,RSTART+RLENGTH,length($x)); printf ("%s %s/n",$3,rhost);}}}'`
if [ -n "$str" ]; then
NEWTIME=`echo $str | awk '{print $1}' |awk -F":" '{printf ("%s:%s",$1,$2);}'`
OLDTIME=`tail -n 1 $TABLES | awk '{print $1}' |awk -F":" '{printf ("%s:%s",$1,$2);}'`
if [ "$NEWTIME" == "$OLDTIME" ]; then
echo $str >> $TABLES
else
echo $str > $TABLES
fi
cat $TABLES | awk '{print $2}' | sort | uniq -c | sort -rn | xargs -l | /
while read amount ip
do
if [ $amount -gt 6 ]; then
iptables -A INPUT -s $ip -j DROP
sed '/$ip/d' $TABLES > $tmp
cat $tmp > $TABLES
fi
done
fi
done<`tail -f $MONITOR_FILE`
#!/bin/bash
MONITOR_FILE="/var/log/messages"
MONITOR_LOG_FILE="/var/crontab/anti_scan.log"
TABLES="/tmp/anti_scan.pid"
tmp="/tmp/anti_scan.pid.tmp"
test -e $TABLES || touch $TABLES
test -e $TABLES || touch $tmp
while read line
do
str=`echo $line | grep "authentication failure" | grep -v "grep" | awk '{for(x=1;x<=NF;x++){if(match($x,"rhost=")){rhost=substr($x,RSTART+RLENGTH,length($x)); printf ("%s %s/n",$3,rhost);}}}'`
if [ -n "$str" ]; then
NEWTIME=`echo $str | awk '{print $1}' |awk -F":" '{printf ("%s:%s",$1,$2);}'`
OLDTIME=`tail -n 1 $TABLES | awk '{print $1}' |awk -F":" '{printf ("%s:%s",$1,$2);}'`
if [ "$NEWTIME" == "$OLDTIME" ]; then
echo $str >> $TABLES
else
echo $str > $TABLES
fi
cat $TABLES | awk '{print $2}' | sort | uniq -c | sort -rn | xargs -l | /
while read amount ip
do
if [ $amount -gt 6 ]; then
iptables -A INPUT -s $ip -j DROP
sed '/$ip/d' $TABLES > $tmp
cat $tmp > $TABLES
fi
done
fi
done<`tail -f $MONITOR_FILE`
//用 kill -9 0 退出