Here’s an HTML injection (aka cross-site scripting) example that’s due to a series of tragic assumptions that conspire to not only leave the site vulnerable, but waste lines of code doing so.
The first clue to the flaw lies in the querystring’s
state parameter. The site renders the
state value into a
title element. Naturally, a first test payload for HTML injection would be attempting to terminate that element. If that works, then a more impactful followup would be to append arbitrary markup such as
<script> tags. A simple probe looks like this:
The site responds by stripping the payload’s
</title> tag and all subsequent characters. Only the text leading up to the injected tag is rendered within the
<HTML> <HEAD> <TITLE>abc</TITLE>
This seems to have effectively countered the attack. Of course, if you’ve been reading this blog for a while, you’ll suspect this initial countermeasure won’t hold up – that which seems secure shatters under scrutiny.
The developers worried that an attacker might try to inject a closing
</title> tag. Consequently, they created a filter to watch for such payloads and strip them. This could be implemented as a basic case-insensitive string comparison or a trivial regex.
And it could be bypassed by just a few characters.
Consider the following closing tags. Regardless of whether they seem surprising or silly, the extraneous characters are meaningless to HTML yet meaningful to our exploit because they belie the assumption that regexes make good parsers.
<%00/title> <""/title> </title""> </title id="">
After inspecting how the site responds to each of the above payloads, it’s apparent that the filter only expected a so-called “good”
</title> tag. Browsers don’t care about an attribute on the closing tag. They’ll ignore such characters as long as they don’t violate parsing rules.
Next, we combine the filter bypass with a payload. In this case, we’ll use an image
The attack works! We should have been less sloppy and added an opening
<TITLE> tag to match the newly orphaned closing one. A nice exploit won’t leave the page messier than it was before.
<HTML> <HEAD> <TITLE>abc</title id="a"> <img src=x onerror=alert(9)> Vulnerable & Exploited Information Resource Center</TITLE>
The tragedy of this flaw is that it shows how the site’s developers were aware of the concept of HTML injection exploits, but failed to grasp the underlying principles of the vuln. The effort spent blocking an attack (i.e. countering an injected closing tag) not only wasted lines of code on an incorrect fix, but instilled a false sense of security. The code became more complex and less secure.
The mistake also highlights the danger of assuming that well-formed markup is the only kind of markup. Browsers are capricious beasts. They must dance around typos, stomp upon (or skirt around) errors, and walk bravely amongst bizarrely nested tags. This syntactic havoc is why regexes are notoriously worse at dealing with HTML than proper parsers.
There’s an ancillary lesson here in terms of automated testing (or quality manual pen testing, for that matter). A scan of the site might easily miss the vuln if it uses a payload that the filter blocks, or doesn’t apply any attack variants. This is one way sites “become” vulnerable when code doesn’t change, but attacks do.
And it’s one way developers must change their attitudes from trying to outsmart attackers to focusing on basic security principles.