CSRF with broken Referer validation

3 min read Easy PortSwigger
CSRF
Contents

On this page

The lab

here is how portswigger describes it

This lab's email change functionality is vulnerable to CSRF. It attempts to detect and block cross domain requests, but the detection mechanism can be bypassed.

To solve the lab, use your exploit server to host an HTML page that uses a CSRF attack to change the viewer's email address.

The idea#

the site is using the Referer header the header that says which page a request came from, to make sure the request originated on its own domain. the problem is the way it looks for its domain. instead of properly parsing the url in the Referer and confirming the real origin is theirs it just checks whether its domain appears somewhere in the Referer string. that is the broken part because a string can contain the domain without the request actually coming from it. if we can get the site's domain to appear anywhere in our own Referer even buried in a query string, the naive check is satisfied while the request still comes from us.

Step 1 - Confirm the referer is required and checked#

1111

logging in and sending an email change to repeater, there is no csrf token so the Referer is the whole defence. changing it to a different domain gets the request rejected, so it is definitely being validated.

2222

unlike the last lab removing the Referer completely does not help either the request is still rejected. so this site wants a Referer and wants its own domain in it, and we have to give it one that satisfies the check while still coming from our page

Step 2 - Slip the domain in through a query string#

here is the test that exposes the weak check. i took an obviously wrong Referer and appended the real lab domain onto it as a query string like this.

HTTP
Referer: https://arbitrary-incorrect-domain.net?your-lab-id.web-security-academy.net
3333

the server accepts it. that tells us everything, the check is just looking for its domain as a substring anywhere in the Referer value, and here the domain sits after the ?, in the query part of a url whose actual host is incorrect-domain.net. the site has confused the domain appearing in the text with the request coming from the domain, and that is the whole vulnerability. so our job in the exploit is to make our page's own Referer carry the lab domain in its query string.

Step 3 - Build the POC#

there are two pieces to get right first the Referer a request carries is the url of the page that made it our exploit page, so we need that page's url to end in ?the-lab-domain. we do that with history.pushState. that is a browser api that quietly changes the url shown in the address bar and the current history entry without loading a new page, and it takes three arguments a state value, a title that browsers ignore, and the new url. the earlier POCs used it just to tidy the url to / but here we use its third argument to append ?your-lab-id.web-security-academy.net to our page's url so that when the form submits, that url now carrying the lab domain, becomes the Referer

javascript
history.pushState("", "", "/?your-lab-id.web-security-academy.net")

the second piece is a browser habit that would otherwise ruin this. most browsers now trim the query string off the Referer by default for privacy which would strip our domain right back off before the request leaves. to force the browser to send the full url instead query string and all we set a referrer policy of unsafe-url, either with a Referrer-Policy: unsafe-url response header on the exploit server or a <meta name="referrer" content="unsafe-url"> tag on the page. here is the finished page

HTML
<html>
<head>
<meta name="referrer" content="unsafe-url">
</head>
<body>
<form action="https://your-lab-id.web-security-academy.net/my-account/change-email" method="POST">
<input type="hidden" name="email" value="[email protected]" />
<input type="submit" value="Submit request" />
</form>
<script>
history.pushState("", "", "/?your-lab-id.web-security-academy.net")
document.forms[0].submit()
</script>
</body>
</html>

putting it together when the victim opens the page, pushState rewrites its url so it ends with the lab domain in a query string the unsafe-url policy makes sure that full url will be sent as the Referer and then the form auto submits its post. the request arrives carrying the victim's session cookie and a Referer that has the lab domain sitting in its query string the broken check spots the domain in the string and accepts it, and the email is changed.

4444

with this, the lab is solved!