downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

Segurança de Bancos de Dados> <Segurança do Sistema de Arquivos
[edit] Last updated: Fri, 26 Apr 2013

view this page in

Problemas relacionados a bytes nulos (Null)

Como o PHP usa funções em C para operações relacionadas ao sistema de arquivos, ele pode lidar com bytes nulos de maneira inexperada. Como bytes nules denotam fim de string em C, strings contendo eles não serão consideradas por inteiro, mas apenas até que um byte nulo ocorra. O seguinte exemplo mostra um código vulnerável que demonstra esse problema:

Exemplo #1 Script vulnerável à bytes nulos

<?php
$file 
$_GET['file']; // "../../etc/passwd\0"
if (file_exists('/home/wwwrun/'.$file.'.php')) {
    
// file_exists will return true as the file /home/wwwrun/../../etc/passwd exists
    
include '/home/wwwrun/'.$file.'.php';
    
// the file /etc/passwd will be included
}
?>

Portanto, qualquer string comprometida que é usada em uma operação de sistema de arquivos deve sempre ser validada corretamente. Aqui está uma versão melhorada do exemplo anterior:

Exemplo #2 Validando entrada corretamente

<?php
$file 
$_GET['file']; 

// Whitelisting possible values
switch ($file) {
    case 
'main':
    case 
'foo':
    case 
'bar':
        include 
'/home/wwwrun/include/'.$file.'.php';
        break;
    default:
        include 
'/home/wwwrun/include/main.php';
}
?>


add a note add a note User Contributed Notes Problemas relacionados a bytes nulos (Null) - [1 notes]
up
-3
kpobococ at gmail dot com
3 years ago
Since problems with null bytes do not stretch to regular string functions, this should be enough to ensure no GET parameter contains them any more:

<?php
function getVar($name)
{
   
$value = isset($_GET[$name]) ? $_GET[$name] : null;
   
    if (
is_string($value)) {
       
$value = str_replace("\0", '', $value);
    }
}
?>

Modifying this to work with other superglobals should not be a problem, so I will leave it up to you.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites