CSP Bypasses: Advanced Exploitation Guide

CSP Bypasses: Advanced Exploitation Guide

Content Security Policies (CSPs) are often deployed as the last line of defense against client-side attacks such as cross-site scripting (XSS) and clickjacking. Since their first introduction in 2012, they’ve enabled developers to control which and what resources are allowed to load and evaluate within a given DOM context.

However, it still commonly occurs that developers rely on this countermeasure as the sole defensive layer against these client-side attacks. Ultimately, introducing new opportunities for us to evade this and manage to execute our malicious JavaScript code.

In this article, we’ll explore in-depth what Content Security Policies are and how we can bypass CSPs to, for example, exploit XSS vulnerabilities.

Let’s dive in!

What is a Content Security Policy (CSP)

Content Security Policy (CSP) is a browser security mechanism designed to mitigate content injection attacks, including cross-site scripting (XSS) and clickjacking vulnerabilities. By specifying which sources the browser should trust for different types of content (scripts, stylesheets, images, etc.), developers can effectively control what resources are allowed to load and execute on their web pages.

When implemented correctly, CSP acts as a defense-in-depth layer that can prevent XSS exploitation even when input validation is missing or insufficient. However, CSP should never be considered as the only line of defense, as misconfigurations and oversights can render it ineffective or allow for complete bypasses, as we’ll cover later on throughout this article.

Let’s go over the most important directive names and sources to help us better understand what CSP bypasses are. If you’re already familiar with CSPs and client-side attacks, you may skip ahead to the bypasses section.

Content Security Policy (CSP) bypasses in bug bounty

Identifying Content Security Policy (CSP) misconfigurations is often report-worthy in pentests. However, this isn’t necessarily the same with bug bounty.

Most programs won’t accept CSP bypass reports as standalone vulnerabilities. You’ll always need to chain your CSP bypass with, for instance, an actual XSS vulnerability to demonstrate real-world impact.

Finding Content Security Policy (CSP) declarations

Content Security Policies (CSPs) can be implemented in two main ways, understanding where to look for them is essential for analyzing potential misconfigurations.

HTTP response header

The most common implementation method is through the Content-Security-Policy HTTP response header. You can easily view this in your browser’s developer tools under the Network tab by inspecting the response headers of any page load.

Content Security Policy (CSP)

HTML meta tag:

Alternatively, CSP can be defined within the HTML document itself using a tag in the page’s section:

Deconstructing Content Security Policy (CSP) directives

A Content Security Policy consists of one or more directives, each controlling a specific type of resource. Below is a comprehensive table of the most important CSP directives you’ll encounter during security testing.

It’s recommended to have a look at both directive names and sources, as this information will help us find misconfigurations in CSP declarations that we can actively abuse:

Content Security Policy (CSP) declaration names explained

Content Security Policy (CSP) bypasses

CSP bypasses typically occur due to a misconfiguration or a weak CSP declaration. In the next sections, we’ll examine a few practical exploitation techniques that demonstrate how you can identify and bypass these CSP misconfigurations to execute arbitrary JavaScript via an XSS vulnerability eventually.

1. No Content Security Policy (CSP) declaration

A missing Content Security Policy (CSP) declaration is the simplest example. When no policy is defined, the browser doesn’t enforce any restrictions on resource loading or script execution. Practically, this means that you can execute any code without restrictions.

To verify if your target makes use of a CSP, you’ll need to check the response header and HTML source for the presence of a CSP declaration.

On some occasions, you’ll notice that some application routes or directories on the same host have different CSPs set. Make sure to also take this into account.

Just as an effective CSP can help websites deter XSS and injection attacks, it can also inadvertently break them by blocking safe scripts from loading. To avoid breaking sites in production, developers will deploy a new CSP in report-only mode. In this mode, CSP violations are reported, but the policy itself is never enforced. Similar to the previous case, this would allow you to execute arbitrary code via XSS uninterrupted.

To locate such targets, you’ll need to examine the HTTP headers returned in the response and search for the presence of the Content-Security-Policy-Report-Only header. This header is widely supported across multiple web browsers, including Google Chrome, Mozilla Firefox & Safari. It essentially instructs these web browsers to only monitor and report CSP violations, but not to enforce them.

A target that only monitors for CSP violations but never enforces them allowing for CSP bypasses to arise

Even if a CSP is declared, we must look for non-restrictive declarations that we can bypass. In most instances, you’ll notice that developers have had to omit a declaration or include a CSP with a non-restrictive declaration to avoid possibly rendering the application unusable in production environments. Fortunately for us, we have access to open-source tooling that can help us identify such loosely-scoped CSP declarations.

Let’s have a look at a few examples.

Bypassing CSP via script-src

As we saw earlier, the script-src declaration specifies what scripts the browser allows to load and execute. If a wildcard (*) has been set as the declaration source, no restrictions will be applied, and a simple payload that loads an external script will be allowed to execute:

In older PHP versions where CR/LF injection is still attainable, you may be able to inject additional response headers that would have facilitated the XSS attack. The key here is always to seek possible ways to override the currently enforced policy.

Although standalone CSP bypasses are rarely considered report-worthy bugs, they should still be taken into account, as they could allow for the execution of arbitrary JavaScript code. In this article, we've explored multiple ways to test for possible Content Security Policy (CSP) bypasses.

So, you've just learned something new about bypassing CSPs... Right now, it's time to put your skills to the test! You can start by practicing on vulnerable labs and CTFs or... browse through our 70+ public bug bounty programs on Intigriti, and who knows, maybe earn a bounty on your next submission!



Source link