打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
互联网反爬虫实践 php实现篇

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

ban_ip_with_memcached_flood.php文件

01<?php
02/* 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//获取客户端ip
10function 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}
37else
38{
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 false
73}
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/crontab
02#创建cron执行的shell文件
03nano /d/env/crontab/nginx_ban_ip.sh
04 
05#!/bin/env sh
06#每2分钟重载nginx配置
07/d/nginx/sbin/nginx -t
08/d/nginx/sbin/nginx -s reload
09 
10#shell文件需要x权限
11chmod +x /d/env/crontab/nginx_ban_ip.sh
12 
13#给ip_ban加上www执行权限,由于我的nginx和php都是www用户执行
14chmod 777 /d/nginx/conf/ip_ban.conf
15chown www:www /d/nginx/conf/ip_ban.conf
16ll /d/nginx/conf/ip_ban.conf
17 
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页面可以自定义)

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
PHP下封杀野蜘蛛Spider/Bot的方法 ? 小米乐园
nginx+php负载均衡集群环境中的session共享方案梳理
服务器的大用户量的承载方案 Nginx Squid Apache PHP MySQL
使用memc-nginx和srcache-nginx构建高效透明的缓存机制
从一道百度面试题说起
centos x86
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服