rdias86

detectaAtaque.func.php

Mar 7th, 2011
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.79 KB | None | 0 0
  1. <?php
  2.     /**
  3.      * Detecta e informa ataques
  4.      * Esta função necessita de uma conexão MySQL ativa
  5.      * Recomenda-se interromper ou alterar o fluxo do script caso esta função retorne uma string
  6.      *
  7.      * @author Rafael A. R. Dias <[email protected]>
  8.      * @name detectaAtaque
  9.      *
  10.      * @package dias_Complementos
  11.      * @version 11.03.09
  12.      *
  13.      * @param (string)&$string String a ser testada
  14.      * @return (boolean/string) TRUE se estiver OK, ou uma string com a mensagem de ataque caso tenha sido detectado.
  15.      */
  16.     function detectaAtaque ( &$string )
  17.     {
  18.         // Se não esta vazio
  19.         //
  20.         if ( ! empty ( $string ) )
  21.         {
  22.             // Se 'magic_quotes_gpc' estiver ativo, retira os escapes
  23.             //
  24.             $string = ( get_magic_quotes_gpc ( ) ) ? stripslashes ( $string ) : $string;
  25.  
  26.             // Se tiver tentativa de...
  27.             //
  28.             try
  29.             {
  30.                 // SQL Injection simples
  31.                 //
  32.                 if ( preg_match ( '/(\%27)|(\')|(\-\-)|(\%23)|(#)/i', $string ) )
  33.                 {
  34.                     // Instanticia exceção
  35.                     //
  36.                     throw new Exception("Tentativa de SQL Injection simples");
  37.                 }
  38.  
  39.                 // SQL Injection por meta-characters
  40.                 //
  41.                 else if (preg_match ( '/((\%3D)|(=))[^\n]*((\%27)|(\')|(\-\-)|(\%3B)|(;))/i' , $string ) )
  42.                 {
  43.                     // Instanticia exceção
  44.                     //
  45.                     throw new Exception("Tentativa de SQL Injection por meta-characters" );
  46.                 }
  47.                 // SQL Injection normal
  48.                 //
  49.                 else if ( preg_match ( '/\w*((\%27)|(\'))((\%6F)|o|(\%4F))((\%72)|r|(\%52))/i ' , $string))
  50.                 {
  51.                     // Instanticia exceção
  52.                     //
  53.                     throw new Exception ( "Tentativa de SQL Injection normal" );
  54.                 }
  55.                 // SQL Injection com execução de core
  56.                 //
  57.                 else if ( preg_match ( '/exec(\s|\+)+(s|x)p\w+/i' , $string ) )
  58.                 {
  59.                     // Instanticia exceção
  60.                     //
  61.                     throw new Exception("Tentativa de SQL Injection com execução de core");
  62.                 }
  63.                 // XSS simples
  64.                 //
  65.                 else if ( preg_match ( '/((\%3C)|<)((\%2F)|\/)*[a-z0-9\%]+((\%3E)|>)/i' , $string ) )
  66.                 {
  67.                     // Instanticia exceção
  68.                     //
  69.                     throw new Exception("Tentativa de XSS simples");  
  70.                 }
  71.                 // XSS
  72.                 //
  73.                 else if ( preg_match ( '/((\%3C)|<)((\%69)|i|(\%49))((\%6D)|m|(\%4D))((\%67)|g|(\%47))[^\n]+((\%3E)|>)/i' , $string ) )
  74.                 {
  75.                     // Instanticia exceção
  76.                     //
  77.                     throw new Exception("Tentativa de XSS");  
  78.                 }
  79.                 // XSS
  80.                 //
  81.                 else if ( preg_match ( '/((\%3C)|<)[^\n]+((\%3E)|>)/i' , $string ) )
  82.                 {
  83.                     // Instanticia exceção
  84.                     //
  85.                     throw new Exception("Tentativa de XSS");  
  86.                 }
  87.             }
  88.             catch ( Exception $ataque )
  89.             {      
  90.                 // Retorna mensagem do ataque
  91.                 //
  92.                 return "[" . date ("d/m/Y H:i:s" ) . "] Foi detectado uma tentativa de invasão  pelo IP {$_SERVER['REMOTE_ADDR']}: " . $ataque -> getMessage ( ) . "\r\n";
  93.             }
  94.  
  95.             // Retorna verdadeiro
  96.             //
  97.             return TRUE;
  98.         }
  99.     }
  100. ?>
Advertisement
Add Comment
Please, Sign In to add comment