当前位置:网站首页>Cross Site Request Forgery in PHP
Cross Site Request Forgery in PHP
2022-07-25 18:51:00 【allway2】
Cross-Site Request Forgery in PHP | SecureFlag Security Knowledge Base
The prevention of
PHP Not available for CSRF Built in protection against attacks ; Developers must implement it manually by checking session tokens or using one of many well tested libraries and frameworks .
PHP
The following steps show how action.php?do=logout Use Synchronizer Token Pattern Protect endpoints from CSRF attack .
- After successful verification , A random token will be generated in a secure manner and stored as a user's session variable .
<?php
$_SESSION["csrf_token"] = bin2hex(random_bytes(32));
?>- Now? , Any request for a state change endpoint
action.php?do=logoutThe token must be used as HTTP Parameter passing . The following code snippet shows how to write<a href>Mark the token as GET Parameter passing .
<a href="action.php?do=logout&csrf=<?php echo $_SESSION["csrf_token"]; ?>">
- The following code snippet shows how to protect a form by passing a token as a hidden field .
<form>
<input type="hidden" name="csrf" value="<?php echo $_SESSION["csrf_token"]; ?>">
</form>- This document
action.phpNow you must verify before executing the protected codecsrfParameters that match the token stored in the user session . Use a secure hash comparison function , for example PHPhash_equals().
if (!empty($_REQUEST["csrf"]) && hash_equals($_REQUEST["csrf_token"], $_SESSION["csrf_token"])) {
// The token is correct, execute the functionality.
}
else {
die("Anti CSRF token is missing or wrong.");
}OWASP CSRF Protector
OWASP CSRF Protector It's an independent php library , be used for Web In the application CSRF relieve . according to Project page Install according to the instructions on . In order to use it , Just include the library and call init() function .
<?php
include_once __DIR__ .'/vendor/owasp/csrf-protector-php/libs/csrf/csrfprotector.php';
// Initialise CSRFGuard library
csrfProtector::init();Symfony frame
Symfony By default, the form component provides automatic CSRF Protect . Other types of resources , As usual HTML Form or any other state change route , Must be generated and checked manually CSRF Token to explicitly protect .
Consider using conventional HTML Form to delete resources . First , Use csrf_token()Twig Function generates a CSRF token , And store it as a hidden form field .
<form action="/delete" method="post">
<input type="hidden" name="token" value="{
{ csrf_token('action-delete') }}"/>
</form> then , Get in the controller action CSRF The value of the token , And use isCsrfTokenValid() To check its effectiveness .
public function delete(Request $request)
{
$submittedToken = $request->request->get('token');
if (!$this->isCsrfTokenValid('action-delete', $submittedToken)) {
// The token is not correct, redirect to the index.
return $this->redirectToRoute('index');
}
// The token is correct, execute the functionality.
}Reference resources
OWASP - Cross Site Request Forgery memo MITRE - CWE 352 OWASP - CSRF Protector Symfony - How to achieve CSRF Protect
边栏推荐
猜你喜欢

Care for front-line epidemic prevention workers, Haocheng JIAYE and Gaomidian sub district office jointly build the great wall of public welfare

什么是3DE体验平台

MySQL子查询篇(精选20道子查询练习题)

How developers choose the right database for react native

How to create an effective help document?

15. Simple salary management system design

Yyds dry inventory interview must brush top101: reverse linked list

Trust multithread security count

浏览器内核有几种,浏览器版本过低怎么升级

Introduction notes of JVM foundation and problem analysis
随机推荐
Alibaba cloud technology expert Qin long: reliability assurance is a must - how to carry out chaos engineering on the cloud?
人人可参与开源活动正式上线,诚邀您来体验!
Youwei low code: use resolutions
Baklib:制作优秀的产品说明手册
Excellent test / development programmers should make breakthroughs and never forget their original intentions, so that they can always
[translation] logstash, fluent, fluent bit, or vector? How to choose the right open source log collector
Vc/pe is running towards Qingdao
Dynamic memory management
接口自动化测试平台FasterRunner系列(四)- 持续集成、解决多域名
APP测试点(思维导图)
通讯录(一)
论文修改回复1
上半年出货量已超去年全年,森思泰克毫米波雷达“夺食”国际巨头
阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践
优秀的测试/开发程序员突破,不忘初心,方得始终......
接口自动化测试平台FasterRunner系列(三)- 操作示例
进程间的通信(管道通信)
PHP 中的跨站请求伪造
A brief history from object detection to image segmentation
Twitter acquired a public opinion war, which was turned into a child quarrel by musk