search
Homephp教程PHP开发Yii2.0 defense against csrf attack method

In yii2, whether you use the testing tool POSTMAN, the command line CURL request, or the ajax request, you will always get the http400: Bad Request error; and if you use the Web page to access GET (removing the POST restriction of verbFilter), it is normal.

After checking the information, I found that this is the reason for CRSF verification

Principle:

Cookie Hashing, so that all forms sent by the server to the client are marked with a random value_csrf , and at the same time save an associated token in the client's COOKIE;

During verification, the server compares an input hidden _csrf received from the request _POST() with the one in the client's COOKIE. Token is compared and verified

The principle of the attacker's attack is to use the client's COOKIE, but the attacker cannot get the specific content of the COOKIE. He just uses (leaving aside the possibility of XSS attacks here, because the user Cookies are easily stolen due to XSS vulnerabilities in the website, which is another 1%. Generally, attackers will basically give up when they see the need to calculate hash values); therefore, attackers cannot add tokens to the attack URL. , thus failing the verification.

This may be the simplest solution, because the attacker cannot obtain the third-party cookie (theoretically), so the data in the form will fail to be constructed

Solution:

1.禁用CRSF验证(不推荐):'enableCsrfValidation' => false,

'components' => [
'request' => [
'cookieValidationKey' => '83r5HbITBiMfmiYPOZFdL-raVp4O1VV4',
'enableCookieValidation' => false,
'enableCsrfValidation' => false,
]

2. When submitting data, carry csrf information

a. When calling component ActiveForm, the submitted data will automatically bring _csrf

b. When submitting ajax, you can add it in the header Obtain the csrf information (as shown below) and submit it together with the data to be submitted

Yii2.0 defense against csrf attack method

c. You can also obtain the csrf information through php

Yii::$app->request->csrfParam;(获取csrf-param)
Yii::$app->request->csrfToken;(获取csrf-token)

The above is the content of Yii2.0 defense against csrf attacks. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement
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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor