JSONP存在的JSON Hijacking漏洞以及與csrf/xss漏洞的關係,hijackingxss

來源:互聯網
上載者:User

JSONP存在的JSON Hijacking漏洞以及與csrf/xss漏洞的關係,hijackingxss

在實習過程中接觸過所謂的JSON Hijacking 漏洞,但最近在寫論文時發現理解得不深,好像跟xss與csrf又有點區別與聯絡,索

性深入學習了下JSONP(JSON with Padding)。


下面一段話截取自:http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about   仔細看看就比較清晰了。

Say you're on domain abc.com, and you want to make a request to domain xyz.com. To do so, you need to cross domain boundaries, a no-no in most of browserland.

The one item that bypasses this limitation is <script> tags. When you use a script tag, the domain limitation is ignored, but under normal circumstances, you can't really DO anything with the results, the script just gets evaluated.

Enter JSONP. When you make your request to a server that is JSONP enabled, you pass a special parameter that tells the server a little bit about your page. That way, the server is able to nicely wrap up its response in a way that your page can handle.

For example, say the server expects a parameter called "callback" to enable its JSONP capabilities. Then your request would look like:

http://www.xyz.com/sample.aspx?callback=mycallback

Without JSONP, this might return some basic JavaScript object, like so:

{ foo: 'bar' }

However, with JSONP, when the server receives the "callback" parameter, it wraps up the result a little differently, returning something like this:

mycallback({ foo: 'bar' });

As you can see, it will now invoke the method you specified. So, in your page, you define the callback function:

mycallback = function(data){  alert(data.foo);};

And now, when the script is loaded, it'll be evaluated, and your function will be executed. Voila, cross-domain requests!


大概解釋一下。舉例:在abc.com 有這麼一段 <script src="http://www.xyz.com/sample.aspx?callback=mycallback"></script>

在 xyz.com 的 sample.aspx 可能是如下實現的:


 PHP Code 
1
2
3
4
5
6
<?php
header('Content-Type:text/html;charset=utf-8');
$callback = $_GET['callback'];
$data     = "{foo: 'bar'}";
echo $callback . "(" . $data . ");";
?>

src 標籤請求返回即 <script> mycallback({foo: 'bar'});</script> 也就是會執行函數mycallback,這裡需要明確的是mycallback 肯定是在

abc.com 上實現的,把 {foo: 'bar' } 作為參數傳遞,以便下一步操作。callback只是個key,也可以寫成jsonp,當然server擷取的key也得變換。


講到這裡,對於正常的請求,jsonp並沒有什麼安全問題。但是,對於一些在登陸態會暴露敏感資訊如使用者id和暱稱的cgi來說,我們直接用瀏覽器訪問

http://www.a.com/json.php?mod=pay&func=openvip&callback=loginInfoCallback   雖然我們並沒有實現loginInfoCallback ,但如上所述,server端會返

回 loginInfoCallback({"ret":-1102321,"data":{"_id":1032412,"_nick":"jnusimba"}});  當然這是在 jnusimba 在瀏覽器的另一個tab已經登入過 a.com 的情

形下。所以攻擊的手法是 在第三方網站通過“<script src=http://www.a.com/json.php?mod=pay&func=openvip&callback=loginInfoCallback></script>”

的形式就可以在函數loginInfoCallback 參數裡面取得目前使用者的id號和暱稱。


上述講到的JSON Hijacking 漏洞可以認為是 csrf漏洞的一種,也就是 csrf 寫-讀 。基本原理還是因為瀏覽器的會話機制在第三方網站請求 a.com 時

也會把a.com 相關的cookie 發送出去。防禦來說,校正refer/ 請求帶上form token 都是比較好的辦法,也就是帶有第三方無法預測的值。


對於上面server端的php代碼來說,返回 {foo: 'bar'}  沒有帶有登陸態資訊,故沒有什麼實質的風險。但不知道有人注意到沒有,它返回的頭部

Content-Type:text/html; 即類型設定錯誤了,應該是content-type:text/javascript;  (or application/javascript) 設定成html格式,容易造成xss攻擊,比

如callback=<script>alert('aaa')</script>,即瀏覽器會彈框(注意,在高版本的ie和chrome下面可能已經限制了xss攻擊,測試在firefox下查看即可)。

如下面的例子是前10來天寫論文時挖到的一個youku網站因為jsonp產生的反射xss漏洞,今天再次訪問已經被修複了,可我找不到當初哥提交烏雲的鏈

接了哭~。(註:The MIME media type for JSON text is application/json. The default encoding is UTF-8. 

For JSONP with callback :application/javascript )


如果大家想玩一玩,可以看 http://lorrylockie.github.io/2015/01/27/jsonp-safe/ 這裡的一個例子,如:

訪問連結 http://login.koudai.com/weidian/sendCodeToBuyerPhone?param=%20{%22telephone%22:%2218601662827%22,%22country_code%22:%2286%22}&callback=%3Cscript%3Ealert%28/xxs/%29;%3C/script%3E%3Cdiv%20style=%22font-size:100px;%22%3EKOU%20DAI%20TONG%20DA%20SHA%20B%3C/div%3E&ver=1031

註:搞安全的需要職業素養,原po主已經提交給烏雲以及口袋通方面了,沒修就不是白帽子的責任了,當然大家也別去惡搞呀。




可以看到回應標頭是html,故造成了xss攻擊,查看網頁原始碼以及點擊確定後的瀏覽器輸出:



從中也可以驗證我們上面所說關於jsonp的應用,比如 正常的請求訪問 http://login.koudai.com/weidian/sendCodeToBuyerPhone?param=%20{%22telephone%22:%2218601662827%22,%22country_code%22:%2286%22}&callback=mycallback&ver=1031

可以猜想 輸入的telephone值在server端進行驗證,用返回的status_code表示是否有效,進而作為mycallback的參數進行下一步操

作,比如正確則發送驗證碼之類。如果我們把號碼換成18601662826 進行訪問,返回的是

mycallback({"status":{"status_code":0,"status_reason":""}})   基於猜想,我認為這個號碼是真實存在的哈。


對於此類由jsonp引起的反射xss漏洞,一來回應標頭應該設定正確;二來應該對callback參數進行過濾或者編碼,避免可以直接執行js。


參考連結:

http://lorrylockie.github.io/2015/01/27/jsonp-safe/

http://en.wikipedia.org/wiki/JSONP

http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.