<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/security.filesystem.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'pt_BR',
  ),
  'this' => 
  array (
    0 => 'security.filesystem.nullbytes.php',
    1 => 'Problemas relacionados aos bytes nulos (NUL)',
    2 => 'Problemas relacionados aos bytes nulos (NUL)',
  ),
  'up' => 
  array (
    0 => 'security.filesystem.php',
    1 => 'Seguran&ccedil;a do Sistema de Arquivos',
  ),
  'prev' => 
  array (
    0 => 'security.filesystem.php',
    1 => 'Seguran&ccedil;a do Sistema de Arquivos',
  ),
  'next' => 
  array (
    0 => 'security.database.php',
    1 => 'Seguran&ccedil;a de Bancos de Dados',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'pt_BR',
    'path' => 'security/filesystem.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="security.filesystem.nullbytes" class="sect1">
    <h2 class="title">Problemas relacionados aos bytes nulos (NUL)</h2>
    <p class="simpara">
     Como o <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> usa funções em C para operações relacionadas ao
     sistema de arquivos, ele pode lidar com bytes nulos de maneira inesperada.
     Como bytes nulos denotam fim de string em C, strings que os contém
     não serão consideradas de forma inteira, mas apenas até que um byte nulo ocorra.

     O seguinte exemplo mostra um código vulnerável que demonstra esse problema:
    </p>
    <div class="example" id="example-5">
     <p><strong>Exemplo #1 Script vulnerável a bytes nulos</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$file </span><span style="color: #007700">= </span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'file'</span><span style="color: #007700">]; </span><span style="color: #FF8000">// "../../etc/passwd\0"<br /><br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">file_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'/home/wwwrun/' </span><span style="color: #007700">. </span><span style="color: #0000BB">$file</span><span style="color: #007700">. </span><span style="color: #DD0000">'.php'</span><span style="color: #007700">)) {<br />    </span><span style="color: #FF8000">// file_exists retornará true porque o arquivo /home/wwwrun/../../etc/passwd existe<br />    </span><span style="color: #007700">include </span><span style="color: #DD0000">'/home/wwwrun/' </span><span style="color: #007700">. </span><span style="color: #0000BB">$file </span><span style="color: #007700">. </span><span style="color: #DD0000">'.php'</span><span style="color: #007700">;<br />    </span><span style="color: #FF8000">// o arquivo /etc/passwd será incluído<br /><br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
    <p class="para">
     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:
    </p>
    <div class="example" id="example-6">
     <p><strong>Exemplo #2 Validando entrada corretamente</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$file </span><span style="color: #007700">= </span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'file'</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">// Autorizando valores possíveis<br /></span><span style="color: #007700">switch (</span><span style="color: #0000BB">$file</span><span style="color: #007700">) {<br />    case </span><span style="color: #DD0000">'main'</span><span style="color: #007700">:<br />    case </span><span style="color: #DD0000">'foo'</span><span style="color: #007700">:<br />    case </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">:<br />        include </span><span style="color: #DD0000">'/home/wwwrun/include/' </span><span style="color: #007700">. </span><span style="color: #0000BB">$file </span><span style="color: #007700">. </span><span style="color: #DD0000">'.php'</span><span style="color: #007700">;<br />        break;<br />    default:<br />        include </span><span style="color: #DD0000">'/home/wwwrun/include/main.php'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
   </div><?php manual_footer($setup); ?>