unitistar

Anti ddos php script

May 21st, 2012
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php  
  2.  
  3.  
  4. $ad_delay=2;  
  5.  
  6. $ad_DirName=$_SERVER['DOCUMENT_ROOT'].'/tmp';  
  7.  
  8.  
  9. $ad_Robots_UserAgent=array(  
  10.    'aipbot',  
  11.    'Aport',  
  12.    'eStyleSearch',  
  13.    'Gigabot',  
  14.    'Gokubot',  
  15.    'Google',  
  16.    'MJ12bot',  
  17.    'msnbot',  
  18.    'PlantyNet_WebRobot',  
  19.    'StackRambler',  
  20.    'TurtleScanner',  
  21.    'Yahoo',  
  22.    'Yandex',  
  23.    'YaDirectBot',  
  24. );  
  25.  
  26. $ad_good_ip = array(  
  27.     '217.107.36.73',  
  28. );  
  29.  
  30.  
  31. function ad_WiteIP($dir){  
  32.    $f=fopen($dir.'/a'.$_SERVER['REMOTE_ADDR'], 'w');  
  33.    fclose($f);  
  34. }  
  35.  
  36. $ad_IsRobot=false;  
  37. foreach ($ad_Robots_UserAgent as $match){  
  38.    if (strstr($_SERVER['HTTP_USER_AGENT'], $match)){  
  39.       $ad_IsRobot=true;  
  40.       break;  
  41.    }  
  42. }  
  43. if( in_array($_SERVER['REMOTE_ADDR'], $ad_good_ip) ) {  
  44.      $good_ip = true;  
  45. } else {  
  46.      $good_ip = false;  
  47. }  
  48.  
  49. if (!$ad_IsRobot AND !$good_ip){  
  50.    session_start();  
  51. }  
  52.  
  53. if (!$ad_IsRobot AND !$good_ip){  
  54.    
  55.    $ad_dir      =opendir($ad_DirName)  
  56.       or die('dir notfount );  
  57.    $ad_now      =time();  
  58.    $ad_forbid   =$ad_now-$ad_delay;  
  59.  
  60.    WHILE (false!==($ad_FName=readdir($ad_dir))){  
  61.       if (ereg('^a[1-9]',$ad_FName)  
  62.          && (@ filemtime($ad_DirName.'/'.$ad_FName)<$ad_forbid)){  
  63.          @ unlink($ad_DirName.'/'.$ad_FName);  
  64.       }  
  65.    }  
  66.    closedir($ad_dir);  
  67.  
  68.    if (file_exists($ad_DirName.'/a'.$_SERVER['REMOTE_ADDR'])){  
  69.    
  70.       header('HTTP/1.0 503 Service Unavailable');  
  71.       header('Status: 503 Service Unavailable');  
  72.       header('Retry-After: '.$ad_delay*3);  
  73. ?>  
  74. <!doctype html public "-//W3C//DTD HTML 4.01//EN"  
  75. "http://www.w3.org/TR/html4/strict.dtd">  
  76. <html>  
  77. <head>  
  78. <title>Ошибка 503</title>  
  79. <meta http-equiv="Content-Type" content="text/html; charset=Windows-1251" />  
  80. </head>  
  81. <body>  
  82. <h1>Error 503 (Service Unavailable)</h1>  
  83. <p>Pless refreh page</p>  
  84. </body>  
  85. </html>  
  86. <?php  
  87.       ad_WiteIP($ad_DirName);  
  88.       exit;  
  89.    }else{  
  90.       ad_WiteIP($ad_DirName);  
  91.    }  
  92. }  
  93. ?>
Advertisement
Add Comment
Please, Sign In to add comment