XSS attacks against $ _ SERVER ['php _ SELF ']

Source: Internet
Author: User

Although the current web servers and development tools will not show any obvious vulnerabilities like asp's % 81, however, some strange vulnerabilities caused by developers' negligence and combinations of various language features still exist. The XSS Woes that we accidentally read today details a dangerous vulnerability related to $ _ SERVER ['php _ SELF.

$ _ SERVER ['php _ SELF '] is often used during development. It is generally used to reference the current webpage address and is a global variable automatically generated by the system. Is there any problem? Let's take a look at the following code:

<Form action = "<? Php echo $ _ SERVER ['php _ SELF '];?> ">
<Input type = "submit" name = "submit" value = "submit"/>
</Form>
This code is very simple. We want to use $ _ SERVER ['php _ SELF '] to submit a webpage to itself when submitting the code. Suppose the code file name is test. php, will we get the desired address during execution? Try the address http://www.bkjia.com/test first. php, the result is of course no problem, don't worry, you visit the http://www.bkjia.com/test again. php/a = 1, the following client code will be obtained:

<Form action = "/fwolf/temp/test. php/a = 1">
<Input type = "submit" name = "submit" value = "submit"/>
</Form>
Obviously, this is beyond our expectation. The web server does not produce errors such as 404, and the page is executed normally, in addition, the generated html code contains some parts that can be entered by the user. This is where the terror lies. Don't underestimate the "a = 1". If you replace it with a piece of js code, it is more dangerous, such as calling this method:

Http://www.bkjia.com/test. php/% 22% 3E % 3 Cscript % 3 Ealert ('xss') % 3C/script % 3E % 3 Cfoo
Have you seen the effect of alert function execution in js? Check the generated html source code to find the cause.

By embedding JavaScript code, attackers can obtain 512 ~ 4 K code space, you can even connect to the js Code of an external website or disguise the js Code by calling the image method, so that the js code length is not limited, and then through js, they can easily retrieve users' cookies or change any content on the current page, such as changing the form submission destination, change the displayed content (for example, add an onclick =... In this way, the user will execute the code specified by the attacker, or even connect to a website that is not the link address itself), or even make an ajax effect. In short, do not ignore the power of js.

So let's take a look at how this vulnerability works. First, test. php /.... This kind of call is allowed by the web server. Many cms systems, such as the plog I used previously, seem to adopt this method. When the server does not support rewrite, it implements http: //... /Index. fixed URLs such as php/archive/999 (I used to think it was for the hand under the 404 error page), so the address with "/" cannot be banned from the web server. Then let's take a look at the recognition of $ _ SERVER ['php _ SELF '] in php. It is a global variable that contains the current URL value. God knows what kind of website the user will enter, in the above example, It is malicious, but on websites such as wikipedia, this method of address can be used normally. Therefore, the final conclusion falls on the developer, and there is no good way to handle the data that interacts with the user.

From a security perspective, when developing applications, especially web applications, data submitted by all users is insecure. This is the basic principle, therefore, we are not tired of client verification and service-side verification. In terms of the security vulnerability mentioned above, the "website" will be added to the insecure content. To solve the security risks of $ _ SERVER ['php _ SELF '], there are two main methods:

1. htmlentities
Use htmlentities ($ _ SERVER ['php _ SELF ']) to replace simple $ _ SERVER ['php _ SELF'], so that even if the website contains malicious code, it will also be "converted" into html code for display, rather than being directly embedded into html code for execution. Simply put, "<" will become "<", it becomes harmless.

2. REQUEST_URI
Use $ _ SERVER ['request _ url'] to replace $ _ SERVER ['php _ SELF ']. You can see the differences between the two variables in phpinfo:

_ SERVER ["REQUEST_URI"]/fwolf/temp/test. php/% 22% 3E % 3 Cscript % 3 Ealert ('xss') % 3C/script % 3E % 3 Cfoo
_ SERVER ["PHP_SELF"]/fwolf/temp/test. php/"> <script> alert ('xss') </script> <foo
$ _ SERVER ['request _ URI '] does not cover the website itself. If % 3C exists in the website, you will also get % 3C, $ _ SERVER ['php _ SELF '] performs a urldecode operation on the URL. The % 3C character in the URL is changed to the character "<", which leads to a vulnerability. Note that, in many cases, the browser will encode the content that the user enters to submit to the web server, and then the server-side program will automatically decode, get the corresponding original meaning, which is the case when we perform post or get operations.

There are also two points to note: the first is <form action "> although this method does not directly use $ _ SERVER ['php _ SELF '], the actual effect is the same, only when an error occurs, it is returned to the next page after the user submits the request. Therefore, do not leave the form action blank. Second, in addition to PHP_SELF, other $ _ SERVER variables may have similar vulnerabilities, such as SCRIPT_URI, SCRIPT_URL, QUERY_STRING, PATH_INFO, PATH_TRANSLATED, etc, before using them, you must perform conversions such as htmlentities.

Finally, provide an address with many XSS examples, which can be used as a negative teaching material or test tool:
XSS (Cross Site Scripting) Cheat Sheet
Http://ha.ckers.org/xss.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.