What is cross site scripting?

With cross-site scripting, it’s possible to infect the HTML document produced without causing the web server itself to be infected. An XSS attack uses the server as a vector to present malicious content back to a client, either instantly from the request (a reflected attack), or delayed though storage and retrieval (a stored attack).

An XSS attack exploits a weakness in the server’s production of a page that allows request data to show up in raw form in the response. The page is only reflecting back what was submitted in a request… but the content of that request might hold characters that break out of ordinary text content and introduce HTML or JavaScript content that the developer did not intend.

Here’s a quick example. Let’s say you have some sort of templating language made to produce an HTML page (like PHP, ASP, CGI, or a Velocity or Freemarker script). It takes the following page and substitutes “<?=$name?>” with the unescaped value of the “name” query parameter.

<html>
<head><title>Example</title></head>
<body>Hi, <?=$name?></body>
</html>

Someone calling that page with the following URL:

http://example.com/unsafepage?name=Rumplestiltskin

Should expect to see this message:

Hi, Rumplestiltskin

Calling the same page with something more malicious can be used to alter the page or user experience substantially.

http://example.com/unsafepage?name=Rumplestiltskin<script>alert('Boo!')</script>

Instead of just saying, “Hi, Rumplestiltskin”, this URL would also cause the page to pop up an alert message that says, “Boo!”. That is, of course, a simplistic example. One could provide a sophisticated script that captures keystrokes or asks for a name and password to be verified, or clears the screen and entirely rewrites the page with shock content. It would still look like it came from example.com, because the page itself did, but the content is being provided somewhere in the request and just reflected back as part of the page.

So, if the page is just spitting back content provided by the person requesting it, and you’re requesting that page, then how does a hacker infect your request? Usually, this is accomplished by providing a link, either on a web page or sent to you by e-mail, or in a URL-shortened request, so it’s difficult to see the mess in the URL.

<a href="http://example.com?name=<script>alert('Malicious content')</script>">
Click Me!
</a>

A server with an exploitable XSS vulnerability does not run any malicious code itself– its programming remains unaltered– but it can be made to serve malicious content to clients.

Leave a Comment