#!/bin/bash
#特性:
#1,可分析多个网站项目日志
#2,每个网站项目日志可根据关键字进行统计分析
#3,可调整访问IP次数的阀值(超过多少次加入到墙)
#4,可调整IP封的时间
#5,可在
定时任务修改分析日志的
时间间隔
#6,
防火墙在封的时候进行了去重,不会封同样的IP
#-----zhaoll@
# www.jb200.com
##############################################################################################
#-----------------------------------------防火墙封IP-----------------------------------------#
##############################################################################################
#当前时间戳,用于比较drop_ip.log里的时间戳
ip_diff_time=`date +%s`
iptables=/sbin/iptables
#防火墙时间阀值--默认10分钟,比较已封IP是否超过10分钟,如果超过10分钟,则解封
ip_return_time=$((10*60))
#ip_return_time=43200
#ip_return_time=86400
for i in `cat /data0/shells/drop_ip.log |
linuxjishu/13830.html target=_blank class=infotextkey>awk -F ":" '{if('$ip_diff_time'-$1>='$ip_return_time'-60){print}}'`
do
IPLIST1=`echo $i | awk -F ":" '{print $1}'`
IPLIST2=`echo $i | awk -F ":" '{print $2}'`
$IPTABLES -D INPUT -s $IPLIST2 -j DROP
#ssh 10.19.1.200 "$IPTABLES -D INPUT -s $IPLIST2 -j DROP"
echo ”$IPLIST2 已解封“
sed -i '/'$IPLIST1'/d' /data0/shells/drop_ip.log
done
#sed -i '/'$IPLIST1'/d' /data0/shells/drop_ip.log
##############################################################################################
#------------------------------------------日志分析------------------------------------------#
##############################################################################################
#清空整合日志信息,进行新一次抽取
>/data1/logs/monitorlog.log
#需要监控的项目列表,如在添加日志,可以此操作
server_project_list=(
my.*.com_access.log
kefu.*.com_access.log
hu.*.com_access.log
ask.*.com_access.log
)
#循环抽取项目列表里的日志信息,
for (( i=0;i<=3;i=i+1 ))
do
url=/data1/logs/`echo ${server_project_list[$i]}`
echo $url
#日志头一行的日志时间
headtime=`head -1 $url | awk -F: '{print $2":"$3}'`
#用于时间比较
h1=`echo $headtime | awk -F ":" '{print $1}'`
m1=`echo $headtime | awk -F ":" '{print $2}'`
#系统当前时间
nowtime=`date +"%H:%M"`
#取得丛1970-01-01到当前时间的时间戳
timestamp=`date +%s`
#设置要查看日志的时间段,这里默认1小时,可更改
#returntime=$((1*60*60))
returntime=$((10*60))
#计算出1小时之前的时间的时间戳
timeperiod=$(($timestamp-$returntime))
#转换1小时之前的时间
oldtime=$(date -d '1970-01-01 UTC '$timeperiod' seconds' +"%H:%M")
#用于时间比较
h2=`echo $oldtime | awk -F ":" '{print $1}'`
m2=`echo $oldtime | awk -F ":" '{print $2}'`
#统计固定关键字的访问记录
if [ $url == '/data1/logs/my.xoyo.com_access.log' ];then
key='isExist|NewIsExist|chargeHistory|normal'
else
key=''
fi
#之所以比较时间,是因为日志里可能没有1小时之前的时间,这样程序就不会在执行操作,如果没有想要的时间戳,那就使用日志头一行日志的时间
if [ "$h2" -ge "$h1" -a "$m2" -ge "$m1" ];then
#截取规定时间内的日志,日志包括访问IP和访问页面路径,并取得另一负载机上的日志信息进行合并到指定的文件里
tail -150000 $url | awk '{print $1,$4,$11}' | sed -n "/${oldtime}:[0-9][0-9]/,/${nowtime}:[0-9][0-9]/p" | egrep "${key}" |awk '{print $1,$3}'>>/data1/logs/monitorlog.log
#tail -150000 $url | awk '{print $1,$4,$11}' | sed -n "/${oldtime}:[0-9][0-9]/,/${nowtime}:[0-9][0-9]/p" |awk '{print $1,$3}'| tee /data1/logs/test.log $url.log.count -
#echo "------------------------------------200
服务器-$1-----------------------------------------">>/data1/logs/test.log
#ssh 10.19.1.200 "tail -200000 $url" | awk '{print $1,$4,$11}' | sed -n "/${oldtime}:[0-9][0-9]/,/${nowtime}:[0-9][0-9]/p" |awk '{print $1,$3}'>>/data1/logs/test.log
else
tail -150000 $url | awk '{print $1,$4,$11}' | sed -n "/${headtime}:[0-9][0-9]/,/${nowtime}:[0-9][0-9]/p" | egrep "${key}" |awk '{print $1,$3}'>>/data1/logs/monitorlog.log
#tail -150000 $url | awk '{print $1,$4,$11}' | sed -n "/${headtime}:[0-9][0-9]/,/${nowtime}:[0-9][0-9]/p" |awk '{print $1,$3}'| tee /data1/logs/test.log $url.log.count -
#echo "------------------------------------200服务器-$1-----------------------------------------">>/data1/logs/test.log
#ssh 10.19.1.200 "tail -200000 $url" | awk '{print $1,$4,$11}' | sed -n "/${headtime}:[0-9][0-9]/,/${nowtime}:[0-9][0-9]/p" |awk '{print $1,$3}'>>/data1/logs/test.log
fi
done
#纯粹是为了打日志
echo "-------统计日志IP---------">>/data0/shells/log_monitor.log
date>>/data0/shells/log_monitor.log
cat /data1/logs/monitorlog.log | sort | uniq -c | sort -n | grep -v "127.0.0.1" | grep -v "219.141" | grep -v "219.239" | grep -v "10.19.1" |grep -v "114.255" | grep -v "117.79.148" | grep -v "61.152" | grep -v "61.129" | grep -v "218.24" | grep -v "221.237" | grep -v "221.4" | grep -v "113.106" | awk '{if($1>100){print}}'>>/data0/shells/log_monitor.log
#两次排序去重,第一次是为了取得访问大行100次的数据,第二次是为了取得最后的IP,添加循环判断,因是为了避免封同样的IP
#cat /data1/logs/monitorlog.log | sort | uniq -c | sort -n | grep -v "127.0.0.1" | grep -v "219.141" | grep -v "219.239" | grep -v "10.19.1" |grep -v "114.255" | grep -v "117.79.148" | grep -v "61.152" | grep -v "61.129" | grep -v "218.24" | grep -v "221.237" | grep -v "221.4" | grep -v "113.106" | awk '{if($1>100){print}}' | awk '{print $2}' | sort -n | uniq -c | awk '{print $2}'| sed "s/^ */${timestamp}:/g" >>/data0/shells/drop_ip.log
for i in `cat /data1/logs/monitorlog.log | sort | uniq -c | sort -n | grep -v "127.0.0.1" | grep -v "219.141" | grep -v "219.239" | grep -v "10.19.1" |grep -v "114.255" | grep -v "117.79.148" | grep -v "61.152" | grep -v "61.129" | grep -v "218.24" | grep -v "221.237" | grep -v "221.4" | grep -v "113.106" | awk '{if($1>100){print}}' | awk '{print $2}' | sort -n | uniq -c | awk '{print $2}'`
do
if grep -q $i /data0/shells/drop_ip.log; then
echo "IP已存在,$i"
else
echo "新IP,$i"
echo $i| sed "s/^ */${timestamp}:/g" >>/data0/shells/drop_ip.log
fi
done
#################################################################################################
#-------------------------------------------防火墙封IP------------------------------------------#
#################################################################################################
IPTABLES=/sbin/iptables
#查找新添加的IP进行IPTABLES封IP
for i in `cat /data0/shells/drop_ip.log | awk -F ":" '{if($1== '${timestamp}'){print $2}}' |
xargs`
do
$IPTABLES -I INPUT -s $i -j DROP
#ssh 10.19.1.200 "$IPTABLES -I INPUT -s $i -j DROP"
echo "$i 已封"
done