作者:
Lobsiinvok
·
2016/03/09 10:20
Author: Lobsiinvok
0x00 前言
Rails是Ruby廣泛應用方式之一,在Rails平臺上設計出一套獨特的MVC開發架構,採取模型(Model)、外觀(View)、控制器(Controller)分離的開發方式,不但減少了開發中的問題,更簡化了許多繁複的動作。
此篇講稿分為上下部份,因為最近在開發Rails,需要針對安全問題做把關,便藉此機會針對歷史上Rails發生過的安全問題進行歸納與整理。
這篇講稿承蒙安全領域研究上的先進,在自行吸收轉換後,如有筆誤或理解錯誤的地方還望各位見諒並糾正我,感謝 :D
快速跳轉
0x01 Mass assignment
- 讓Rails developers愛上的毒藥(toxic)
- ActiveRecord在新增物件時可傳入Hash直接設定多項屬性
若沒有限制可傳入的引數會造成物件屬性可被任意修改

透過新增/修改送出的屬性,可以變更任意物件屬性
Case
Rails 3.2.3後,config.active_record.whitelist_attributes = true

Rails 4後,Rails Core內建strong_parameters
更適當地將處理的過程鎖定在Controller layer
更有彈性地針對屬性作過濾
0x02 Unsafe Query Generation
Rake在處理params時,有時候會產生Unsafe的query

透過偽造params[:token]成[], [nil], [nil, nil, ...]或['foo', nil],都能夠透過.nil?的檢查,使得SQL語句被安插IS NULL or IN ('foo', NULL)造成非預期的結果
在Rails 3.2.8增加deep_munge方法來消除掉Hash裡的nil
commit中可看到類似的檢查

Code for Testing

Rails 3.1.0: 成功繞過nil?的檢查

Rails 4.2.5: 被攔截,直接替換成nil

0x03 Content_tag
Rails提供content_tag方便產生HTML
- 儘管方便,產生出的HTML是safe的嗎?很顯然的並不是!
Ref: brakeman

In latest rails 4.2.5, attr still can be injected with any html data.

儘管attr values有escape,但跟button_to一起作用時卻……

Why?
- Content_tag回傳
html_safe
的字串,代表此字串在後續輸出時不再做escape
- 建立在attacker無法構建
html_safe
型的字串(等價於raw)
- 丟給button_to時因為不再做escape導致XSS問題
0x04 YAML.load
難得一見的RCE漏洞(CVE-2013-0156)
- 主因出在YAML
- CVE-2013-0156發生在可透過YAML解析時指定tag的方式覆蓋已經載入的instance
在rails3後已從DEFAULT_PARSERS
移除

此次問題發生在XML解析
在解析時會經過Hash.from_xml(request.raw_post),底處是到typecast_xml_value進行xml的處理,這篇前輩的文章解釋得很清楚,因為typecast_xml_value裡針對xml node type可以進行YAML的解析呼叫(允許的type定義在ActiveSupport::XmlMini::PARSING
),因此造成RCE問題
透過patch可以更明顯看到修補後的不同


Ref: Rails 3.2
Proof
Rails 3.1: 成功執行指令

難得一見的RCE漏洞(CVE-2013-0333)
- CVE-2013-0333問題一樣發生在
YAML.load
- 在rails 3.0.19(含)前,rails3.0.x的JSON Parser竟然是使用YAML作為Backend
- 問題發生在YAML backend中的
convert_json_to_yaml
- 這篇講得很詳細
- Patch for CVE-2013-0333

Rails 3.0.20
0x05 Dynamic Render Path
Render是處理request的一連串過程
- 除了Insecure Direct Object Reference的安全問題,DEVCORE也在進行滲透測試時發現潛在的RCE問題
- rails目前最新版本4.2.5預設也是用ERB去做樣板處理,但在rails5開發過程中已經加入此次commit
動態樣板間接變成LFI問題,搭配上面所述的default_template_handler
為ERB,只要找到有呼叫ruby code的樣板或是可自行寫入的檔案,就能夠造成RCE


真實環境下發生的問題可以前往DEVCORE檢視
如果有類似開發環境應立即處理,default_template_handler
要到rails5才轉換成RAW
改以白名單的方式限制template名稱或是根據commit的內容手動Patch
0x06 Reference
本文章來源於烏雲知識庫,此映象為了方便大家學習研究,文章版權歸烏雲知識庫!