scripts

Auto-blocking Server

Jun 27th, 2014
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.68 KB | None | 0 0
  1. <?php
  2. //I recommend you run this from the command line
  3. $bind_ip = "0.0.0.0"; //all interfaces
  4. $bind_port = "22"; //SSH port
  5. $server = stream_socket_server("tcp://" . $bind_ip . ":" . $bind_port, $errno, $errorMessage);
  6.  
  7. if (!$server)
  8. {
  9.     throw new UnexpectedValueException("Couldn't bind to socket: $errorMessage");
  10. }
  11.  
  12. while (true)
  13. {
  14.     $client = @stream_socket_accept($server);
  15.  
  16.     if ($client)
  17.     {
  18.         $re = explode(":", stream_socket_get_name($client, true));
  19.         $ip = $re[0];
  20.         $string = "iptables -A INPUT -s " . $ip .  " -j DROP";
  21.         echo $ip . " has been blocked!" . "\n";
  22.         exec($string);
  23.         fclose($client);
  24.     }
  25. }
  26.  
  27. ?>
Advertisement
Add Comment
Please, Sign In to add comment