您的位置:首页 > 运维架构 > Shell

shell结合iptables自动处理CC攻击(转载)

2012-01-31 13:31 190 查看
www.*****.com:80 118.251.244.183 - - [26/May/2010:20:22:16 +0800] "POST /syxcms/vote.php?act=submit HTTP/1.1" 200 56 "http://www.*****.com/news/201005/news-6213.shtml" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"
www.*****.com:80 118.251.244.183 - - [26/May/2010:20:22:16 +0800] "POST /syxcms/vote.php?act=submit HTTP/1.1" 200 72 "http://www.*****.com/news/201005/news-6213.shtml" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"
www.*****.com:80 118.251.244.183 - - [26/May/2010:20:22:16 +0800] "POST /syxcms/vote.php?act=submit HTTP/1.1" 200 56 "http://www.*****.com/news/201005/news-6213.shtml" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"
大概原理就是对最新日志里面的客户端访问IP进行采样统计分析,然后对超出正常访问次数的IP进行屏蔽,如下面统计分析后的结果:
对最新1000条日志的客户端访问IP进行排序统计访问次数。比如第一个IP 219.128.20.68 1000条日志就有295条,访问肯定不正常
root@ubuntu134:# tail access.log -n 1000 |grep vote.php |awk {'print $2'} |sort |uniq -c |sort -nr
295 219.128.20.68
175 113.250.97.209
164 218.87.140.39
153 59.61.215.42
98 222.240.182.234
83 220.181.110.65
73 120.38.1.255
62 221.3.99.106
21 220.249.83.74
12 218.22.10.114
1 123.52.158.16
1 114.81.115.201
然后就是自动处理,如果1000条日志单IP超过50条就屏蔽掉
*/2 * * * * /usr/local/nginx/var/log/drop.sh
#!/bin/sh
cd /usr/local/nginx/var/log
tail access.log -n 1000 |grep vote.php |awk {'print $2'} |sort |uniq -c |sort -nr |awk '{if ($2!=null && $1>50) {print $2}}' > drop_ip.txt
for i in `cat drop_ip.txt`
do
/sbin/iptables -I INPUT -s $i -j DROP;
done
这shell 每几分钟执行一次,就可自动屏蔽那些不正常IP,相信大家都看的懂,下面是针对连接数屏蔽代码
#!/bin/sh
/bin/netstat -ant |grep 80 |awk '{print $5}' |awk -F : '{print $1}' |sort |uniq -c |sort -rn |grep -v -E '192.168|127.0' |awk '{if ($2!=null && $1>50) {print $2}}' > drop_ip.txt
for i in `cat drop_ip.txt`
do
/sbin/iptables -I INPUT -s $i -j DROP;
done
说下,grep -v -E '192.168|127.0' 也就是排除内网IP,免得把自己给屏蔽了,当然还可以加些自己的IP。

http://sookk8.blog.51cto.com/455855/448539
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: