#!/usr/local/bin/php -q $this->loadlimit){ return 0; } else{ return 1; } } function ignore($ip){ $ips=explode(",",$this->ignore); for($i=0;$i<=count($ips)-1;$i++){ $ignore[]=trim($ips[$i]); } $rules=file_get_contents($this->path.'deny_hosts.rules'); $rules=explode("\n",$rules); foreach($rules as $rule){ if(!eregi('#', $rule)){ $ignore[]=$rule; } } foreach($ip as $i){ if(!in_array($i,$ignore)){ $good[]=$i; } } return $good; } function restartapf(){ ob_start(); $sh=shell_exec("{$this->path}apf -r"); if($sh){return 1;} else{ return 0; } ob_end_clean(); } function banip($ip){ $handle=fopen($this->path.'deny_hosts.rules', "a"); fwrite($handle,"\n".$ip); fclose($handle); return 1; } function getattackers(){ $shell=shell_exec('netstat -ntu | awk \'{print $5}\' | cut -d: -f1 | sort | uniq -c | sort -nr | more'); //$pattern="/.{1,} [1-9]{1,3}\.[1-9]{1,3}\.[1-9]{1,3}\.[1-9]{1,3}/"; //preg_match_all($pattern, $shell,$out); //$ips=$out[0]; $gips=explode("\n",$shell); foreach($gips as $wip){ if(eregi('.',$wip)){ $ips[]=trim($wip); } } foreach($ips as $ip){ $ip=explode(" ",$ip); if( ($ip[0]>=$this->conlimit) ){ $killthem[]=$ip[1]; } } if( eregi('warning, got duplicate',$killthem) ){ return 0; } $killthem=@$this->ignore($killthem); return $killthem; } function report($ips){ foreach($ips as $ip){ $rip .= "$ip\n"; } $subject = "YOUR SERVER WAS ATTACKED!"; $headers= "From: SENTINEL email,$subject,$body,$headers); } function guard(){ do{ sleep($this->rate); $load=$this->goodload(); if(!$load){ echo '.'; $ips=$this->getattackers(); if($ips){ foreach($ips as $ip){ $this->banip($ip); } $this->restartapf(); $this->report($ips); } } } while(1>0); } } $Sentinel=new Sentinel; $Sentinel->guard(); ?>