如果處理未驗證的 XSL 樣式表,可能會使攻擊者修改得到的 XML 的結構和內容,從文件系統中加入任意文件,或者執行任意 PHP 代碼。
XSLT injection 會在以下情況中出現:
1. 數據從一個不可信賴的數據源進入程序。
2. 數據寫入到 XSL 樣式表中。
通常,應用程序利用 XSL 樣式表來轉換 XML 文檔的格式。XSL 樣式表中包括特殊函數,雖然此類函數能改善轉換進程,但如果使用不當也會帶來更多漏洞。
如果攻擊者能夠在樣式表中寫入 XSL 元素,則可以更改 XSL 樣式表和處理的語義。攻擊者可能會更改樣式表的輸出,使 XSS (cross-site scripting) 攻擊被啟用,暴露本地文件系統資源的內容,或者執行任意 PHP 命令。如果攻擊者完全控制了提交給應用程序的樣式表,則攻擊者可能還會執行 XXE (Xml eXternal Entity) Injection 攻擊。
例 1:下面是一些易受 XSLT Injection 攻擊的代碼:
...
<?php
$xml = new DOMDocument;
$xml->load('local.xml');
$xsl = new DOMDocument;
$xsl->load($_GET['key']);
$processor = new XSLTProcessor;
$processor->registerPHPFunctions();
$processor->importStyleSheet($xsl);
echo $processor->transformToXML($xml);
?>
...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl">
<xsl:template match="/">
<script>alert(123)</script>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl">
<xsl:template match="/">
<xsl:copy-of select="document('/etc/passwd')"/>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl">
<xsl:template match="/">
<xsl:value-of select="php:function('passthru','ls -la')"/>
</xsl:template>
</xsl:stylesheet>
[1] Standards Mapping - OWASP Top 10 2010 - (OWASP 2010) A1 Injection
[2] Standards Mapping - OWASP Top 10 2007 - (OWASP 2007) A2 Injection Flaws
[3] Standards Mapping - OWASP Top 10 2004 - (OWASP 2004) A6 Injection Flaws
[4] Standards Mapping - Security Technical Implementation Guide Version 3 - (STIG 3) APP3510 CAT I
[5] Standards Mapping - Security Technical Implementation Guide Version 3.4 - (STIG 3.4) APP3510 CAT I
[6] Standards Mapping - Common Weakness Enumeration - (CWE) CWE ID 631
[7] Standards Mapping - Payment Card Industry Data Security Standard Version 1.2 - (PCI 1.2) Requirement 6.3.1.1, Requirement 6.5.2
[8] Standards Mapping - Payment Card Industry Data Security Standard Version 2.0 - (PCI 2.0) Requirement 6.5.1
[9] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 - (PCI 1.1) Requirement 6.5.6
[10] Standards Mapping - FIPS200 - (FISMA) SI