What is Cross-Site Request Forgery (CSRF)

CSRF-feature-image-1.jpg

You may have heard of someone who is losing their online account access without even clicking on anything suspicious. This could happen because of a CSRF attack. The CSRF full form is Cross-Site Request Forgery. It refers to a problem where hackers can trick your browser and induce users to perform an action that they performed already or not intended to perform. In simple words, CSRF can make your browser perform actions, like changing settings or sending data, without your acknowledgment. In this blog, we will learn everything about CSRF and how attackers can create fake requests using tools like the CSRF PoC generator, including best practices and examples.

Table of Contents:

Cross-Site Request Forgery (CSRF)

The full form of CSRF is Cross-Site Request Forgery. It is defined as the type of online attack that is performed by a hacker to trick your browser into doing something that you didn’t mean to do.

For example, let’s say that you are logged into your bank account in one tab, and in a new tab, another website is there. If the second website that you opened is made by a hacker, it can mislead your browser and allow your browser to do something like change your password or send money to someone. Now, your browser follows all the given commands because you’re already logged in. The bank thinks that the request came from you (user). But in reality, it was the hacker who made the request. This kind of trick is called a CSRF attack.

Impact of a CSRF Attack

A CSRF attack is one of the most dangerous attacks. When a CSRF attack happens, hackers don’t steal your data directly. Instead of this, they use your current session information to perform irrelevant actions. Here are some common impacts that a victim finds when they have suffered from a CSRF attack:

  • An attacker may change your personal details like email, phone number, or other personal information.
  • Attackers may send a request to change your current password, and if they succeed, then you will not be able to access your own account.
  • An attacker may send money to someone or make an online purchase if they get your bank account credentials.
  • An attacker can delete your important data on your behalf.
  • In social media platforms, a CSRF attack can be used to post spam messages or share some unwanted content from your account.
Launch Your Web Development Career
Join Today
quiz-icon

How does CSRF work?

CSRF attack is a common attack that is used to get the user’s personal information. Here is how cross-site request forgery works:

  1. Logged in to a Website: In the first step, the user is logged in to their bank or social media account. The website quickly stores the session information by using browser cookies.
  2. You Visit Another Website: While still logged in, you may visit another website. If this new website is created by a hacker, then it can include some hidden code that can push your browser to send a request to the first site (bank or social media site).
  3. Sending Request Automatically: Your browser recognizes that a request is going to your bank, so it adds your login cookies without your acknowledgment.
  4. Request Processing: The website (bank or social media) accepts the incoming request and changes your password, sends money, or updates your account information without your knowledge.

Common Attack Surfaces

A CSRF attack works by causing your browser to send illegal or fake requests to websites where you’re already logged in. Any part of the website where users can make changes or send data is a possible target for a CSRF attack. These are some common attack surfaces where the hacker can attack and get your personal details:

  • Hackers can steal your personal information, like your email, phone number, or other profile information.
  • They can change your personal credentials, like your account password.
  • They can transfer money or make payments.
  • With your social media accounts, they may do spam, send irrelevant messages, or leave abusive comments.

How to Construct a CSRF Attack?

Hackers use a CSRF attack to hack personal information. Here are the steps that attackers can perform to carry out a CSRF attack:

  1. Find the Target Action: At first, attackers have to find a target action, like changing the password, sending money, or updating account details. This target action is performed by the hacker on the victim’s browser.
  2. Create a Fake Request: In this step, the attacker builds a fake HTML form that is the same as the real one, and this is done manually or with a tool called CSRF PoC generator.

Note: CSRF PoC generator is defined as the tool that is used to create attack code.

  1. Host the Malicious Website: In this step, the attacker places this fake form on the website that was created by the attackers themselves.
  2. Wait for Victim: Everything will be prepared, and now the attacker is just waiting for the victim. When the victim visits the malicious site, the fake form is submitted in the background, and the browser automatically includes cookies with each request, so the website understands the fake request as a real request.
  3. Final Step: If the website doesn’t check for a valid CSRF token, then the request is granted and the target action is performed.

Prevention from CSRF Attack

In order to stop a CSRF attack, we need to make sure that only real and trusted requests are accepted by a website. Here are the prevention strategies for the users and the developers:

User Side: There is not very much effort required from the user side to prevent a CSRF attack. However, users can reduce the risk by being careful. It is recommended for users to don’t visit unknown websites while they are logged into important websites like bank accounts or social media websites.

Server Side: The most commonly used method that is used by developers to prevent a CSRF attack is adding a CSRF token to forms or any sensitive component. A CSRF token is defined as a random token value that is attached to the user’s session information. When a request is made by the user, the server will check whether the token matches or not. The server rejects the request and gives you the CSRF token mismatch error if the token is missing or doesn’t match with real CSRF token.

Best Practices for Developers

To keep websites safe from hackers, Developers need to follow these best practices:

  • The most effective way to protect users from a CSRF attack is by using a CSRF token, which is nothing but a random string that is linked to the user’s session.
  • Set the SameSite attribute on cookies because this allows the browser to send cookies only when the request comes from the same site.
  • Never use the GET requests for requests that make changes (like deleting a user or updating settings). Always try to use POST, PUT, or DELETE methods.
  • Some popular frameworks like Django, Laravel, Spring, and Express have strong CSRF protection. Thus, try to use these frameworks for developing projects.

Get 100% Hike!

Master Most in Demand Skills Now!

Conclusion

A Cross-Site Request Forgery allows a hacker to trick your browser into performing some target actions on a website without your knowledge. It can perform various unwanted actions like changing your account password, sending some money, or changing other personal information. To stay safe from CSRF attacks, users need to avoid clicking on unwanted links, and developers have to follow some best practices to prevent CSRF attacks. Understanding CSRF helps you to keep your website and account safe from hackers.

What is CSRF used for?

A CSRF attack is a type of online attack that is performed by a hacker to trick your browser into doing something that you didn’t mean to do.

What is a CSRF token?

CSRF token is defined as a random token value that is attached to the user’s session information and further used to check valid requests that come to the server.

What is XSRF vs CSRF?

XSRF is just another name for CSRF (cross-site request forgery).

Is CSRF the same as XSS?

The CSRF tricks your browser into sending fake requests. While XSS is another attacking way in which hacker injects some malicious code into websites.

What is an XSS Attack?

XSS stands for cross-site scripting attack. It is defined as an attack in which the attacker injects some harmful JavaScript code into a website.

Cross-Site Request Forgery (CSRF) – FAQs

About the Author

Lead Penetration Tester, Searce Inc

Shivanshu is a distinguished cybersecurity expert and Penetration tester. He specialises in identifying vulnerabilities and securing critical systems against cyber threats. Shivanshu has a deep knowledge of tools like Metasploit, Burp Suite, and Wireshark. 

Become a Cyber Security Expert