CopyCode The 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 % 3 cscript % 3 ealert ('xss') % 3C/script % 3E % 3 cfoo
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 wocould be/test. php/Foo. Bar. The _ file __
Constant contains the full path and filename of the current (I. e. Encoded DED) 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.