Advertisement
Klajdi369

DDoS

Aug 6th, 2017
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.57 KB | None | 0 0
  1. <?php
  2. # get the visitor ip
  3. $i = $_SERVER["REMOTE_ADDR"];
  4. # get the filename and location
  5. $f = $_SERVER["DOCUMENT_ROOT"].'/dir/log/'.ip2long($i).'.dat';
  6. # check if the file exists and we can write
  7. if ( is_file($f) ) {
  8.     # get the last filetime
  9.    $a = filemtime($f);
  10.     # get the file content
  11.    $b = file_get_contents($f);
  12.     # create array from hits & seconds
  13.    $d = explode(':',$b);
  14.     # calculate the new result
  15.    $h = (int)$d[0] + 1;
  16.     $s = (int)$d[1] + (time()-$a);  
  17.     # add the new data tot text file
  18.    file_put_contents($f,"$h:$s",LOCK_EX);
  19.     unset($d);
  20. }else{
  21.     # create the file if it doesn't exist hits:seconds
  22.    file_put_contents($f,"1:1",LOCK_EX); #size: 3kb
  23.    # to make sure we can write
  24.    # chmod($f,0755);
  25.    # set the hits to zero
  26.    $h = 0;
  27. }
  28. # create a result var
  29. $r = $h > 10 ? (float)$s/$h : (float)1;
  30. # calculate the diff after 10 hits, and ban when the avg is smaller than 0.20 seconds (5 hits per second)
  31. if( $r < 0.20 ) {
  32.     # check if we can open htaccess
  33.    $fp = @fopen('../.htaccess','a');
  34.     if($fp){
  35.         # add the ip to htaccess
  36.        @fwrite($fp,"\r\n#DDoS\r\ndeny from $i");
  37.         # close
  38.        @fclose($fp);
  39.         # mail the admin
  40.        //@mail("email@yahoo.com","IP Banned","Ip: $i with $r sbh (Seconds Between Hits)");
  41.     }
  42.     # remove file and let the user know why we deny him or her access
  43.    unlink($f);
  44.     die('To many requests.');
  45.    
  46. }
  47. # if the user leaves, reset
  48. if( $r > 30 ) {
  49.     unlink($f);
  50. }
  51. echo '<a>Result: '.$r.'sbh (Seconds Between Hits)</a>';
  52. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement