jQuery <= 1.11.3 DomXSS漏洞

听团里说WordPress又爆跨站漏洞了:“ XSS漏洞在Jetpack和二十五默认主题影响百万WordPress用户 ”,分析发现原来是jQuery老版本的DOM XSS漏洞【错误#9521】。
11年dmethvin提交jQuery 1.6.1版本的Ticket#9521,其原因是由$() | jQuery()预选的CSS选择器在其他情况下可用于创建HTML元素,如果编码不当(事实上很多编码不当的情况),将会导致产生DomXSS漏洞。

示例(jQuery 1.6.1)

  1. <html>
  2. <head>
  3.     <title>jQuery DomXSS test</title>
  4.     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
  5.     <script>
  6.         $(location.hash);
  7.     </script>
  8. </head>
  9. <body>
  10. Hello, jQuery.
  11. </body>
  12. </html>
复制代码



WordPress默认主题二十一个例子

example.html 297-299行:
  1. // set permalink
  2. var permalink = cssclass.split(' genericon-')[1];
  3. window.location.hash = permalink;
复制代码

console.log永久链接:

  • http://linux.im/wp-content/themes/twentyfifteen/genericons/example.html#123
  • console.log(permalink):genericon-123
335-343行:
  1. // pick random icon if no permalink, otherwise go to permalink
  2. if ( window.location.hash ) {
  3.     permalink = "genericon-" + window.location.hash.split('#')[1];
  4.     attr = jQuery( '.' + permalink ).attr( 'alt' );
  5.     cssclass = jQuery( '.' + permalink ).attr('class');
  6.     displayGlyph( attr, cssclass );
  7. } else {
  8.     pickRandomIcon();
  9. }
复制代码
如果存在window.location.hash则拼接固定链接并使用jQuery的进行属性操作,问题出现,当我们将的location.hash为设置<img src=@ onerror=alert(1)>时,导致跨站。

jQuery 1.6.1源码

  1. >_ $
  2. jquery.js:25 function ( selector, context ) {
  3.         // The jQuery object is actually just the init constructor 'enhanced'
  4.         return new jQuery.fn.init( selector, context, rootjQuery );
  5.     }
  6. >_ jQuery.fn.init
  7. jquery.js:93 function ( selector, context, rootjQuery ) {
  8.         var match, elem, ret, doc;
  9.         // Handle $(""), $(null), or $(undefined)
  10.         if ( !selector ) {
  11.             return this;
  12.         }
  13.         // Handle $(DOMElement)
  14.         if ( selector.nodeType ) {
  15.             this.context = this[0] = selector;
  16.             this.length = 1;
  17.             return this;
  18.         }
  19. ......
  20.         if (selector.selector !== undefined) {
  21.             this.selector = selector.selector;
  22.             this.context = selector.context;
  23.         }
  24.         return jQuery.makeArray( selector, this );
  25.     }
复制代码

其中jQuery.fn.init:

  1. if ( typeof selector === "string" ) {
  2.     // Are we dealing with HTML string or an ID?
  3.     if ( selector.charAt(0) === "<" && selector.charAt( selector.length - 1 ) === ">" && selector.length >= 3 ) {
  4.         // Assume that strings that start and end with <> are HTML and skip the regex check
  5.         match = [ null, selector, null ];
  6.     } else {
  7.         match = quickExpr.exec( selector );
  8.     }
复制代码

quickExpr对选择器进行过滤,正则为:

  1. quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
复制代码
显然我们上面的Payload是能通过的。

jQuery 1.7.2源码

当时漏洞报告者在#9521中提到修复方案:
  1. the quick patch by jquery is here
  2. -       quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
  3. +       quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
复制代码

尽管在开始的例子代码中不能生效,但由于程序开发人员的编码习惯显然按照上面的修复并没有什么卵用,修复后原有的攻击代码效果:

  1. >_ location.hash
  2. "#test<img src=1 onerror=alert(1)>"
  3. >_$(location.hash)
  4. []
复制代码

因为正则新增#的原因导致增加失败,在真实环境中属性或其他操作直接使用location.hash的可能性叫小,开发人员以及业务需求使得上面的修复方案没有意义,例如开始提到的WordPress默认主题XSS漏洞337行:

  1. permalink = "genericon-" + window.location.hash.split('#')[1];
复制代码
程序将获取到的哈希['#test111'] split后,只保存test111,也就可以得到我们能忽略到1.7.2的修复。

jQuery 1.11.3源码

在前面版本中其实能够得以证明jQuery团队确实修复#9521的问题就是quickExpr的上方注释:
  1. // A simple way to check for HTML strings or ID strings
  2. // Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
  3. quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
复制代码

可能开发团队遇到了1.7.2中我提到问题的尴尬窘境,他们在1.11.3又对其进行了升级:

  1. rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/,
复制代码
看到这个正则我几乎无语,使用开头和结尾<就能轻易绕过:

终于,他们在2.x系列正式修复了这个问题:
  1. rquickExpr = /^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/,
复制代码

其他浏览器

如何所见,上面这些Payload并不会在Safari中成效,通过调试即可发现Chrome未对location.hash部分进行URL编码处理进入函数,而Safari会经过URL编码进入函数,是这样的:

但是我们仍然可以使用html5的一些特性,引发错误并onerror出来:
  1. file:///Users/evi1m0/Desktop/1.html#<video><source/onerror=alert(1)>
复制代码


来源:

http://www.hack80.com/forum.php?mod=viewthread&tid=47045

猜你喜欢

转载自www.cnblogs.com/coolid/p/9237258.html