![Security Security](http://a.fsdn.com/sd/topics/security_64.png)
The Anatomy of Cross Site Scripting 208
LogError writes "Many documents discuss the actual insertion of HTML into a vulnerable script, but stop short of explaining the full ramifications of what can be done with a successful XSS attack. While this is adequate for prevention, the exact impact of cross site scripting attacks has not been fully appreciated. This paper will explore those possibilities."
Text Version for People Who Hate PDFs (Score:3, Informative)
Anatomy, Discovery, Attack, Exploitation
by Gavin Zuchlinski (gav@libox.net )
http://libox.net/
November 5, 2003
Introduction
Cross site scripting (XSS) flaws are a relatively common issue in web
application security, but they are still extremely lethal. They are
unique in that, rather than attacking a server directly, they use a
vulnerable server as a vector to attack a client. This can lead to
extreme difficulty in tracing attackers, especially when requests are
not fully logged (such as POST requests). Many documents discuss the
actual insertion of HTML into a vulnerable script, but stop short of
explaining the full ramifications of what can be done with a successful
XSS attack. While this is adequate for prevention, the exact impact of
cross site scripting attacks has not been fully appreciated. This paper
will explore those possibilities.
Anatomy of a Cross Site Scripting Attack
A cross site scripting attack is typically done with a specially crafted
URI that an attacker provides to their victim. The XSS attack could be
considered analogous to a buffer overflow, where the injected script is
similar to overwriting an EIP. In both techniques, there are two options
once a successful attack has occurred: insert funny data or jump to
another location. Insertion of funny data during a buffer overflow
typically results in a denial of service attack. In the case of a XSS
attack, it allows the attacker to display arbitrary information, and
suppress the display of the original webpage. When jumping to
another location during a buffer overflow attack, the new location is
another location in memory where shellcode or other important data
resides - allowing the attacker to take control of the flow of the
program. Within the XSS context, the attacker instead jumps the
victim to another location on the Internet (typically under the
attacker's control), hijacking the victim's web browsing session.
Discovery
But how do cross site scripting attacks occur? XSS attacks are the
result of flaws in server- side web applications and are rooted in user
input which is not properly sanitized for HTML characters. If the
attacker can insert arbitrary HTML then they could control execution of
the page under permissions of the site. A simple page vulnerable to
cross site scripting looks like:
Once the page is accessed, the variable sent via the GET method is
placed directly on the rendered page. Since the input is not marked as
variable input , the user- supplied input is interpreted exactly as its
metacharacters command, very similar to SQL injection. Passing
"Gavin Zuchlinski" as an argument outputs the content in correct form:
Sending input with HTML metacharacters allows for unexpected output:
The input is not validated by the script before rendering by the victim's
web browser. This allows for user controlled HTML to be inserted on to
the vulnerable page. Occasionally user input not directly parsed by the
script it is sent to. Rather, the data is inserted into a file or database
and retrieved later to be reinserted on the page.
Common points where cross site scripting exists are confirmation
pages (such as search engines which echo back user input in the event
of a search) and error pages that help the user by filling in parts of the
form which were correct. Commonly in the latter case (and sometimes
the former) the containment of the form text box must be escaped
with a quote and a greater than sign ("> - the quote closes the value
property and the greater than closes the tag).
Attack
Once a vulnerable input is identified the valid HTTP methods must be
determined. The way in which the variables are sent to the target
script is an important consideration; are variables sent by GET, POST,
or will either work? Some scripts are specific, but several which use
canned methods (like PHP and Perl scr
Re:Text Version for People Who Hate PDFs (Score:3, Funny)
Re:Text Version for People Who Hate PDFs (Score:2)
If when displaying some text you assume that text == HTML and just paste it in, so that
Re:Text Version for People Who Hate PDFs (Score:2, Insightful)
Re:Text Version for People Who Hate PDFs (Score:1)
Many people consider cut-and-pasting the article to be inherently redundant. I generally agree with them.
Re:Text Version for People Who Hate PDFs (Score:5, Insightful)
An accurate judgement, no doubt, but the point is this:
Is there any value in moderating the post as redundant - redundant it may be, but useful, and arguably more useful than its moderation as redundant
Re:Text Version for People Who Hate PDFs (Score:2, Insightful)
Can someone explain? (Score:2, Informative)
Re:Can someone explain? (Score:2, Funny)
DUH.
Re:Can someone explain? (Score:2)
One of those mysteries of life that philosophers have spent years pondering.
Re:Can someone explain? (Score:1)
In catacombs under Rome you can see 'X' carved into the walls, which was done by Christians who were persacuted by the Romans.
Re:Can someone explain? (Score:2)
Re:Can someone explain? (Score:2)
Actually, Chi is quite simply the first letter of the Greek (the new testament was written in Greek, remember?) spelling of Christ, and it just happens to look like an X.
Jesus on the other hand just with an Iota (Greek didn't have a J equivalent).
Here's what that actually looks like [rr.com].
Re:Can someone explain? (Score:3, Interesting)
The fish thing is from the Greek word "Ikhthus" {iota, chi, theta, upsilon, sigma} meaning "fish" but also forming an acronym of the Greek words which translate as "Jesus Christ, Son of God, Saviour". The early Christians, forced to meet in secrecy, identified themselves to one ano
Re:Can someone explain? (Score:3, Informative)
Re:Can someone explain? (Score:1)
Aw hell, it's not my religion anyways, what would I know.
Re:Can someone explain? (Score:1)
Re:Can someone explain? (Score:2, Informative)
Google, dumbass. Do you speak it?
Re:Can someone explain? (Score:2)
Re:Can someone explain? (Score:2, Informative)
It was used on bugtraq once or twice and just stuck - there are a few more examples in this XSS FAQ [cgisecurity.com].
Re:Can someone explain? (Score:5, Informative)
The main reason Cross Site Scripting is abbreviated XSS (instead of CSS) is to avoid confusing it with Cascading Style Sheets (this confusion is likely to happen since both of these things are related to web pages).
Re:Can someone explain? (Score:1)
Re:Can someone explain? (Score:1)
Re:Can someone explain? (Score:2)
In HTML 4.0, you can use ∴
To support HTML 3.2 you have to use
The lowest common denominator (no pun intended) is
But of course (Score:5, Funny)
Why yes.
You ever notice that "C" stands for "Cookie"?
It's good enough for me.
Now find me some Crescent shaped cookies.
Re:But of course (Score:1)
Re:Can someone explain? (Score:2)
Re:Can someone explain? (Score:2)
Re:Can someone explain? (Score:2)
Xtreme Windows
Window XtremeP
javaXtreme.swing
Mac OS Xtreme
XtremeML
XtremeSL
mmmh... I dunno...
Booring (Score:2, Interesting)
Re:Booring (Score:5, Insightful)
Your target is one or more users of a community web site. The site itself isn't the target, only the means to your own ends. Remember, it's the users you are after, not the site itself. So you smash the stack on the server, grap the mySQL database, and open it up. Bummber, all the passwords are md5'd and basically useless. With XSS you could conceivably alter the login for that they get, and before md5($password) is executed you export $password (still in plain text) off to your little database.
Cracking isn't about what is the most "exciting and leet" way to do it, it's about using the tools you have at your disposal to get what you want done, done. Sometimes this is a buffer overflow, sometimes it a XSS attack, sometimes an emailed trojan, and sometimes even social engineering to gain physical access (even via an unwitting human proxy).
Re:Booring (Score:3, Informative)
1) You can put the user ID and MD5-encoded password in your own cookies, and log in as the user.
2) You can find another site that user logs in on, find their user ID, and use the captured MD5 password to log in as them -- people tend to use the same password in many places
3) You can feed the MD5 password into a password cracker. If it's in a dictionary, you'll get the cleartext version in seconds; brute-forcing all possible 7-character pass
Re:Booring (Score:2)
What if the intended target server is using https? Would the grabbing script {presumably ordinary http} get the encrypted or the plaintext version? If the browser thinks it's sending the form via https, then it has no reason to send out the unencrypted version - does it?
The big threat from XSS is that, assuming the site isn't using secure cookies, it doesn't ma
Re:Booring (Score:2)
A cross-site scripting exploit would be posting a specially crafted comment to the forums what would result in visitors' cookies being sent to the attacker.
Depends on your definition of "boring" (Score:1)
I think it would be possible to design a self-propagating exploit, that would "infect" user sessions and then use those sessions to "infect" more vulnerable pages, which leads to more infected sessions, and so on. If the attack script was be com
Re:Depends on your definition of "boring" (Score:1)
This actually happened on Advogato [advogato.org].
There was a lack of filtering on one of the sites variables and a crafty user created a "virus" that spread from profile to profile.
If you viewed an infected page when logged in your own profile was updated to contain a copy of the infectuous code.
Full details here [advogato.org].
XSS Protection (Score:5, Informative)
Cross Site Scripting attack protection is a standard feature of many network security products these days. Check Point NG with Application Intelligence (Feature Pack 4 in other words) includes XSS protection as part of its' so-called SmartDefense. I am curious if anyone has run into situations where SmartDefense is screwing up legitimate traffic, especially traffic that resembles an XSS attack.
BTW, does anybody have some good recommendations for cheaper alternatives with pretty comparable protection to Check Point? I would like something that is as defensive, but not as configurable or extensible.
Re:XSS Protection (Score:2)
"Cross-site scripting" or XSS sounds like a fancy way of saying "failed to properly escape arbitrary client-submitted data". If user input is properly escaped (or otherwise cleansed) when outputted to a web page, there shouldn't be any vulnerability or interference with ligitimate input submission.
Re:XSS Protection (Score:2)
Re:XSS Protection (Score:1)
Re:XSS Protection (Score:3, Informative)
hardware accelerated vpns, available redundancy/HA, straightforward config, and no need to buy/maintain server class hardware + os in order to run it (no moving parts except fan I think).
not a bad deal if you don't need specific Checkpoint features. unfortunately their last firmware update seems to have undone the "simplicity factor" that they were so popula
Re:XSS Protection (Score:1)
Re:XSS Protection (Score:3, Informative)
Short solution (Score:5, Insightful)
Handle all user input as it was written by satan himself, and only allow input complying to your strict specification.
Just like (good) firewalls: (Score:2)
Of course, you should also familiarize yourself with the mechanisms your language of choice has to help defend against such attacks. In PHP, this means register_globals = off, and there are also freely available input validation functions designed with XSS in mind.
I like to maks sure that as many of my forms use POST as possible, and include code at the top to halt processing if anyone attempts to pass a PHP p
Re:Just like (good) firewalls: (Score:1)
The issue is addressed by simply parsing any user input that's sent back to the browser. This parsing can be as simple as replacing HTML brackets <> with entity codes <>
This is as basic as web development 101 gets. Any site that falls vicitm to XSS does so due to sloppy coding at its best, and rightly deserves to be compromised!
Re:Just like (good) firewalls: (Score:2)
I don't see how you can trick someone into clicking on a URL that sends POST values using their Web browser. You could send it using something like netcat, sure. Just because something doesn't completely eliminate the problem doesn't mean it does nothing at all.
Also, you can't just rely on replacing HTML brackets, especially if you're using any sort of SQL database (or any database, really). Even if you're not, your scripts could be tricked into revealing the contents of files.
Re:Just like (good) firewalls: (Score:1)
And you're right, it's not just brackets that need replacing, quotes should be replaced too. This massive set of 3 characters are all that defines the bulk of HTML.
However, the issue with XSS is formatting user input that is sent back to the browser.
Obviously user input must be parsed for insertion into SQL queries, but this is not an XSS issue.
As for code being tricked by user input, I've never heard of anyone actually writing code that attempts
Re:Just like (good) firewalls: (Score:2)
Really?
<a href="mylink.html" onClick="javascript:document.forms.length++;
theForm=document.forms[document.forms.length-1];
theForm.action='http://myserver.com';
theForm.elements.length=1;
theForm.elements[0].name='password';
theForm.elements[0].value='yourpw';
theForm.elements[0].type='text';
theForm.submit();">
English for Geeks 101 (Score:1)
Supposing that the above hello.php was on the same domain as a message board, posting the link to the board would illicit many victims.
That's 'elicit', not 'illicit'.
Re:English for Geeks 101 (Score:4, Funny)
Hey, if you don't like the affect of English
spelling history, you can just immigrate to
some place where they speak Canadian. Your
allusions of superiority try to make capitol
of the principals of colloquial language, but
in doing so they create a climactic change
which I find frankly unseasoned.
Re:English for Geeks 101 (Score:1)
Re:English for Geeks 101 (Score:2)
spelling history, you can just immigrate to
some place where they speak Canadian.
Like up in here in Canadia?
fwiw, I was watching British comedian Jimmy Carr the other night on TV, and he used the following bit:
"You might be wondering where I am from considering my accent. Although you could say I don't have an accent at all. I am from England, this is what English sounds like when it is pronounced correctly."
Re:English for Geeks 101 (Score:1)
Re:English for Geeks 101 (Score:1)
Re:English for Geeks 101 (Score:2)
Re:English for Geeks 101 (Score:2)
This is news? (Score:5, Informative)
Webmonkey [lycos.com] had a similar article three and a half years ago, that provide some more solid examples of what happens.
I designed an e-commerce site over the last six years, and evaluated where there might be XSS vulnerabilities. Not having a bulletin board or guestbook removes many areas for exploitation.
So if someone types contaminated data into their address field when checking out, you'd think all it hoses is their own purchase, right?
Well, with PHP or Perl CGI, it's possible that the inputted variables could exploit server knowledge: if you know the variable names used in the PHP code for, say, the MySQL password, then embedding this in the input to be evaluated on output can open an avenue for hacking. The variable has to be evaluated in most cases, although code which generates new PHP pages could result in similar problems.
HTML encode EVERYTHING the user sends to you.
Its news to you (Score:5, Informative)
*cough*
Its this kind of lack of understanding that makes the problem so prevelant.
First it doesn't make sense to htmlencode everything just as id doesn't make sense to addslashes everything (now turned off by default in all good php configurations).
Here's why: Not everything that comes in is to be displayed as html, just as not everything that comes in is destined for the database.
Unless you understand the risks, you can't guard against them though it appears some people are still able to be certain they have guarded against them.
If you do this,
sqlquery("select * from user where username='$user'") then you need to think what the problem is, its a well defined problem, it is that $user may contain a final ' mark and then some; maybe:
$user="jimjoe' or 1'"
so your preferences page now shows the first user in the db, or depending on your web page, all of them.
In php, htmlentities doesn't encode the '
If you are invoking system commands (and yes I one had to do a LOT of this from php) then be careful about shell meta characters like ` ' " and $ in certain cases.
The principle is that you need to make sure the system you are passing data on to interprets it in the literal sense that you require and you cannot do this unless you understand completely how each of the systems you will pass the data on to really does interpret data.
So if your user data is destined for the database, then escape it, something like:
sqlquery(sprintf("select * from user where username='%s'",addslashes($user)));
(yes there are other better was of doing it)
If you want to display on the web page inline:
echo htmlentities($user);
on the other hand if you want to display in an text area I think there is other encoding to use. If it is for a url you need to urlencode and htmlentities but I forget the order.
Understand the system you are communicating with.
Sam
Re:Its news to you (Score:2)
Which is why you should be using bound variables or placeholders in the first place:
sqlquery("select * from user where username=?", $user)
The db library should do the right thing. It will even take care of stuff like VARCHAR vs NUMBER. No need to remember to quote or escape.
As an aside, you should never do 'SELECT *', because it's ALWAYS overkill. For example, in the above query you don't have to retrieve 'username' as you obviously already have i
Clarifications (Score:3, Interesting)
1) I meant "HTML Encode anything that will end up in HTML output again."
2) I didn't bother talking about SQL insertion, that's a different gremlin from XSS.
3) I didn't implement the things I said were stupid to do... I avoided them for that particular reason. I'm saying that there are traps to avoid, such as evaluating the contents of inputted variables. Some ways of implementing template toolkits will have you build a large
Re:This is news? (Score:2)
Disappointing (Score:1)
I was hoping for some enlightened progression into the interesting-for-some field of cross-siting, but am sorely disappointed at this basic-tut-in-PDF-clothing. Most people don't really discuss much when it comes to XSS as there isn't much to discuss. Well, maybe there is, but this paper doesn't highlight anything new or advent
Speaking of cookies (Score:2)
Right now the copied cookie is ignored by the web site.
Re:Speaking of cookies (Score:2, Insightful)
Free Article on Cross-Site Attacks (Score:5, Informative)
Although it is PHP-specific, this free article explains XSS and CSRF in quite a bit of detail and might be useful for Web developers using any language:
http://www.phparch.com/sample.php?mid=16 [phparch.com]
Enjoy
Lethal !!! (Score:5, Funny)
You better believe it. Why only last week I had one of my web developers executed for writing code vunerable to a Cross Scripting Attack. I dont want any slackers on my team.
PS I now have an opening for an experienced web developer. Sent resumes to spareme@icodetolive.com
Re:Lethal !!! (Score:3, Funny)
Asp.NET 1.1 and XSS (Score:4, Interesting)
Of course I was bitten by this feature when upgrading from 1.0 to 1.1, but that's just because I didn't bother reading the readme.txt
Automatic protection bundled with any application server is good, especially if you can turn it off [you can in asp.net , validateRequest=false et voila].
Re:Asp.NET 1.1 and XSS (Score:2)
I was amazed that they would get into trouble to check for scripts and yet permit all this stuff.
If ASP does it for them then it explains a lot.
It means they are plain idiots and not twisted idiots.
another cross site script problem... (Score:2)
Hands-on experience (Score:1)
Re:Hands-on experience (Score:2)
A few techniques used and more (Score:3, Interesting)
volnerable scripts which display text which came
from URL encoded data, This is one of many methods
to display the attackers HTML in an unsuspecting
users browser.
It is very common for the 404 message on a website to contain the URL which was entered, In the past this was done mostly by copying it as is. This would allow an attack.
In order to hide the attack hex encoding is used in the URL so the victim would not notice the script in the URL.
Still the attacker needs to minimize the length of the URL this causes him to use HTML options
such as iframe in order to insert a lot of HTML
taken from a diffrent site.
The main point of intrest is that the page appears to be comming from the (probably trusted) server, this can convince the user to do stuff he may not do on the attackers web site, say for example enter credit card info.
Also one could collect cookies this way, the cookies are likely to contain passwords or equivelent informations for sites with user login.
In some forums a user can put scripts in his signature or profile, this allows similar results,
but with out sending funny URLs.
DO NOT TRUST USER INPUT, it may harm not only you
but also the user, they must be protected.
Me.
How to prevent cross-site scripting attacks (Score:2)
Seriously, the only problem is that of control structures. Most tags don't change the flow, or make data modifications, they'll simply set the style (eg: the bold and italic tags) or inject characters (eg: the paragraph tag).
However, if you want to be safe, simply don't allow any HTML in a page, and require users to format in TeX.
This happened to me. (Score:1)
My expirence with XSS was due to my lazy programming. I didn't filter user content before it was displayed. Someone posted guestbook content with malformed PHP commands. Luckly they didn't know PHP that well, and an error was returned.
As far as PHP goes, functions like str_replace(), and striptags() should be used to parse all user-inputed data befo
Re:This happened to me. (Score:2)
But its also possible to use str_replace and striptags in ways that DON'T protect from malformed user input and how are you to know the difference?
Cavalier input processing is another curse of the internet, like email validators that think a-z0-9. are all the characters allowed in an email address, or t
Re:This happened to me. (Score:2)
Some folk have seen a form of str_replace that supposedly gave some form of protection against some kind of XSS and then invoke it in all kinds of unsuitable situations expecting to get the benefits.
The poster said all user input should be parsed with str_replace and striptags - true enough, but the protection is knowing and parsing for the right things, not what you use to parse it.
You're not "OK" because you use str_replace, only if you use str_replace in a way that protects you.
Perl CGI coders (Score:3, Interesting)
--
hecubas
Re:Perl CGI coders (Score:2)
Sam
Re:Perl CGI coders (Score:2)
I remember in the early days of the Web... (Score:2, Interesting)
Re:I remember in the early days of the Web... (Score:1)
Re:I remember in the early days of the Web... (Score:1)
I was adding a new module to an ASP shopping cart a few years back and found it was calculating the grand total to display on the confirmation page. Instead of recalculating the value when submitting the credit card, they passed the value from the confirmation page via a hidden input field directly to the CC processor.
The company was grateful enough when I pointed out the problem I had fixed that they gave me a $200 gift certificate.
I don't understand (Score:1)
Re:I don't understand (Score:1)
Stealing user credentials, for identity theft, gaining administrator access etc.
Modifying transactions performed on the site
Inserting false information
And the list goes on. Cross-site scripting attacks very often take advantage of browser security holes, but even with a 100% secure browser but less-than-perfect script, some types of attacks are still possible.
Re:I don't understand (Score:2)
Re:I don't understand (Score:2)
Or, make a link to a really good deal at amazon.com, and grab their credit cards.
Of course, ebay and Amazon are probably not that stupid, but many smaller sites are. (Banks maybe?)
Oh, these guys don't know the half of it... (Score:1)
Cross Site Scripting (Score:1)
The old VeriSign's ad campaign with naked women (Score:1)
This article somehow reminds me about the old VeriSign's ad campaign with naked women [slashdot.org] by which I was outrageously offended once.
Seriously, there was a huge cross site scripting vulnerability in Omniture [omniture.com]'s "award-winning web analytics solution for large, complex sites" (too complex for them, I guess) which was included in the famous VeriSign's Site Finder we all loved so much. It's not that VeriSign handles any sensitive data, fortunately...
(My link doesn't work any more, but the comment is still +5,
I learnt this lesson a long time ago. (Score:5, Insightful)
On the website you would enter in the amount of stock, stock symbol, and BUY or SELL in a form. That form would POST to a confirmation page and from there you would click "TRADE" and it would post to some server side page to execute the trade. The fools that designed the site thought it would be a good idea to validate all the data on the confirmation page and NOT on the server side page. We created a local version of the initial confirmation page, changed the action of the form to "http://www.tradingsite.com/cgi-bin/trade.pl". We then proceeded to buy -100000 shares of MSFT for about 40 bucks a pop.
The server had a formula of something like:
(STOCKPRICE * SHARES) + COMMISION = SUM
The sum was then checked against your accounts cash balance.
Something like:
IF (SUM > CASHBALANCE)
ERROR;
ELSE
EXECUTE TRADE;
Well we had a big negative number for our SUM so it passed.
The server then procceeded to:
CASHBALANCE = CASHBALANCE - SUM
Well anyone who has taken 5th grade math knows what happens when you subtract a negative number.
To make a long story short....we come into school about 2 weeks later and there is a big list of all the teams playing the stock market game in NY state. Our team is number 1 by about 2 million bucks, 2nd place is at about 105k. We confessed to whole the thing explained to the site what they did wrong and didn't get in any trouble.
The morale of this story:
Validate all user input before you perform ANY actions with it.
The most dangerous web problem out there (Score:3, Funny)
Our next paper - how to survive a slashdotting.
Paper mirror (Score:1, Informative)
http://cgisecurity.com/lib/xss_anatomy.pdf [cgisecurity.com]
Know of a sanitizing script in PHP? (Score:2)
I need something with a GPL-compatible license.
I guess I could just re-write Brad Choates's Sanitize Plugin [bradchoate.com] in PHP, but it would be nice to not have to go through the trouble.
Re:Know of a sanitizing script in PHP? (Score:4, Informative)
If you want to allow <A> or <IMG> tags, you should use preg_match expressions for elementary sanity checking.
Re:Know of a sanitizing script in PHP? (Score:2)
Say you want to strip everything but bold and italic tags from some text:
This by itself isn't sufficient to prevent XSS problems, but it's a start. Read over the user contributed notes on that page for some more good tips and example code.
Re:Static (Score:1, Funny)
Neither is my Ford Taurus, Orangutans, or bananas. What's your point?
Re:Static (Score:5, Funny)
You're sure bananas aren't vulnerable?
Now he tells me. Oh, oh, the time I have wasted.
Re:Static (Score:2, Insightful)
Static web pages also went out with beta video cassettes.