11年dmethvin提交jQuery 1.6.1版本的Ticket#9521,其缘由是由$() | jQuery()预选的CSS选择器在其余状况下可用于建立HTML元素,若是编码不当(事实上不少编码不当的状况),将会致使产生DomXSS漏洞。
示例(jQuery 1.6.1)javascript
- <html>
- <head>
- <title>jQuery DomXSS test</title>
- <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
- <script>
- $(location.hash);
- </script>
- </head>
- <body>
- Hello, jQuery.
- </body>
- </html>
复制代码
WordPress默认主题二十一个例子php
example.html 297-299行:
- // set permalink
- var permalink = cssclass.split(' genericon-')[1];
- window.location.hash = permalink;
复制代码
console.log永久连接:css
- http://linux.im/wp-content/themes/twentyfifteen/genericons/example.html#123
- console.log(permalink):genericon-123
335-343行:
- // pick random icon if no permalink, otherwise go to permalink
- if ( window.location.hash ) {
- permalink = "genericon-" + window.location.hash.split('#')[1];
- attr = jQuery( '.' + permalink ).attr( 'alt' );
- cssclass = jQuery( '.' + permalink ).attr('class');
- displayGlyph( attr, cssclass );
- } else {
- pickRandomIcon();
- }
复制代码
若是存在window.location.hash则拼接固定连接并使用jQuery的进行属性操做,问题出现,当咱们将的location.hash为设置<img src=@ onerror=alert(1)>时,致使跨站。
jQuery 1.6.1源码html
- >_ $
- jquery.js:25 function ( selector, context ) {
- // The jQuery object is actually just the init constructor 'enhanced'
- return new jQuery.fn.init( selector, context, rootjQuery );
- }
- >_ jQuery.fn.init
- jquery.js:93 function ( selector, context, rootjQuery ) {
- var match, elem, ret, doc;
- // Handle $(""), $(null), or $(undefined)
- if ( !selector ) {
- return this;
- }
- // Handle $(DOMElement)
- if ( selector.nodeType ) {
- this.context = this[0] = selector;
- this.length = 1;
- return this;
- }
- ......
- if (selector.selector !== undefined) {
- this.selector = selector.selector;
- this.context = selector.context;
- }
- return jQuery.makeArray( selector, this );
- }
复制代码
其中jQuery.fn.init:html5
- if ( typeof selector === "string" ) {
- // Are we dealing with HTML string or an ID?
- if ( selector.charAt(0) === "<" && selector.charAt( selector.length - 1 ) === ">" && selector.length >= 3 ) {
- // Assume that strings that start and end with <> are HTML and skip the regex check
- match = [ null, selector, null ];
- } else {
- match = quickExpr.exec( selector );
- }
复制代码
quickExpr对选择器进行过滤,正则为:java
- quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
复制代码
显然咱们上面的Payload是能经过的。
jQuery 1.7.2源码node
当时漏洞报告者在#9521中提到修复方案:
- the quick patch by jquery is here
- - quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
- + quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
复制代码
尽管在开始的例子代码中不能生效,但因为程序开发人员的编码习惯显然按照上面的修复并无什么卵用,修复后原有的攻击代码效果:jquery
- >_ location.hash
- "#test<img src=1 onerror=alert(1)>"
- >_$(location.hash)
- []
复制代码
由于正则新增#的缘由致使增长失败,在真实环境中属性或其余操做直接使用location.hash的可能性叫小,开发人员以及业务需求使得上面的修复方案没有意义,例如开始提到的WordPress默认主题XSS漏洞337行:linux
- permalink = "genericon-" + window.location.hash.split('#')[1];
复制代码
程序将获取到的哈希['#test111'] split后,只保存test111,也就能够获得咱们能忽略到1.7.2的修复。
jQuery 1.11.3源码ajax
在前面版本中其实可以得以证实jQuery团队确实修复#9521的问题就是quickExpr的上方注释:
- // A simple way to check for HTML strings or ID strings
- // Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
- quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
复制代码
可能开发团队遇到了1.7.2中我提到问题的尴尬窘境,他们在1.11.3又对其进行了升级:
- rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/,
复制代码
看到这个正则我几乎无语,使用开头和结尾<就能轻易绕过:

终于,他们在2.x系列正式修复了这个问题:
- rquickExpr = /^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/,
复制代码
其余浏览器
如何所见,上面这些Payload并不会在Safari中成效,经过调试便可发现Chrome未对location.hash部分进行URL编码处理进入函数,而Safari会通过URL编码进入函数,是这样的:

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

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