views:

294

answers:

5

From ha.ckers.org/xss.html:

IMG Embedded commands - this works when the webpage where this is injected (like a web-board) is behind password protection and that password protection works with other commands on the same domain. This can be used to delete users, add users (if the user who visits the page is an administrator), send credentials elsewhere, etc.... This is one of the lesser used but more useful XSS vectors:

<IMG SRC="http://www.thesiteyouareon.com/somecommand.php?somevariables=maliciouscode"&gt;

or:

Redirect 302 /a.jpg http://victimsite.com/admin.asp&amp;deleteuser

I allow users to post images in the forum. How can this be protected against?

I'm using Java Struts but any generic answers are welcome.

+2  A: 

This attack is simply an HTTP GET request made to any URL. You cannot reliably block it by prevent certain <img> tags.

Instead, you need to make sure that your website has no targets (URLs that respond to GET requests and change things)

If there aren't any "juicy" URLs that respond to HTTP GETs (not POSTs) and change data, the attacker will have nothing to attack. (<img> tags cannot be used to create HTTP POSTs)

SLaks
Thanks Slaks, so basically posts are safe? There's no way to forge a post request using this?
Pool
Sort of, as long as you block Javascript and `<form>`/`<iframe>` tags. POSTs are still vulnerable to normal XSS from other sites.
SLaks
Right... but its still xsrf even if the forged request originates from another server. POST requests can still be forged.
Rook
NOOO, reqeusts cannot be vulnerable to xss, they are vulnerable to XSRF/CSRF! You need to read more before you give people advice! Someone is going to get hacked. Okay! `</meltdown>`
Rook
A: 

Cross-site scripting is one reason why you should not allow forum users to post images by linking to images outside your site. Image posting should be provided by allowing users to upload the image file to your site and using internal relative URI.

geekGod
What are you talking about, because its not xss.
Rook
+1  A: 

By injecting an <img> tag someone can bypass referer based XSRF protection for a GET request. The reason why is because the referer for the GET request produced by the <img> has the same referer as the host its self. So this would bypass code checking to see if the referer and the host where different.

You shouldn't allow people to put html on your page. In this case you should let users upload them and then host images locally. If you really want people to put IMG tags on your site, make sure the URL isn't pointing to your server, because this what an attack would do! Also don't use referer based XSRF protection, use token based. <img> tag injection cannot bypass token based xsrf protection.

Rook
+3  A: 

If you follow the rules of the HTTP specification, such a kind of attack will make no harm. The section 9.1.1 Safe Methods says:

[…] GET and HEAD methods SHOULD NOT have the significance of taking an action other than retrieval. These methods ought to be considered "safe". This allows user agents to represent other methods, such as POST, PUT and DELETE, in a special way, so that the user is made aware of the fact that a possibly unsafe action is being requested.

Naturally, it is not possible to ensure that the server does not generate side-effects as a result of performing a GET request; in fact, some dynamic resources consider that a feature. The important distinction here is that the user did not request the side-effects, so therefore cannot be held accountable for them.

So all requests that change data on the server side should only be allowed via POST. And even there you should only allow those requests that your system has authenticated by generating tokens that are only valid for a specific form/action.

Gumbo
Thanks. I can work with this.
Pool
@The Feast: It is possible to forge POST requests, so this answer is incorrect.
BlueRaja - Danny Pflughoeft
@The Feast: BlueRaja is right. POST request can also be forged. Either on your site by exploiting a XSS vulnerability. Or on a third party site via JavaScript. That why I added the remark on only allowing authentic POST requests.
Gumbo
This answer is explaining best practices of fundamental aspects of the HTTP protocol, and there's nothing wrong with it. Web apps with gaping security holes (implied by the somecommand.php?somevariables=maliciouscod example) will be a serious problem whether requests are forged or not.
micahwittman
@micahwittman: That's true, which is why I did not downvote this answer; but this neither prevents XSRF attacks, nor does it answer the question asked by The Feast.
BlueRaja - Danny Pflughoeft
Why not, BlueRaja? Grumbo mentions that a unique request token should be passed within the form too which covers the particular exploit as far as I can see.
Pool
You can use an authentication algorithm like HMAC to make a random authentication token only valid for a specific form.
Gumbo
A: 

No one seemed to mention that the threat in allowing people to post images is not to you, it's to other sites.

If you allow people to post images but your site has no XSRF vulnerabilities, your site is not in danger; other sites with XSRF vulnerabilities are, as your users will unknowingly make requests to the other site via the embedded image when they visit your site. The malicious <img> tag will look something like this:

<img src="http://my-bank-website.com/withdraw_money.php?amount=100000&amp;account=mandy-the-hacker" />

Note that this is not a real image, but the browser will not know that, so it will make the request anyways, transferring $100,000 to mandy-the-hacker's account, assuming the user is currently logged on to my-bank-website.com. This is how XSRF vulnerabilities work.

The only way to prevent this is to force users to upload images, rather than providing URLs for them. However, the malicious user could still just provide a link to the XSRF vulnerability, so removing the ability to provide URLs doesn't really help anything; you are not really harming the other site by allowing <img> tags, they are harming themselves by not using user-specific tokens in forms.

BlueRaja - Danny Pflughoeft
Most sites, even this, allow remote linking of images. As mentioned by Michael Brooks the same flaw can be exploited using links as well as images so I don't follow your reasoning why images are specifically dangerous.
Pool
@The Feast: I didn't say that, read the last paragraph again. I'm arguing *for* allowing linking images, since removing that ability is a very annoying solution that does not actually solve the problem. My point was, from your point of view, allowing images really has nothing to do with XSRF.
BlueRaja - Danny Pflughoeft
OK, thanks, yes I agree.
Pool