Skip to content


shell防采集脚本

介绍
脚本使用bash编写,仅作简单防御,增加采集复杂度
原理是通过采集器的特点,请求频率高并只请求关键文件(html,php…),而不请求无关文件(css,js,jp…)来进行筛选并放入iptables

采集者请求的log样例

122.70.137.104 – – [10/Jun/2010:13:11:05 +0800] “GET /chongqingjob/list_1.html HTTP/1.0” 200 24013 “-” “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 2.0.93341)” –
122.70.137.104 – – [10/Jun/2010:13:11:05 +0800] “GET /chongqingjob/list_2.html HTTP/1.0” 200 22803 “-” “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 2.0.97314)” –
122.70.137.104 – – [10/Jun/2010:13:11:05 +0800] “GET /chongqingjob/list_3.html HTTP/1.0” 200 22397 “-” “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 2.0.99305)” –
122.70.137.104 – – [10/Jun/2010:13:11:05 +0800] “GET /yunnanjob/list_1.html HTTP/1.0” 200 23545 “-” “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 2.0.2415)” –
122.70.137.104 – – [10/Jun/2010:13:11:05 +0800] “GET /yunnanjob/list_2.html HTTP/1.0” 200 22274 “-” “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 2.0.96797)” –
122.70.137.104 – – [10/Jun/2010:13:11:06 +0800] “GET /yunnanjob/list_3.html HTTP/1.0” 200 22222 “-” “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 2.0.33279)” –
122.70.137.104 – – [10/Jun/2010:13:11:06 +0800] “GET /guizhoujob/list_1.html HTTP/1.0” 200 23911 “-” “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 2.0.34389)” –
122.70.137.104 – – [10/Jun/2010:13:11:06 +0800] “GET /guizhoujob/list_2.html HTTP/1.0” 200 22928 “-” “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 2.0.4951)” –
122.70.137.104 – – [10/Jun/2010:13:11:06 +0800] “GET /guizhoujob/list_3.html HTTP/1.0” 200 22232 “-” “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 2.0.18697)” –

脚本默认配置
1.每3分钟运行一次
2.检查是否清除iptables中的采集ip
2.取出前3分钟排除百度等蜘蛛后的访问日志
3.取出防问量大于60的前三个ip
4.检查是否是可信任ip
5.检查请求favicon.ico的次数是否大于2
6.将采集ip放入iptable

参数说明

#运行log日志位置
V_DEBUGLOG=./kickleech.log
#apache或nginx的访问日志
V_LOG=/opt/nginx/logs/blog.c1gstudio.com.log
#访问日志临时文件
V_TMPFILE=/opt/nginx/logs/kickleechtmp.log
#ip临时存放位置
V_IPTMPFILE=./kickleechip.log
#最前几分钟的访问日志
V_TIMELIMIT=3
#请求频率,大于此值才会进行审核
V_THRESHOLD=60
#取多少个ip放入iptables中(按请求次数降序)
V_MAXIP=3
#可信任的ip地址
V_SAFEIP=”192.168.0.15 222.147.111.3 222.236.154.162″
#验证字符,如果在${V_TIMELIMI}分钟内请求${V_CODE}少于${V_REQUESTNUM}次将会被认为是采集者
V_CODE=’favicon.ico’
#是否放入iptables中(iptables不作保存,重载或重启会清除之前记录的ip),1=放入,0=仅记录
V_IPTABLES=1
#用于iptables的web访问端口
V_HTTPPORT=80
#清除采集者的时间,当系统为3,12…分会清除采集ip,采集者又可以采了
#需和crontab的时间设定配合,仅仅清空采集相关ip
V_IPTABLESFLUSHTIME=”3 12 21 33 42 51″
V_REQUESTNUM=2

V_CODE参数说明
V_CODE=’favicon.ico’
可以设置成某个css或图片等
这里的配置为favicon.ico,也就是收藏的小图标,浏览器访问时默认会自动请求根目录下此文件,确保存在。
不要设置expires时间,否则日志中不会记录访问请求
以下为nginx设置

location ~(favicon.ico) {
#log_not_found off;
expires -1;
break;
}

这样可以在有缓存时也产生个304请求

222.236.154.162 – – [10/Jun/2010:10:50:37 +0800] “GET /favicon.ico HTTP/1.1” 200 3638 “-” “Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9 ( .NET CLR 3.5.30729)” –
222.236.154.162 – – [10/Jun/2010:10:52:13 +0800] “GET /favicon.ico HTTP/1.1” 304 0 “-” “Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9 ( .NET CLR 3.5.30729)” –

kickleech.log日志记录格式

=====Thu Jun 10 13:12:01 CST 2010========
Flush iptables #清空iptables中相关ip
148 122.70.137.104#请求次数和ip
Bad guy 122.70.137.104 #审核为采集者
iptables 122.70.137.104 #放入了iptables
=====Thu Jun 10 13:15:01 CST 2010========
None IP #没有超过指定请求数
=====Thu Jun 10 13:18:01 CST 2010========
165 222.236.154.162
155 125.69.85.71
Safe ip 222.236.154.162 #信任的ip
That’s ok 125.69.85.71 #审核为不是采集者
=====Thu Jun 10 13:18:03 CST 2010========
235 202.102.111.124
That’s ok 202.102.111.124 #审核为不是采集者

安装使用
下载脚本并上传到/opt/shell

chmod 0755 /opt/shell/kickleech.sh

以root身份运行

crontab -e

添加自动运行

*/3 * * * * cd /opt/shell && /bin/sh ./kickleech.sh > /dev/null 2>&1

下载脚本
kickleech.zip

Posted in shell, 技术.

Tagged with , .


2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. 福州网站 says

    写的很好,顶支持一下

Continuing the Discussion

  1. sourceforge 上如何使用shell | 吃杂烩 linked to this post on 2012/08/09

    […] shell防采集脚本 ( 2010-06-10) […]



Some HTML is OK

or, reply to this post via trackback.