<?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 => 'es',
  ),
  'this' => 
  array (
    0 => 'security.filesystem.nullbytes.php',
    1 => 'Cuestiones relacionadas a bytes nulos',
    2 => 'Cuestiones relacionadas a bytes nulos',
  ),
  'up' => 
  array (
    0 => 'security.filesystem.php',
    1 => 'Seguridad del Sistema de Archivos',
  ),
  'prev' => 
  array (
    0 => 'security.filesystem.php',
    1 => 'Seguridad del Sistema de Archivos',
  ),
  'next' => 
  array (
    0 => 'security.database.php',
    1 => 'Seguridad en bases de datos',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'es',
    '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">Cuestiones relacionadas a bytes nulos</h2>
    <p class="simpara">
     Como <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> utiliza las funciones de C para operaciones relacionadas al sistema de archivos,
     se podría manejar bytes nulos de manera bastante inesperada.
     Como un byte nulo denota el fin de una cadena en C, las cadenas que contengan estos
     no serán consideradas por completo, sino sólo hasta que ocurra un byte nulo.

     El siguiente ejemplo muestra un código vulnerable que presenta este problema:
    </p>
    <div class="example" id="example-5">
     <p><strong>Ejemplo #1 Script vulnerable 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 /></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 devolverá true si el archivo /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">// el archivo /etc/passwd se incluirá<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
    <p class="para">
     Por lo tanto, cualquier cadena que se utiliza en una operación de sistema de archivos siembre deben
     ser validados correctamente. He aquí una versión mejorada del ejemplo anterior:
    </p>
    <div class="example" id="example-6">
     <p><strong>Ejemplo #2 Validando correctamente la entrada</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">// Lista blanca de valores posibles<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); ?>