The information in this article applies to:
SUMMARY
While dynamically generating Web pages, you might inadvertently introduce some security risks to clients that support scripting. Malicious script can be embedded within input submitted to Web servers. If the Web server returns this data to the client without modification, the client assumes that the script originated at the Web server. If the Web server is trusted by the browser, then the script is executed even though the source of the script is not originally the Web server. MORE INFORMATIONThe ProblemMany Web servers dynamically generate HTML-based input that is not confirmed to contain valid data. If input is not validated, then malicious script can be embedded within the data. If a server-side application such as a CGI script, ISAPI Extension, ISAPI Filter, and so forth, returns HTML based on malicious input, the script runs on the browser as though the trusted site generated it. The following is one scenario:
It is important to realize that instead of "Joe," the user could have entered malicious script, which would then be passed back to the browser by the server application without any validation.
RamificationsIf input to your dynamic Web pages is not validated, you might encounter the following:
PreventionYou need to evaluate your specific situation to determine which techniques work best for you.NOTE: In all techniques, you are validating data that you receive from input, and not your trusted script. Essentially, prevention means that you follow good coding practice by running sanity checks on input to your Web application. The following general approaches for preventing cross-site scripting attacks are presented here:
Data inserted into an output stream originating from a server appears as originating from that server to a client application. Consider hard-coding your output rather than dynamically generating output based on submitted data. For example, if you have a Web page that accepts an input parameter that writes out the user's name to the Web page, such as "Hello Fred," you might consider writing out a something more generic such as "Hello user." Filter input parameters for special characters To filter input, remove some or all "special" characters from your input. Special characters are characters that enable script to be generated within an HTML stream. Special characters include the following:
Note that your individual situation may warrant the filtering of additional characters or strings beyond the special characters noted above. While filtering can be an effective technique, there are a few caveats:
This technique is similar to filtering input except that you filter characters that will be written out to the client. While this can be an effective technique, it might present a problem for Web pages that write out HTML elements. For example, on a page that writes out <TABLE> elements, a generic function that removes the special characters would strip the < and > characters, thus ruining the <TABLE> tag. Therefore, in order for this technique to be useful, you would only filter data passed in or data that was previously entered by a user and/or stored in a database. Encode output based on input parameters for special characters Encode data received as input when you write it out as HTML. This technique is effective on data that was not validated for some reason during input. By using techniques such as HTML Encoding and URL encoding, you can prevent malicious script from executing. HTML Encoding replaces special characters such as < > & " with strings < > & ". URL encoding replaces non-printable characters with their hexadecimal equivalents. So "Hello, World!" looks like "Hello,+World%21". The following function demonstrates how to encode output data in C, and therefore can be used in ISAPI Extensions, ISAPI Filters, or CGIs that pass user input directly to the server. Note that this function only encodes < > & and ". You may need to encode other characters as well.
Here is an example of ISAPI code that is susceptible to cross-site security issues:
Here is the corrected code:
Please note that the issue equally affects MFC ISAPI extensions, MFC ISAPI filters, and CGI.
Possible Sources of Malicious DataWhile the problem applies to any page that uses input to dynamically generate HTML, the following are possible sources of malicious data to help you locate potential security risks:
ConclusionThe following are key points to remember regarding the cross-site Scripting security problem:
REFERENCES
For additional information, please see: Additional query words: Cross-site; CERT Advisories; CERT; CA-2000-02; Security
Keywords : kbCGI kbIE kbISAPI kbIIS |
Last Reviewed: February 2, 2000 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |