Home Web Front-end HTML Tutorial What is CSRF? The dangers of CSRF and how to defend against it

What is CSRF? The dangers of CSRF and how to defend against it

Sep 19, 2018 pm 03:25 PM
csrf

The content of this article is about what is CSRF? The hazards of CSRF and defense methods have certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What is CSRF

Before understanding CSRF, we need to popularize two prerequisites. First of all, there are many ways to verify login permissions. Currently, most websites use session session task mode. Simply speaking, the session mechanism is that the server uses a key-value pair to record login information, and at the same time, the session is stored in the cookie. The id (the key just mentioned) is stored in the cookie. In addition, we also know that HTTP(s) requests in the browser will automatically save cookies for us. Passed to the server. In this way, the session id is obtained through the cookie during each request, and then the login information is obtained from the server through it to complete the verification of user permissions.

Originally this was also a good feature. But due to Cookies are really open. If a user logs in to website A, if the user sends an A cookie when visiting website B, Website request, then this request actually carries the user's login information on website A. If A at station B at this time If the website request is unknown to the user, it is a very serious harm. The above process is a cross-site request attack, that is, Cross-Site Request Forgery, that is CSRF.

The dangers of CSRF

A brief summary of CSRF vulnerabilities is to use vulnerabilities in website authority verification to send requests without the user's awareness, thereby "disguising" the user. Purpose. The main types of attacks implemented by attackers using CSRF are as follows:

The attacker can trick the victim user into completing any status change operation allowed by the victim, such as: updating account details, completing shopping, logging out, and even Login and other operations

Obtain users' private data

Cooperate with other vulnerability attacks

CSRF worm

Among them The CSRF worm, as its name implies, produces a worm effect and will The attack spreads from one to ten, and from ten to a hundred. For example, the interface for privately messaging friends in a community and the interface for obtaining the friend list both have CSRF vulnerabilities. An attacker can combine them into a CSRF worm - when a user visits a malicious page, he obtains his friend list information through CSRF, and then uses The CSRF vulnerability of private messaging friends sends a message pointing to a malicious page to each friend. As long as someone views the link in this message, the CSRF worm will continue to spread, and the harm and impact it may cause is huge!

Defense Method

From the above description, we can know that CSRF has two characteristics: the feature of automatically carrying cookies and cross-site attacks. Then the following solutions can be used for these two features.

Check the Referer field

Everyone knows that there is a Referer field in the HTTP header. This field is used to indicate the address from which the request comes. By verifying this field of the request in the website, we can know whether the request is issued from this site. We can reject all requests not issued by this site, thus avoiding the cross-site characteristics of CSRF.

const { parse } = require('url');module.exports = class extends think.Logic {
  indexAction() {
    const referrer = this.ctx.referrer();
    const {host: referrerHost} = parse(referrer);
    if(referrerHost !== 'xxx') {
        return this.fail('REFERRER_ERROR');
    }
  }}
Copy after login

Also taking ThinkJS as an example, just make a simple judgment in Logic. This method takes advantage of the fact that the client cannot construct a Referrer. Although it is simple, it will become very troublesome when the website has multiple domain names or the domain names are frequently changed, and it also has certain limitations.

Token Verification

Since CSRF takes advantage of the browser's ability to automatically pass cookies, another defense idea is to not pass the verification information through cookies, and add random encrypted strings to other parameters for verification. test. There are two methods here:

Random string: Add a random string parameter to each submission. The parameter is sent by the server through the page. It is added to the submission parameter every time it is requested. The server passes Verify whether the parameters are consistent to determine whether it is a user request. Since the attacker in a CSRF attack has no way of knowing the value of the random string in advance, the server can reject the request by verifying the value.

JWT: Actually except In addition to session login, JWT token login verification is becoming increasingly popular. This method is to record the login token on the front end, and pass it in the Header every time a request is made. The login verification process is implemented by adding an authentication header. Since the attacker cannot know the token value in a CSRF attack, CSRF attacks can also be prevented in this way. certainly In addition to JWT, token login methods include OAuth and many other methods.

The above is the detailed content of What is CSRF? The dangers of CSRF and how to defend against it. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Cross-site scripting (XSS) and cross-site request forgery (CSRF) protection in Laravel Cross-site scripting (XSS) and cross-site request forgery (CSRF) protection in Laravel Aug 13, 2023 pm 04:43 PM

Cross-site scripting (XSS) and cross-site request forgery (CSRF) protection in Laravel With the development of the Internet, network security issues have become more and more serious. Among them, Cross-SiteScripting (XSS) and Cross-SiteRequestForgery (CSRF) are one of the most common attack methods. Laravel, as a popular PHP development framework, provides users with a variety of security mechanisms

Comparative analysis of PHP Session cross-domain and cross-site request forgery Comparative analysis of PHP Session cross-domain and cross-site request forgery Oct 12, 2023 pm 12:58 PM

Comparative analysis of PHPSession cross-domain and cross-site request forgery With the development of the Internet, the security of web applications has become particularly important. PHPSession is a commonly used authentication and session tracking mechanism when developing web applications, while cross-domain requests and cross-site request forgery (CSRF) are two major security threats. In order to protect the security of user data and applications, developers need to understand the difference between Session cross-domain and CSRF, and adopt

PHP Framework Security Guide: How to Prevent CSRF Attacks? PHP Framework Security Guide: How to Prevent CSRF Attacks? Jun 01, 2024 am 10:36 AM

PHP Framework Security Guide: How to Prevent CSRF Attacks? A Cross-Site Request Forgery (CSRF) attack is a type of network attack in which an attacker tricks a user into performing unintended actions within the victim's web application. How does CSRF work? CSRF attacks exploit the fact that most web applications allow requests to be sent between different pages within the same domain name. The attacker creates a malicious page that sends requests to the victim's application, triggering unauthorized actions. How to prevent CSRF attacks? 1. Use anti-CSRF tokens: Assign each user a unique token, store it in the session or cookie. Include a hidden field in your application for submitting that token

CSRF attack in PHP CSRF attack in PHP May 25, 2023 pm 08:31 PM

With the continuous development of the Internet, there are more and more web applications. However, security issues are also attracting more and more attention. CSRF (CrossSiteRequestForgery, cross-site request forgery) attack is a common network security problem. What is a CSRF attack? The so-called CSRF attack means that the attacker steals the user's identity and performs illegal operations in the user's name. In layman's terms, it means that the attacker uses the user's login status to perform some illegal operations without the user's knowledge.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

What is the process and principle of SpringBoot's defense against CSRF attacks? What is the process and principle of SpringBoot's defense against CSRF attacks? May 12, 2023 pm 09:13 PM

CSRF Principle If we want to defend against CSRF attacks, we need to first understand what a CSRF attack is. Let us sort out the CSRF attack process through the following illustration: In fact, this process is very simple: 1. Assume that the user opens the China Merchants Online Banking website and logs in. 2. After successful login, online banking will return the cookie to the front end, and the browser will save the cookie. 3. The user opened a new tab in the browser without logging out of online banking, and then visited a dangerous website. 4. There is a hyperlink on this dangerous website, and the address of the hyperlink points to China Merchants Online Banking. 4. The user clicks this link. Since this hyperlink will automatically carry the cookie saved in the browser,

PHP and Vue.js develop applications that defend against cross-site request forgery (CSRF) attacks PHP and Vue.js develop applications that defend against cross-site request forgery (CSRF) attacks Jul 05, 2023 pm 07:21 PM

PHP and Vue.js develop applications that defend against cross-site request forgery (CSRF) attacks. With the development of Internet applications, cross-site request forgery (CSRF) attacks have become a common security threat. It uses the user's logged-in identity to make forged requests to perform malicious operations, such as changing user passwords, publishing spam, etc. To protect the security of our users and the integrity of our data, we need to implement effective CSRF in our applications

Analysis of cross-site request forgery (CSRF) defense technology in PHP Analysis of cross-site request forgery (CSRF) defense technology in PHP Jun 29, 2023 am 09:20 AM

Analysis of Cross-site Request Forgery (CSRF) Defense Technology in PHP With the rapid development of the Internet, network security issues have become increasingly prominent. Cross-site request forgery (CSRF) attack is a common network security threat. It uses the user's logged-in identity information to send malicious operations through disguised requests, causing users to perform malicious operations without their knowledge. In PHP development, how to defend against CSRF attacks has become an important issue. Principles of CSRF attacks Before understanding how to defend against CSRF attacks, first understand CS

See all articles