ga白色车牌:互联网反爬虫实践 php实现篇

来源:百度文库 编辑:偶看新闻 时间:2024/05/03 06:22:49

互联网反爬虫实践 php实现篇

UncategorizedAdd comments

ban_ip_with_memcached_flood这篇文章根据 互联网网站的反爬虫策略浅析 使用php实现
假设现有一phpwind搭建论坛,在global.php文件内require_once(‘ban_ip_with_memcached_flood.php’)

ban_ip_with_memcached_flood.php文件 0102/* begin iptable with memcache for ban the flood ip*/03 04//memcache 连接05$memcache_obj = new Memcache;06$memcache_host = "127.0.0.1";07$memcache_obj->connect($memcache_host, 11211) or die ("Could not connect memcached at localserver");08 09//获取客户端ip10function getIp()11{12//php获取ip的算法13if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])14{15$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];16}17elseif ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])18{19$ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];20}21elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])22{23$ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];24}25elseif (getenv("HTTP_X_FORWARDED_FOR"))26{27$ip = getenv("HTTP_X_FORWARDED_FOR");28}29elseif (getenv("HTTP_CLIENT_IP"))30{31$ip = getenv("HTTP_CLIENT_IP");32}33elseif (getenv("REMOTE_ADDR"))34{35$ip = getenv("REMOTE_ADDR");36}37else38{39$ip = "Unknown";40}41echo "你的IP:".$ip ;42return $ip;43}44 45$client_ip = getIp();46//防火墙拦截语句 可惜开了 php.ini的safe_mode为on才可使用,并且需要相应权限才能让php重新加载php配置文件47$iptable_statement= "iptables -A INPUT -i eth0 -j DROP -p tcp --dport 80 -s $client_ip";48#echo($iptable_statement);49$ip_counter = $memcache_obj->increment($client_ip);50#var_dump($ip_counter);51if (!$ip_counter) {52   $memcache_obj->set($client_ip,1, 0, 60);53}elseif ($ip_counter>300) {54   $crawler_counter = $memcache_obj->increment("crawler/$client_ip");55   if (!$crawler_counter) {56        $memcache_obj->set("crawler/$client_ip",1, 0, 60);57   }58   elseif ($crawler_counter>50){59        #BlackList.add(ip_sec)60        //执行iptable 防火墙指令61        #echo exec($iptable_statement);62        echo exec("echo 'deny $client_ip;\n'>>/d/nginx/conf/ip_ban.conf");63        #注意nginx 配置文件路径64        #echo exec("nginx -s reload");65        header('HTTP/1.1 403 Forbidden');66        die("Unauthorized access forbidden! 爬虫再见");67   }68   #var_dump($crawler_counter);69 70   header('HTTP/1.0 401 Unauthorized');71   die("Unauthorized access forbidden! 未认证授权");72   #render :template => 'test', :status => 401 and return false73}74 75/* end iptable with memcache for ban the flood ip*/76?>

这里的 ip_ban.conf 是nginx的 httpaccess module 加载配置文件,利用它和crontab来实现每2秒重新reload
nginx配置文件禁止ip访问,从0.8版本开始nginx的reload方法 /d/env/nginx/sbin/nginx -s reload

01mkdir -p /d/env/crontab02#创建cron执行的shell文件03nano /d/env/crontab/nginx_ban_ip.sh04 05#!/bin/env sh06#每2分钟重载nginx配置07/d/nginx/sbin/nginx -t08/d/nginx/sbin/nginx -s reload09 10#shell文件需要x权限11chmod +x /d/env/crontab/nginx_ban_ip.sh12 13#给ip_ban加上www执行权限,由于我的nginx和php都是www用户执行14chmod 777 /d/nginx/conf/ip_ban.conf15chown www:www /d/nginx/conf/ip_ban.conf16ll /d/nginx/conf/ip_ban.conf17 18#crontab -e 添加2分钟执行shell的语句19*/2 *  *  *  *  /d/env/crontab/nginx_ban_ip.sh

找到一款不错的网站压力测试工具webbench[原创]

使用该压力测试对nginx服务器进行虐待,测试以上反爬虫效果,注意linux系统专用
webbench -c 200 -t 30 http://www.xxxxxxxxxxxxxxxxxxxxxxxx.com/

30秒内开启浏览器访问网址吧 ,应该会显示“爬虫再见”,2分钟之后nginx就会重读配置文件,之后返回403,ip地址被禁止访问了。
(403页面可以自定义)