<link href="/user/themes/antimatter/css/pure-0.5.0/grids-min.css" type="text/css" rel="stylesheet">
<link href="/user/themes/antimatter/css-compiled/nucleus.css" type="text/css" rel="stylesheet">
<link href="/user/themes/antimatter/css-compiled/template.css" type="text/css" rel="stylesheet">
<link href="/user/themes/antimatter/css/font-awesome.min.css" type="text/css" rel="stylesheet">
<link href="/user/plugins/markdown-notices/assets/notices.css" type="text/css" rel="stylesheet">
<link href="/user/plugins/breadcrumbs/css/breadcrumbs.css" type="text/css" rel="stylesheet">
<link href="/user/plugins/form/assets/form-styles.css" type="text/css" rel="stylesheet">
<link href="/user/plugins/simplesearch/css/simplesearch.css" type="text/css" rel="stylesheet">
<link href="/user/plugins/login/css/login.css" type="text/css" rel="stylesheet">
<link href="/user/themes/antimatter/css/slidebars.min.css" type="text/css" rel="stylesheet">
<script src="/system/assets/jquery/jquery-2.1.4.min.js"></script>
<script src="/user/themes/antimatter/js/modernizr.custom.71422.js"></script>
<script src="/user/themes/antimatter/js/antimatter.js"></script>
<script src="/user/themes/antimatter/js/slidebars.min.js"></script>
<h1>Introduction</h1>
<p>In the <a href="https://kernelpanic.cryptid.fr/en/blog/nonsense-mayhem-samesite-cors-and-csrf">last blog post</a>, we detailed the various conditions necessary to <strong>perform a successful CSRF attack</strong>, considering today's browser security. I mentioned that it was mostly written out of frustration, as I received numerous erroneous pentest reports regarding CSRF... Well, it seems that <strong>CORS (Cross-Origin Resource Sharing) mechanisms are also a widely misunderstood</strong> by some pentesters. Let us dive in the wonderful world of CORS misconfigurations.</p>
<h2>SOP (Same Origin Policy)</h2>
<p>Once again, let's go back to the basics. <strong>CORS cannot be explained without SOP (Same Origin Policy)</strong>. The Same Origin Policy is a security mechanism that is implemented in all web browsers, and serves as a corner stone of web security. Basically, the SOP says that <strong>scripts that were loaded from a given page</strong> can only interact with another page <strong>if they share the same origin</strong>. Let us consider that the script loaded by your browser is located at the following URL:</p>
<blockquote>
<p>https://mysite.com:8443/flop.js</p>
</blockquote>
<p>The origin of the document is defined by the combination of three parameters. If one of them differs from the script's origin, the script's interaction will be denied. </p>
<ul>
<li><strong>Protocol - https</strong>
A XHR call to <strong>http</strong>://mysite.com:8443/flop.js would be forbidden as protocols do not match;</li>
<li><strong>Host - mysite.com</strong>:
A XHR call to any other host - test.mysite.com, cryptid.fr or mysite.net would be forbidden as host names do not match;</li>
<li><strong>Port - 8443</strong>:
A XHR call to https://mysite.com would default to port 443, which does not match 8443, thus would be forbidden</li>
</ul>
<p>CORS was designed to bypass the SOP policy, in order to <strong>allow the browser to interact from a given origin to another</strong>. This is a more and more common need, because of the rise of stateless APIs, which are often not hosted on the same domain than frontend JS web applications. However, these misconfigurations may have quite important consequences.</p>
<h2>Wildcard Origin misconfiguration</h2>
<p>The following sections assume that you already know how CORS works, and the functions of the main headers. We will not go over the details of preflighted requests, feel free to consult <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS"><strong>Mozilla's documentation</strong></a> regarding this matter! Let us consider the following CORS Policy:</p>
<blockquote>
<p>Access-Control-Allow-Origin: *</p>
<p>Access-Control-Allow-Credentials: true</p>
</blockquote>
<p>Most of the reports I received were stating that it was possible to leak the user's data, and perform actions with the logged-in user privilege. This is simply false. When the <strong>wildcard</strong> character is used in conjunction with <strong>Access-Control-Allow-Credentials: true</strong>, the browser will not send any cookies automatically, as the configuration is considered as insecure.</p>
<p>This has been designed to prevent lazy people from just whitelisting any origin easily (good call), which is a major vulnerability as you all know. In this situation, the consequences are not as critical as only unauthenticated requests are possible but a few scenarios can be imagined, such as indirect bruteforce attacks: let us assume that an attacker has found an XSS vulnerability in a high profile website with a lot of traffic. The attacker can craft a specific playload in order to attempt to bruteforce a login form through the visitors browsers, not even needing to interact with the target system!</p>
<p>I found many blog posts that were misleading about this wilcard configuration, but the browser will not allow you to set Access-Control-Allow-Credential to true in this case. Don't belive me? PoC it!</p>
<h2>Reflected Origin misconfiguration</h2>
<p>This scenario is more problematic as it completely breaks the SOP policy. This happens when the Origin provided by the client is reflected in the server's response, and not checked against a known whitelist of authorized origins (this also works with the null origin):</p>
<blockquote>
<p>Access-Control-Allow-Origin: attackers-website.com</p>
<p>Access-Control-Allow-Credentials: true</p>
</blockquote>
<p>In this case, the attacker can host a malicious web page on a domain he controls (or use a third party website where he has exploited an XSS vulnerability). The risk is that the cookie should be forwarded automatically as the configuration allows it, which results in <strong>data leaks</strong> as the attacker's website can query the APIs with the user's privileges, or perform <strong>CSRF-like attacks</strong>, by performing specific modifications that would only be allowed to the targeted user.</p>
<p>However the success of this attack is conditioned by other factors, remember our previous article?</p>
<ul>
<li>The <strong>session</strong> token should be stored as a <strong>cookie</strong>, or standard authentication headers (Basic, Digest, NTLM...),</li>
<li>If the <strong>session</strong> token is a <strong>cookie</strong>, the SameSite parameter should be set to <strong>None</strong>, or it will not be sent cross domain, with the exception of cookies defaulting to <strong>Lax</strong>, which shall be allowed to be sent cross domain if they are less than two minutes old.</li>
</ul>
<p>If these conditions are not met, the impacts are much less important, as they will be similar to the previous case, the "Wildcard Origin misconfiguration".</p>
<h2>Closing words (PoC, or it didn't happen - again)</h2>
<p>Once again, if you are unsure if a given exploit works, try to write a proof of concept for it. As stated in the previous article, BurpSuite includes a tool to generate CSRF and CORS attack PoCs, just right-click on the targeted request, choose <strong>engagement tools, CSRF PoC, then Cross-Domain XHR (modern browsers only) in the Options menu</strong>. Stayed tuned for more security-related articles! </p>
<p>Need <strong>offensive security services</strong>, or <strong>application security support</strong>? <a href="https://cryptid.fr"><strong>CryptID has got you covered</strong></a></p>
<script src="/user/plugins/simplesearch/js/simplesearch.js"></script>