Home  >  Article  >  Backend Development  >  PHP Security - Filename Manipulation

PHP Security - Filename Manipulation

黄舟
黄舟Original
2017-02-21 09:17:371159browse



File name manipulation

Dynamic inclusion is used in many situations, where part of the directory name or file name is saved in a variable. For example, you can cache some of your dynamic pages to reduce the load on your database server.

 <?php
 
  include "/cache/{$_GET[&#39;username&#39;]}.html";
 
  ?>


## To make this vulnerability more obvious, $_GET is used in the example. This vulnerability also exists if you use contaminated data. Using $_GET['username'] is an extreme example through which the problem can be seen more clearly.

While the above flow has its advantages, it also provides an attacker with an opportunity to freely choose to cache pages. For example, a user can easily view other users' cache files by editing the value of username in the URL. In fact, an attacker can view all files with the .html extension in the /cache directory by simply changing the value of username to the corresponding file name (without the extension).

http://www.php.cn/

Although the program limits the directories and file names that attackers can manipulate, changing file names is not the only method available. An attacker can creatively reach across the file system and view .html files in other directories to discover sensitive information. This is because you can use the parent directory in the string to perform directory spanning:

http://www.php.cn/

The running results of the above URL are as follows:

 <?php
 
  include "/cache/../admin/users.html";
 
  ?>


At this time,...means The parent directory of /cache, which is the root directory. So the above example is equivalent to:

<?php
 
  include "/admin/users.html";
 
  ?>


Since all files will be in the root directory of the file system, this process allows an attacker to access all .html files on your server.

On some platforms, the attacker can also use a NULL to terminate the string, for example:

http://www.php.cn/

This successfully bypasses the restriction of .html file extension.

Of course, it is impossible to blindly guess all the malicious attack methods of attackers. No matter how many controls you add to the file, you cannot eliminate the risk. It is important to never use tainted data when including dynamically. The means of attack are not static, but the vulnerabilities will not change. This vulnerability can be fixed by simply filtering the data (see Chapter 1):

  <?php
 
  $clean = array();
 
  /* $_GET[&#39;filename&#39;] is filtered and stored in
$clean[&#39;filename&#39;]. */
 
  include "/path/to/{$clean[&#39;filename&#39;]}";
 
  ?>


If you confirm that there is only the file name part in the parameter but no path information, another effective trick is to use basename( ) to filter data:

<?php
 
  $clean = array();
 
  if (basename($_GET[&#39;filename&#39;] ==
$_GET[&#39;filename&#39;])
  {
    $clean[&#39;filename&#39;] = $_GET[&#39;filename&#39;];
  }
 
  include "/path/to/{$clean[&#39;filename&#39;]}";
 
  ?>


## If you allow path information but want to simplify it before detection, you can use the realpath() function:

<?php
 
  $filename =
realpath("/path/to/{$_GET[&#39;filename&#39;]}");
 
  ?>


## Via above The result of program processing ($filename) can be used to confirm whether it is located in the /path/to directory:

<?php
 
  $pathinfo = pathinfo($filename);
 
  if ($pathinfo[&#39;dirname&#39;] == &#39;/path/to&#39;)
  {
    /* $filename is within /path/to */.
  }
 
  ?>

If the detection fails, you should record the request in the attack log for later investigation. This is especially important when you use this process as a defense-in-depth measure, because you want to determine why other security measures are failing.

The above is the content of PHP security-file name manipulation. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn