Copy codeThe Code is as follows:
<Html>
<Body>
<? Php
If (isset ($ _ REQUEST ['submitted']) & $ _ REQUEST ['submitted'] = '1 '){
Echo "Form submitted! ";
}
?>
<Form action = "<? Php echo $ _ SERVER ['php _ SELF '];?> ">
<Input type = "hidden" name = "submitted" value = "1"/>
<Input type = "submit" value = "Submit! "/>
</Form>
</Body>
</Html>
Seemingly accurate code, but hidden in danger. Let's save it as foo. php and then use it in the PHP environment.
foo.php/%22%3E%3Cscript%3Ealert('xss')%3C/script%3E%3Cfoo
Access, you will find a Javascript alert-this is obviously an XSS injection vulnerability. The reason is that
echo $_SERVER['PHP_SELF'];
This statement directly outputs unfiltered values. Let's take a look at the description in the PHP manual.
'PHP_SELF'
The filename of the currently executing script, relative to the document root.
For instance, $_SERVER['PHP_SELF'] in a script at the address
http://example.com/test.php/foo.bar would be /test.php/foo.bar. The __FILE__
constant contains the full path and filename of the current (i.e. included) file.
If PHP is running as a command-line processor this variable contains the script
name since PHP 4.3.0. Previously it was not available.
The reason is clear. It turns out that $ _ SERVER ['php _ SELF '] is an environment variable provided by the SERVER, but this is indeed the same as $ _ POST and $ _ GET, which can be changed by users.
There are many other similar variables, such as $ _ COOKIE (if you want to "play" their cookies, we can't do it ). The solution is simple. Use such functions as strip_tags and htmlentities to filter or escape them.
echo htmlentities($_SERVER['PHP_SELF']);
-- Split --
The above example requires us to always be cautious about coding. Chris Shiflett summarized the following in his Blog: the two basic security ideas of preventing XSS are:
Filter input
Escape output
I translated the above into"Filter input and escape output". For details, refer to his Blog article, which is omitted here.