Linux简单处理CC攻击shell脚本
第一个脚本是通过查找日志中访问次数过多的ip,并用iptables屏蔽。
1.#!/bin/bash
r=`date +%H%M%S`
cur=`date -d "1 minute ago" +%H%M%S`
dip=`tail -n 10000 /home/www/log/g | egrep -v "\.(gif|jpg|jpeg|png|css|js)" | awk -v a="$becur" -v b="$cur" -F [' ':] '{t=$5$6$7;if (t>=a && t<=b) print $1}' | sort | uniq -c | awk '{if ($1>=20) print $2}'`
[ ! -z "$badip" ];then
r ip in $badip;
test -z "`/sbin/iptables -nL | grep $ip`";then
9./sbin/iptables -I INPUT -s $ip -j DROP
ne
将此代码保存为,加入cronjob使每分钟执行一次。
此脚本的作用是:利用iptables屏蔽每分钟访问页面超过20的IP,这些页面已经排除图片,css,js等静态文件。
第二个脚本是通过在日志中查找cc攻击的特征进行屏蔽。
1.#!/bin/bash
yword="cc-atack"
dip=`tail -n 5000 /home/www/log/g | grep "$keyword" | awk '{print $1}' | sort | uniq -c | sort -nr | awk '{print $2}'`
[ ! -z "$badip" ];then
r ip in $badip;
test -z "`/sbin/iptables -nL | grep $ip`";then
8./sbin/iptables -I INPUT -s $ip -j DROP
ne
keyword则是日志中cc的特征,替换成有效的即可。
No tags for this post. Posted in: 建站手札