PHP Security - Filename Manipulation

黄舟
Release: 2023-03-05 21:24:01
Original
1032 people have browsed it



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.

 
Copy after login


## 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:

 
Copy after login


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

Copy after login


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):

  
Copy after login


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:

Copy after login


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

Copy after login


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

Copy after login

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)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!