您的位置:首页 > Web前端 > JavaScript

通过 iframe 实现 xss(Enabling cross-site scripting XSS via an iframe)

2013-07-25 15:02 169 查看
这篇文章虽然是2008年写的,比较旧,但是问题讲的通俗易懂,一看就明白那种,感觉不错,就翻译出来了。

有这样的一个想当普遍的场景就是:你有一个包含iframe的页面,而这个iframe的src是指向和当前页面不同的域名。这样做是没错的,记住iframe就是这样设计的!

接下来的示例图展示我们要讨论的一种方案:



现在你想单击iframe里的一个链接,在父页面里来触发一个动作(可能是在父页面里提交一个表单,可能是在父页面里调整iframe的高度)。这时你遇到了困难,因为浏览器不让你访问不用域名下的javascript脚本里的方法或者属性。

如果一个iframe的src指向的域名与包含他的父页面的域名不一样,这时父页面和iframe通信就会遇到同源的限制,因为浏览器基于内建的安全策略会阻止这种行为。一个相对不那么痛苦的解决方案是需要一个额外的iframe(请看下图)。



现实中的例子:

你可以在  sky movies上看到一个正在运行的例子,在这个网站上iframe随着它包含的iframe改变而改变大小。这是在iframe内部控制的。

首先,为什么我们需要实现像这样的解决方案?

因为,movies.sky.com的首页能够设置iframe的高度,但不能获得iframe的内容高度,而iframe 的document能够获得高度但是不能设置包围他的iframe的高度。

关于解决方案每个组件块的解释:

使用下面的例子页面,我们能够创建一个关于这种技术的演示。

下面是主页面的源码(上边草图中标的[1]),它包含一个iframe和一个将被用来设置iframe大小的方法。

<html>
<head>
<title>Page hosted on example.com</title>
<script type="text/javascript">
function resizeIframeHeight(nHeight) {
var iframe = document.getElementById('mainIframe');
iframe.setAttribute('height', nHeight);
}
</script>
</head>

<body>
<h1>This page is hosted on the example.com domain</h1>
<p>The iframe below is hosted on the example.org domain</p>
<iframe id="mainIframe" width="400" height="200" src="http://example.org/iframedDocument.html">Iframes not supported.</iframe>
</body>
</html>


下面是iframe页面的源码(上边草图中标的[2]),它包含一个额外的iframe和一些实习技术的脚本。
<html>
<head>
<title>Page hosted on example.org</title>
<script type="text/javascript">
function updateIframeHeight() {
var iframe = document.getElementById('hiddenIframe');
var newHeight = parseInt(document.body.offsetHeight,10) + 10;
iframe.src = 'http://example.com/xssEnabler.html?height=' + newHeight;
}
</script>
</head>

<body onload="updateIframeHeight()">
<h1>This page is hosted on the example.org domain</h1>
<p>The iframe below is hosted on the example.com domain (and be styled to be hidden)</p>
<iframe id="hiddenIframe" width="100" height="100" src="http://example.com/xssEnabler.html">Iframes not supported.</iframe>
</body>
</html>
最后一个源文件驻留在同一个域中(上边草图中标的[3]),它包含一些脚本来截取高度参数,然后把它传给主要页面中用来设置iframe大小的函数。在上面的代码中,它指的是“http://example.org/xssEnabler.html”的内容,被保存成一个静态文件,实现的代码就是下面你看到的。
<script type="text/javascript">
function getFirstParamFromLocation() {
var pair = window.location.search.substring(1);
var parts = pair.split('=');
return parts[1];
}
var nHeight = getFirstParamFromLocation();
try {
window.top.resizeIframeHeight(height);
} catch(e) {};
</script>
很明显你不能使用“example.com”和“ example.org”在你的方案中,它们仅仅是用来给你使用的每个文件标识不同域名的。

原文:

http://www.codecouch.com/2008/10/cross-site-scripting-xss-using-iframes/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