> For the complete documentation index, see [llms.txt](https://hacktricks.xsx.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacktricks.xsx.tw/network-services-pentesting/pentesting-web/special-http-headers.md).

# Special HTTP headers

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

## 字典和工具

* <https://github.com/danielmiessler/SecLists/tree/master/Miscellaneous/web/http-request-headers>
* <https://github.com/rfc-st/humble>

## 更改位置的头部

重写**IP来源**：

* `X-Originating-IP: 127.0.0.1`
* `X-Forwarded-For: 127.0.0.1`
* `X-Forwarded: 127.0.0.1`
* `Forwarded-For: 127.0.0.1`
* `X-Forwarded-Host: 127.0.0.1`
* `X-Remote-IP: 127.0.0.1`
* `X-Remote-Addr: 127.0.0.1`
* `X-ProxyUser-Ip: 127.0.0.1`
* `X-Original-URL: 127.0.0.1`
* `Client-IP: 127.0.0.1`
* `X-Client-IP: 127.0.0.1`
* `X-Host: 127.0.0.1`
* `True-Client-IP: 127.0.0.1`
* `Cluster-Client-IP: 127.0.0.1`
* `Via: 1.0 fred, 1.1 127.0.0.1`
* `Connection: close, X-Forwarded-For`（检查逐跳头部）

重写**位置**：

* `X-Original-URL: /admin/console`
* `X-Rewrite-URL: /admin/console`

## 逐跳头部

逐跳头部是设计为由当前处理请求的代理处理和消耗的头部，而不是端到端头部。

* `Connection: close, X-Forwarded-For`

{% content-ref url="/pages/JYbS7PM6Wmt9lCFnP5Dl" %}
[hop-by-hop headers](/pentesting-web/abusing-hop-by-hop-headers.md)
{% endcontent-ref %}

## HTTP请求劫持

* `Content-Length: 30`
* `Transfer-Encoding: chunked`

{% content-ref url="/pages/gSY9Wl8qDT62zuF9z6lu" %}
[HTTP Request Smuggling / HTTP Desync Attack](/pentesting-web/http-request-smuggling.md)
{% endcontent-ref %}

## 缓存头部

**服务器缓存头部**：

* **响应中的`X-Cache`可能具有值`miss`**（当请求未被缓存时）和值\*\*`hit`\*\*（当已被缓存时）
* \*\*`Cf-Cache-Status`\*\*头部中的行为类似
* \*\*`Cache-Control`\*\*指示资源是否被缓存以及下次资源将再次被缓存的时间：`Cache-Control: public, max-age=1800`
* **`Vary`通常在响应中用于指示额外的头部**，即使它们通常不是关键的也会被视为**缓存键**的一部分。
* \*\*`Age`\*\*定义了对象在代理缓存中存在的时间（以秒为单位）。
* \*\*`Server-Timing: cdn-cache; desc=HIT`\*\*还表示资源已被缓存

{% content-ref url="/pages/fpRfH38gu3LMdXUvYe7i" %}
[Cache Poisoning and Cache Deception](/pentesting-web/cache-deception.md)
{% endcontent-ref %}

**本地缓存头部**：

* `Clear-Site-Data`：指示应该被清除的缓存的头部：`Clear-Site-Data: "cache", "cookies"`
* `Expires`：包含响应应该过期的日期/时间：`Expires: Wed, 21 Oct 2015 07:28:00 GMT`
* `Pragma: no-cache` 等同于 `Cache-Control: no-cache`
* `Warning`：\*\*`Warning`\*\*通用HTTP头部包含有关消息状态可能存在问题的信息。一个响应中可能出现多个`Warning`头部。`Warning: 110 anderson/1.3.37 "Response is stale"`

## 条件

* 使用这些头部的请求：\*\*`If-Modified-Since`**和**`If-Unmodified-Since`\*\*将仅在响应头部\*\*`Last-Modified`\*\*包含不同时间时响应数据。
* 使用\*\*`If-Match`**和**`If-None-Match`\*\*的条件请求使用Etag值，因此如果数据（Etag）已更改，Web服务器将发送响应内容。`Etag`取自HTTP响应。
* **Etag**值通常基于响应内容计算。例如，`ETag: W/"37-eL2g8DEyqntYlaLp5XLInBWsjWI"`表示`Etag`是响应内容的**Sha1**值，长度为**37字节**。

## 范围请求

* **`Accept-Ranges`**：指示服务器是否支持范围请求，以及范围可以用哪种单位表示。`Accept-Ranges: <range-unit>`
* **`Range`**：指示服务器应返回文档的部分。
* **`If-Range`**：创建一个条件范围请求，仅当给定的ETag或日期与远程资源匹配时才会被满足。用于防止从不兼容版本的资源下载两个范围。
* **`Content-Range`**：指示完整消息体中部分消息所属的位置。

## 消息体信息

* **`Content-Length`**：资源的大小，以十进制字节为单位。
* **`Content-Type`**：指示资源的媒体类型
* **`Content-Encoding`**：用于指定压缩算法。
* **`Content-Language`**：描述面向受众的人类语言，以便用户根据自己的首选语言进行区分。
* **`Content-Location`**：指示返回数据的备用位置。

从渗透测试的角度来看，这些信息通常是“无用的”，但如果资源受到401或403的保护，并且您可以找到某种方法来获取这些信息，那可能会很有趣。\
例如，通过HEAD请求中\*\*`Range`**和**`Etag`\*\*的组合可以通过HEAD请求泄露页面内容：

* 具有头部`Range: bytes=20-20`的请求，并且响应包含`ETag: W/"1-eoGvPlkaxxP4HqHv6T3PNhV9g3Y"`，泄露了字节20的SHA1为`ETag: eoGvPlkaxxP4HqHv6T3PNhV9g3Y`。

## 服务器信息

* `Server: Apache/2.4.1 (Unix)`
* `X-Powered-By: PHP/5.3.3`

## 控制

* **`Allow`**：此标头用于指定资源可以处理的HTTP方法。例如，可以指定为 `Allow: GET, POST, HEAD`，表示资源支持这些方法。
* **`Expect`**：客户端使用此标头传达服务器需要满足的请求处理期望。常见用例涉及 `Expect: 100-continue` 标头，表示客户端打算发送大数据负载。客户端在继续传输之前会寻找 `100 (Continue)` 响应。此机制有助于通过等待服务器确认来优化网络使用。

## 下载

* HTTP响应中的 **`Content-Disposition`** 标头指示文件是应该内联显示（在网页内）还是作为附件（下载）。例如：

```
Content-Disposition: attachment; filename="filename.jpg"
```

这意味着名为"filename.jpg"的文件旨在被下载并保存。

## 安全头部

### 内容安全策略（CSP） <a href="#csp" id="csp"></a>

{% content-ref url="/pages/MNBsDILE5555KkghznPR" %}
[Content Security Policy (CSP) Bypass](/pentesting-web/content-security-policy-csp-bypass.md)
{% endcontent-ref %}

### **受信任类型**

通过CSP强制执行受信任类型，应用程序可以受到保护，免受DOM XSS攻击。受信任类型确保只有符合既定安全策略的特定对象可以在危险的Web API调用中使用，从而默认情况下保护JavaScript代码。

```javascript
// Feature detection
if (window.trustedTypes && trustedTypes.createPolicy) {
// Name and create a policy
const policy = trustedTypes.createPolicy('escapePolicy', {
createHTML: str => str.replace(/\</g, '&lt;').replace(/>/g, '&gt;');
});
}
```

```javascript
// Assignment of raw strings is blocked, ensuring safety.
el.innerHTML = 'some string'; // Throws an exception.
const escaped = policy.createHTML('<img src=x onerror=alert(1)>');
el.innerHTML = escaped;  // Results in safe assignment.
```

### **X-Content-Type-Options**

该标头可防止 MIME 类型嗅探，这种做法可能导致 XSS 漏洞。它确保浏览器遵守服务器指定的 MIME 类型。

```
X-Content-Type-Options: nosniff
```

### **X-Frame-Options**

为了防范点击劫持，该标头限制了文档如何嵌入到 `<frame>`, `<iframe>`, `<embed>`, 或 `<object>` 标签中，建议所有文档明确指定其嵌入权限。

```
X-Frame-Options: DENY
```

### **跨源资源策略（CORP）和跨源资源共享（CORS）**

CORP对于指定网站可以加载哪些资源至关重要，有助于减轻跨站点泄漏。另一方面，CORS允许更灵活的跨源资源共享机制，在特定条件下放宽同源策略。

```
Cross-Origin-Resource-Policy: same-origin
Access-Control-Allow-Origin: https://example.com
Access-Control-Allow-Credentials: true
```

### **跨源嵌入策略（COEP）和跨源打开策略（COOP）**

COEP和COOP对于启用跨源隔离至关重要，大大降低了类似Spectre的攻击风险。它们分别控制跨源资源的加载和与跨源窗口的交互。

```
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin-allow-popups
```

### **HTTP Strict Transport Security (HSTS)**

最后，HSTS 是一项安全功能，强制浏览器只能通过安全的 HTTPS 连接与服务器通信，从而增强隐私和安全性。

```
Strict-Transport-Security: max-age=3153600
```

## 参考资料

* [Content-Disposition](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition)
* [HTTP Headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers)
* [安全头部](https://web.dev/security-headers/)
* [安全头部文章](https://web.dev/articles/security-headers)

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