加固你的ECSHOP,阻止CC攻击
blacklist.php
备注:写在前面,可以说,这段代码现在看来没JB毛用。。。。
<?php
/**
* blacklist.php
* 加固你的ECSHOP,阻止CC攻击
*
* @author:Honk Tang <honk@soido.org>
* @blog: http://soido.org
* @see: http://soido.org/blog/877
*
**/
$debug = isset($_GET[‘debug’])?true:false; //传入debug参数可开启调试模式
ini_set(“display_errors”,(($debug)?”on”:”off”));
$ip = $_SERVER[‘REMOTE_ADDR’]; //访问者IP
$ip_max = 10; //同一IP最大在线用户数
$block_ip = true; //用户数超限时是否将IP加入黑名单
$blacklist = array();
$file = dirname(__FILE__).”/black.list”; //黑名单
require_once($file); if($debug){var_dump($blacklist);}
if (in_array($ip, $blacklist)){ //阻止黑名单中的IP访问
header(‘HTTP/1.0 404 Not Found’);
exit;
}
$link = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name, $link);
$sql = <<<SQL
select distinct(ip) from `ecs_sessions`
where ip in
(select ip from `ecs_sessions` group by ip
having(count(ip)>$ip_max))
SQL;
$result = mysql_query($sql); //查出用户数超限的IP
$ips = array();
$tmp = “”;
while($row = mysql_fetch_array($result)){
array_push($ips, $row[‘ip’]);
}
mysql_free_result($result);
if(count($ips)>0){
//踢走该IP的所有用户,以免ecs_sessions表被撑爆
$sql = “delete from `ecs_sessions`”;
$sql .= ” where ip in (‘”.implode(“‘,'”, $ips).”‘)”;
mysql_query($sql); if($debug){echo($sql);}
if($block_ip){ //加入黑名单
$tmp = “\r\n\$blacklist[]='”.implode(“‘;\r\n\$blacklist[]='”,$ips).”‘;”;
$fp = fopen($file, “a+”);
fwrite($fp, $tmp);
fclose($fp);
}
}
mysql_close($link);
//exit;
?>