Beware of the invisible click! Understanding CSRF Attacks
- Bhupendra budha
- Jan 26
- 3 min read
CSRF is one of the web security vulnerabilities that lures the user into executing unwanted or malicious actions on a trusted website where the user has been authenticated. It is related to Broken Access Control but is categorized as an injection-based attack rather than a direct access issue.
The attack involves the exploitation of the trust between the website and the user’s browser session.
The actions might involve changing emails, passwords, submitting payments, etc.
CSRF was included in the OWASP Top 10 (2013) but was later removed in 2017 because most modern frameworks like Django, Laravel, etc., enforce CSRF protection by default. Moreover, Broken Access Control (A01:2021) covers broader authorization issues, including improper enforcement of CSRF protection.
CSRF Attack Demo
Let’s dive into the demonstration of a CSRF attack, where we will craft a malicious HTML page and lure a logged-in user into updating their email address without their consent.
Step 1: Understanding a Scenario
Imagine a banking site where users can change their credentials, such as their email address, while logged in. Normally, the user updates their email using the following form:

The user's email address is updated successfully whenever the “Submit” button is pressed.
NOTE: We haven’t enforced CSRF protection for this demonstration.
Step 2: Exploitation
When the victim logs into their bank account (victim-bank.com), the website remembers their session using cookies, so the user doesn’t have to log in every time. During this time, the attacker can redirect the victim to a malicious page (attack-site.com).
Thus, the malicious site can secretly submit a fake request to the victim’s bank site, changing the victim’s email address.
The malicious site is designed to automatically send the request as soon as the victim visits it.

This attack.html (attack-site.com/attack.html) is loaded on the attacker’s page, where the value="attacker@example.com" will be set automatically. Thus, the attacker exploits the existing session, improper tokenization of CSRF, etc., to perform the CSRF attack.
There are several ways to lure the victim into clicking the malicious site, including:✔ Phishing emails✔ Fake social media posts/ads✔ Compromised websites
Step 3: Preventing CSRF Attacks
One can prevent a CSRF attack by enforcing several security protocols, authentication mechanisms, and sanitization techniques.
I. CSRF Tokens: The server must include a unique CSRF token during every form submission. This ensures that the server can validate genuine form submissions from the user and reject requests from an attacker.

II. SameSite Cookies (SameSite=Strict): Setting SameSite=Strict in the Set-Cookie field prevents cookies from being sent along with requests from other sites.

III. Re-authentication: For every critical action (such as changing passwords or making transactions), the server must request re-authentication from the user (e.g., OTP or password confirmation).
IV. Request Validation & Secure Logout:
The server should validate whether the request originates from the same site.
Auto-logout inactive users to prevent attackers from exploiting existing sessions.
References
Bright Security. "3 Simple CSRF Examples: Understand CSRF Once and For All." BrightSec, https://brightsec.com/blog/csrf-example/. Accessed January 26, 2025.
OWASP Foundation. "Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet." OWASP Cheat Sheet Series, https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html. Accessed January 26, 2025.
PortSwigger Ltd. "What is CSRF (Cross-site request forgery)? Tutorial & Examples." PortSwigger Web Security Academy, https://portswigger.net/web-security/csrf. Accessed January 26, 2025.
Bright Security. "6 CSRF Mitigation Techniques You Must Know." BrightSec, https://brightsec.com/blog/csrf-mitigation/. Accessed January 26, 2025.
OWASP Foundation. "Cross Site Request Forgery (CSRF)." OWASP, https://owasp.org/www-community/attacks/csrf. Accessed January 26, 2025.
PortSwigger Ltd. "How to prevent CSRF vulnerabilities." PortSwigger Web Security Academy, https://portswigger.net/web-security/csrf/preventing. Accessed January 26, 2025.
Snyk Ltd. "CSRF Attack | Tutorial & Examples." Snyk Learn, https://learn.snyk.io/lesson/csrf-attack/. Accessed January 26, 2025.
Mozilla Developer Network (MDN). "Cross-site request forgery (CSRF) prevention." MDN Web Docs, https://developer.mozilla.org/en-US/docs/Web/Security/Practical_implementation_guides/CSRF_prevention. Accessed January 26, 2025.
Imperva Inc. "Cross site request forgery (CSRF) attack." Imperva, https://www.imperva.com/learn/application-security/csrf-cross-site-request-forgery/. Accessed January 26, 2025.
Bright Security. "What Is Cross-Site Request Forgery (CSRF)? Impact and Prevention." BrightSec, https://brightsec.com/blog/cross-site-request-forgery-csrf/. Accessed January 26, 2025.
Wiz Inc. "Cross-Site Request Forgery (CSRF) Examples and Prevention." Wiz Academy, https://www.wiz.io/academy/cross-site-request-forgery-csrf. Accessed January 26, 2025.
Invicti Security. "CSRF Attacks: Anatomy, Prevention, and XSRF Tokens." Invicti, https://www.acunetix.com/websitesecurity/csrf-attacks/. Accessed January 26, 2025.
GuardRails.io. "What Is CSRF And How Do You Prevent It?" GuardRails Blog, https://www.guardrails.io/blog/what-is-csrf-and-how-do-you-prevent-it/. Accessed January 26, 2025.
Microsoft. "Prevent Cross-Site Request Forgery (XSRF/CSRF) attacks in ASP.NET Core." Microsoft Learn, https://learn.microsoft.com/en-us/aspnet/core/security/anti-request-forgery?view=aspnetcore-9.0. Accessed January 26, 2025.
Invicti Security. "How To Prevent CSRF Attacks by Using Anti-CSRF Tokens." Invicti, https://www.invicti.com/blog/web-security/protecting-website-using-anti-csrf-token/. Accessed January 26, 2025.
Sucuri Inc. "What is Cross Site Request Forgery (CSRF)?" Sucuri Guides, https://sucuri.net/guides/what-is-csrf/. Accessed January 26, 2025.
Wikipedia. "Cross-site request forgery." Wikipedia, https://en.wikipedia.org/wiki/Cross-site_request_forgery. Accessed January 26, 2025.








Comments