CSRF (Cross-Site Request Forgery)
Cross-Site Request Forgery (CSRF)
What is it?
CSRF, short for Cross-site request forgery, is a type of web security flaw that enables an attacker to trick users into executing actions they didn't intend to do.
A simple example:
A vulnerable web application has the endpoint
/updateProfile?id={userid}When a
POSTrequest is made to this endpoint the application:Checks the ID is the current user
If it is, update the profile with the provided information in the request body
When the victim visits the attacker's malicious site, it will:
Send a request to the vulnerable web application
Because the user is logged into that application, the browser will include cookies (importantly, the session cookie)
The vulnerable application processes the request as normal since it came from the user
It's important to note that we need some user interaction for CSRF to work. Typically an attacker would place their payload on a site that they control, and try to entice the target with phishing emails, direct messages on social media, etc. Once the user clicks the link and lands on the page, the payload is triggered.
CSRF defences are now pretty common, so along with just finding places where users can carry out actions, we also need to be able to bypass defences that have not been properly implemented.
Other learning resources:
PortSwigger: Web Security Academy https://portswigger.net/web-security/csrf
The XSS Rat: Bug Bounty Beginner Methodology: CSRF https://www.youtube.com/watch?v=uirJsgvN7Hc
Writeups:
Checklist
Is the referer header being used to validate the request origin?
Do the cookies have SameSite set? (Chrome is lax by default)
Can we submit the request with GET?
Can we override HTTP methods with `X-Http-Method-Override: GET`
Can we override HTTP methods with `_method=POST`
Exploitation
CSRF (Cross-Site Request Forgery)
CSRF is an attacking methodology that forces an end user to execute unexpected actions on a web application. CSRF is an attack against a user's web browser.
Account Takeover
If the victim user access to the page where the payload put in, the victim account information will be changed to the attacker's information e.g. Email address or Username.
CSRF Token Bypass
Below is to bypass CSRF token by using another CSRF token that we get.
Cookie Injection
Referrer Validation
Referrer validation may be enabled depending on the website. If so, we can try to bypass the validation by manipulating the browser's session history stack.
Reveal Another User Information
We can retrieve the information of the another user account which accesses to the web page where the payload inserted. Most of the time, this attack can be executed if the web page allows us to XSS.
Force Requesting to Our Server
Start web server in local machine for looking at the access log information.
Now send POST request with the victim's information in the target website. For example, assume the target web page contains the current logged-in user information in the element of the id named "userinfo".
After a while, we can retrieve access logs contain the information of victims in local machine.
Bypass SameSite Restriction
Reference: https://portswigger.net/web-security/csrf/bypassing-samesite-restrictions
If the SameSite restriction is set as below, we need to bypass it to perform CSRF.
If it’s not set, SameSite=Lax is set by default.
Lax
The Lax is set in the SameSite by default if the website does not specify the SameSite value in the Cookie. It restricts the GET method only so we cannot use POST method in the payload. However, we can overwrite this method in the GET parameter by as below.
So our CSRF payload as the following:
Encode the URL value if necessary. (URL encoding)
Strict
If the Strict is set as the SameSite value, we cannot send any cross-site requests. However, we may be able to perform CSRF by using redirects. For example, when we send a comment in a blog page, we see the message such as "Thank you for your message" in the result page then redirect to the post page.
Encode the URL value if necessary. (URL encoding)
Summary
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.
3 conditions:
A relevant action.
Cookie-based session handling.
No unpredictable request parameters.
How to find:
Remove CSRF token from requests and/or put a blank space.
Change POST to GET.
Replace the CSRF token with a random value (for example 1).
Replace the CSRF token with a random token of the same restraints.
Extract token with HTML injection.
Use a CSRF token that has been used before.
Bypass regex.
Remove referer header.
Request a CSRF by executing the call manually and use that token for the request.
Approach
Quick attacks
Tools
Example 1
Example 2
Json CSRF
CSRF Token Bypass
CSRF sample POC
CSRF to reflected XSS
Mindmaps


Last updated