因一个朋友的网站终日遭一心理变态人士的攻击,四层攻击: SYN洪水攻击 TCP连接攻击;七层攻击: HTTP Flood(CC攻击)。虽然配置了个8核的服务器,但依然被击垮。为了减轻攻击带来的危害,特写了以下代码进行抵御(基于discuz x3 框架)。经实践,起到了作用,虽说不能完全杜绝,但至少让对方攻击的成本大大提高。
//获取客户端IP $fip = $_G['clientip']; //取被列入黑名单的IP地址 $blackipstr = memory('get', 'blackips'); //判断是否被列入黑名单了,若是则中止访问 if($blackipstr){ if(strexists($blackipstr, $fip)){ exit(); } } //未被列入黑名单的话则对其访问进行控制.TIMESTAMP. $usertag = $fip."@".cutstr(TIMESTAMP,9,''); if(memory('get', $usertag)){ $ppp = memory('get', $usertag); if($ppp>50){ //允许的蜘蛛IP $zhizhu = array("220.181","123.125","66.249","157.55","157.56","65.55","131.253"); $efiparr = explode(".",$fip); $efip = $efiparr[0].".".$efiparr[1]; //加入黑名单 if(!in_array($efip,$zhizhu)){ memory('set', 'blackips',$blackipstr."|".$fip,18000); exit(); } }else{ memory('set', $usertag,$ppp+1,20); } }else{ memory('set', $usertag,1,20); }