ie8 fix
madison

Zero Day

Ryan Naraine, Emil Protalinski and Dancho Danchev

Defeating the Same Origin Policy part 1

By | March 14, 2008, 7:27am PDT

Summary: The Same Origin Policy is one of the guiding principles that seek to protect our browsing experience. The Same Origin Policy was originally released with Netscape Navigator 2.0 and has been incorporated in one form or another in every major browser since. The concept has additionally been extended to protect such technologies as [...]

Billy RiosThe Same Origin Policy is one of the guiding principles that seek to protect our browsing experience. The Same Origin Policy was originally released with Netscape Navigator 2.0 and has been incorporated in one form or another in every major browser since. The concept has additionally been extended to protect such technologies as the Java Virtual Machine, Flash (they’re still working on it), and Silverlight (yet to be put to the real test). John Heasman

Same Origin Policy’s goal is to prevent a document, script, applet, etc. loaded from one site of origin from manipulating properties of or communicating with another site of origin. Unfortunately, the Same Origin Policy has been the victim of some brutal attacks as anti-DNS Pinning and DNS Rebinding attacks have come into full scope.

Why is this important? Well, there’s a number of reasons as there are a number of technologies that Same Origin Policy protects, but let’s focus on one example that really exemplifies the danger of bypassing the Same Origin Policy, Java Applets.

Java Applets have been traditionally protected by the Java Virtual Machine’s DNS pinning mechanisms. These protections simply state if an applet is loaded from abc.com (whether through an HTML page or the codebase parameter of the applet tag), it is allowed only to communicate with abc.com and not with def.com (unless it is a signed-applet). Billy Rios (shown in the top image above) and I presented at Hack in the Box Malaysia in 2007 on how this pinning could be broken. There’s a nice excerpt on Billy’s XS-Sniper blog, which I used to post on, that covers the subject in depth. The basics of it were as follows:

  • Attacker forces victim to load an cache a Java Applet (this might be done through XSS)
    • This applet would be pinned to the attacker’s site where it was loaded from, say xs-sniper.com.
  • The attacker controls the domain the applet was loaded from, as he owns the domain; therefore, he can change the IP Address that xs-sniper.com is associated to.
  • The victim is forced to load the applet again, this time in a new JVM instance
    • In our example at Hack in the Box Malaysia, this was done by forcing the victim’s IE browser to load Firefox through the firefoxurl:// protocol handler, which we could point to our site and applet like firefoxurl://xs-sniper.com/appletpage.html.
    • This can be done in a couple other ways which we mentioned in the powerpoint presentation we gave.
  • Since the applet has already been loaded and cached, there’s no need for the victim to download the applet again, which is good since xs-sniper.com is no longer pointing to our IP Address, it’s pointing to an IP Address internal to the victim’s network.
  • As the applet has been loaded in a separate JVM (IE and Firefox will each have their own instance of the JVM), the applet will become pinned to the IP Address now associated with xs-sniper.com.
  • We can now use the full power of Java applets against the Internal IP Address. In our example, we used a Java applet to connect to an oracle database server, dictionary attack the username/password to gain access to the server, and then allow us to run arbitrary SQL queries.

The ramifications here are clear, and quite devastating. Fortunately, Sun patched this issue quite quickly, protecting the unsuspecting. That is until John Heasman (shown in the second image above) of NGSSoftware came around. When Billy and I published the attack that we used against Java Applets, John emailed us about something he was working on, which was even easier and accomplished the same results. Here’s a summary of that flaw:

By specifying a codebase URI prefixed by “verbatim:” it is possible to load an applet from a remote location but have the browser plugin believe it has been loaded from the local host. This allows an untrusted applet to connect to and attempt to exploit network services running on the local host. It should be noted that unlike binary sockets in Flash 9, an applet can connect to any port, not just those greater than 1024.

At the time of reporting this issue, NGS provided Sun with a demonstration applet that exploited MS06-040 (”Vulnerability in Server Service could allow remote code execution”) on a vulnerable XP SP1 system.

Scared much yet? With applets, you have this greater capability and flexibility then was possible with these types of attacks against Flash or browsers (until Dan Kaminsky came along and implemented full-blown sockets in Javascript). It’s feasible that one could re-engineer Metasploit into Java (eek) and use an applet to perform grand attacks against Internal systems, thus bypassing the firewall through the victim’s browser!

