六、跨域JSON注入
JSON是一个用来进行信息交换的轻量级的结构。像Google,Yahoo和其他一些公司都使用JSON回调来扩展他们的Web Service。可以通过一些函数捕获和处理跨域信息。
让我们看一个例子,假设一个站点运行在http://blog.example.org,扩展了他的JSON服务,允许任何人通过JavaScript访问站点信息。这时JSON需要提供一个回调名字。
假设要访问id为10的profile,只要发送如下的Get请求。
Request:
http://blog.example.org/Getprofile.html?callback=profileCallback&id=10
|
服务器返回的是:
Response:
profileCallback({"profile":[{"name":"John","email":"john@my.com"}]})
|
返回的profileCallback函数以JSON的输出做为参数。类似的如果我们发送的id是11,那么获得的返回是:
Response:
profileCallback({"profile":[{"name":"Smith","email":"smith@cool.com"}]})
|
我们的目标站点example.com ,在它的应用中集合了博客服务。如果扫描客户端代码,寻找document.write,可以在showprofile.html中找到如下代码片段:
<div class="code">
<pre><script>
function profileCallback(result) {
document.write(result.profile[0].name);
document.write("<br>");
document.write(result.profile[0].email);
}
</script>
<script src="http://blog.example.org/Getprofile.html?
callback=profileCallback&id=10" src="text/javascript"></script></pre></div>
|
这段代码跨域访问blog.example.org把获得的输出作为参数传递给“profileCallBack” 通过调用这个页面,我们获得的输出如图7所示:
图 7. 简单的JSON回调
显然运行在example.com上的应用,在未做任何验证的情况下,使用了第三方不可信的JSON信息,这样把终端用户的Session和cookie等敏感信息暴露出来。这意味着如果博客站点将我们要访问的信息注入了恶意的Java Script代码,那么我们的机器就可能被感染。示例代码如下:
profileCallback({"profile":[{"name":"Jack","email":"<script>alert('JSON
XSS');</script>"}]})
|
这样,我们的机器就会弹出如图8所示的对话框。
图 8. 通过JSON注入进行跨站脚本攻击