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.
- 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:
- Have two Rails routes that accept only per form csrf tokens
- Validate that the
authenticity_tokensent in the POST data returns an HTTP 422 when sent in the other form - Forge a per form token with the attached exploit script
- the
authenticity_tokenparameter is taken from the <meta> tag in the header of any page for the session - the
routeparameter is the action of the target HTML form (e.g /articles/2) - the
methodparameter is the value from_methodparameter sent in the POST data (e.g. patch)
- the
- Take the forged token from the exploit script, URL-encode it, and send it as the
authenticity_tokenin the POST data. For reliability, ensure that:- the route parameter matches the endpoint,
- the
_methodparameter in the POST data matches what used formethodin the exploit script - the forged
authenticity_tokenin 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.
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
- Set up two Rails routes that only accept per-form CSRF tokens.
- Confirm a token from one form is rejected with HTTP 422 when sent to the other form.
- Run the exploit script using the authenticity_token from the page meta tag, the target route, and the method.
- 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.