遵循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 退出
本文介绍了一个使用Bash脚本实现的简单系统,该系统通过监控日志文件来检测潜在的攻击行为,并自动采取措施阻止这些攻击。具体来说,脚本会查找与认证失败相关的记录,统计IP地址的尝试次数,对于尝试次数超过阈值的IP,将通过iptables进行屏蔽。
4388

被折叠的 条评论
为什么被折叠?



