Advertisement
Guest User

Listar IPs com Muitos Acessos a Determinada Porta

a guest
Oct 15th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.36 KB | None | 0 0
  1. <?
  2.  
  3.     if ( count($argv) < 3 ) {
  4.         print "Argumento invalido: informe a porta e o numero maximo de conexoes permitidas\n";
  5.         exit('Sintaxe de uso: ' . $argv[0] . ' <porta_a_ser_observada> <numero_maximo_de_conexoes>' . "\n");
  6.     }
  7.        
  8.     $file = str_replace('.php', '.txt', __FILE__);
  9.     @exec('/bin/netstat -nt | /bin/cut -d: -f2 | /bin/sort | /usr/bin/uniq -c | /bin/sort -n > ' . $file . ' 2>/dev/null');
  10.     $lines = file($file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
  11.    
  12.     foreach ($lines as $line) {
  13.    
  14.         $info = preg_split('#\s+#', $line);
  15.         list($number, $port, $ip) = trim_empty_values($info);
  16.         if ( !is_numeric($port) ) continue;
  17.        
  18.         if ( $port == $argv[1] ) {
  19.             echo 'Ha ', $number, ' conexoes a porta ', $port, ' a partir do IP ', $ip, "\n";  
  20.             if ( $number >= $argv[2] ) {           
  21.                 echo "O host $ip ultrapassou o limite de conexoes ($number)\n";
  22.                 if ( !in_array($ip, ['127.0.0.1', trim(`/bin/hostname --ip-address`)]) ) {
  23.                     # passthru('/usr/sbin/csf -d ' . $ip . ' "Ataque ao servidor" ');
  24.                 }
  25.             }
  26.         }
  27.         }
  28.        
  29.         function trim_empty_values ($array) {
  30.             foreach ( $array as $key => $value ) {
  31.                 if ( empty($value) ) unset($array[$key]);
  32.             }
  33.             return array_values($array);
  34.         }
  35. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement