Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Detecta e informa ataques
- * Esta função necessita de uma conexão MySQL ativa
- * Recomenda-se interromper ou alterar o fluxo do script caso esta função retorne uma string
- *
- * @author Rafael A. R. Dias <[email protected]>
- * @name detectaAtaque
- *
- * @package dias_Complementos
- * @version 11.03.09
- *
- * @param (string)&$string String a ser testada
- * @return (boolean/string) TRUE se estiver OK, ou uma string com a mensagem de ataque caso tenha sido detectado.
- */
- function detectaAtaque ( &$string )
- {
- // Se não esta vazio
- //
- if ( ! empty ( $string ) )
- {
- // Se 'magic_quotes_gpc' estiver ativo, retira os escapes
- //
- $string = ( get_magic_quotes_gpc ( ) ) ? stripslashes ( $string ) : $string;
- // Se tiver tentativa de...
- //
- try
- {
- // SQL Injection simples
- //
- if ( preg_match ( '/(\%27)|(\')|(\-\-)|(\%23)|(#)/i', $string ) )
- {
- // Instanticia exceção
- //
- throw new Exception("Tentativa de SQL Injection simples");
- }
- // SQL Injection por meta-characters
- //
- else if (preg_match ( '/((\%3D)|(=))[^\n]*((\%27)|(\')|(\-\-)|(\%3B)|(;))/i' , $string ) )
- {
- // Instanticia exceção
- //
- throw new Exception("Tentativa de SQL Injection por meta-characters" );
- }
- // SQL Injection normal
- //
- else if ( preg_match ( '/\w*((\%27)|(\'))((\%6F)|o|(\%4F))((\%72)|r|(\%52))/i ' , $string))
- {
- // Instanticia exceção
- //
- throw new Exception ( "Tentativa de SQL Injection normal" );
- }
- // SQL Injection com execução de core
- //
- else if ( preg_match ( '/exec(\s|\+)+(s|x)p\w+/i' , $string ) )
- {
- // Instanticia exceção
- //
- throw new Exception("Tentativa de SQL Injection com execução de core");
- }
- // XSS simples
- //
- else if ( preg_match ( '/((\%3C)|<)((\%2F)|\/)*[a-z0-9\%]+((\%3E)|>)/i' , $string ) )
- {
- // Instanticia exceção
- //
- throw new Exception("Tentativa de XSS simples");
- }
- // XSS
- //
- else if ( preg_match ( '/((\%3C)|<)((\%69)|i|(\%49))((\%6D)|m|(\%4D))((\%67)|g|(\%47))[^\n]+((\%3E)|>)/i' , $string ) )
- {
- // Instanticia exceção
- //
- throw new Exception("Tentativa de XSS");
- }
- // XSS
- //
- else if ( preg_match ( '/((\%3C)|<)[^\n]+((\%3E)|>)/i' , $string ) )
- {
- // Instanticia exceção
- //
- throw new Exception("Tentativa de XSS");
- }
- }
- catch ( Exception $ataque )
- {
- // Retorna mensagem do ataque
- //
- return "[" . date ("d/m/Y H:i:s" ) . "] Foi detectado uma tentativa de invasão pelo IP {$_SERVER['REMOTE_ADDR']}: " . $ataque -> getMessage ( ) . "\r\n";
- }
- // Retorna verdadeiro
- //
- return TRUE;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment