CSRF
Introduction
Cross-site request forgery (also known as CSRF) is a web security vulnerability that allows an attacker to induce users to perform actions that they do not intend to perform. It allows an attacker to partly circumvent the same origin policy, which is designed to prevent different websites from interfering with each other.
For a CSRF attack to be possible, three key conditions must be in place:
A relevant action. There is an action within the application that the attacker has a reason to induce. This might be a privileged action (such as modifying permissions for other users) or any action on user-specific data (such as changing the user's own password).
Cookie-based session handling. Performing the action involves issuing one or more HTTP requests, and the application relies solely on session cookies to identify the user who has made the requests. There is no other mechanism in place for tracking sessions or validating user requests.
No unpredictable request parameters. The requests that perform the action do not contain any parameters whose values the attacker cannot determine or guess. For example, when causing a user to change their password, the function is not vulnerable if an attacker needs to know the value of the existing password.
Although CSRF is normally described in relation to cookie-based session handling, it also arises in other contexts where the application automatically adds some user credentials to requests, such as HTTP Basic authentication and certificate-based authentication.
Basic Payload to automatically submit the request when the web page opens.
<html> <body> <form action="https://vulnerable-website.com/email/change" method="POST"> <input type="hidden" name="email" value="pwned@evil-user.net" /> </form> <script> document.forms[0].submit(); </script> </body> </html>
Testing CSRF
Base Steps:
Bypass Method -1 : Change the request method POST → GET
POST → GETBypass Method - 2: Remove csrf param from POST request.
POST request.Bypass Method - 3: Feed your own account generated CSRF token in attack.
Bypass Method - 4 : Chain any other vulnerability to add your cookie for example XSS, CRLF → CSRF
XSS, CRLF → CSRFBypass Method - 5 : Delete the Referrer Header Completely or Suppress it.
Bypass Method - 6 : Try attacker.com or similar payload in referer header. (Validation of Referer can be circumvented)
Bypass Method - 7: Send null value in csrf token.
null value in csrf token.Mitigation
Defending against CSRF with SameSite cookies
The
SameSiteattribute can be used to control whether and how cookies are submitted in cross-site requests. By setting the attribute on session cookies, an application can prevent the default browser behavior of automatically adding cookies to requests regardless of where they originate.The
SameSiteattribute is added to theSet-Cookieresponse header when the server issues a cookie, and the attribute can be given two values,StrictorLax. For example:
SetCookie: SessionId=sYMnfCUrAlmqVVZn9dqevxyFpKZt30NN; SameSite=Strict;SetCookie: SessionId=sYMnfCUrAlmqVVZn9dqevxyFpKZt30NN; SameSite=Lax;If the
SameSiteattribute is set toStrict, then the browser will not include the cookie in any requests that originate from another site. This is the most defensive option, but it can impair the user experience, because if a logged-in user follows a third-party link to a site, then they will appear not to be logged in, and will need to log in again before interacting with the site in the normal way.If the
SameSiteattribute is set toLax, then the browser will include the cookie in requests that originate from another site but only if two conditions are met:The request uses the GET method. Requests with other methods, such as POST, will not include the cookie.
The request resulted from a top-level navigation by the user, such as clicking a link. Other requests, such as those initiated by scripts, will not include the cookie.
Using
SameSitecookies inLaxmode does then provide a partial defense against CSRF attacks, because user actions that are targets for CSRF attacks are often implemented using the POST method. Two important caveats here are:Some applications do implement sensitive actions using GET requests.
Many applications and frameworks are tolerant of different HTTP methods. In this situation, even if the application itself employs the POST method by design, it will in fact accept requests that are switched to use the GET method.
For the reasons described, it is not recommended to rely solely on SameSite cookies as a defense against CSRF attacks. Used in conjunction with CSRF tokens, however, SameSite cookies can provide an additional layer of defense that might mitigate any defects in the token-based defenses.
Using CSRF Token
The most robust way to defend against CSRF attacks is to include a CSRF token within relevant requests. The token should be:
Unpredictable with high entropy, as for session tokens in general.
Tied to the user's session.
Strictly validated in every case before the relevant action is executed.
Reference
What is CSRF (Cross-site request forgery)? Tutorial & Examples | Web Security Academy
Top 25 CSRF Bug Bounty Reports
Checklist
Author:
Last updated