当前位置:网站首页>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
边栏推荐
- 怎么禁止使用360浏览器(怎么才能把自带的浏览器停用)
- Is Cinda securities a state-owned enterprise? Is it safe to open an account in Cinda securities?
- 3DE reply
- 接口自动化测试平台FasterRunner系列(四)- 持续集成、解决多域名
- 字符串函数和内存函数(二)
- 软件测试进阶篇—测试分类
- 虚拟机vmware安装步骤(如何在虚拟机安装软件)
- Interpretation of "cross chain interconnection smart contract"
- hough变换理解[通俗易懂]
- Interview shock: why does TCP need three handshakes?
猜你喜欢

Communication between processes (pipeline communication)

15. Simple salary management system design

优秀的测试/开发程序员突破,不忘初心,方得始终......

SQL 实现 Excel 的10个常用功能,附面试原题

什么是hpaPaaS平台?

阿里云技术专家秦隆:可靠性保障必备——云上如何进行混沌工程?

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

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

Pixel2Mesh从单个RGB图像生成三维网格ECCV2018

单臂路由实验演示(Huawei路由器设备配置)
随机推荐
「Wdsr-3」蓬莱药局 题解
CircleIndicator组件,使指示器风格更加多样化
通讯录(一)
SQL 实现 Excel 的10个常用功能,附面试原题
Go代码检查工具
JMeter performance test actual video (what are the common performance test tools)
从目标检测到图像分割简要发展史
信达证券是国企吗?在信达证券开户资金安全吗?
Alibaba cloud technology expert Qin long: reliability assurance is a must - how to carry out chaos engineering on the cloud?
Yes, UDP protocol can also be used to request DNS server
What is hpapaas platform?
3de 回复
Paper revision reply 1
Virtual machine VMware installation steps (how to install software in virtual machine)
人人可参与开源活动正式上线,诚邀您来体验!
进程通信(SystemV通信方式:共享内存,消息队列,信号量)
Ping command details [easy to understand]
[applet development] page navigation details
Introduction notes of JVM foundation and problem analysis
n-queens problem