Well, again, Sun came and patched John’s verbatim attack, which is great news. Then just recently we had this Sun advisory come out, which we blogged about here on ZDNet. As soon as I read some of the shady attack vectors that were involved, I knew it had to be Heasman. I contacted John about this, and he mentioned he would be making a few postings as demos of the bugs he was involved with on his blog, which you can now see here.

The first attack John covers is against Java Web Start. I’ll cut out pieces of John’s blog posting here to paraphrase the attack:

Applets are instantiated via the <APPLET> or <OBJECT> HTML tag. Both the code and codebase attributes/parameters must be set e.g. <APPLET code=”foo” codebase=”http://bar”/> will cause foo.class to be loaded from http://bar. The code that loads the class creates a URL object via the following constructor:

This constructor has an interesting property, namely:

public URL(URL context, String spec) throws MalformedURLException

If the authority component is present in the spec then the spec is treated as absolute and the spec authority and path will replace the context authority and path. If the authority component is absent in the spec then the authority of the new URL will be inherited from the context.

This effectively means that executing:

URL url1 = new URL(”http://baz”); URL url2 = new URL(url1, “http://bar”);

returns us url2 representing http://bar. So what happens if we instantiate an applet as follows:

<APPLET code=”http://baz/foo” codebase=”http://bar” />

What happens next may be obvious, but to recap, basically it will load the foo applet from the baz website, but report it as loaded from bar, so we’ve effectively broken Java’s same origin policy. Of course, things are never that simple, so there’s some complications that John had to get around, as John mentions below:

Of course, the devil is in the detail… there are some complications to get this attack working. Firstly, if we specify a code parameter containing a ‘.’, e.g.:

<APPLET code=”http://baz.com/foo” codebase=”http://bar” />

then an internal canonicalisation routine is triggered, converting ‘/’ characters into ‘.’ so we end up with a URL looking like “http:..baz.com.foo” and the attack fails. The easiest way round this limitation is to use the decimal representation of an IP address, as is apparently common with spammers. If you’re too lazy to do the maths, there’s an online converter here. So our code parameter will look like:

<APPLET code=”http://2130706433/foo” codebase=”http://bar” />

The final complication is that the Java plugin loads foo.class expecting to find a class called “http://2130706433/foo”. This is easy to solve - we compile our class with a class name of “aaaaaaaaaaaaaaaaaaaaa” and use a hex editor to replace this string with “http://2130706433/foo” (the compiler doesn’t like it but the JVM will load it).

AHA, now we’ve broken Same Origin Policy! Of course, it doesn’t end there, John has an attack that will actually break us out of the Java sandbox! I’ll cover that here as well, and I’ll also be covering a few more attack vectors against Same Origin Policy over the next week or so, so stay tuned!

-Nate

Kick off your day with ZDNet's daily e-mail newsletter. It's the freshest tech news and opinion, served hot. Get it.

Topics

Disclosure

Nathan McFeters

http://i.zdnet.com/images/auth/nmcfeters_53x53.jpg

Biography

Nathan McFeters

Nathan McFeters is a Senior Security Advisor for Ernst & Young's Advanced Security Center in Chicago. Nathan has performed web application, deep source code, Internet, Intranet, wireless, dial-up, and social engineering engagements for numerous clients in the Fortune 500 during his career at Ernst & Young and has spoken at a number of prestigious conferences, including Black Hat, DEFCON, ToorCon, and Hack in the Box. He can be found at his Pwn* blog and XS-Sniper, a blog with Billy Rios.

Related Discussions on TechRepublic

Did you know you can take part in these discussions with your ZDNet membership?
31
Comments

Join the conversation!

Just In

RE: Defeating the Same Origin Policy part 1
lovedong 13th Sep
Thank you and good luck everyone! replica watches
0 Votes
+ -
ouh... That made my head hurt.
dawgit 14th Mar 2008
Excellent post. I wasn't sure wither to laugh or scream. Worrisome, I will say that.And while this subject is, in fact, no laughing matter, the way you made it look so easy made me laugh anyway. Good Post, excellently written. I thoroughly enjoyed it. (the article, not the subject matter.) Let this be a wake-up call to us all.

Hat's off to Sun, for being so responsive, and responsible. There's hope yet in the World. -d (waiting for part II now)
0 Votes
+ -
Re: ouh...
nmcfeters 14th Mar 2008
Yeah, it really is funny how easy it looks... wait till you see the discussion of how Rob Carter, Billy Rios, and I used anti-DNS Pinning against Picasa. It's not quite as simple as it all sounds when you have the head down churning through the research.

Personally, I've been consistently impressed by John Heasman's work, which is why I was so glad that I got a chance to cover one of his flaws. The Litchfield brothers get a ton of press with regards to NGSSoftwares dealings (and rightfully so), but let us not forget the bad assery (spell check that, if it isn't a word, it should be) of Heasman.

-Nate
0 Votes
+ -
LOL
Hallowed are the Ori 14th Mar 2008
but let us not forget the bad assery (spell check that, if it isn't a word, it should be)

Aw man, you made me spew soda all over my monitor.

I'll forgive you this time though, because your article was a very good read.
0 Votes
+ -
Yeah, it is a head-acher.
seanferd 17th Mar 2008
The realities even more so than the technical aspects of article.

It is nice to know that the good guys are finding flaws to be patched, and that they are, in fact, being patched.
0 Votes
+ -
Thank you and good luck everyone! replica watches
0 Votes
+ -
fallacy of division
billappleton@... 14th Mar 2008
A simpler attack on the same origin policy is related to the use of composite web pages and JavaScript widgets in enterprise systems.

Some platform vendors provide partner applications written in JavaScript access to secure services or session IDs. Any JavaScript widget on the current page can subclass the XMLHttpRequest prototype and watch all service traffic coming and going. All it takes is one malicious script linked to anything on the current page for this attack.

Just because the browser is a trusted container for potentially untrusted applications does not mean that a web page is also a trusted container. This basic fallacy of division is a big misconception among platform vendors that enterprise customers should be aware of...
0 Votes
+ -
RE: Fallacy
nmcfeters 14th Mar 2008
Yep, I'll be talking about this too.
0 Votes
+ -
Thanks, I wish I wouldve thought to put that in the article!
0 Votes
+ -
Screen door on a submarine
terry flores 14th Mar 2008
The "functionality" of browsers to download and execute code without security being engineered in FIRST will be a nightmare of problems over the next few years. As more and more people and companies fall victim to attacks and theft, the designers will finally start backing away from "whiz-bang" features that leave systems open to attack. Many of these techniques may make the developers lives' easier, but at the cost of exposing users to attack.

It's easy to take a "short cut" like leaving your front door open so your kids can get in without a key, until you get tired of being robbed every week ...
0 Votes
+ -
Dive dive dive
SteveTheWirePuller 26th Mar 2008
What would be the fun in that? "Innovators" get to spill more ink on endless "solutions".
0 Votes
+ -
I don't quite get this
CobraA1 15th Mar 2008
"Since the applet has already been loaded and cached, there???s no need for the victim to download the applet again, which is good since xs-sniper.com is no longer pointing to our IP Address, it???s pointing to an IP Address internal to the victim???s network."

Correct me if I'm wrong, but if the victim's computer is pointing to an internal IP address and loads the same applet from an outside address, isn't that already violating the Same Origin Policy?

"The final complication is that the Java plugin loads foo.class expecting to find a class called 'http://2130706433/foo'."

In other words, they've essentially applied a bandage to the problem rather than rewriting the involved class loading code to be secure from the ground up. Yuck.

That's the problem with applying bandages vs rewriting the code from the ground up to be secure. The bandage solution almost never works. Yes, rewriting lots of code is a pain - but in the end, it's well worth the rewrite.
0 Votes
+ -
RE: I don't quite get this...
nmcfeters 15th Mar 2008
So, I'm not sure which part you aren't quite getting. What
you said sounds mostly correct. Basically, initially when
the victim was forced to download the applet, the applet
could only communicate with the server it was loaded
from, xs-sniper.com, which for arguments sake let's say
points at 1.2.3.4. We as an attacker FORCE the applet to
be cached.

Now, we change the ip address associated with xs-
sniper.com to 127.0.0.1 and then forced the victim to load
a new JVM with the firefoxurl: and again load our applet.
The applet code is cached, so it doesn't need to download
it again, and our servers ip address has changed from
1.2.3.4 to their localhost, so now the applet can
communicate with the victim's machine without being
signed. That is the violation of SOP. It's not reloading the
applet from a remote source, as it sounds like you are
saying, it's simply reloading the cached applet, which is
STILL pinned in the cache to xs-sniper.com, from a
different JVM, so a new DNS request is made to xs-
sniper.com and the applet is effectively pinned to the
localhost.

Nate
Vista is not for the love of homebase users that download everything they can while exploring personnal interests and creating a profile. This article has brass and does explain reasons and somemore with JVM just why you should respect your Vista operating system and just use it and getoff. There is a whole new expression with these Ultimate choices for personnal prestige; is there a need to say more?
0 Votes
+ -
Uhh...
Hallowed are the Ori 15th Mar 2008
is there a need to say more?

Uh, no, there isn't. Please don't.
0 Votes
+ -
RE: Uhh...
nmcfeters 15th Mar 2008
Umm... yeah, so something must've got lost in translation
there.

-Nate
0 Votes
+ -
Cache seems a major contributor
LittleGuy 15th Mar 2008
Everything is Cached these days and often causes problems.
Not reloading something that has changed!
I hate these new OS's that cache files in directory. You copy
a new file into a directory across a network and have to
refresh the directory before it shows up. On a Macintosh you
don't even have a refresh option and just have to wait.
We super computers now, why do we have to cache, and if
we have to, can't we make it work.
0 Votes
+ -
RE:
nmcfeters 15th Mar 2008
Caching isn't really done as much for client-side speed but
to off-lay server side requests. Imagine a site like the eBay
market place and how many requests are going back and
forth and all over SSL... you'd need more than a
supercomputer to handle all that.

-Nate
0 Votes
+ -
Imagine how much faster, simpler, efficient, powerful, etc,
etc...

It is nice to have a web interface that can be used
anywhere, anytime, but none of them are useful with a 56k
connection. Client side code can support slow connections
because it only need to communicate data. We could
eliminate security issues since the clients would be
certified by the company that provides them. The world is
mis-lead into thinking client code is bad, we can only trust
the operating systems and a browser. Let the developers
develop powerful code without browser limitations and let
the operating systems allow these tools to be installed
easily. Keep web interfaces for your blackberry's and when
no other options exist.

The Operating systems should have a trace mode which
shows the user exactly what their programs AND OS's are
doing. What's sent out, what brought in. What is my
computer doing now? If we don't like something, we
should be able to uninstall it on the spot. No more hide
and seek. Some of these tools exist, but they are far from
built in.

Why do we use our computers as dumb terminals?
Why don't people see this?
Give me back my PERSONAL COMPUTER!
0 Votes
+ -
RE: Thin Clients
nmcfeters 16th Mar 2008
>We could eliminate security issues since the clients would be certified by the company that provides them.

Wow, I completely disagree with you here. In fact, I firmly believe the move towards more client side code is going to make things more insecure. Besides that, thin clients would have the same issues with Same Origin Policy, so in the context of this talk, it doesn't make a whole lot of sense... I mean, we're talking about Java Web Start, Applets, Flash, Silverlight, these are all things that push down onto the client side a good piece of code, albeit that some work with the browser.

>The Operating systems should have a trace mode which shows the user exactly what their programs AND OS's are doing.

Who needs it? Regmon, Filemon, Wireshark, DTrace on Mac and *Nix, etc.

-Nate
0 Votes
+ -
Browser+JVM have become a Frankenstein of a thin client that executes mutable code and has effectively unlimited access to OS and user data. If everything was executing server-side then we wouldn't have so many problems.

If you meant trusted, tested, and signed thin clients, well ... Who do you trust? I've seen signed applets with malware embedded in them. I've seen MANY reputable sites that just refuse to use signed code (too lazy, can't be bothered, you name the excuse). One reason the security system is so broken is that so many websites just ignore it and force you to drop your pants just to access their sites. Users have been trained to violate their own security by everyone from CNN to Microsoft downloading code, so they hit the "OK" button out of resignation as opposed to any intelligent decision.

And of course, what does signed/certified mean? There isn't a SINGLE service provider out there who takes responsibility for their code or their actions. Not one. Everybody has a multi-page TOS or EULA that describes in detail how they are not responsible for any reason, including negligence or fraud. Why would you trust anyone like that to execute code on your computer??
0 Votes
+ -
RE:
nmcfeters 16th Mar 2008
Neither approach by itself is desireable. I would say the greatest push towards thin client type applications has little to do with security and far more to do with performance.

-Nate
0 Votes
+ -
Performance?
terry flores 17th Mar 2008
Most of the browser enhancements I see in use today are put to use making pop-unders and rollover ads more intrusive and obnoxious.

On many of the sites that I frequent "performance" has gone DOWN due to the amount of Flash, scripts, and general nonsense that is getting loaded. I realize that websites need to sell ads and make a profit, but honking off your viewers is not a good way to do things, there has to be a balance.
0 Votes
+ -
Re: Performance
seanferd 17th Mar 2008
How fast do you thing those great honking ads would load and/or run if it was all done server side? Running scripts, flash, etc., client side improves network and server performance (your local machine is doing all the work). Streaming flash constantly would clog the InterWeb tubes. For the same reason, static HTML and images are cached, so that every time you reload a page or revisit a site, the data is available locally for faster load times and less server and bandwidth usage.

Have you ever had a slow computer or internet connection where it was better to download and listen to a podcast rather than listen to it streaming over the 'net?

Yeah, annoying flash ads suck rocks. They would be even worse if they all ran server-side. Many scripst, on the other hand, should run server-side, for security reasons.
0 Votes
+ -
"Most of the browser enhancements I see in use today are put to use making pop-unders and rollover ads more intrusive and obnoxious"

We've seen this too!
We were wondering how it was IE7 or IE8 was unablle to defeat them...

Nice article, Nathan!

The Team
http://iserviceshop.blogspot.com
0 Votes
+ -
RE: Defeating the Same Origin Policy part 1
billappleton@... 17th Mar 2008
The ability for JavaScript to crawl the entire page and invisibly subclass anything on it is just a devastating problem for anybody trying to use of "widgets" connected to enterprise data systems.

This is the "on demand" version of a same origin attack. But there is nothing obsure about this, it is a practical real world method of stealing data.
0 Votes
+ -
I'd like to see Giorgio Maone
mhenriday 17th Mar 2008
weigh in on this - would NoScript stop this kind of attack on Firefox ?...

Henri
0 Votes
+ -
RE: Giorgio Malone
nmcfeters 17th Mar 2008
I've talked to Giorgio before, good guy. I don't want to put words in his mouth, but I can probably shed some light on that.

NoScript would certainly help mitigate the deployment mechanism for some of these attacks, which could be cross-site scripting; however, NoScript couldn't protect you against the underlying attacks against the Same Origin Policy and since there is other deployment mechanisms, I guess the answer would be yes and no.

-Nate
0 Votes
+ -
What about Force Field?
bart001fr 17th Mar 2008
It's a Zone Alarm add-on which helps protect your browser. Check it out at

http://www.zonealarm.com/store/content/home.jsp

bottom right of the page has a button to direct you to the download and information page.

Would it be of any help?
0 Votes
+ -
RE: Zone Alarm
nmcfeters 17th Mar 2008
I can't say because I've never used it, but I think that this discussion points out an interesting problem with security. Everyone is looking for a tool that's going to prevent all these attacks, it's just not feasible. There is no silver bullet.

These particular attacks are against the implementation of the Same Origin Policy against various technologies. The flaw rests on the fact that Same Origin Policy is based on trust of domain name and domains can be controlled by attackers. Additionally, the implementations of Same Origin Policy are flawed in that they allow the attacker to convince the technology to make a second DNS request and repin on that.

Short answer is, there's not a tool out their that can protect you from this.

-Nate
0 Votes
+ -
RE: RE Giorgio Malone (that's Maone, no l)
Greenknight_z 19th Mar 2008
I use NoScript; I also long ago disabled Java caching. Will that help protect?
0 Votes
+ -
defeating with which JRE?
wellumies 8th Apr 2008
I have tried now with 1.4, 1.5 and 1.6 JREs and I can't find one that would allow to specify code-attribute with an URL.

1.4 tries to do name lookup for the http://2130706433/foo
1.5 and 1.6 throw ClassFormatException : illegal class name.

Is there a JRE that "works" and where can I obtain it?

All JREs I used were unpatched ones I found over the internet.

Join the conversation!

Formatting +
BB Codes - Note: HTML is not supported in forums
  • [b] Bold [/b]
  • [i] Italic [/i]
  • [u] Underline [/u]
  • [s] Strikethrough [/s]
  • [q] "Quote" [/q]
  • [ol][*] 1. Ordered List [/ol]
  • [ul][*] · Unordered List [/ul]
  • [pre] Preformat [/pre]
  • [quote] "Blockquote" [/quote]
ie8 fix
Click Here
ie8 fix

The best of ZDNet, delivered

ZDNet Newsletters

Get the best of ZDNet delivered straight to your inbox

Facebook Activity

White Papers, Webcasts, & Resources
ie8 fix
ie8 fix