When doing web development, we often do code inspections. Many times, we will randomly check some core functions or logic where loopholes often appear. As the technical team grows, the team members’ skills become increasingly mature. Common fool-type SQL injection vulnerabilities and XSS vulnerabilities. There will be fewer and fewer, but we will also find some emerging covert vulnerabilities that occasionally appear. These vulnerabilities mostly come from developers' insufficient design of a function or common module functions, leaving problems left behind. In the past, we were able to complete some functional modules, but now the requirement is to complete the modules in a safe and correct way. Next, I will share some common functional modules that cause vulnerabilities due to design reasons. Next, let’s first look at the file-reading function vulnerability.
Let’s first look at the following piece of code, which contains different files through the user’s input of different directories
$mod enters ?mod=…%2F…%2F…%2F…%2Fetc%2Fpasswd%00 through construction, we see that the result will be:
Actually include("/etc/passwd") file.
How did you escape my parameter restrictions?
First of all: It is not a good method to use parameter filtering type to limit user input. The general rule is: if it can be tested, do not replace it. As long as it fails the test, pass it directly. Lose! This is one of our principles. There are countless filtering failures. Let’s take a look at the actual process.
1. Enter "…/…/…/" by replacing ".." with "."
2. The result is "../../../" and it becomes Got this
Some friends will ask, would it be better if I just replace it with spaces? It can indeed be replaced in this one. But it doesn’t mean that you can replace everything with spaces in the future. Let’s take another example. For example: someone replaced the javascript in the string. The code is as follows:
The code is as follows:
For example:
$mod = isset($_GET['m'])?trim($_GET['m']):'index'; ///After reading the module name
If the mod variable value range is an enumeration type:
if(!in_array($mod,array('user','index','add','edit'))) exit('err! !!');
$mod is completely restricted and can only be in this array, so cruel! ! ! !
2. How to implement whitelist restrictions
Through the example just now, we know that if it is an enumeration type, just put the value directly into the list. However, sometimes, this is not enough. We have another whitelist restriction method. It is to limit the character range