![]() |
VOOZH | about |
Cross-Site Request Forgery (CSRF) is a critical web vulnerability that allows attackers to trick authenticated users into performing unintended actions, such as changing account details or even taking full control of their accounts.
Web applications typically rely on cookies to maintain user sessions, since HTTP is a stateless protocol and does not natively support persistent authentication. Without cookies, users would need to re-enter their credentials for every request, which would severely impact usability. Cookies solve this by storing session information, but they also make applications susceptible to CSRF attacks if not properly protected.
Imagine you’re logged into your online banking account to check your balance. Meanwhile, a malicious website silently tricks your browser into making unauthorized transactions on your behalf. This illustrates a Cross-Site Request Forgery (CSRF) attack, a serious web security vulnerability that impacts countless websites. Recognizing this threat is vital for both users and developers to build safer online experiences.
For a CSRF attack to succeed all three of the following must hold:
A Relevant Action: The application exposes an action the attacker wants to trigger, for example:
Cookie-Based Session Handling
Predictable Request Parameters
An attacker could create a malicious web page like this:
Below are real-world-inspired CSRF examples
When an application performs a state-changing action on an HTTP GET request (bad practice), an attacker can trigger that action via image/script tags that the browser will load automatically and the browser will include cookies with those requests.
Vulnerable endpoint:
GET /vote?post=123&up=1forum.example.forum.example has a vulnerable endpoint GET /vote?post=123&up=1 that records votes (state change via GET).<img src="https://forum.example/vote?post=123&up=1"> on a page or email.Sanitised attacker snippet
<img src="https://forum.example/vote?post=123&up=1" alt="">GET must never change server state; use safe HTTP methods (POST/PUT/DELETE) plus CSRF protections.
Single-page apps (SPAs) that use cookie-based sessions are vulnerable if their APIs accept state-changing requests authenticated only by cookies and lack CSRF defenses.
example.com. The website gives their browser a small file called a session cookie to remember that they’re logged in.fetch("https://api.example.com/user/delete-account", { method: "POST", credentials: "include" });This code tells the browser: “Send a request to delete the user’s account, and include any cookies you already have.”api.example.com).Given below the step by step hands on lab of CSRF
Step 1: Set DVWA security to LOW
Login to DVWA as admin and in the DVWA Security page set the level to Low. This makes the CSRF protection minimal or absent for learning.
Step 2: Find the vulnerable action and inspect the request
New Password, Confirm New Password)http://127.0.0.1/dvwa/vulnerabilty/csrf?password_new=password1&password_conf=password1&Change=Change#Step 3: Create a simple attacker page (csrf.html)
.html containing a form that mimics the DVWA request. Replace the URL and field names with the ones you saw in step 2.Step 4: Simulate the victim and trigger the attack
http://localhost/csrf.html.Step 5: Verify the effect
CSRF attacks usually target HTTP requests that change something about a user’s account, like their name, email, password, or even logging them in/out without permission.
example.com (session stored in cookies).evil.com.example.com (e.g., “change password”).evil.com, the hidden form is automatically submitted.example.com thinks the request is genuine and changes the password to whatever the attacker set.On User Side: User side prevention is very inefficient in terms of browsing experience, prevention can be done by browsing only a single tab at a time and not using the "remember-me" functionality.
On Server Side: There are many proposed ways to implement CSRF protection on server side, among which the use of CSRF tokens is most popular. A CSRF token is a string that is tied to a user's session but is not submitted automatically. A website proceeds only when it receives a valid CSRF token along with the cookies, since there is no way for an attacker to know a user specific token, the attacker can not perform actions on user's behalf.