Skip to content
CSRF Disclosed report Advanced CVE-2020-8166

The authenticity_token can be reversed and used to forge valid per_form_csrf_tokens for arbitrary routes

When per-form CSRF tokens are enabled, the authenticity_token is built so that its first half is a non-secret one-time pad, letting anyone reverse it to recover the session CSRF secret. With that secret an attacker can compute valid per-form tokens for any route, defeating the per-form CSRF protection.

jregele Follow

Originally disclosed on HackerOne

Medium 5.9 View original
  • Triaged in 17 days
  • Fixed in 6 months
  • Paid in 6 months
  • Disclosed after 10 months

When per_form_csrf_tokens is set to true, each form should protected against CSRF with a unique token that is not predictable by an attacker. Theper_form_csrf_token is generated using a HMAC SHA-256 using a key that is exposed in a reversed authenticity_token. The authenticity_token is a Base64 encoding of

one_time_pad | (one_time_pad XOR session[:csrf_token])

Because the one_time_pad is the first half of the authenticity_token, it is not secret and an attacker can reverse the token to learn session[:csrf_token].

From there the attacker can forge a hash for an arbitrary route (e.g. /articles/2): HMAC ( session[:csrf_token], "/articles/2#patch")

To reproduce:

  1. Have two Rails routes that accept only per form csrf tokens
  2. Validate that the authenticity_token sent in the POST data returns an HTTP 422 when sent in the other form
  3. Forge a per form token with the attached exploit script
    • the authenticity_token parameter is taken from the <meta> tag in the header of any page for the session
    • the route parameter is the action of the target HTML form (e.g /articles/2)
    • the method parameter is the value from _method parameter sent in the POST data (e.g. patch)
  4. Take the forged token from the exploit script, URL-encode it, and send it as the authenticity_token in the POST data. For reliability, ensure that:
    • the route parameter matches the endpoint,
    • the _method parameter in the POST data matches what used for method in the exploit script
    • the forged authenticity_token in the POST data is properly URL-encoded

Impact

Exploitation allow an attacker to forge valid per-form CSRF tokens even in hardened situations where the global authenticity_token itself is not allowed.

For the attack to be successful, an attacker would need a valid global authenticity_token. This can be extracted out of a web page without any protections that cookies have (such as HTTP-Only). An attacker could leverage an XSS vulnerability to bypass per-form CSRF on unrelated pages. Because the token can be forged for any form, code execution on a page without forms could still lead to attackers bypassing CSRF protections of forms related to password changes, deletion of data, creation of new users, etc.

The original report as published on HackerOne, by jregele. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The token encoding places the one-time pad in plaintext alongside the XOR result, so the session CSRF secret can be recovered by reversing the encoding.

Impact

An attacker who obtains a valid global authenticity_token, for example via XSS, could forge per-form tokens and bypass CSRF protection on forms such as password changes or data deletion.

What the attacker needed

Per_form_csrf_tokens enabled; attacker has a valid global authenticity_token; often needs an XSS to obtain the token

How it was found

  1. Set up two Rails routes that only accept per-form CSRF tokens.
  2. Confirm a token from one form is rejected with HTTP 422 when sent to the other form.
  3. Run the exploit script using the authenticity_token from the page meta tag, the target route, and the method.
  4. URL-encode the forged token and send it as the authenticity_token in the POST data, matching the route and _method.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

Websites use a secret token in forms to prove a request came from the real site and not a malicious one; this is called CSRF protection. Here the token was built in a way that let anyone who saw it work out the underlying secret. Once you know the secret, you can make valid tokens for any form on the site.

What is CSRF?

Another site makes a signed-in user's browser perform an action they did not intend.

More from Ruby on Rails

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site request forgery (CSRF)

The lesson behind this report.

Another site makes a signed-in user's browser perform an action they did not intend.