No matter how you use a file, you must specify a file name somewhere. In many cases, the file name is used as a parameter of the fopen () function, and other functions call the handle returned by it:
File system overlay
No matter how you use a file, you must specify a file name somewhere. In many cases, the file name is used as a parameter of the fopen () function, and other functions call the handle returned by it:
When you use contaminated data as part of a file name, the vulnerability occurs:
In this example, the first and second parts of the path and file name cannot be manipulated by attackers, and the possibility of attacks is limited. However, it should be noted that some attacks will use NULL (expressed as % 00 in the URL) to terminate the string, so that any file extension restrictions can be bypassed. In this case, the most dangerous attack means is to use multiple ../to ask the upper-level directory to achieve the goal of crossing the file system. For example, imagine that the value of filename is specified as follows:
#... Nother/path/to/file
Like many attacks, if contaminated data is used when constructing a string, attackers are given the opportunity to change the string, this will cause your application to run in a way you don't want. If you develop the habit of using only filtered data to create dynamic strings, you can prevent the emergence of many types of vulnerabilities that you are not familiar.
Because the static part leading to the name of the file called by fopen () is/path/to, the number of times the above attacks crossed the directory upwards is more than required. Because the attacker cannot view the source code before initiating an attack, the typical strategy is to repeat the.../string multiple times... The usage of the/string is too many times and does not damage the above attack effect. Therefore, attackers do not have to guess the depth of the Directory.
In the above attack, the fopen () call is run in a way you don't want. it is simplified and equivalent:
After realizing this problem or encountering an attack, many developers attempt to correct potential malicious data errors, and sometimes do not check the data first. As described in Chapter 1, it is best to take filtering as a check process and force users to follow your rules. For example, if a valid file name only contains letters, the following code can enhance this restriction:
There is no need to escape the filename value because the data is only transmitted to the remote system in the PHP function.
The basename () function is useful when checking for unnecessary paths:
This process is a little less secure than only allowing file names to be letters, but it is unlikely that you want to be that strict. A better deep defense process is to combine the above two methods, especially when you use a regular expression to check the validity of the code (instead of using the ctype_alpha () function ()).
When the end of a file name is composed of unfiltered data, a high-risk vulnerability occurs:
Giving attackers more flexibility means more vulnerabilities. In this example, attackers can manipulate the filename parameter to point to any file in the file system, regardless of the path and file extension, this is because the file extension is part of $ _ GET ['filename. Once the WEB server has the permission to read the file, the processing will be directed to the file specified by the attacker.
If the leading part of the path uses contaminated data, this type of vulnerability will become even larger. This is also the topic of the next section.
The above is the PHP Security-file system crossing content. For more information, please follow the PHP Chinese network (www.php1.cn )!