聽團里說WordPress又爆跨站漏洞了:“ XSS漏洞在Jetpack和二十五默認主題影響百萬WordPress用戶 ”,分析發現原來是jQuery老版本的DOM XSS漏洞【錯誤#9521】。
11年dmethvin提交jQuery 1.6.1版本的Ticket#9521,其原因是由$() | jQuery()預選的CSS選擇器在其他情況下可用於創建HTML元素,如果編碼不當(事實上很多編碼不當的情況),將會導致產生DomXSS漏洞。
示例(jQuery 1.6.1)
- <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默認主題二十一個例子
example.html 297-299行:
- // set permalink
- var permalink = cssclass.split(' genericon-')[1];
- window.location.hash = permalink;
console.log永久鏈接:
- 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源碼
- >_ $
- 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:
- 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對選擇器進行過濾,正則為:
- quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
顯然我們上面的Payload是能通過的。
jQuery 1.7.2源碼
當時漏洞報告者在#9521中提到修復方案:
- the quick patch by jquery is here
- - quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
- + quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
盡管在開始的例子代碼中不能生效,但由於程序開發人員的編碼習慣顯然按照上面的修復並沒有什么卵用,修復后原有的攻擊代碼效果:
- >_ location.hash
- "#test<img src=1 onerror=alert(1)>"
- >_$(location.hash)
- []
因為正則新增#的原因導致增加失敗,在真實環境中屬性或其他操作直接使用location.hash的可能性叫小,開發人員以及業務需求使得上面的修復方案沒有意義,例如開始提到的WordPress默認主題XSS漏洞337行:
- permalink = "genericon-" + window.location.hash.split('#')[1];
程序將獲取到的哈希['#test111'] split后,只保存test111,也就可以得到我們能忽略到1.7.2的修復。
jQuery 1.11.3源碼
在前面版本中其實能夠得以證明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